-
Notifications
You must be signed in to change notification settings - Fork 3
/
cli.js
executable file
·153 lines (142 loc) · 3.9 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
const readline = require('readline')
const esprima = require('esprima')
const gettextParser = require('gettext-parser')
const argv = require('minimist')(process.argv)
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
})
const whiteList = [
'String',
'Template'
]
const prefixs = [
'i18n',
'_'
]
const comments = []
let n = 0
let check = false
let identifier = []
const po = {
charset: 'utf-8',
headers: {
'content-type': 'text/plain; charset=UTF-8',
'content-transfer-encoding': 'UTF-8'
},
translations: { '': {}}
}
rl.on('line', (line) => {
line = line.replace(/^\.\//g, '')
const file = path.resolve(process.cwd() + '/' + line)
const code = fs.readFileSync(file).toString()
let tokens = esprima.tokenize(code, {
loc: true,
comment: true
})
tokens = tokens.reduce((total, current, index) => {
if (argv.all && !check) {
check = true
}
if (current.type == 'LineComment' && current.value.match(/i18n/i)) {
comments.push(current)
} else if (current.type == 'Identifier' && prefixs.includes(current.value)) {
check = true
} else if (check) {
if (current.type == 'Identifier' && argv.value) {
if (identifier[n]) {
identifier[n] += `.${current.value}`
} else {
identifier[n] = `${current.value}`
}
}
if (whiteList.includes(current.type) && typeof check == 'boolean') {
check = current.type
}
if (!whiteList.includes(current.type)) {
if (check == whiteList[0] && current.type == 'Punctuator' && current.value == ')') {
check = false
}
return total
}
if (check == whiteList[0]) { //String
total.push(current)
//check = false
} else if (current.value.startsWith('}')) {
const old = total[total.length-1]
total[total.length-1].value = `${old.value}${n}${current.value}`
total[total.length-1].loc.end = current.loc.end
n++
if (argv.value) {
identifier[n] = false
}
} else if (current.value.startsWith('`') && !total[total.length-1].value.endsWith('${')) {
n = 0
if (argv.value) {
identifier = [false]
}
total.push(current)
}
if (check == current.type && (
total[total.length-1].value.endsWith('"') ||
total[total.length-1].value.endsWith('\'') ||
total[total.length-1].value.endsWith('`')
)) {
const comment = comments[comments.length-1]
if (comment) {
const commentLine = comment.loc.start.line
const codeLine = total[total.length-1].loc.start.line
if (commentLine == codeLine || commentLine+1 == codeLine) {
total[total.length-1].comment = comment.value.replace(/[#\s]*i18n[\s:]*/i, '')
}
} else if (argv.value) {
total[total.length-1].comment = ''
}
if (argv.value) {
identifier.map((value, index) => {
if (value) {
if (index > 0) {
total[total.length-1].comment += '\n'
}
total[total.length-1].comment += `\${${index}} = "${value}"`
}
})
}
check = false
}
}
return total
}, [{value: ''}])
tokens = tokens.filter(t => t.value != '')
tokens.map((token) => {
token.value = token.value.replace(/['"`](.*)['"`]/s, '$1')
if (Object.keys(po.translations['']).includes(token.value)) {
po.translations[''][token.value].comments.reference += (
`\n${line}:${token.loc.start.line}:${token.loc.start.column}`
)
if (token.comment) {
po.translations[''][token.value].comments.extracted += '\n' + token.comment
}
} else {
po.translations[''][token.value] = {
msgid: token.value,
comments: {
reference: `${line}:${token.loc.start.line}:${token.loc.start.column}`
},
msgstr: []
}
if (token.comment) {
po.translations[''][token.value].comments.extracted = token.comment
}
}
})
})
rl.on('close', async (line) => {
console.log(
gettextParser.po.compile(po).toString().trim().replace(/\\\\/sg, '\\')
)
})