-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
53 lines (41 loc) · 1.38 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
const path = require('path');
const fs = require('fs-extra');
function flatMap(src, prevKey = '') {
let result = {};
if (typeof src === 'object') {
Object.keys(src).forEach(key => {
const nextKey = `${prevKey ? `${prevKey}_` : ''}${key}`;
Object.assign(result, flatMap(src[key], nextKey));
});
}
else if (typeof src === 'string') {
result = {
[prevKey]: {message: src}
};
}
return result;
}
function generate({inputDir, outputDir} = {}) {
if (typeof inputDir !== 'string') {
throw new Error('Expected the "inputDir" to be a string.');
}
if (typeof outputDir !== 'string') {
throw new Error('Expected the "outputDir" to be a string.');
}
const srcDir = path.resolve(inputDir);
const distDir = path.resolve(outputDir);
const files = fs.readdirSync(srcDir);
if (!files.length) {
throw new Error('No files found.');
}
return new Promise(resolve => {
files.forEach(filename => {
const name = path.basename(filename, '.js');
// eslint-disable-next-line global-require, import/no-dynamic-require
const content = require(path.join(srcDir, filename));
fs.outputJSONSync(path.join(distDir, name, 'messages.json'), flatMap(content));
});
resolve();
});
}
module.exports = generate;