Skip to content
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

feat: support langs option in find command #139

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/keys-detective/compare-keys-to-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -28,6 +28,7 @@ interface CompareKeysOptions
| 'translationsPath'
> {
scopeToKeys: ScopeMap;
langs?: ReadonlyArray<string>;
}

export function compareKeysToFiles({
Expand All @@ -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 */
Expand All @@ -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') {
Copy link
Collaborator

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

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;
Expand All @@ -89,7 +96,8 @@ export function compareKeysToFiles({
...res,
files: normalizedGlob(
`${res.baseFilesPath}/${isGlobal ? '' : scope}/*.${fileFormat}`
),
)
.filter(filterPathByLang(langsToProcess, scopeAndLangFromPathOption)),
Copy link
Collaborator

Choose a reason for hiding this comment

The 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?

});
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/keys-detective/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.
Also, the default config should be different based on what action you are running, otherwise, you need to override it locally like you did here but I'm not sure that's a good practice in the long run.

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.');
Expand All @@ -37,5 +38,6 @@ export function findMissingKeys(inlineConfig: Config) {
addMissingKeys,
emitErrorOnExtraKeys,
fileFormat,
langs,
});
}
16 changes: 16 additions & 0 deletions src/utils/path.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ interface Options extends Pick<Config, 'fileFormat' | 'translationsPath'> {
filePath: string;
}

export function filterPathByLang(
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 }
Expand Down