forked from umami-software/umami
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat-lang.js
35 lines (26 loc) · 959 Bytes
/
format-lang.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
const fs = require('fs');
const path = require('path');
const del = require('del');
const prettier = require('prettier');
const chalk = require('chalk');
const src = path.resolve(__dirname, '../lang');
const dest = path.resolve(__dirname, '../build');
const files = fs.readdirSync(src);
const removed = del.sync([path.join(dest, '*.json')]);
if (removed.length) {
console.log(removed.map(n => `${n} ${chalk.redBright('✗')}`).join('\n'));
}
if (!fs.existsSync(dest)) {
fs.mkdirSync(dest);
}
files.forEach(file => {
const lang = require(`../lang/${file}`);
const keys = Object.keys(lang).sort();
const formatted = keys.reduce((obj, key) => {
obj[key] = { defaultMessage: lang[key] };
return obj;
}, {});
const json = prettier.format(JSON.stringify(formatted), { parser: 'json' });
fs.writeFileSync(path.resolve(dest, file), json);
console.log(path.resolve(src, file), chalk.greenBright('->'), path.resolve(dest, file));
});