Skip to content

Commit 27a6419

Browse files
authored
feat(cli): add preservedKeys support to all commands (#1962)
1 parent 92e75d8 commit 27a6419

File tree

10 files changed

+59
-4
lines changed

10 files changed

+59
-4
lines changed

.changeset/wild-points-stop.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"lingo.dev": minor
3+
---
4+
5+
Add preservedKeys support to all CLI commands (purge, cleanup, status, lockfile, i18n) and create `show preserved-keys` command

packages/cli/src/cli/cmd/cleanup.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export default new Command()
7373
bucket.lockedKeys,
7474
bucket.lockedPatterns,
7575
bucket.ignoredKeys,
76+
bucket.preservedKeys,
7677
);
7778
bucketLoader.setDefaultLocale(sourceLocale);
7879

packages/cli/src/cli/cmd/i18n.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ export default new Command()
226226
bucket.lockedKeys,
227227
bucket.lockedPatterns,
228228
bucket.ignoredKeys,
229+
bucket.preservedKeys,
229230
);
230231
bucketLoader.setDefaultLocale(sourceLocale);
231232
await bucketLoader.init();
@@ -265,6 +266,7 @@ export default new Command()
265266
bucket.lockedKeys,
266267
bucket.lockedPatterns,
267268
bucket.ignoredKeys,
269+
bucket.preservedKeys,
268270
);
269271
bucketLoader.setDefaultLocale(sourceLocale);
270272
await bucketLoader.init();
@@ -377,6 +379,7 @@ export default new Command()
377379
bucket.lockedKeys,
378380
bucket.lockedPatterns,
379381
bucket.ignoredKeys,
382+
bucket.preservedKeys,
380383
);
381384
bucketLoader.setDefaultLocale(sourceLocale);
382385
await bucketLoader.init();

packages/cli/src/cli/cmd/lockfile.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export default new Command()
4646
bucket.lockedKeys,
4747
bucket.lockedPatterns,
4848
bucket.ignoredKeys,
49+
bucket.preservedKeys,
4950
);
5051
bucketLoader.setDefaultLocale(sourceLocale);
5152

packages/cli/src/cli/cmd/purge.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ export default new Command()
105105
bucket.lockedKeys,
106106
bucket.lockedPatterns,
107107
bucket.ignoredKeys,
108+
bucket.preservedKeys,
108109
);
109110
await bucketLoader.init();
110111
bucketLoader.setDefaultLocale(sourceLocale);

packages/cli/src/cli/cmd/show/_shared-key-command.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
formatDisplayValue,
66
} from "../../utils/key-matching";
77

8-
export type KeyFilterType = "lockedKeys" | "ignoredKeys";
8+
export type KeyFilterType = "lockedKeys" | "ignoredKeys" | "preservedKeys";
99

1010
export interface KeyCommandOptions {
1111
bucket?: string;

packages/cli/src/cli/cmd/show/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import localeCmd from "./locale";
55
import filesCmd from "./files";
66
import lockedKeysCmd from "./locked-keys";
77
import ignoredKeysCmd from "./ignored-keys";
8+
import preservedKeysCmd from "./preserved-keys";
89

910
export default new Command()
1011
.command("show")
@@ -14,4 +15,5 @@ export default new Command()
1415
.addCommand(localeCmd)
1516
.addCommand(filesCmd)
1617
.addCommand(lockedKeysCmd)
17-
.addCommand(ignoredKeysCmd);
18+
.addCommand(ignoredKeysCmd)
19+
.addCommand(preservedKeysCmd);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { Command } from "interactive-commander";
2+
import Ora from "ora";
3+
import { getConfig } from "../../utils/config";
4+
import { CLIError } from "../../utils/errors";
5+
import { getBuckets } from "../../utils/buckets";
6+
import { executeKeyCommand } from "./_shared-key-command";
7+
8+
export default new Command()
9+
.command("preserved-keys")
10+
.description(
11+
"Show which key-value pairs in source files match preservedKeys patterns",
12+
)
13+
.option("--bucket <name>", "Only show preserved keys for a specific bucket")
14+
.helpOption("-h, --help", "Show help")
15+
.action(async (options) => {
16+
const ora = Ora();
17+
try {
18+
const i18nConfig = await getConfig();
19+
20+
if (!i18nConfig) {
21+
throw new CLIError({
22+
message:
23+
"i18n.json not found. Please run `lingo.dev init` to initialize the project.",
24+
docUrl: "i18nNotFound",
25+
});
26+
}
27+
28+
const buckets = getBuckets(i18nConfig);
29+
30+
await executeKeyCommand(i18nConfig, buckets, options, {
31+
filterType: "preservedKeys",
32+
displayName: "preserved",
33+
});
34+
} catch (error: any) {
35+
ora.fail(error.message);
36+
process.exit(1);
37+
}
38+
});

packages/cli/src/cli/cmd/status.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ export default new Command()
203203
bucket.lockedKeys,
204204
bucket.lockedPatterns,
205205
bucket.ignoredKeys,
206+
bucket.preservedKeys,
206207
);
207208

208209
bucketLoader.setDefaultLocale(sourceLocale);

packages/cli/src/cli/loaders/index.spec.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,7 @@ describe("bucket loaders", () => {
10271027
undefined, // lockedKeys
10281028
undefined, // lockedPatterns
10291029
["ignored.key", "nested/ignored"], // ignoredKeys
1030+
undefined, // preservedKeys
10301031
);
10311032

10321033
jsonLoader.setDefaultLocale("en");
@@ -1078,6 +1079,7 @@ describe("bucket loaders", () => {
10781079
undefined, // lockedKeys
10791080
undefined, // lockedPatterns
10801081
["wildcard_*"], // ignoredKeys with wildcard
1082+
undefined, // preservedKeys
10811083
);
10821084

10831085
jsonLoader.setDefaultLocale("en");
@@ -4266,13 +4268,14 @@ Línea 3`;
42664268
// Locked keys should be filtered out (they get flattened and encoded)
42674269
// After flat loader, keys become "hello%20world/stringUnit" and "%25lld%20unit_days/variations/plural"
42684270
expect(dataForTranslation).not.toHaveProperty("hello%20world/stringUnit");
4269-
expect(dataForTranslation).not.toHaveProperty("%25lld%20unit_days/variations/plural");
4271+
expect(dataForTranslation).not.toHaveProperty(
4272+
"%25lld%20unit_days/variations/plural",
4273+
);
42704274

42714275
// Non-locked keys should remain (flattened)
42724276
expect(dataForTranslation).toHaveProperty("regular_key/stringUnit");
42734277
expect(dataForTranslation["regular_key/stringUnit"]).toBe("Regular");
42744278
});
4275-
42764279
});
42774280

42784281
describe("typescript bucket loader", () => {

0 commit comments

Comments
 (0)