@svelte-i18n/cli 0.1.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/LICENSE.md +21 -0
- package/README.md +5 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +39 -0
- package/dist/index.js.map +1 -0
- package/dist/utils.d.ts +6 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +30 -0
- package/dist/utils.js.map +1 -0
- package/package.json +42 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":""}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { Command } from 'commander';
|
|
3
|
+
import PACKAGE from '../package.json' with { type: 'json' };
|
|
4
|
+
import { importJSON, fetchJSON, isURL, t } from './utils.js';
|
|
5
|
+
import { z } from 'zod';
|
|
6
|
+
import { writeFile, mkdir } from 'node:fs/promises';
|
|
7
|
+
import { dirname } from 'node:path';
|
|
8
|
+
const program = new Command();
|
|
9
|
+
const schema = z.record(z.string(), z.string());
|
|
10
|
+
// prettier-ignore
|
|
11
|
+
program.name('svelte-i18n')
|
|
12
|
+
.description('CLI tool for svelte-i18n')
|
|
13
|
+
.version(PACKAGE.version);
|
|
14
|
+
program
|
|
15
|
+
.command('generate-types')
|
|
16
|
+
.description('Generate TypeScript types for translations')
|
|
17
|
+
.option('--input <path>', 'Path to the translations file (e.g., src/locales/en.json) or a URL (e.g., https://example.com/locales/en.json)')
|
|
18
|
+
.option('--output <path>', 'Path to the output file (e.g., src/locales/en.d.ts)', './i18n-types.d.ts')
|
|
19
|
+
.action(async ({ input, output }) => {
|
|
20
|
+
const [json, error] = await t(() => (isURL(input) ? fetchJSON(input) : importJSON(input)));
|
|
21
|
+
if (error) {
|
|
22
|
+
return program.error(`Failed to load JSON from ${input}: ${error instanceof Error ? error.message : String(error)}`);
|
|
23
|
+
}
|
|
24
|
+
const [validatedJson, validationError] = await t(() => schema.parse(json));
|
|
25
|
+
if (validationError || !validatedJson) {
|
|
26
|
+
return program.error(`Failed to validate JSON from ${input}: ${validationError instanceof Error ? validationError.message : String(validationError)}`);
|
|
27
|
+
}
|
|
28
|
+
const keys = Object.keys(validatedJson);
|
|
29
|
+
const typeName = 'I18nDictionary';
|
|
30
|
+
const typeDefinition = `export type ${typeName} = {\n${keys
|
|
31
|
+
.map((key) => ` '${key.replace(/'/g, "\\'")}': string;`)
|
|
32
|
+
.join('\n')}\n};\n`;
|
|
33
|
+
await mkdir(dirname(output), { recursive: true });
|
|
34
|
+
writeFile(output, typeDefinition, {})
|
|
35
|
+
.then(() => console.log('Type definitions generated successfully in i18n-types.d.ts'))
|
|
36
|
+
.catch((err) => program.error(`Failed to write type definitions: ${err instanceof Error ? err.message : String(err)}`));
|
|
37
|
+
});
|
|
38
|
+
program.parse();
|
|
39
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AACA,OAAO,EAAU,OAAO,EAAE,MAAM,WAAW,CAAC;AAC5C,OAAO,OAAO,MAAM,iBAAiB,CAAC,OAAO,IAAI,EAAE,MAAM,EAAE,CAAC;AAC5D,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC,EAAE,MAAM,YAAY,CAAC;AAC7D,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AACpD,OAAO,EAAS,OAAO,EAAE,MAAM,WAAW,CAAC;AAE3C,MAAM,OAAO,GAAG,IAAI,OAAO,EAAE,CAAC;AAC9B,MAAM,MAAM,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC;AAEhD,kBAAkB;AAClB,OAAO,CAAC,IAAI,CAAC,aAAa,CAAC;KACtB,WAAW,CAAC,0BAA0B,CAAC;KACvC,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;AAE9B,OAAO;KACF,OAAO,CAAC,gBAAgB,CAAC;KACzB,WAAW,CAAC,4CAA4C,CAAC;KACzD,MAAM,CACH,gBAAgB,EAChB,gHAAgH,CACnH;KAC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IAExB,MAAM,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;IAElD,SAAS,CAAC,MAAM,EAAE,cAAc,EAAE,EAAE,CAAC;SAChC,IAAI,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC,4DAA4D,CAAC,CAAC;SACrF,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE,CACX,OAAO,CAAC,KAAK,CACT,qCAAqC,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,CAC1F,CACJ,CAAC;AACV,CAAC,CAAC,CAAC;AAEP,OAAO,CAAC,KAAK,EAAE,CAAC"}
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export type MaybePromise<T> = T | Promise<T>;
|
|
2
|
+
export declare function t<T>(fn: () => T): MaybePromise<[T, null] | [null, unknown]>;
|
|
3
|
+
export declare function isURL(str: string): boolean;
|
|
4
|
+
export declare function importJSON(path: string): Promise<any>;
|
|
5
|
+
export declare function fetchJSON(url: string): Promise<any>;
|
|
6
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;AAE7C,wBAAgB,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,CAAC,GAAG,YAAY,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,CAW3E;AAED,wBAAgB,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAO1C;AAED,wBAAsB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,CAE3D;AAED,wBAAsB,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,CAGzD"}
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export function t(fn) {
|
|
2
|
+
try {
|
|
3
|
+
const result = fn();
|
|
4
|
+
return result instanceof Promise
|
|
5
|
+
? result
|
|
6
|
+
.then((res) => [res, null])
|
|
7
|
+
.catch((err) => [null, err])
|
|
8
|
+
: [result, null];
|
|
9
|
+
}
|
|
10
|
+
catch (error) {
|
|
11
|
+
return [null, error];
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export function isURL(str) {
|
|
15
|
+
try {
|
|
16
|
+
new URL(str);
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export async function importJSON(path) {
|
|
24
|
+
return (await import(path, { with: { type: 'json' } })).default;
|
|
25
|
+
}
|
|
26
|
+
export async function fetchJSON(url) {
|
|
27
|
+
const result = await fetch(url);
|
|
28
|
+
return await result.json();
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,CAAC,CAAI,EAAW;IAC5B,IAAI,CAAC;QACD,MAAM,MAAM,GAAG,EAAE,EAAE,CAAC;QACpB,OAAO,MAAM,YAAY,OAAO;YAC5B,CAAC,CAAC,MAAM;iBACD,IAAI,CAAC,CAAC,GAAG,EAAa,EAAE,CAAC,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;iBACrC,KAAK,CAAC,CAAC,GAAG,EAAmB,EAAE,CAAC,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;YACnD,CAAC,CAAC,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IACzB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACb,OAAO,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;IACzB,CAAC;AACL,CAAC;AAED,MAAM,UAAU,KAAK,CAAC,GAAW;IAC7B,IAAI,CAAC;QACD,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC;QACb,OAAO,IAAI,CAAC;IAChB,CAAC;IAAC,MAAM,CAAC;QACL,OAAO,KAAK,CAAC;IACjB,CAAC;AACL,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,UAAU,CAAC,IAAY;IACzC,OAAO,CAAC,MAAM,MAAM,CAAC,IAAI,EAAE,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC;AACpE,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,SAAS,CAAC,GAAW;IACvC,MAAM,MAAM,GAAG,MAAM,KAAK,CAAC,GAAG,CAAC,CAAC;IAChC,OAAO,MAAM,MAAM,CAAC,IAAI,EAAE,CAAC;AAC/B,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@svelte-i18n/cli",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "CLI tool for svelte-i18n",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"svelte-i18n": "./dist/index.js"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/svelte-intl/cli.git"
|
|
12
|
+
},
|
|
13
|
+
"main": "./dist/index.js",
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"commander": "^14.0.3",
|
|
19
|
+
"zod": "^4.3.6"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@changesets/cli": "^2.30.0",
|
|
23
|
+
"@types/node": "^25.5.0",
|
|
24
|
+
"prettier": "^3.8.1",
|
|
25
|
+
"tsx": "^4.21.0",
|
|
26
|
+
"typescript": "^5.9.3"
|
|
27
|
+
},
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=22"
|
|
30
|
+
},
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "tsc",
|
|
34
|
+
"dev": "tsx watch src/index.ts generate-types --input http://localhost:5173/api/locales/en --output ./.i18n/types.ts",
|
|
35
|
+
"start": "node dist/index.js",
|
|
36
|
+
"format": "prettier --write src",
|
|
37
|
+
"format:check": "prettier --check src",
|
|
38
|
+
"changeset": "changeset",
|
|
39
|
+
"version": "changeset version",
|
|
40
|
+
"release": "pnpm build && changeset publish"
|
|
41
|
+
}
|
|
42
|
+
}
|