-
-
Notifications
You must be signed in to change notification settings - Fork 52
feat: support langs option in find command #139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<string>; | ||
} | ||
|
||
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,25 +59,25 @@ 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<Result, 'files'> = { | ||
keys, | ||
scope, | ||
baseFilesPath: path, | ||
}; | ||
result.push({ | ||
...res, | ||
files: normalizedGlob(`${res.baseFilesPath}/*.${fileFormat}`), | ||
files: normalizedGlob(`${res.baseFilesPath}/*.${fileFormat}`) | ||
.filter(filterPathByLang(langsToProcess, scopeAndLangFromPathOption)), | ||
}); | ||
} | ||
} | ||
const cache = {}; | ||
|
||
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)), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like you can create this filter once and use it across the function, generally speaking you can create it once with the command config no? |
||
}); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,21 +3,22 @@ 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'; | ||
import { getTranslationFilesPath } from './get-translation-files-path'; | ||
|
||
export function findMissingKeys(inlineConfig: Config) { | ||
const logger = getLogger(); | ||
const config = resolveConfig(inlineConfig); | ||
const config = resolveConfig({ langs:[], ...inlineConfig }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only thing is that this config property has 2 different purposes when running the find command and when running the extract. we need to make it clear in the docs if we want to use the same property. |
||
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, | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,22 @@ interface Options extends Pick<Config, 'fileFormat' | 'translationsPath'> { | |
filePath: string; | ||
} | ||
|
||
export function filterPathByLang( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add an explanation to why this was created and what is this used for |
||
langs: ReadonlyArray<string>, | ||
options: Pick<Config, 'fileFormat' | 'translationsPath'> | ||
) { | ||
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 } | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this needed? path is supposed to be a string