aboutsummaryrefslogtreecommitdiff
path: root/scripts/format-lang.js
blob: 95c390eebced9f0f721e0485619731d545da90e6 (plain) (blame)
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
import path from 'node:path';
import del from 'del';
import fs from 'fs-extra';
import { createRequire } from 'module';

const require = createRequire(import.meta.url);
const src = path.resolve(process.cwd(), 'src/lang');
const dest = path.resolve(process.cwd(), 'build/messages');
const files = fs.readdirSync(src);

del.sync([path.join(dest)]);

/*
This script takes the files from the `lang` folder and formats them into
the format that format-js expects.
 */
async function run() {
  await fs.ensureDir(dest);

  files.forEach(file => {
    const lang = require(path.resolve(process.cwd(), `src/lang/${file}`));
    const keys = Object.keys(lang).sort();

    const formatted = keys.reduce((obj, key) => {
      obj[key] = { defaultMessage: lang[key] };
      return obj;
    }, {});

    const json = JSON.stringify(formatted, null, 2);

    fs.writeFileSync(path.resolve(dest, file), json);
  });
}

run();