From 3304981bbe0d96e1e9726bfe7a30eb53955a4f83 Mon Sep 17 00:00:00 2001 From: Robin Date: Thu, 17 Mar 2022 10:07:23 +0100 Subject: [PATCH] Add support for translations in actual addons --- .gitignore | 3 ++ __snapshots__/test.js.snap | 14 +++++++ .../app/controllers/application.js | 7 ++++ .../app/templates/application.hbs | 3 ++ .../scoped-addon/templates/application.hbs | 1 + .../scoped-addon/translations/en.yaml | 4 ++ .../external-addon/templates/application.hbs | 1 + .../external-addon/translations/en.yaml | 3 ++ .../translations/en.yaml | 2 + index.js | 37 ++++++++++++++++--- 10 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 fixtures/external-addon-translations/app/controllers/application.js create mode 100644 fixtures/external-addon-translations/app/templates/application.hbs create mode 100644 fixtures/external-addon-translations/node_modules/@company/scoped-addon/templates/application.hbs create mode 100644 fixtures/external-addon-translations/node_modules/@company/scoped-addon/translations/en.yaml create mode 100644 fixtures/external-addon-translations/node_modules/external-addon/templates/application.hbs create mode 100644 fixtures/external-addon-translations/node_modules/external-addon/translations/en.yaml create mode 100644 fixtures/external-addon-translations/translations/en.yaml diff --git a/.gitignore b/.gitignore index eeab00af..4a9c2108 100644 --- a/.gitignore +++ b/.gitignore @@ -36,6 +36,9 @@ build/Release node_modules/ jspm_packages/ +# Include node modules used in tests +!fixtures/**/node_modules + # TypeScript v1 declaration files typings/ diff --git a/__snapshots__/test.js.snap b/__snapshots__/test.js.snap index 3bb02842..35ad3782 100644 --- a/__snapshots__/test.js.snap +++ b/__snapshots__/test.js.snap @@ -44,6 +44,20 @@ exports[`Test Fixtures emblem 1`] = ` exports[`Test Fixtures emblem 2`] = `Map {}`; +exports[`Test Fixtures external-addon-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... + + 👏 No unused translations were found! + + 👏 No missing translations were found! +" +`; + +exports[`Test Fixtures external-addon-translations 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... diff --git a/fixtures/external-addon-translations/app/controllers/application.js b/fixtures/external-addon-translations/app/controllers/application.js new file mode 100644 index 00000000..468eb331 --- /dev/null +++ b/fixtures/external-addon-translations/app/controllers/application.js @@ -0,0 +1,7 @@ +import Controller from '@ember/controller'; + +export default class ApplicationController extends Controller { + get foo() { + return this.intl.t('js-translation'); + } +} diff --git a/fixtures/external-addon-translations/app/templates/application.hbs b/fixtures/external-addon-translations/app/templates/application.hbs new file mode 100644 index 00000000..6be37de4 --- /dev/null +++ b/fixtures/external-addon-translations/app/templates/application.hbs @@ -0,0 +1,3 @@ +{{t "hbs-translation"}} +{{t "external-addon.used-by-app-translation"}} +{{t "company.scoped-addon.used-by-app-translation"}} diff --git a/fixtures/external-addon-translations/node_modules/@company/scoped-addon/templates/application.hbs b/fixtures/external-addon-translations/node_modules/@company/scoped-addon/templates/application.hbs new file mode 100644 index 00000000..490e0dd5 --- /dev/null +++ b/fixtures/external-addon-translations/node_modules/@company/scoped-addon/templates/application.hbs @@ -0,0 +1 @@ +{{t "scoped-addon.missing.but.unmarked-translation"}} diff --git a/fixtures/external-addon-translations/node_modules/@company/scoped-addon/translations/en.yaml b/fixtures/external-addon-translations/node_modules/@company/scoped-addon/translations/en.yaml new file mode 100644 index 00000000..6bcbc1cd --- /dev/null +++ b/fixtures/external-addon-translations/node_modules/@company/scoped-addon/translations/en.yaml @@ -0,0 +1,4 @@ +company: + scoped-addon: + used-by-app-translation: This translation is used by app, and is not marked missing + unused-by-app-translation: This translation is not used by app, but is not marked unused, because it's external diff --git a/fixtures/external-addon-translations/node_modules/external-addon/templates/application.hbs b/fixtures/external-addon-translations/node_modules/external-addon/templates/application.hbs new file mode 100644 index 00000000..fe32f55c --- /dev/null +++ b/fixtures/external-addon-translations/node_modules/external-addon/templates/application.hbs @@ -0,0 +1 @@ +{{t "external-addon.missing.but.unmarked-translation"}} diff --git a/fixtures/external-addon-translations/node_modules/external-addon/translations/en.yaml b/fixtures/external-addon-translations/node_modules/external-addon/translations/en.yaml new file mode 100644 index 00000000..5defc244 --- /dev/null +++ b/fixtures/external-addon-translations/node_modules/external-addon/translations/en.yaml @@ -0,0 +1,3 @@ +external-addon: + used-by-app-translation: This translation is used by app, and is not marked missing + unused-by-app-translation: This translation is not used by app, but is not marked unused, because it's external diff --git a/fixtures/external-addon-translations/translations/en.yaml b/fixtures/external-addon-translations/translations/en.yaml new file mode 100644 index 00000000..91079a90 --- /dev/null +++ b/fixtures/external-addon-translations/translations/en.yaml @@ -0,0 +1,2 @@ +hbs-translation: HBS! +js-translation: JS! diff --git a/index.js b/index.js index 683a5177..a6d1283d 100755 --- a/index.js +++ b/index.js @@ -37,12 +37,15 @@ async function run(rootDir, options = {}) { log(`${step(3)} ⚙️ Checking for unused translations...`); - let translationFiles = await findTranslationFiles(rootDir); - let existingTranslationKeys = await analyzeTranslationFiles(rootDir, translationFiles); + let ownTranslationFiles = await findOwnTranslationFiles(rootDir); + let addonTranslationFiles = await findAddonTranslationFiles(rootDir); + let existingOwnTranslationKeys = await analyzeTranslationFiles(rootDir, ownTranslationFiles); + let existingAddonTranslationKeys = await analyzeTranslationFiles(rootDir, addonTranslationFiles); + let existingTranslationKeys = mergeMaps(existingOwnTranslationKeys, existingAddonTranslationKeys); let whitelist = config.whitelist || []; let unusedTranslations = findDifferenceInTranslations( - existingTranslationKeys, + existingOwnTranslationKeys, usedTranslationKeys, whitelist ); @@ -82,7 +85,7 @@ async function run(rootDir, options = {}) { let totalErrors = missingTranslations.size + unusedTranslations.size; if (shouldFix) { - removeUnusedTranslations(writeToFile, rootDir, translationFiles, unusedTranslations); + removeUnusedTranslations(writeToFile, rootDir, ownTranslationFiles, unusedTranslations); log(); log(' 👏 All unused translations were removed'); } @@ -113,8 +116,15 @@ async function findInRepoFiles(cwd) { return globby(joinPaths(inRepoFolders, ['**/*.js', '**/*.hbs', '**/*.emblem']), { cwd }); } -async function findTranslationFiles(cwd) { - let inputFolders = ['', ...findInRepoPaths(cwd)]; +async function findOwnTranslationFiles(cwd) { + return findTranslationFiles(cwd, ['', ...findInRepoPaths(cwd)]); +} + +async function findAddonTranslationFiles(cwd) { + return findTranslationFiles(cwd, ['node_modules/*', 'node_modules/@*/*']); +} + +async function findTranslationFiles(cwd, inputFolders) { let translationPaths = joinPaths(inputFolders, ['translations']); return globby(joinPaths(translationPaths, ['**/*.json', '**/*.yaml', '**/*.yml']), { @@ -388,4 +398,19 @@ function getNestedAttribute(parent, keys) { return attribute; } +function mergeMaps(mapA, mapB) { + let resultMap = new Map([...mapA]); + + for (let [key, bFiles] of mapB) { + if (!resultMap.has(key)) { + resultMap.set(key, bFiles); + } else { + let aFiles = resultMap.get(key); + resultMap.set(key, new Set([...aFiles, ...bFiles])); + } + } + + return resultMap; +} + module.exports = { run, generateFileList };