better-translation 0.4.6 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.d.mts ADDED
@@ -0,0 +1 @@
1
+ export { };
package/dist/cli.mjs ADDED
@@ -0,0 +1,84 @@
1
+ #!/usr/bin/env node
2
+ import { n as getBetterTranslationPluginApi } from "./plugin-tu0NzIIg.mjs";
3
+ import { resolve } from "node:path";
4
+ import { resolveConfig } from "vite";
5
+ //#region src/cli.ts
6
+ const PREFIX = "\x1B[36m[better-translation]\x1B[0m";
7
+ const BOLD = "\x1B[1m";
8
+ const RESET = "\x1B[0m";
9
+ async function main() {
10
+ const [command, ...args] = process.argv.slice(2);
11
+ if (!command || command === "--help" || command === "-h") {
12
+ printHelp();
13
+ return;
14
+ }
15
+ if (command !== "generate") throw new Error(`${PREFIX} unknown command: ${command}`);
16
+ const options = parseGenerateOptions(args);
17
+ if (options.help) {
18
+ printHelp();
19
+ return;
20
+ }
21
+ const pluginApis = (await resolveConfig({
22
+ configFile: options.configFile,
23
+ root: options.root
24
+ }, "serve", options.mode ?? "development")).plugins.flatMap((plugin) => {
25
+ const api = getBetterTranslationPluginApi(plugin);
26
+ return api ? [api] : [];
27
+ });
28
+ if (pluginApis.length === 0) throw new Error(`${PREFIX} no betterTranslation() plugin was found in the resolved Vite config`);
29
+ for (const api of pluginApis) await api.generate();
30
+ }
31
+ function parseGenerateOptions(args) {
32
+ const options = {
33
+ help: false,
34
+ root: process.cwd()
35
+ };
36
+ for (let index = 0; index < args.length; index += 1) {
37
+ const arg = args[index];
38
+ if (!arg) continue;
39
+ if (arg === "--help" || arg === "-h") {
40
+ options.help = true;
41
+ continue;
42
+ }
43
+ if (arg === "--root") {
44
+ options.root = resolve(readOptionValue(args, index, arg));
45
+ index += 1;
46
+ continue;
47
+ }
48
+ if (arg === "--config") {
49
+ options.configFile = resolve(readOptionValue(args, index, arg));
50
+ index += 1;
51
+ continue;
52
+ }
53
+ if (arg === "--mode") {
54
+ options.mode = readOptionValue(args, index, arg);
55
+ index += 1;
56
+ continue;
57
+ }
58
+ throw new Error(`${PREFIX} unknown generate option: ${arg}`);
59
+ }
60
+ return options;
61
+ }
62
+ function readOptionValue(args, index, option) {
63
+ const value = args[index + 1];
64
+ if (!value || value.startsWith("-")) throw new Error(`${PREFIX} missing value for ${option}`);
65
+ return value;
66
+ }
67
+ function printHelp() {
68
+ console.log([
69
+ `${BOLD}Usage:${RESET}`,
70
+ ` better-translation generate [--root <dir>] [--config <file>] [--mode <mode>]`,
71
+ ` bt generate [--root <dir>] [--config <file>] [--mode <mode>]`,
72
+ "",
73
+ `${BOLD}Commands:${RESET}`,
74
+ " generate Scan the Vite project and regenerate local Runtime bundles without starting a dev server."
75
+ ].join("\n"));
76
+ }
77
+ main().then(() => process.exit(0), (error) => {
78
+ console.error(error instanceof Error ? error.message : error);
79
+ process.exit(1);
80
+ });
81
+ //#endregion
82
+ export {};
83
+
84
+ //# sourceMappingURL=cli.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.mjs","names":[],"sources":["../src/cli.ts"],"sourcesContent":["#!/usr/bin/env node\n\nimport { resolve } from \"node:path\"\nimport { resolveConfig } from \"vite\"\n\nimport { getBetterTranslationPluginApi } from \"./plugin.js\"\n\nconst PREFIX = \"\\x1b[36m[better-translation]\\x1b[0m\"\nconst BOLD = \"\\x1b[1m\"\nconst RESET = \"\\x1b[0m\"\n\ninterface GenerateCliOptions {\n configFile?: string\n help: boolean\n mode?: string\n root: string\n}\n\nasync function main() {\n const [command, ...args] = process.argv.slice(2)\n\n if (!command || command === \"--help\" || command === \"-h\") {\n printHelp()\n return\n }\n\n if (command !== \"generate\") {\n throw new Error(`${PREFIX} unknown command: ${command}`)\n }\n\n const options = parseGenerateOptions(args)\n if (options.help) {\n printHelp()\n return\n }\n\n const config = await resolveConfig(\n {\n configFile: options.configFile,\n root: options.root,\n },\n \"serve\",\n options.mode ?? \"development\",\n )\n\n const pluginApis = config.plugins.flatMap((plugin) => {\n const api = getBetterTranslationPluginApi(plugin)\n return api ? [api] : []\n })\n\n if (pluginApis.length === 0) {\n throw new Error(`${PREFIX} no betterTranslation() plugin was found in the resolved Vite config`)\n }\n\n for (const api of pluginApis) {\n await api.generate()\n }\n}\n\nfunction parseGenerateOptions(args: string[]): GenerateCliOptions {\n const options: GenerateCliOptions = { help: false, root: process.cwd() }\n\n for (let index = 0; index < args.length; index += 1) {\n const arg = args[index]\n if (!arg) continue\n\n if (arg === \"--help\" || arg === \"-h\") {\n options.help = true\n continue\n }\n\n if (arg === \"--root\") {\n options.root = resolve(readOptionValue(args, index, arg))\n index += 1\n continue\n }\n\n if (arg === \"--config\") {\n options.configFile = resolve(readOptionValue(args, index, arg))\n index += 1\n continue\n }\n\n if (arg === \"--mode\") {\n options.mode = readOptionValue(args, index, arg)\n index += 1\n continue\n }\n\n throw new Error(`${PREFIX} unknown generate option: ${arg}`)\n }\n\n return options\n}\n\nfunction readOptionValue(args: string[], index: number, option: string) {\n const value = args[index + 1]\n if (!value || value.startsWith(\"-\")) throw new Error(`${PREFIX} missing value for ${option}`)\n return value\n}\n\nfunction printHelp() {\n console.log(\n [\n `${BOLD}Usage:${RESET}`,\n ` better-translation generate [--root <dir>] [--config <file>] [--mode <mode>]`,\n ` bt generate [--root <dir>] [--config <file>] [--mode <mode>]`,\n \"\",\n `${BOLD}Commands:${RESET}`,\n \" generate Scan the Vite project and regenerate local Runtime bundles without starting a dev server.\",\n ].join(\"\\n\"),\n )\n}\n\nmain().then(\n () => process.exit(0),\n (error: unknown) => {\n console.error(error instanceof Error ? error.message : error)\n process.exit(1)\n },\n)\n"],"mappings":";;;;;AAOA,MAAM,SAAS;AACf,MAAM,OAAO;AACb,MAAM,QAAQ;AASd,eAAe,OAAO;CACpB,MAAM,CAAC,SAAS,GAAG,QAAQ,QAAQ,KAAK,MAAM,CAAC;CAE/C,IAAI,CAAC,WAAW,YAAY,YAAY,YAAY,MAAM;EACxD,UA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