From 0f6b75d1456dcc0125c443b0e4c1676814a861c0 Mon Sep 17 00:00:00 2001 From: Aymeric Duchein Date: Mon, 19 Feb 2024 15:13:59 +0100 Subject: [PATCH] feat: handle scoped only libs --- src/keys-builder/create-translation-files.ts | 30 +++++++++++--------- src/keys-builder/index.ts | 15 +++++++++- src/keys-detective/index.ts | 15 +++++++++- src/marker.ts | 6 +++- src/types.ts | 1 + 5 files changed, 51 insertions(+), 16 deletions(-) diff --git a/src/keys-builder/create-translation-files.ts b/src/keys-builder/create-translation-files.ts index f68ad7a..526f111 100644 --- a/src/keys-builder/create-translation-files.ts +++ b/src/keys-builder/create-translation-files.ts @@ -14,6 +14,7 @@ export async function createTranslationFiles({ removeExtraKeys, scopes, fileFormat, + scopedOnly, }: Config & { scopeToKeys: ScopeMap }) { const logger = getLogger(); @@ -23,21 +24,24 @@ export async 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) { diff --git a/src/keys-builder/index.ts b/src/keys-builder/index.ts index 58726b4..6fcd819 100644 --- a/src/keys-builder/index.ts +++ b/src/keys-builder/index.ts @@ -1,6 +1,6 @@ import { setConfig } from '../config'; import { messages } from '../messages'; -import { Config } from '../types'; +import { Config, ScopeMap } from '../types'; import { countKeys } from '../utils/keys.utils'; import { getLogger } from '../utils/logger'; import { resolveConfig } from '../utils/resolve-config'; @@ -23,6 +23,19 @@ export async 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 as Partial).__global; + } logger.success(`${messages.extract} 🗝`); let keysFound = 0; diff --git a/src/keys-detective/index.ts b/src/keys-detective/index.ts index e5d0d48..af04c3f 100644 --- a/src/keys-detective/index.ts +++ b/src/keys-detective/index.ts @@ -1,7 +1,7 @@ import { setConfig } from '../config'; import { buildKeys } from '../keys-builder/build-keys'; import { messages } from '../messages'; -import { Config } from '../types'; +import { Config, ScopeMap } from '../types'; import { getLogger } from '../utils/logger'; import { resolveConfig } from '../utils/resolve-config'; @@ -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 as Partial).__global; + } const { addMissingKeys, emitErrorOnExtraKeys } = config; compareKeysToFiles({ diff --git a/src/marker.ts b/src/marker.ts index 4a8ad4d..94a2273 100644 --- a/src/marker.ts +++ b/src/marker.ts @@ -1,3 +1,7 @@ -export function marker(key: T): T { +export function marker( + key: T, + params?: unknown, + lang?: string +): T { return key; } diff --git a/src/types.ts b/src/types.ts index 380b366..5accb4d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -9,6 +9,7 @@ export type Config = { addMissingKeys: boolean; removeExtraKeys: boolean; emitErrorOnExtraKeys: boolean; + scopedOnly?: boolean; scopes: Scopes; scopePathMap?: { [scopeAlias: string]: string;