-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bundle.js
75 lines (68 loc) · 2.25 KB
/
bundle.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
// @ts-check
const fs = require('fs/promises');
const exists = require('fs').existsSync;
const path = require('path');
/**
* @param {string} filePath
* @param {string} content
*/
async function writeFile(filePath, content) {
const dir = path.dirname(filePath);
if (!exists(dir)) {
await fs.mkdir(dir, {recursive: true});
}
await fs.writeFile(filePath, content);
}
/**
* @param {string} filePath
* @param {string | string[] | ((localeName: string) => (string | string[]))} outputPathOrPaths
*/
async function bundleLocale(filePath, outputPathOrPaths) {
let file = await fs.readFile(filePath, 'utf8');
file = file.replace(/^#.*?$/gm, '');
const messages = {};
const regex = /@([a-z0-9_]+)/ig;
let match;
while ((match = regex.exec(file))) {
const messageName = match[1];
const messageStart = match.index + match[0].length;
let messageEnd = file.indexOf('@', messageStart);
if (messageEnd < 0) {
messageEnd = file.length;
}
messages[messageName] = {
message: file.substring(messageStart, messageEnd).trim()
};
}
const fileName = filePath.substring(filePath.lastIndexOf('/') + 1);
const locale = fileName.substring(0, fileName.lastIndexOf('.')).replace('-', '_');
const json = `${JSON.stringify(messages, null, 4)}\n`;
/** @type {string[]} */
let outputPaths;
if (typeof outputPathOrPaths === 'string') {
outputPaths = [outputPathOrPaths];
} else if (Array.isArray(outputPathOrPaths)) {
outputPaths = outputPathOrPaths;
} else if (typeof outputPathOrPaths === 'function') {
const result = outputPathOrPaths(locale);
outputPaths = Array.isArray(result) ? result : [result];
}
await Promise.all(outputPaths.map((p) => writeFile(p, json)));
}
/**
* @param {(locale: string) => (string | string[])} getOutputPath
*/
async function bundleLocales(getOutputPath) {
const localesSrcDir = './locales';
const list = await fs.readdir(localesSrcDir);
for (const name of list) {
if (!name.endsWith('.config')) {
continue;
}
await bundleLocale(`${localesSrcDir}/${name}`, getOutputPath);
}
}
module.exports = {
bundleLocale,
bundleLocales,
};