-
Notifications
You must be signed in to change notification settings - Fork 368
Expand file tree
/
Copy pathgenerate-string-types.cjs
More file actions
44 lines (35 loc) · 1011 Bytes
/
generate-string-types.cjs
File metadata and controls
44 lines (35 loc) · 1011 Bytes
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
const fs = require('fs');
const path = require('path');
const prettier = require('prettier');
const prettierConfig = require('../.prettierrc');
const flatten = (obj, target, prefix) => {
(target = target || {}), (prefix = prefix || '');
Object.keys(obj).forEach(function (key) {
if (typeof obj[key] === 'object' && obj[key] !== null) {
flatten(obj[key], target, prefix + key + '.');
} else {
return (target[prefix + key] = 'string');
}
});
return target;
};
const strings = fs.readFileSync(
path.resolve(process.cwd(), 'strings/languages/en/strings.json'),
'utf8',
);
const stringTypes = `/**\n * This file is auto-generated\n */\n\n
export interface StringMap ${JSON.stringify(flatten(JSON.parse(strings)))}
`;
const formatContent = prettier.format(stringTypes, {
parser: 'typescript',
...prettierConfig,
});
fs.writeFile(
path.resolve(process.cwd(), 'strings/types/index.ts'),
formatContent,
err => {
if (err) {
console.log(err);
}
},
);