umami/scripts/format-lang.js

35 lines
865 B
JavaScript
Raw Normal View History

2022-03-17 05:50:24 +01:00
const fs = require('fs-extra');
2020-09-12 13:19:33 +02:00
const path = require('path');
const del = require('del');
2020-09-12 13:19:33 +02:00
const prettier = require('prettier');
const src = path.resolve(__dirname, '../lang');
2023-04-22 09:55:55 +02:00
const dest = path.resolve(__dirname, '../build/messages');
2020-09-12 13:19:33 +02:00
const files = fs.readdirSync(src);
2023-04-22 09:55:55 +02:00
del.sync([path.join(dest)]);
2023-04-22 09:55:55 +02:00
/*
This script takes the files from the `lang` folder and formats them into
the format that format-js expects.
*/
2022-03-17 05:50:24 +01:00
async function run() {
await fs.ensureDir(dest);
files.forEach(file => {
const lang = require(`../lang/${file}`);
const keys = Object.keys(lang).sort();
2020-09-12 13:19:33 +02:00
2022-03-17 05:50:24 +01:00
const formatted = keys.reduce((obj, key) => {
obj[key] = { defaultMessage: lang[key] };
return obj;
}, {});
2020-09-12 13:19:33 +02:00
2022-03-17 05:50:24 +01:00
const json = prettier.format(JSON.stringify(formatted), { parser: 'json' });
2020-09-12 13:19:33 +02:00
2022-03-17 05:50:24 +01:00
fs.writeFileSync(path.resolve(dest, file), json);
});
}
2020-09-12 13:19:33 +02:00
2022-03-17 05:50:24 +01:00
run();