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: handle scoped only libs #179

Open
wants to merge 1 commit 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
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 async function createTranslationFiles({
removeExtraKeys,
scopes,
fileFormat,
scopedOnly,
}: Config & { scopeToKeys: ScopeMap }) {
const logger = getLogger();

Expand All @@ -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) {
Copy link
Collaborator

@shaharkazaz shaharkazaz Apr 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally speaking, shouldn't we just skip creation if there aren't any keys there? instead of introducing a new config? or maybe introduce "createEmptyFiles" or something, WDYT?

actions.push(
buildTranslationFile({
path,
translation: scopeToKeys.__global,
replace,
removeExtraKeys,
fileFormat,
}),
);
}
}

for (const { path, scope } of scopeFiles) {
Expand Down
15 changes: 14 additions & 1 deletion src/keys-builder/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why exit code 2? 🤔

}
}
delete (scopeToKeys as Partial<ScopeMap>).__global;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to delete the key?

}
logger.success(`${messages.extract} 🗝`);

let keysFound = 0;
Expand Down
15 changes: 14 additions & 1 deletion src/keys-detective/index.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down 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 as Partial<ScopeMap>).__global;
}
Comment on lines +32 to +44
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate code from the builder


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
Loading