-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
149 lines (145 loc) · 4.45 KB
/
index.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
const ascjs = require('ascjs');
const escape = require('html-escaper').escape;
const parser = require('babylon');
const defaultOptions = {
sourceType: 'module',
plugins: [
'estree',
'jsx',
'flow',
'typescript',
'doExpressions',
'objectRestSpread',
'decorators',
'decorators2',
'classProperties',
'classPrivateProperties',
'classPrivateMethods',
'exportExtensions',
'asyncGenerators',
'functionBind',
'functionSent',
'dynamicImport',
'numericSeparator',
'optionalChaining',
'importMeta',
'bigInt',
'optionalCatchBinding',
'throwExpressions',
'pipelineOperator',
'nullishCoalescingOperator'
]
};
const fs = require('fs');
const path = require('path');
const index = fs.readFileSync(path.join(__dirname, 'index.html')).toString();
const chunks = (chunk, i) => (i ? ('${' + (i - 1) + '}') : '') + chunk;
const parse = (options, base, file, cache, db) => {
let code = fs.readFileSync(file).toString();
if (/^(?:import|export)\s+/m.test(code)) code = ascjs(code);
const parseMore = (module, name) => {
if (!cache.has(name)) {
cache.add(name);
parse(options, path.dirname(name), name, cache, db);
}
};
const findRequire = item => {
switch (item.type) {
case 'TaggedTemplateExpression':
if (item.tag.name === 'i18n') {
const template = item.quasi.quasis.map(quasi => quasi.value.raw);
(db[template.join('\x01')] = {})[options.locale] = {
t: null,
v: item.quasi.expressions.map((expression, i) => i)
};
findRequire(item.quasi);
} else {
(item.quasi.expressions || []).forEach(findRequire);
}
break;
case 'CallExpression':
switch (item.callee.name) {
case 'import':
case 'require':
const module = item.arguments[0];
if (/^[./]/.test(module.value)) {
let name = '';
try { name = require.resolve(path.resolve(base, module.value)); } catch(o_O) {}
if (/\.m?js$/.test(name)) parseMore(module, name);
} else {
process.chdir(base);
try {
const name = require.resolve(module.value);
if (name !== module.value) parseMore(module, name);
} catch(meh) {}
}
break;
}
default:
for (let key in item) {
if (typeof item[key] === 'object') {
findRequire(item[key] || {});
}
}
break;
}
};
const parsed = parser.parse(code, options);
parsed.program.body.forEach(findRequire);
return db;
};
module.exports = {
createDefaultDB: (main, options) => {
let base = path.dirname(main);
main = path.resolve(base, main);
base = path.dirname(main);
return parse(
Object.assign(
{},
defaultOptions,
options
),
base,
main,
new Set,
{}
);
},
createTable: (db, locale, existent, translations) => {
const table = [];
const keys = Object.keys(db);
Object.keys(existent).forEach(key => {
if (keys.indexOf(key) < 0) delete existent[key];
});
keys.forEach(key => {
const out = [];
const target = db[key];
const source = {
t: key.split('\x01'),
v: target[locale].v
};
const sentence = source.t.map(chunks).join('');
translations.forEach(lang => {
let content;
if (existent.hasOwnProperty(key) && existent[key].hasOwnProperty(lang)) {
target[lang] = existent[key][lang];
content = target[lang].t.map(chunks).join('');
} else {
target[lang] = {t: source.t, v: source.v};
content = sentence;
}
out.push(`<tr><td valign="top">${lang}</td><td valign="top"><textarea>${content}</textarea></td></tr>`);
});
if (out.length) {
table.push(`<tr class="native" data-key="${escape(key)}"><td valign="top">${locale}</td><td valign="top"><textarea disabled>${sentence}</textarea></td></tr>`);
table.push(...out);
}
});
const indentation = '\n ';
return table.length ? `<table cellpadding="0" cellspacing="0">${indentation}${table.join(indentation)}\n</table>` : '';
},
createUpdate: (db, locale, table) => {
const info = {table, db: JSON.stringify({locale, db})};
return index.replace(/<\!--\$\{(.+?)\}-->|\/\*\$\{(.+?)\}\*\//g, ($0, $1, $2) => info[$1 || $2]);
}
};