diff --git a/src/keys-detective/compare-keys-to-files.ts b/src/keys-detective/compare-keys-to-files.ts index 8b02c34..079e898 100644 --- a/src/keys-detective/compare-keys-to-files.ts +++ b/src/keys-detective/compare-keys-to-files.ts @@ -6,7 +6,7 @@ import { messages } from '../messages'; import { Config, ScopeMap } from '../types'; import { readFile, writeFile } from '../utils/file.utils'; import { getLogger } from '../utils/logger'; -import { getScopeAndLangFromPath } from '../utils/path.utils'; +import { filterPathByLang, getScopeAndLangFromPath } from '../utils/path.utils'; import { buildTable } from './build-table'; import { getTranslationFilesPath } from './get-translation-files-path'; @@ -28,6 +28,7 @@ interface CompareKeysOptions | 'translationsPath' > { scopeToKeys: ScopeMap; + langs?: ReadonlyArray; } export function compareKeysToFiles({ @@ -36,10 +37,16 @@ export function compareKeysToFiles({ addMissingKeys, emitErrorOnExtraKeys, fileFormat, + langs: langsToProcess = [], }: CompareKeysOptions) { const logger = getLogger(); logger.startSpinner(`${messages.checkMissing} ✨`); + const scopeAndLangFromPathOption = { + translationsPath, + fileFormat, + }; + const diffsPerLang = {}; /** An array of the existing translation files paths */ @@ -52,7 +59,7 @@ export function compareKeysToFiles({ const scopePaths = getGlobalConfig().scopePathMap || {}; for (const [scope, path] of Object.entries(scopePaths)) { const keys = scopeToKeys[scope]; - if (keys) { + if (keys && typeof path === 'string') { const res: Omit = { keys, scope, @@ -60,7 +67,8 @@ export function compareKeysToFiles({ }; result.push({ ...res, - files: normalizedGlob(`${res.baseFilesPath}/*.${fileFormat}`), + files: normalizedGlob(`${res.baseFilesPath}/*.${fileFormat}`) + .filter(filterPathByLang(langsToProcess, scopeAndLangFromPathOption)), }); } } @@ -68,9 +76,8 @@ export function compareKeysToFiles({ for (const filePath of translationFiles) { const { scope = '__global' } = getScopeAndLangFromPath({ + ...scopeAndLangFromPathOption, filePath, - translationsPath, - fileFormat, }); if (cache[scope]) { continue; @@ -89,7 +96,8 @@ export function compareKeysToFiles({ ...res, files: normalizedGlob( `${res.baseFilesPath}/${isGlobal ? '' : scope}/*.${fileFormat}` - ), + ) + .filter(filterPathByLang(langsToProcess, scopeAndLangFromPathOption)), }); } } diff --git a/src/keys-detective/index.ts b/src/keys-detective/index.ts index a026cd5..5f7c3c8 100644 --- a/src/keys-detective/index.ts +++ b/src/keys-detective/index.ts @@ -3,6 +3,7 @@ import { buildKeys } from '../keys-builder/build-keys'; import { messages } from '../messages'; import { Config } from '../types'; import { getLogger } from '../utils/logger'; +import { filterPathByLang } from '../utils/path.utils'; import { resolveConfig } from '../utils/resolve-config'; import { compareKeysToFiles } from './compare-keys-to-files'; @@ -10,14 +11,14 @@ import { getTranslationFilesPath } from './get-translation-files-path'; export function findMissingKeys(inlineConfig: Config) { const logger = getLogger(); - const config = resolveConfig(inlineConfig); + const config = resolveConfig({ langs:[], ...inlineConfig }); setConfig(config); - const { translationsPath, fileFormat } = config; + const { translationsPath, fileFormat, langs = [] } = config; const translationFiles = getTranslationFilesPath( translationsPath, fileFormat - ); + ).filter(filterPathByLang(langs, config)); if (translationFiles.length === 0) { console.log('No translation files found.'); @@ -37,5 +38,6 @@ export function findMissingKeys(inlineConfig: Config) { addMissingKeys, emitErrorOnExtraKeys, fileFormat, + langs, }); } diff --git a/src/utils/path.utils.ts b/src/utils/path.utils.ts index 1ac36ed..e9698ca 100644 --- a/src/utils/path.utils.ts +++ b/src/utils/path.utils.ts @@ -24,6 +24,22 @@ interface Options extends Pick { filePath: string; } +export function filterPathByLang( + langs: ReadonlyArray, + options: Pick +) { + const langsAsSet = new Set(langs); + + return (filePath: string) => { + if (langsAsSet.size > 0) { + const { lang } = getScopeAndLangFromPath({ filePath, ...options }); + return langsAsSet.has(lang); + } else { + return true; + } + }; +} + /** * /Users/username/www/folderName/src/assets/i18n/admin/es.json => { scope: admin, lang: es } * /Users/username/www/folderName/src/assets/i18n/es.json => { scope: undefined, lang: es }