aboutsummaryrefslogtreecommitdiff
path: root/scripts/build-locales-files.mjs
blob: db1483dee4efb2d01646b4a36e70d39b0a09a538 (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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { existsSync, writeFileSync } from 'node:fs';
import { Glob } from 'bun';
import _ from 'lodash';

async function getPathsFromGlobs({ patterns, onlyFiles = true }) {
  const filePaths = [];

  for (const pattern of patterns) {
    const glob = new Glob(pattern);

    for await (const filePath of glob.scan({ onlyFiles, cwd: '.' })) {
      filePaths.push(filePath);
    }
  }

  return { filePaths };
}

function getLocaleKey({ filePath }) {
  const fileName = filePath.split('/').pop();
  return fileName.replace(/\.yml$/, '');
}

async function createMissingLocaleFile({ localeKey }) {
  const fileName = `${localeKey}.yml`;

  const { filePaths: localesDirs } = await getPathsFromGlobs({
    patterns: [
      'locales',
      'src/tools/*/locales',
    ],
    onlyFiles: false,
  });

  for (const localesDir of localesDirs) {
    const filePath = `${localesDir}/${fileName}`;

    if (existsSync(filePath)) {
      console.log(`Locale file already exists: ${filePath}`);
      continue;
    }

    console.log(`Creating missing locale file: ${filePath}`);
    writeFileSync(filePath, '', 'utf8');
  }
}

const { filePaths } = await getPathsFromGlobs({
  patterns: [
    'locales/*.yml',
    'src/tools/*/locales/*.yml',
  ],
});

await Promise.all(
  _.chain(filePaths)
    .map(filePath => getLocaleKey({ filePath }))
    .uniq()
    .map(localeKey => createMissingLocaleFile({ localeKey }))
    .value(),
);