Skip to content

Commit

Permalink
feat(i18n-lint): use .eslintrc.json as translation fils path source (#76
Browse files Browse the repository at this point in the history
)

Co-authored-by: jb-coquet <jecoquet.externe@m6.fr>
  • Loading branch information
jcoquet and jb-coquet committed May 12, 2021
1 parent d4af219 commit 6e61fdc
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
10 changes: 9 additions & 1 deletion packages/i18n-lint/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
#!/usr/bin/env node
import Runner from './runner';
import defaultConfig from '../default.config';
import ConfigLoader from './configLoader';

let defaultConfig;

try {
defaultConfig = ConfigLoader.load(`.eslintrc.json`);
} catch (e) {
console.info('No .eslintrc.json was found. Using --config...');
}

const runner = new Runner(defaultConfig);
runner.run();
6 changes: 2 additions & 4 deletions packages/i18n-lint/src/reader.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import fs from 'fs';

export default class JSONReader {
static parse(folderName, fileName) {
static parse(filePath) {
try {
const path = `${folderName}/${fileName}`;

const text = fs.readFileSync(path, 'utf-8');
const text = fs.readFileSync(filePath, 'utf-8');

return JSON.parse(text);
} catch (e) {
Expand Down
12 changes: 8 additions & 4 deletions packages/i18n-lint/src/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ export default class Runner {
}

run() {
const { mainLanguages, path } = this.config;
const {
settings: {
i18n: { principalLangs = [], secondaryLangs = [] },
},
} = this.config;

const reports = _.flatMap(mainLanguages, lang => {
const tradForLang = Reader.parse(path, `${lang}.json`);
const reports = _.flatMap([...principalLangs, ...secondaryLangs], ({ name, translationPath }) => {
const tradForLang = Reader.parse(translationPath);

return _.flatMap(reporters, reporter => reporter(tradForLang, lang));
return _.flatMap(reporters, (reporter) => reporter(tradForLang, name));
});

process.exit(_.find(reports, { error: true }) ? 1 : 0);
Expand Down
11 changes: 10 additions & 1 deletion packages/i18n-lint/tests/test.config.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
{
"path": "./tests/i18n"
"settings": {
"i18n": {
"principalLangs": [
{
"name": "test-en",
"translationPath": "./tests/i18n/en.json"
}
]
}
}
}

0 comments on commit 6e61fdc

Please sign in to comment.