Skip to content

Commit e8138ba

Browse files
authored
1.4.0 (#3)
* 1.4.0 * fix readme for v1.4.0
1 parent 3fae61c commit e8138ba

File tree

8 files changed

+71
-13
lines changed

8 files changed

+71
-13
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@ The check command will check if the yaml files in your current working directory
1010

1111
## generate
1212

13-
Generate provides typescript language files according to your specification. It only works in your current working directoy's `language` folder and will output to `ssrc/locales` by default.
13+
Generate provides typescript language files according to your specification. It only works in your current working directory's `language` folder and will output to `ssrc/locales` by default.
1414

1515
Options:
1616

17-
- `--strict-types` apply strict types, so that typescript will error on build if the structures missmatch
17+
- `--strict-types` apply strict types, so that typescript will error on build if the structures mismatch
1818
- `--verbatim-module-syntax` will export the type as a named export instead of a default one
19-
- `--split` splits the language files at the top level keys.. Thhis leads to smaller files when using dynamic imports. Usually you want `--no-translations-file` as well in case of splitting.
20-
- `--no-translations-file` dissables the creation of a translation object containing all languages
19+
- `--simplified-hash-map` will convert the multi tiered object to a single level hash map for speed reasons.
20+
- `--inject-default-language` will create a file based on the default language. Only works with `--simplified-hash-map` active at the same time.
21+
- `--split` splits the language files at the top level keys. This leads to smaller files when using dynamic imports. Usually you want `--no-translations-file` as well in case of splitting.
22+
- `--no-translations-file` disables the creation of a translation object containing all languages
2123

2224
## watch
2325

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@idrinth-api-bench/typescript-language-from-yaml",
3-
"version": "1.3.1",
3+
"version": "1.4.0",
44
"description": "Translates yaml files to ts for translation autocompletion, autocorrection and better developer support",
55
"bin": {
66
"itlfy": "bin/itlfy.js"

src/build-translation-file.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import toTypescriptObject from './to-typescript-object.js';
22
import Config from './config.js';
3+
import simplifyObject from './simplify-object.js';
34

4-
export default (config: Config, data: object,) => {
5+
export default (config: Config, data: object, def: object,) => {
56
const head = (() => {
67
if (! config.isStrictTypes) {
78
return 'const lang = ';
@@ -12,5 +13,6 @@ export default (config: Config, data: object,) => {
1213
return 'import {\n lang as langType,\n} from \'./type.js\';' +
1314
'\nconst lang: langType = ';
1415
})();
15-
return `/* eslint max-len:0 */\n${ head }${ toTypescriptObject(data,) };\n\nexport default lang;\n`;
16+
const tsObj = toTypescriptObject(simplifyObject(config, data, def,),);
17+
return `/* eslint max-len:0 */\n${ head }${ tsObj };\n\nexport default lang;\n`;
1618
};

src/config.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,14 @@ export default class Config {
2626

2727
public readonly isSplit: boolean;
2828

29+
public readonly isSimplifiedHashMap: boolean;
30+
31+
public readonly shouldInjectDefaultLanguage: boolean;
32+
2933
public readonly folders: string[];
3034

35+
public readonly overwrites: string[] = [];
36+
3137
// eslint-disable-next-line complexity
3238
constructor(cwd: string, args: string[] = [],) {
3339
const file = `${ cwd }/${ CONFIG_FILE }`;
@@ -38,6 +44,8 @@ export default class Config {
3844
this.targetDirectory = 'src/locales';
3945
this.isStrictTypes = false;
4046
this.isVerbatimModuleSyntax = false;
47+
this.isSimplifiedHashMap = false;
48+
this.shouldInjectDefaultLanguage = false;
4149
if (existsSync(file,)) {
4250
const data = parse(readFileSync(file, 'utf8',),);
4351
if (typeof data.hasNoTranslationsFile === 'boolean') {
@@ -61,21 +69,40 @@ export default class Config {
6169
if (typeof data.hasNoTranslationsFile === 'boolean') {
6270
this.isVerbatimModuleSyntax = data.isVerbatimModuleSyntax;
6371
}
72+
if (typeof data.shouldInjectDefaultLanguage === 'boolean') {
73+
this.shouldInjectDefaultLanguage = data.shouldInjectDefaultLanguage;
74+
}
75+
if (typeof data.isSimplifiedHashMap === 'boolean') {
76+
this.isSimplifiedHashMap = data.isSimplifiedHashMap;
77+
}
6478
}
6579
if (args.includes('--no-translation-files',)) {
6680
this.hasNoTranslationsFile = true;
81+
this.overwrites.push('--no-translation-files',);
6782
}
6883
if (args.includes('--split',)) {
6984
this.isSplit = true;
85+
this.overwrites.push('--split',);
7086
}
7187
if (args.includes('--fail-on-warning',)) {
7288
this.isFailOnWarning = true;
89+
this.overwrites.push('--fail-on-warning',);
7390
}
7491
if (args.includes('--strict-types',)) {
7592
this.isStrictTypes = true;
93+
this.overwrites.push('--strict-types',);
7694
}
7795
if (args.includes('--verbatim-module-syntax',)) {
7896
this.isVerbatimModuleSyntax = true;
97+
this.overwrites.push('--verbatim-module-syntax',);
98+
}
99+
if (args.includes('--inject-default-language',)) {
100+
this.shouldInjectDefaultLanguage = true;
101+
this.overwrites.push('--inject-default-language',);
102+
}
103+
if (args.includes('--simplified-hash-map',)) {
104+
this.isSimplifiedHashMap = true;
105+
this.overwrites.push('--simplified-hash-map',);
79106
}
80107
this.folders = args
81108
.slice(SECOND_ARGUMENT,)

src/generate.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export default (
2424
config: Config,
2525
) => {
2626
for (const folder of config.folders) {
27-
const localConfig = new Config(`${ folder }`,);
27+
const localConfig = new Config(`${ folder }`, config.overwrites,);
2828
if (existsSync(`${ folder }/${ localConfig.targetDirectory }`,)) {
2929
for (const file of readdirSync(
3030
`${ folder }/${ localConfig.targetDirectory }`,
@@ -34,7 +34,12 @@ export default (
3434
}
3535
}
3636
const yamlFiles = findYamlFiles(folder, localConfig,);
37-
37+
const def = localConfig.shouldInjectDefaultLanguage
38+
? parse(readFileSync(
39+
`${ folder }/${ localConfig.originDirectory }/en.yml`,
40+
'utf8',
41+
),)
42+
: {};
3843
const files = [];
3944
const out = `${ folder }/${ localConfig.targetDirectory }`;
4045
if (! existsSync(out,)) {
@@ -50,7 +55,7 @@ export default (
5055
for (const key of Object.keys(data,)) {
5156
writeFileSync(
5257
`${ out }/${ yamlFile.language }-${ key }.ts`,
53-
buildTranslationFile(localConfig, data[key],),
58+
buildTranslationFile(localConfig, data[key], def[key],),
5459
'utf8',
5560
);
5661
files.push(`${ yamlFile.language }-${ key }`,);
@@ -67,7 +72,7 @@ export default (
6772
} else {
6873
writeFileSync(
6974
`${ out }/${ yamlFile.language }.ts`,
70-
buildTranslationFile(localConfig, data,),
75+
buildTranslationFile(localConfig, data, def,),
7176
'utf8',
7277
);
7378
files.push(`${ yamlFile.language }`,);

src/simplify-object.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import Config from './config.js';
2+
3+
export default (config: Config, data: object, def: object,): object => {
4+
if (! config.isSimplifiedHashMap) {
5+
return data;
6+
}
7+
const simplified = {};
8+
const simplify = (prefix: string, obj: object,) => {
9+
for (const key of Object.keys(obj,)) {
10+
if (typeof obj[key] === 'string') {
11+
simplified[prefix + key] = obj[key];
12+
} else if (typeof obj[key] === 'object') {
13+
simplify(prefix + key + '.', obj[key],);
14+
}
15+
}
16+
};
17+
if (config.shouldInjectDefaultLanguage) {
18+
simplify('', def,);
19+
}
20+
simplify('', data,);
21+
return simplified;
22+
};

src/to-typescript-object.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
export default (data: object,): string => JSON
66
.stringify(data, null, DEFAULT_INDENTATION,)
77
.replace(/'/ug, '\\\\\'',)
8-
.replace(/"([a-z][^"-]+?)":/ug, '$1:',)
8+
.replace(/"([a-z][^a-z0-9A-Z_]+?)":/ug, '$1:',)
99
.replace(/"([^"]+?)":/ug, '\'$1\':',)
1010
.replace(/\n/ug, ',\n',)
1111
.replace(/"([^"]+?)",/ug, '\'$1\',',)

src/watch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default async(
2020
while (true) {
2121
const now = Date.now();
2222
for (const folder of config.folders) {
23-
const localConfig = new Config(folder,);
23+
const localConfig = new Config(folder, config.overwrites,);
2424
let modified = false;
2525
for (const file of readdirSync(
2626
`${ folder }/${ localConfig.originDirectory }`,

0 commit comments

Comments
 (0)