Skip to content

Commit 3ed5734

Browse files
committed
1.2.0
1 parent acb3b0a commit 3ed5734

File tree

9 files changed

+293
-173
lines changed

9 files changed

+293
-173
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,21 @@ Options:
2424
Watching will watch one or more language folders and rebuild the typescript files whenever the yaml files change.If you don't give it a root folder, it will assume the current working directory.
2525

2626
- `ilfy watch rootfolder1 rootfolder2`
27+
28+
## init
29+
30+
Generates a configuration file with the current values for the given folder or folders.
31+
32+
## configuration
33+
34+
use a `.idrinth-typesscript-language-from-yaml.yml` file in the root of your project to configure the tool with defaults that can be overwritten by the command line.
35+
36+
```yaml
37+
hasNoTranslationsFile: false
38+
isSplit: false
39+
originDirectory: language
40+
isFailOnWarning: false
41+
targetDirectory: src/locales
42+
isStrictTypes: false
43+
isVerbatimModuleSyntax: false
44+
```

package.json

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

src/check.ts

Lines changed: 53 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,60 +8,68 @@ import {
88
} from 'yaml';
99
import {
1010
EMPTY,
11-
ORIGIN_DIRECTORY,
1211
} from './constants.js';
1312
import Logger from './logger.js';
1413
import loadKeys from './loadKeys.js';
15-
export default (logger: Logger, folder: string,) => {
16-
if (! existsSync(`${ folder }/${ ORIGIN_DIRECTORY }`,)) {
17-
logger.error(`folder ${ folder }/${ ORIGIN_DIRECTORY } doesn't exist`,);
18-
return false;
19-
}
20-
logger.info(
21-
`Checking translations in folder ${ folder }/${ ORIGIN_DIRECTORY }`,
22-
);
23-
logger.info('',);
24-
const yamlFiles = readdirSync(`${ folder }/${ ORIGIN_DIRECTORY }`,)
25-
.filter((file,) => file.endsWith('.yml',),);
26-
const keys = {
27-
en: [],
28-
};
14+
import Config from "./config.js";
15+
export default (
16+
logger: Logger,
17+
config: Config,
18+
) => {
2919
let errors = 0;
30-
yamlFiles.forEach((yamlFile,) => {
31-
const lang = yamlFile.replace('.yml', '',);
32-
keys[lang] = [];
33-
const yamlPath = `${ folder }/${ ORIGIN_DIRECTORY }/${ yamlFile }`;
34-
35-
const content = readFileSync(yamlPath, 'utf8',);
36-
37-
try {
38-
const data = parse(content,);
39-
keys[lang] = loadKeys(data,);
40-
} catch (e) {
41-
logger.error(`Failed parsing ${ yamlPath }: ${ e }`,);
20+
let warnings = 0;
21+
for (const folder of config.folders) {
22+
const localConfig = new Config(`${ folder }`);
23+
if (! existsSync(`${ folder }/${ localConfig.originDirectory }`,)) {
24+
logger.error(`folder ${ folder }/${ localConfig.originDirectory } doesn't exist`,);
4225
errors ++;
26+
continue;
4327
}
44-
},);
45-
let warnings = 0;
46-
for (const lang of Object.keys(keys,)) {
47-
if (lang !== 'en') {
48-
const tooMany = keys[lang].filter((key,) => ! keys.en.includes(key,),);
49-
const missing = keys.en.filter((key,) => ! keys[lang].includes(key,),);
50-
for (const key of tooMany) {
28+
logger.info(
29+
`Checking translations in folder ${ folder }/${ localConfig.originDirectory }`,
30+
);
31+
logger.info('',);
32+
const yamlFiles = readdirSync(`${ folder }/${ localConfig.originDirectory }`,)
33+
.filter((file,) => file.endsWith('.yml',),);
34+
const keys = {
35+
en: [],
36+
};
37+
yamlFiles.forEach((yamlFile,) => {
38+
const lang = yamlFile.replace('.yml', '',);
39+
keys[lang] = [];
40+
const yamlPath = `${ folder }/${ localConfig.originDirectory }/${ yamlFile }`;
41+
42+
const content = readFileSync(yamlPath, 'utf8',);
43+
44+
try {
45+
const data = parse(content,);
46+
keys[lang] = loadKeys(data,);
47+
} catch (e) {
48+
logger.error(`Failed parsing ${ yamlPath }: ${ e }`,);
5149
errors ++;
52-
logger.error(
53-
`ERROR: ${ lang } defines ${ key }, that doesn't exist in english.`,
54-
);
5550
}
56-
for (const key of missing) {
57-
warnings ++;
58-
logger.warn(
59-
`WARN: ${ lang } lacks ${ key }, that is defined in english.`,
60-
);
51+
},);
52+
for (const lang of Object.keys(keys,)) {
53+
if (lang !== 'en') {
54+
const tooMany = keys[lang].filter((key,) => ! keys.en.includes(key,),);
55+
const missing = keys.en.filter((key,) => ! keys[lang].includes(key,),);
56+
for (const key of tooMany) {
57+
errors ++;
58+
logger.error(
59+
`ERROR: ${ lang } defines ${ key }, that doesn't exist in english.`,
60+
);
61+
}
62+
for (const key of missing) {
63+
warnings ++;
64+
logger.warn(
65+
`WARN: ${ lang } lacks ${ key }, that is defined in english.`,
66+
);
67+
}
6168
}
6269
}
70+
logger.info('',);
71+
logger.info(`Found ${ errors } errors and ${ warnings } warnings.`,);
72+
logger.info('',);
6373
}
64-
logger.info('',);
65-
logger.info(`Found ${ errors } errors and ${ warnings } warnings.`,);
66-
return errors === EMPTY;
74+
return errors === EMPTY && (! config.isFailOnWarning || warnings === EMPTY);
6775
};

src/cli.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,57 @@ import {
22
EXIT_FAILURE,
33
EXIT_SUCCESS,
44
FIRST_ARGUMENT,
5-
SECOND_ARGUMENT,
65
} from './constants.js';
76
import watch from './watch.js';
87
import generate from './generate.js';
98
import check from './check.js';
109
import Logger from './logger.js';
10+
import Config from './config.js';
11+
import init from './init.js';
1112

1213
// eslint-disable-next-line complexity
1314
export default async(args: string[], cwd: string,): Promise<number> => {
1415
const logger = new Logger();
16+
const config = new Config(cwd, args,);
1517
switch (args[FIRST_ARGUMENT]) {
1618
case 'check':
1719
return check(
1820
logger,
19-
args[SECOND_ARGUMENT] || cwd,
21+
config,
2022
) ? EXIT_SUCCESS : EXIT_FAILURE;
2123
case 'generate':
2224
generate(
2325
logger,
24-
cwd,
25-
args.includes('--split',),
26-
args.includes('--verbatim-module-syntax',),
27-
args.includes('--no-translations-file',),
28-
args.includes('--strict-types',),
26+
config,
27+
);
28+
return EXIT_SUCCESS;
29+
case 'init':
30+
init(
31+
logger,
32+
config,
2933
);
3034
return EXIT_SUCCESS;
3135
case 'watch':
3236
await watch(
3337
logger,
34-
cwd,
35-
args
36-
.splice(SECOND_ARGUMENT,)
37-
.map((arg,) => arg.split('=',),),
38+
config,
3839
);
3940
return EXIT_SUCCESS;
4041
default:
4142
logger.info(
42-
'itlfy check - checks the current working directory\'s yaml files.',
43+
'itlfy check [...folder] - checks the watched folders\' yaml files.',
4344
);
4445
logger.info(
4546
'itlfy watch [...folder] - watches and rebuilds ' +
46-
'the watched folder\'s language files on change.',
47+
'the watched folders\' language files on change.',
48+
);
49+
logger.info(
50+
'itlfy generate [...folder] - generates typescript files from ' +
51+
'the watched folders\' files.',
4752
);
4853
logger.info(
49-
'itlfy generate - generates typescript files from ' +
50-
'the current working directory\'s yaml files.',
54+
'itlfy init [...folder] - generates config files for the given' +
55+
'directories.',
5156
);
5257
return EXIT_SUCCESS;
5358
}

src/config.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import {
2+
existsSync,
3+
readFileSync,
4+
} from 'fs';
5+
import {
6+
parse,
7+
} from 'yaml';
8+
import {
9+
EMPTY,
10+
SECOND_ARGUMENT
11+
} from "./constants.js";
12+
13+
export default class Config {
14+
public readonly originDirectory: string;
15+
public readonly targetDirectory: string;
16+
public readonly isVerbatimModuleSyntax: boolean;
17+
public readonly hasNoTranslationsFile: boolean;
18+
public readonly isFailOnWarning: boolean;
19+
public readonly isStrictTypes: boolean;
20+
public readonly isSplit: boolean;
21+
public readonly folders: string[];
22+
constructor(cwd: string, args: string[] = []) {
23+
const file = `${ cwd }/.idrinth-typescript-from-yaml.yml`;
24+
this.hasNoTranslationsFile = false;
25+
this.isSplit = false;
26+
this.originDirectory = 'language';
27+
this.isFailOnWarning = false;
28+
this.targetDirectory = 'src/locales';
29+
this.isStrictTypes = false;
30+
this.isVerbatimModuleSyntax = false;
31+
if (existsSync(file,)) {
32+
const data = parse(readFileSync(file, 'utf8'));
33+
if (typeof data.hasNoTranslationsFile === 'boolean') {
34+
this.hasNoTranslationsFile = data.hasNoTranslationsFile;
35+
}
36+
if (typeof data.isSplit === 'boolean') {
37+
this.isSplit = data.isSplit;
38+
}
39+
if (typeof data.originDirectory === 'string') {
40+
this.originDirectory = data.originDirectory;
41+
}
42+
if (typeof data.isFailOnWarning === 'boolean') {
43+
this.isFailOnWarning = data.isFailOnWarning;
44+
}
45+
if (typeof data.targetDirectory === 'string') {
46+
this.targetDirectory = data.targetDirectory;
47+
}
48+
if (typeof data.isStrictTypes === 'boolean') {
49+
this.isStrictTypes = data.isStrictTypes;
50+
}
51+
if (typeof data.hasNoTranslationsFile === 'boolean') {
52+
this.isVerbatimModuleSyntax = data.isVerbatimModuleSyntax;
53+
}
54+
}
55+
if (args.includes('--no-translation-files',)) {
56+
this.hasNoTranslationsFile = true;
57+
}
58+
if (args.includes('--split',)) {
59+
this.isSplit = true;
60+
}
61+
if (args.includes('--fail-on-warning',)) {
62+
this.isFailOnWarning = true;
63+
}
64+
if (args.includes('--strict-types',)) {
65+
this.isStrictTypes = true;
66+
}
67+
if (args.includes('--verbatim-module-syntax',)) {
68+
this.isVerbatimModuleSyntax = true;
69+
}
70+
this.folders = args
71+
.splice(SECOND_ARGUMENT,)
72+
.filter((arg,) => ! arg.startsWith('--'),)
73+
.map((rel,) => `${ cwd }/${ rel }`);
74+
if (this.folders.length === EMPTY) {
75+
this.folders.push(cwd,);
76+
}
77+
}
78+
}

src/constants.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
export const WAIT_DURATION = 100;
2-
export const ORIGIN_DIRECTORY = 'language';
3-
export const TARGET_DIR = 'src/locales';
42
export const EXIT_SUCCESS = 0;
53
export const EXIT_FAILURE = 1;
64
export const FIRST_ARGUMENT = 2;

0 commit comments

Comments
 (0)