Skip to content

Commit 32a4fb0

Browse files
feat: handle scoped only libs
1 parent 9affd8e commit 32a4fb0

File tree

5 files changed

+49
-14
lines changed

5 files changed

+49
-14
lines changed

src/keys-builder/create-translation-files.ts

+17-13
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export function createTranslationFiles({
1414
removeExtraKeys,
1515
scopes,
1616
fileFormat,
17+
scopedOnly,
1718
}: Config & { scopeToKeys: ScopeMap }) {
1819
const logger = getLogger();
1920

@@ -23,21 +24,24 @@ export function createTranslationFiles({
2324
output,
2425
fileFormat,
2526
});
26-
const globalFiles = langs.map((lang) => ({
27-
path: `${output}/${lang}.${fileFormat}`,
28-
}));
27+
2928
const actions: FileAction[] = [];
3029

31-
for (const { path } of globalFiles) {
32-
actions.push(
33-
buildTranslationFile({
34-
path,
35-
translation: scopeToKeys.__global,
36-
replace,
37-
removeExtraKeys,
38-
fileFormat,
39-
})
40-
);
30+
if (!scopedOnly) {
31+
const globalFiles = langs.map((lang) => ({
32+
path: `${output}/${lang}.${fileFormat}`,
33+
}));
34+
for (const { path } of globalFiles) {
35+
actions.push(
36+
buildTranslationFile({
37+
path,
38+
translation: scopeToKeys.__global,
39+
replace,
40+
removeExtraKeys,
41+
fileFormat,
42+
})
43+
);
44+
}
4145
}
4246

4347
for (const { path, scope } of scopeFiles) {

src/keys-builder/index.ts

+13
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ export function buildTranslationFiles(inlineConfig: Config) {
2323
const result = buildKeys(config);
2424
const { scopeToKeys, fileCount } = result;
2525

26+
if (config.scopedOnly) {
27+
if (Object.keys(scopeToKeys.__global).length) {
28+
logger.log(
29+
'\n\x1b[31m%s\x1b[0m',
30+
'⚠️',
31+
'Global keys found with scopedOnly flag active\n'
32+
);
33+
if (config.emitErrorOnExtraKeys) {
34+
process.exit(2);
35+
}
36+
}
37+
delete scopeToKeys.__global;
38+
}
2639
logger.success(`${messages.extract} 🗝`);
2740

2841
let keysFound = 0;

src/keys-detective/index.ts

+13
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ export function findMissingKeys(inlineConfig: Config) {
2929

3030
const result = buildKeys(config);
3131
logger.success(`${messages.extract} 🗝`);
32+
if (config.scopedOnly) {
33+
if (Object.keys(result.scopeToKeys.__global).length) {
34+
logger.log(
35+
'\n\x1b[31m%s\x1b[0m',
36+
'⚠️',
37+
'Global keys found with scopedOnly flag active\n'
38+
);
39+
if (config.emitErrorOnExtraKeys) {
40+
process.exit(2);
41+
}
42+
}
43+
delete result.scopeToKeys.__global;
44+
}
3245

3346
const { addMissingKeys, emitErrorOnExtraKeys } = config;
3447
compareKeysToFiles({

src/marker.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
export function marker<T extends string | string[]>(key: T): T {
1+
export function marker<T extends string | string[]>(
2+
key: T,
3+
params?: unknown,
4+
lang?: string
5+
): T {
26
return key;
37
}

src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export type Config = {
99
addMissingKeys?: boolean;
1010
removeExtraKeys?: boolean;
1111
emitErrorOnExtraKeys?: boolean;
12+
scopedOnly?: boolean;
1213
scopes?: Scopes;
1314
scopePathMap?: {
1415
[scopeAlias: string]: string;

0 commit comments

Comments
 (0)