Skip to content

Commit 0f6b75d

Browse files
feat: handle scoped only libs
1 parent 16a3382 commit 0f6b75d

File tree

5 files changed

+51
-16
lines changed

5 files changed

+51
-16
lines changed

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

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

@@ -23,21 +24,24 @@ export async 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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { setConfig } from '../config';
22
import { messages } from '../messages';
3-
import { Config } from '../types';
3+
import { Config, ScopeMap } from '../types';
44
import { countKeys } from '../utils/keys.utils';
55
import { getLogger } from '../utils/logger';
66
import { resolveConfig } from '../utils/resolve-config';
@@ -23,6 +23,19 @@ export async 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 as Partial<ScopeMap>).__global;
38+
}
2639
logger.success(`${messages.extract} 🗝`);
2740

2841
let keysFound = 0;

src/keys-detective/index.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { setConfig } from '../config';
22
import { buildKeys } from '../keys-builder/build-keys';
33
import { messages } from '../messages';
4-
import { Config } from '../types';
4+
import { Config, ScopeMap } from '../types';
55
import { getLogger } from '../utils/logger';
66
import { resolveConfig } from '../utils/resolve-config';
77

@@ -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 as Partial<ScopeMap>).__global;
44+
}
3245

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

src/marker.ts

Lines changed: 5 additions & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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)