1
0
mirror of https://github.com/kremalicious/umami.git synced 2024-12-22 17:23:54 +01:00
umami/scripts/format-lang.js

36 lines
959 B
JavaScript
Raw Normal View History

2020-09-12 13:19:33 +02:00
const fs = require('fs');
const path = require('path');
const del = require('del');
2020-09-12 13:19:33 +02:00
const prettier = require('prettier');
const chalk = require('chalk');
2020-09-12 13:19:33 +02:00
const src = path.resolve(__dirname, '../lang');
2020-09-14 04:01:02 +02:00
const dest = path.resolve(__dirname, '../build');
2020-09-12 13:19:33 +02:00
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'));
}
2020-09-12 13:19:33 +02:00
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));
2020-09-12 13:19:33 +02:00
});