Skip to content

Commit

Permalink
Merge pull request #480 from robinborst95/feature/in-repo-paths
Browse files Browse the repository at this point in the history
Add support for translations in in-repo addons
  • Loading branch information
Mikek2252 authored Apr 20, 2022
2 parents 8f5ed7f + f59f331 commit 7b61f00
Show file tree
Hide file tree
Showing 13 changed files with 123 additions and 3 deletions.
14 changes: 14 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# http://editorconfig.org

root = true

[*]
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = space
indent_size = 2

[*.{diff,md}]
trim_trailing_whitespace = false
20 changes: 20 additions & 0 deletions __snapshots__/test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,26 @@ exports[`Test Fixtures emblem 1`] = `

exports[`Test Fixtures emblem 2`] = `Map {}`;

exports[`Test Fixtures in-repo-translations 1`] = `
"[1/4] 🔍 Finding JS and HBS files...
[2/4] 🔍 Searching for translations keys in JS and HBS files...
[3/4] ⚙️ Checking for unused translations...
[4/4] ⚙️ Checking for missing translations...
⚠️ Found 1 unused translations!
You can use --fix to remove all unused translations.
- in-repo-addon.unused (used in lib/in-repo-addon/translations/en.yaml)
⚠️ Found 2 missing translations!
- translation-missing-in-addon (used in lib/in-repo-addon/addon/templates/addon.hbs)
- translation-missing-in-app (used in lib/in-repo-addon/app/templates/application.hbs)
"
`;

exports[`Test Fixtures in-repo-translations 2`] = `Map {}`;

exports[`Test Fixtures missing-translations 1`] = `
"[1/4] 🔍 Finding JS and HBS files...
[2/4] 🔍 Searching for translations keys in JS and HBS files...
Expand Down
7 changes: 7 additions & 0 deletions fixtures/in-repo-translations/app/controllers/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Controller from '@ember/controller';

export default class ApplicationController extends Controller {
get foo() {
return this.intl.t('js-translation');
}
}
2 changes: 2 additions & 0 deletions fixtures/in-repo-translations/app/templates/application.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{t "hbs-translation"}}
{{t "in-repo-addon.translation-outside-addon"}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Controller from '@ember/controller';

export default class AddonController extends Controller {
get foo() {
return this.intl.t('in-repo-addon.addon.js-translation');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{t "in-repo-addon.addon.hbs-translation"}}
{{t "translation-outside-app-in-addon"}}
{{t "translation-missing-in-addon"}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Controller from '@ember/controller';

export default class ApplicationController extends Controller {
get foo() {
return this.intl.t('in-repo-addon.app.js-translation');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{t "in-repo-addon.app.hbs-translation"}}
{{t "translation-outside-app-in-app"}}
{{t "translation-missing-in-app"}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
in-repo-addon:
addon:
hbs-translation: HBS in in-repo addon addon
js-translation: JS in in-repo addon addon
app:
hbs-translation: HBS in in-repo addon app
js-translation: JS in in-repo addon app
translation-outside-addon: This translation is used outside the in-repo addon
unused: This translation is unused
8 changes: 8 additions & 0 deletions fixtures/in-repo-translations/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "in-repo-translations",
"ember-addon": {
"paths": [
"lib/in-repo-addon"
]
}
}
4 changes: 4 additions & 0 deletions fixtures/in-repo-translations/translations/en.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
hbs-translation: HBS!
js-translation: JS!
translation-outside-app-in-addon: This translation is only used in the addon folder of the addon
translation-outside-app-in-app: This translation is only used in the app folder of the addon
35 changes: 33 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ async function run(rootDir, options = {}) {
let config = readConfig(rootDir);

log(`${step(1)} 🔍 Finding JS and HBS files...`);
let files = await findAppFiles(rootDir);
let appFiles = await findAppFiles(rootDir);
let inRepoFiles = await findInRepoFiles(rootDir);
let files = [...appFiles, ...inRepoFiles];

log(`${step(2)} 🔍 Searching for translations keys in JS and HBS files...`);
let usedTranslationKeys = await analyzeFiles(rootDir, files);
Expand Down Expand Up @@ -104,12 +106,41 @@ async function findAppFiles(cwd) {
return globby(['app/**/*.js', 'app/**/*.hbs', 'app/**/*.emblem'], { cwd });
}

async function findInRepoFiles(cwd) {
let inRepoPaths = findInRepoPaths(cwd);
let inRepoFolders = joinPaths(inRepoPaths, ['addon', 'app']);

return globby(joinPaths(inRepoFolders, ['**/*.js', '**/*.hbs', '**/*.emblem']), { cwd });
}

async function findTranslationFiles(cwd) {
return globby(['translations/**/*.json', 'translations/**/*.yaml', 'translations/**/*.yml'], {
let inputFolders = ['', ...findInRepoPaths(cwd)];
let translationPaths = joinPaths(inputFolders, ['translations']);

return globby(joinPaths(translationPaths, ['**/*.json', '**/*.yaml', '**/*.yml']), {
cwd,
});
}

function findInRepoPaths(cwd) {
let pkgJSONPath = path.join(cwd, 'package.json');

if (!fs.existsSync(pkgJSONPath)) return [];

let pkgJSON = JSON.parse(fs.readFileSync(path.join(cwd, 'package.json')));
let inRepoPaths = pkgJSON && pkgJSON['ember-addon'] && pkgJSON['ember-addon']['paths'];

return inRepoPaths || [];
}

function joinPaths(inputPathOrPaths, outputPaths) {
if (Array.isArray(inputPathOrPaths)) {
return inputPathOrPaths.map(inputPath => joinPaths(inputPath, outputPaths)).flat();
} else {
return outputPaths.map(directory => path.join(inputPathOrPaths, directory));
}
}

async function analyzeFiles(cwd, files) {
let allTranslationKeys = new Map();

Expand Down
7 changes: 6 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ let fixtures = fs.readdirSync(`${__dirname}/fixtures/`);
describe('Test Fixtures', () => {
let output;
let writtenFiles;
let fixturesWithErrors = ['emblem', 'missing-translations', 'unused-translations'];
let fixturesWithErrors = [
'emblem',
'missing-translations',
'unused-translations',
'in-repo-translations',
];
let fixturesWithFix = ['remove-unused-translations', 'remove-unused-translations-nested'];

beforeEach(() => {
Expand Down

0 comments on commit 7b61f00

Please sign in to comment.