-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlang.js
More file actions
65 lines (57 loc) · 1.67 KB
/
lang.js
File metadata and controls
65 lines (57 loc) · 1.67 KB
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
62
63
64
65
import glob from 'fast-glob';
import { assign, isMain, readJSON, sortKeys, writeFile } from './utils.js';
export default async function main(filePath = 'lang/underscript.json') {
// Load base "en" first
const data = await bundle(
await glob('lang/en/*.json'),
);
// Now load other languages (if any)
Object.entries(await bundle(
await glob([
'lang/*/*.json',
'!lang/en',
]),
)).forEach(
([lang, obj]) => Object.entries(obj).forEach(
([key, value]) => {
// Check if key exists in "en"
if (!Object.hasOwn(data.en, key)) {
console.warn(`${lang} has unknown key "${key}"`);
// assign(data, lang, `???${key}`, value);
// return;
}
assign(data, lang, key, value);
},
),
);
const file = JSON.stringify(sortKeys(data), null, 2);
if (!file) return;
await writeFile(filePath, file);
}
/**
* Bundle all files into a single object
* @returns {Promise<Record<string, Record<string, string>>>}
*/
async function bundle(files = []) {
const ret = {};
await Promise.all(files.map(async (file) => {
const [lang, name] = getFileParts(file);
Object.entries(await readJSON(file)).forEach(
([key, value]) => {
if (key.startsWith('//')) return;
const fullKey = name === 'vanilla' ? key : `underscript.${name}.${key}`;
assign(ret, lang, fullKey, value);
},
);
}));
return ret;
}
// Split file path into [lang, name]
function getFileParts(file = '') {
const [/* dir */, lang, name] = file.split('/');
return [lang, name.substring(0, name.lastIndexOf('.'))];
}
// Only automatically run if main script...
if (isMain(import.meta)) {
main();
}