Skip to content

Commit 81c20fc

Browse files
committed
1.1.0
1 parent 1e58e49 commit 81c20fc

File tree

8 files changed

+102
-48
lines changed

8 files changed

+102
-48
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@idrinth/typescript-language-from-yaml",
3-
"version": "1.0.7",
3+
"version": "1.1.0",
44
"description": "Translates yaml files to ts for translation autocompletion, autocorrection and better developer support",
55
"bin": {
66
"itlfy": "bin/itlfy.js"
@@ -16,7 +16,7 @@
1616
},
1717
"repository": {
1818
"type": "git",
19-
"url": "https:://github.com/idrinth/typescript-language-from-yaml"
19+
"url": "https://github.com/idrinth/typescript-language-from-yaml"
2020
},
2121
"keywords": [
2222
"transslation",

src/check.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ export default (logger: Logger, folder: string,) => {
1717
logger.error(`folder ${ folder }/${ ORIGIN_DIRECTORY } doesn't exist`,);
1818
return false;
1919
}
20-
logger.info(`Checking translations in folder ${ folder }/${ ORIGIN_DIRECTORY }`,);
20+
logger.info(
21+
`Checking translations in folder ${ folder }/${ ORIGIN_DIRECTORY }`,
22+
);
2123
logger.info('',);
2224
const yamlFiles = readdirSync(`${ folder }/${ ORIGIN_DIRECTORY }`,)
2325
.filter((file,) => file.endsWith('.yml',),);

src/cli.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import generate from './generate.js';
99
import check from './check.js';
1010
import Logger from './logger.js';
1111

12+
// eslint-disable-next-line complexity
1213
export default async(args: string[], cwd: string,): Promise<number> => {
1314
const logger = new Logger();
1415
switch (args[FIRST_ARGUMENT]) {
@@ -22,7 +23,9 @@ export default async(args: string[], cwd: string,): Promise<number> => {
2223
logger,
2324
cwd,
2425
args.includes('--split',),
25-
args.includes('--verbatimModuleSyntax',),
26+
args.includes('--verbatim-module-syntax',),
27+
args.includes('--no-translations-file',),
28+
args.includes('--strict-types',),
2629
);
2730
return EXIT_SUCCESS;
2831
case 'watch':
@@ -35,9 +38,17 @@ export default async(args: string[], cwd: string,): Promise<number> => {
3538
);
3639
return EXIT_SUCCESS;
3740
default:
38-
logger.info('itlfy check - checks the current working directory\'s yaml files.',);
39-
logger.info('itlfy watch folder [...folder] - watches and rebuilds the watched folder\'s language files on change.',);
40-
logger.info('itlfy generate - generates typescript files from the current working directory\'s yaml files.',);
41+
logger.info(
42+
'itlfy check - checks the current working directory\'s yaml files.',
43+
);
44+
logger.info(
45+
'itlfy watch [...folder] - watches and rebuilds ' +
46+
'the watched folder\'s language files on change.',
47+
);
48+
logger.info(
49+
'itlfy generate - generates typescript files from ' +
50+
'the current working directory\'s yaml files.',
51+
);
4152
return EXIT_SUCCESS;
4253
}
4354
};

src/delay.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
export default (time,) => new Promise((resolve,) => setTimeout(resolve, time,),);
1+
export default (time,) => new Promise((resolve,) => setTimeout(
2+
resolve,
3+
time,
4+
),);

src/generate.ts

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
readdirSync,
55
readFileSync,
66
writeFileSync,
7+
unlinkSync,
78
} from 'fs';
89
import {
910
parse,
@@ -15,18 +16,25 @@ import {
1516
import Logger from './logger.js';
1617
import toTypescriptObject from './to-typescript-object.js';
1718
import loadKeys from './loadKeys.js';
18-
import {unlinkSync} from "node:fs";
1919

20-
export default (logger: Logger, cwd: string, shouldSplit = false, isVerbatimModuleSyntax = false) => {
21-
if (existsSync(`${cwd}/${TARGET_DIR}`,)) {
22-
for (const file of readdirSync(`${cwd}/${TARGET_DIR}`, 'utf8',)) {
23-
unlinkSync(`${cwd}/${TARGET_DIR}/${file}`,);
20+
export default (
21+
logger: Logger, cwd: string,
22+
shouldSplit = false,
23+
isVerbatimModuleSyntax = false,
24+
isWithoutTranslationFile = false,
25+
isStrictTyping = false,
26+
// eslint-disable-next-line max-params
27+
) => {
28+
if (existsSync(`${ cwd }/${ TARGET_DIR }`,)) {
29+
for (const file of readdirSync(`${ cwd }/${ TARGET_DIR }`, 'utf8',)) {
30+
unlinkSync(`${ cwd }/${ TARGET_DIR }/${ file }`,);
2431
}
2532
}
2633
const yamlFiles = readdirSync(`${ cwd }/${ ORIGIN_DIRECTORY }`, 'utf8',)
2734
.filter((file,) => file.endsWith('.yml',),);
2835

2936
const files = [];
37+
// eslint-disable-next-line complexity
3038
yamlFiles.forEach((yamlFile,) => {
3139
const lang = yamlFile.replace('.yml', '',);
3240
const yamlPath = `${ ORIGIN_DIRECTORY }/${ yamlFile }`;
@@ -39,22 +47,45 @@ export default (logger: Logger, cwd: string, shouldSplit = false, isVerbatimModu
3947

4048
const content = readFileSync(yamlPath, 'utf8',);
4149
const data = parse(content,);
42-
if (shouldSplit) {
50+
const typeName = isStrictTyping ? 'langType' : 'Partial<langType>';
51+
if (shouldSplit && typeof data[Object.keys(data,).pop()] !== 'string') {
4352
for (const key of Object.keys(data,)) {
4453
writeFileSync(
4554
`${ TARGET_DIR }/${ lang }-${ key }.ts`,
46-
`/* eslint max-len:0 */\nconst lang = ${ toTypescriptObject(data[key]) };\n\nexport default lang;\n`,
55+
isVerbatimModuleSyntax
56+
? `/* eslint max-len:0 */\nimport {\n lang as langType,\n} from './type-${ key }.js';\nconst lang: ${ typeName } = ${ toTypescriptObject(data[key],) };\n\nexport default lang;\n`
57+
: `/* eslint max-len:0 */\nimport langType from './type-${ key }.js';\nconst lang: ${ typeName } = ${ toTypescriptObject(data[key],) };\n\nexport default lang;\n`,
4758
'utf8',
4859
);
4960
files.push(`${ lang }-${ key }`,);
61+
if (lang === 'en') {
62+
writeFileSync(
63+
`${ TARGET_DIR }/type-${ key }.ts`,
64+
isVerbatimModuleSyntax
65+
? `/* eslint max-len:0 */\ntype ln = ${ toTypescriptObject(data[key],).replace(/: '.*?',/ug, ': string,',) };\n\nexport type lang = ln;\n`
66+
: `/* eslint max-len:0 */\ntype lang = ${ toTypescriptObject(data[key],).replace(/: '.*?',/ug, ': string,',) };\n\nexport default lang;\n`,
67+
'utf8',
68+
);
69+
}
5070
}
5171
} else {
5272
writeFileSync(
5373
`${ TARGET_DIR }/${ lang }.ts`,
54-
`/* eslint max-len:0 */\nconst lang = ${ toTypescriptObject(data) };\n\nexport default lang;\n`,
74+
isVerbatimModuleSyntax
75+
? `/* eslint max-len:0 */\nimport {\n lang as langType,\n} from './type.js';\nconst lang: ${ typeName } = ${ toTypescriptObject(data,) };\n\nexport default lang;\n`
76+
: `/* eslint max-len:0 */\nimport langType from './type.js';\nconst lang: ${ typeName } = ${ toTypescriptObject(data,) };\n\nexport default lang;\n`,
5577
'utf8',
5678
);
5779
files.push(`${ lang }`,);
80+
if (lang === 'en') {
81+
writeFileSync(
82+
`${ TARGET_DIR }/type.ts`,
83+
isVerbatimModuleSyntax
84+
? `/* eslint max-len:0 */\ntype ln = ${ toTypescriptObject(data,).replace(/: '.*?',/ug, ': string,',) };\n\nexport type lang = ln;\n`
85+
: `/* eslint max-len:0 */\ntype lang = ${ toTypescriptObject(data,).replace(/: '.*?',/ug, ': string,',) };\n\nexport default lang;\n`,
86+
'utf8',
87+
);
88+
}
5889
}
5990
if (lang === 'en') {
6091
const keys = loadKeys(data,);
@@ -78,20 +109,22 @@ export default (logger: Logger, cwd: string, shouldSplit = false, isVerbatimModu
78109
);
79110
writeFileSync(
80111
TARGET_DIR + '/files.ts',
81-
`const files = ${ toTypescriptObject(files) };\nexport default files;\n`,
112+
`const files = ${ toTypescriptObject(files,) };\nexport default files;\n`,
82113
'utf8',
83114
);
84-
let fileImporter = '';
85-
let fileExporter = 'const translations = {';
86-
for (const f of files) {
87-
const v = f.replace(/-/gu, '_',);
88-
fileImporter += `import ${ v } from './${ f }.js';\n`;
89-
fileExporter += `\n '${ f }': ${ v },`;
115+
if (! isWithoutTranslationFile) {
116+
let fileImporter = '';
117+
let fileExporter = 'const translations = {';
118+
for (const f of files) {
119+
const v = f.replace(/-/gu, '_',);
120+
fileImporter += `import ${ v } from './${ f }.js';\n`;
121+
fileExporter += `\n '${ f }': ${ v },`;
122+
}
123+
fileExporter += '\n};';
124+
writeFileSync(
125+
TARGET_DIR + '/translations.ts',
126+
`${ fileImporter }${ fileExporter }\nexport default translations;\n`,
127+
'utf8',
128+
);
90129
}
91-
fileExporter += '\n};';
92-
writeFileSync(
93-
TARGET_DIR + '/translations.ts',
94-
`${ fileImporter }${ fileExporter }\nexport default translations;\n`,
95-
'utf8',
96-
);
97130
};

src/loadKeys.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default (data: object,): string[]=> {
1+
export default (data: object,): string[] => {
22
const output = [];
33
const loadKeys = (object: object, prefix = '',) => {
44
for (const key of Object.keys(object,)) {
@@ -14,4 +14,4 @@ export default (data: object,): string[]=> {
1414
};
1515
loadKeys(data,);
1616
return output;
17-
}
17+
};

src/to-typescript-object.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@ import {
22
DEFAULT_INDENTATION,
33
} from './constants.js';
44

5-
export default (data: object,): string => {
6-
return JSON.stringify(data, undefined, DEFAULT_INDENTATION,)
7-
.replace(/'/ug, '\\\\\'',)
8-
.replace(/"([a-z][^"-]+?)":/ug, '$1:',)
9-
.replace(/"([^"]+?)":/ug, '\'$1\':',)
10-
.replace(/\n/ug, ',\n',)
11-
.replace(/"([^"]+?)",/ug, '\'$1\',',)
12-
.replace(/,,/ug, ',',)
13-
.replace(/\{,/ug, '{',)
14-
.replace(/\\\\'/ug, '\\\'',)
15-
.replace(/\\'(.*?)\\':/ug, '\'$1\':',)
16-
.replace(/: "(.+?)",/ug, ': \'$1\',',)
17-
.replace(/\[,/ug, '[',);
18-
}
5+
export default (data: object,): string => JSON
6+
.stringify(data, null, DEFAULT_INDENTATION,)
7+
.replace(/'/ug, '\\\\\'',)
8+
.replace(/"([a-z][^"-]+?)":/ug, '$1:',)
9+
.replace(/"([^"]+?)":/ug, '\'$1\':',)
10+
.replace(/\n/ug, ',\n',)
11+
.replace(/"([^"]+?)",/ug, '\'$1\',',)
12+
.replace(/,,/ug, ',',)
13+
.replace(/\{,/ug, '{',)
14+
.replace(/\\\\'/ug, '\\\'',)
15+
.replace(/\\'(.*?)\\':/ug, '\'$1\':',)
16+
.replace(/: "(.+?)",/ug, ': \'$1\',',)
17+
.replace(/\[,/ug, '[',);

src/watch.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import delay from './delay.js';
1111
import generate from './generate.js';
1212
import Logger from './logger.js';
1313

14+
// eslint-disable-next-line complexity
1415
export default async(logger: Logger, cwd: string, folders: string[][],) => {
1516
if (folders.length === EMPTY) {
1617
folders.push([
@@ -27,12 +28,17 @@ export default async(logger: Logger, cwd: string, folders: string[][],) => {
2728
const [
2829
folder,
2930
split,
30-
verbatim
31+
verbatim,
3132
] = data;
3233
let modified = false;
33-
for (const file of readdirSync(`${ cwd }/${ folder }/${ ORIGIN_DIRECTORY }`, 'utf8',)) {
34+
for (const file of readdirSync(
35+
`${ cwd }/${ folder }/${ ORIGIN_DIRECTORY }`,
36+
'utf8',
37+
)) {
3438
if (file.endsWith('.yml',)) {
35-
const stats = statSync(`${ cwd }/${ folder }/${ ORIGIN_DIRECTORY }/${ file }`,);
39+
const stats = statSync(
40+
`${ cwd }/${ folder }/${ ORIGIN_DIRECTORY }/${ file }`,
41+
);
3642
// eslint-disable-next-line max-depth
3743
if (stats.mtimeMs < now && stats.mtimeMs >= last) {
3844
modified = true;

0 commit comments

Comments
 (0)