Skip to content

Commit

Permalink
feat: handle scoped only libs
Browse files Browse the repository at this point in the history
  • Loading branch information
aymeric-duchein committed Feb 19, 2024
1 parent 9affd8e commit 32a4fb0
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 14 deletions.
30 changes: 17 additions & 13 deletions src/keys-builder/create-translation-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export function createTranslationFiles({
removeExtraKeys,
scopes,
fileFormat,
scopedOnly,
}: Config & { scopeToKeys: ScopeMap }) {
const logger = getLogger();

Expand All @@ -23,21 +24,24 @@ export function createTranslationFiles({
output,
fileFormat,
});
const globalFiles = langs.map((lang) => ({
path: `${output}/${lang}.${fileFormat}`,
}));

const actions: FileAction[] = [];

for (const { path } of globalFiles) {
actions.push(
buildTranslationFile({
path,
translation: scopeToKeys.__global,
replace,
removeExtraKeys,
fileFormat,
})
);
if (!scopedOnly) {
const globalFiles = langs.map((lang) => ({
path: `${output}/${lang}.${fileFormat}`,
}));
for (const { path } of globalFiles) {
actions.push(
buildTranslationFile({
path,
translation: scopeToKeys.__global,
replace,
removeExtraKeys,
fileFormat,
})
);
}
}

for (const { path, scope } of scopeFiles) {
Expand Down
13 changes: 13 additions & 0 deletions src/keys-builder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ export function buildTranslationFiles(inlineConfig: Config) {
const result = buildKeys(config);
const { scopeToKeys, fileCount } = result;

if (config.scopedOnly) {
if (Object.keys(scopeToKeys.__global).length) {
logger.log(
'\n\x1b[31m%s\x1b[0m',
'⚠️',
'Global keys found with scopedOnly flag active\n'
);
if (config.emitErrorOnExtraKeys) {
process.exit(2);
}
}
delete scopeToKeys.__global;
}
logger.success(`${messages.extract} 🗝`);

let keysFound = 0;
Expand Down
13 changes: 13 additions & 0 deletions src/keys-detective/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ export function findMissingKeys(inlineConfig: Config) {

const result = buildKeys(config);
logger.success(`${messages.extract} 🗝`);
if (config.scopedOnly) {
if (Object.keys(result.scopeToKeys.__global).length) {
logger.log(
'\n\x1b[31m%s\x1b[0m',
'⚠️',
'Global keys found with scopedOnly flag active\n'
);
if (config.emitErrorOnExtraKeys) {
process.exit(2);
}
}
delete result.scopeToKeys.__global;
}

const { addMissingKeys, emitErrorOnExtraKeys } = config;
compareKeysToFiles({
Expand Down
6 changes: 5 additions & 1 deletion src/marker.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export function marker<T extends string | string[]>(key: T): T {
export function marker<T extends string | string[]>(
key: T,
params?: unknown,
lang?: string
): T {
return key;
}
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type Config = {
addMissingKeys?: boolean;
removeExtraKeys?: boolean;
emitErrorOnExtraKeys?: boolean;
scopedOnly?: boolean;
scopes?: Scopes;
scopePathMap?: {
[scopeAlias: string]: string;
Expand Down

0 comments on commit 32a4fb0

Please sign in to comment.