Skip to content

Commit d7d85ad

Browse files
authored
Fix eslint issues (#1)
* 1.3.1 * 1.3.1
1 parent dcc2af1 commit d7d85ad

File tree

11 files changed

+190
-110
lines changed

11 files changed

+190
-110
lines changed

.eslintrc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ extends:
44
ignorePatterns:
55
- node_modules
66
root: true
7+
env:
8+
node: true
79
parser: "@typescript-eslint/parser"
810
rules:
911
no-await-in-loop: error

.github/workflows/eslint.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: ESLINT
2+
on:
3+
pull_request:
4+
branches:
5+
- the-one
6+
push:
7+
branches:
8+
- the-one
9+
jobs:
10+
lint:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
- name: Use Node.js
15+
uses: actions/setup-node@v4
16+
with:
17+
node-version: latest
18+
cache: 'npm'
19+
cache-dependency-path: |
20+
package-lock.json
21+
- name: Install
22+
run: npm ci
23+
- name: Check
24+
run: npm run lint

package.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
{
2-
"name": "@idrinth/typescript-language-from-yaml",
3-
"version": "1.3.0",
2+
"name": "@idrinth-api-bench/typescript-language-from-yaml",
3+
"version": "1.3.1",
44
"description": "Translates yaml files to ts for translation autocompletion, autocorrection and better developer support",
55
"bin": {
66
"itlfy": "bin/itlfy.js"
77
},
88
"type": "module",
99
"scripts": {
10-
"lint": "eslint .",
10+
"lint": "eslint . --ext ts",
1111
"lint-fix": "eslint . --fix",
1212
"tsc": "tsc -p tsconfig.json",
1313
"prepublishOnly": "npm run tsc",
1414
"clear": "node tools/clear-js-files-from-src.js",
1515
"postpublish": "npm run clear"
1616
},
17+
"homepage": "https://idrinth-api-ben.ch",
18+
"bugs": {
19+
"url": "https://github.com/idrinth-api-bench/api-bench"
20+
},
1721
"repository": {
1822
"type": "git",
19-
"url": "https://github.com/idrinth/typescript-language-from-yaml"
23+
"url": "https://github.com/idrinth-api-bench/typescript-language-from-yaml"
2024
},
2125
"keywords": [
2226
"transslation",

src/build-translation-file.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import toTypescriptObject from './to-typescript-object.js';
2+
import Config from './config.js';
3+
4+
export default (config: Config, data: object,) => {
5+
const head = (() => {
6+
if (! config.isStrictTypes) {
7+
return 'const lang = ';
8+
}
9+
if (! config.isVerbatimModuleSyntax) {
10+
return 'import langType from \'./type.js\';\nconst lang: langType = ';
11+
}
12+
return 'import {\n lang as langType,\n} from \'./type.js\';' +
13+
'\nconst lang: langType = ';
14+
})();
15+
return `/* eslint max-len:0 */\n${ head }${ toTypescriptObject(data,) };\n\nexport default lang;\n`;
16+
};

src/check-folder.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import Config from './config.js';
2+
import {
3+
existsSync, readFileSync,
4+
} from 'fs';
5+
import {
6+
parse,
7+
} from 'yaml';
8+
import loadKeys from './loadKeys.js';
9+
import Logger from './logger.js';
10+
import findYamlFiles from './find-yaml-files.js';
11+
12+
// eslint-disable-next-line complexity
13+
export default (folder: string, logger: Logger,) => {
14+
let errors = 0;
15+
let warnings = 0;
16+
const localConfig = new Config(`${ folder }`,);
17+
const input = `${ folder }/${ localConfig.originDirectory }`;
18+
if (! existsSync(input,)) {
19+
logger.error(`folder ${ input } doesn't exist`,);
20+
errors ++;
21+
return {
22+
errors,
23+
warnings,
24+
};
25+
}
26+
logger.info(
27+
`Checking translations in folder ${ input }`,
28+
);
29+
logger.info('',);
30+
const yamlFiles = findYamlFiles(folder, localConfig,);
31+
const keys = {
32+
en: [],
33+
};
34+
yamlFiles.forEach((yamlFile,) => {
35+
keys[yamlFile.language] = [];
36+
37+
const content = readFileSync(yamlFile.input, 'utf8',);
38+
39+
try {
40+
const data = parse(content,);
41+
keys[yamlFile.language] = loadKeys(data,);
42+
} catch (e) {
43+
logger.error(`Failed parsing ${ yamlFile.input }: ${ e }`,);
44+
errors ++;
45+
}
46+
},);
47+
for (const lang of Object.keys(keys,)) {
48+
if (lang !== 'en') {
49+
const tooMany = keys[lang].filter((key,) => ! keys.en.includes(key,),);
50+
const missing = keys.en.filter((key,) => ! keys[lang].includes(key,),);
51+
for (const key of tooMany) {
52+
errors ++;
53+
logger.error(
54+
`ERROR: ${ lang } defines ${ key }, that doesn't exist in english.`,
55+
);
56+
}
57+
for (const key of missing) {
58+
warnings ++;
59+
logger.warn(
60+
`WARN: ${ lang } lacks ${ key }, that is defined in english.`,
61+
);
62+
}
63+
}
64+
}
65+
logger.info('',);
66+
logger.info(`Found ${ errors } errors and ${ warnings } warnings.`,);
67+
logger.info('',);
68+
return {
69+
errors,
70+
warnings,
71+
};
72+
};

src/check.ts

Lines changed: 4 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,19 @@
1-
import {
2-
existsSync,
3-
readdirSync,
4-
readFileSync,
5-
} from 'fs';
6-
import {
7-
parse,
8-
} from 'yaml';
91
import {
102
EMPTY,
113
} from './constants.js';
124
import Logger from './logger.js';
13-
import loadKeys from './loadKeys.js';
145
import Config from './config.js';
6+
import checkFolder from './check-folder.js';
157
export default (
168
logger: Logger,
179
config: Config,
1810
) => {
1911
let errors = 0;
2012
let warnings = 0;
2113
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`,);
25-
errors ++;
26-
continue;
27-
}
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 }`,);
49-
errors ++;
50-
}
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-
}
68-
}
69-
}
70-
logger.info('',);
71-
logger.info(`Found ${ errors } errors and ${ warnings } warnings.`,);
72-
logger.info('',);
14+
const partial = checkFolder(folder, logger,);
15+
errors += partial.errors;
16+
warnings += partial.warnings;
7317
}
7418
return errors === EMPTY && (! config.isFailOnWarning || warnings === EMPTY);
7519
};

src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export default class Config {
2828

2929
public readonly folders: string[];
3030

31+
// eslint-disable-next-line complexity
3132
constructor(cwd: string, args: string[] = [],) {
3233
const file = `${ cwd }/${ CONFIG_FILE }`;
3334
this.hasNoTranslationsFile = false;

src/find-yaml-files.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {
2+
readdirSync,
3+
} from 'fs';
4+
import Config from './config.js';
5+
6+
export default (folder: string, config: Config,) => readdirSync(
7+
`${ folder }/${ config.originDirectory }`,
8+
'utf8',
9+
)
10+
.filter((file,) => file.endsWith('.yml',) || file.endsWith('.yaml',),)
11+
.map((file,) => ({
12+
language: file.replace(/\.ya?ml$/u, '',),
13+
input: `${ folder }/${ config.originDirectory }/${ file }`,
14+
}),);

0 commit comments

Comments
 (0)