@ai-translate/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 +21 -0
- package/README.md +199 -0
- package/dist/bin.d.mts +1 -0
- package/dist/bin.mjs +9 -0
- package/dist/bin.mjs.map +1 -0
- package/dist/index.d.mts +15 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +2 -0
- package/dist/src-Cnnq284f.mjs +947 -0
- package/dist/src-Cnnq284f.mjs.map +1 -0
- package/package.json +65 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Thiago Peres
|
|
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
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
# @ai-translate/cli
|
|
2
|
+
|
|
3
|
+
The `ai-translate` command line interface. It loads your config, runs a sync, check, or audit, and prints a JSON report.
|
|
4
|
+
|
|
5
|
+
See the [project README](../../README.md) for what the toolkit does and how a sync decides what to translate.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install --save-dev @ai-translate/cli
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Requires Node 20.19 or newer.
|
|
14
|
+
|
|
15
|
+
## Configuration
|
|
16
|
+
|
|
17
|
+
The CLI looks for the first of these in the current working directory, unless you pass `--config`:
|
|
18
|
+
|
|
19
|
+
```
|
|
20
|
+
ai-translate.config.ts
|
|
21
|
+
ai-translate.config.mts
|
|
22
|
+
ai-translate.config.js
|
|
23
|
+
ai-translate.config.mjs
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
TypeScript configs are loaded with [jiti](https://github.com/unjs/jiti), so no build step is needed. The file must default-export an object with at least `catalogs`, `provider`, `sourceLocale`, `state`, and `targetLocales`. Wrap it in `defineConfig` for type checking:
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
import { defineConfig } from "@ai-translate/cli";
|
|
30
|
+
|
|
31
|
+
export default defineConfig({
|
|
32
|
+
sourceLocale: "en",
|
|
33
|
+
targetLocales: ["de", "fr"],
|
|
34
|
+
catalogs: [/* ... */],
|
|
35
|
+
state: /* ... */,
|
|
36
|
+
provider: /* ... */,
|
|
37
|
+
});
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
The full config surface is documented as types in [`@ai-translate/core`](../ai-translate-core), on the `AiTranslateConfig` interface.
|
|
41
|
+
|
|
42
|
+
### Environment variables
|
|
43
|
+
|
|
44
|
+
Before every command the CLI loads, in order and without overriding anything already set in the environment:
|
|
45
|
+
|
|
46
|
+
```
|
|
47
|
+
.env
|
|
48
|
+
.env.local
|
|
49
|
+
.env.$NODE_ENV
|
|
50
|
+
.env.$NODE_ENV.local
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
This is how a provider API key normally reaches your config, for example `apiKey: process.env.OPENAI_API_KEY`.
|
|
54
|
+
|
|
55
|
+
## Commands
|
|
56
|
+
|
|
57
|
+
### `init`
|
|
58
|
+
|
|
59
|
+
Detects the project's Next.js localization setup and writes `ai-translate.config.ts` for it. This is the only command that runs without an existing config.
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
ai-translate init
|
|
63
|
+
ai-translate init --preview
|
|
64
|
+
ai-translate init --integration i18next
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Recognises **next-intl** and **i18next** (including `react-i18next` and `next-i18next`), inferring the message layout, the locale list, and the source locale, then printing the evidence behind each conclusion.
|
|
68
|
+
|
|
69
|
+
It writes exactly one file and nothing else. Installing packages, setting `OPENAI_API_KEY`, and reviewing the model choice are printed as next steps rather than done for you, so running it against an unfamiliar repository is safe.
|
|
70
|
+
|
|
71
|
+
| Flag | Effect |
|
|
72
|
+
| --- | --- |
|
|
73
|
+
| `--preview` | Print the config that would be written and touch nothing. |
|
|
74
|
+
| `--integration <id>` | Choose between setups when a project matches more than one. |
|
|
75
|
+
| `--force` | Overwrite an existing `ai-translate.config.ts`. Refuses without it. |
|
|
76
|
+
|
|
77
|
+
Exits non-zero when nothing is recognised, when a named integration was not detected, or when a config already exists and `--force` was not passed.
|
|
78
|
+
|
|
79
|
+
### `sync`
|
|
80
|
+
|
|
81
|
+
Translates everything that needs it, validates each candidate, runs semantic audits if configured, and writes the results.
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
ai-translate sync
|
|
85
|
+
ai-translate sync --dry-run
|
|
86
|
+
ai-translate sync --locale de --catalog messages
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Writes happen inside a staged transaction: files and state are committed together only if the run converges, so an interrupted or failing sync leaves your content untouched. When semantic audits reject a translation, the run retries it up to `validation.semanticRepairAttempts` times before failing.
|
|
90
|
+
|
|
91
|
+
Exits non-zero if any entry failed, if audits did not converge, or if a `--dry-run` exceeded the configured `validation.dryRunBudget`.
|
|
92
|
+
|
|
93
|
+
### `check`
|
|
94
|
+
|
|
95
|
+
The CI gate. Runs validation, a dry-run sync, and an audit provenance check, and refuses to write anything — the state store is swapped for a read-only snapshot for the duration.
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
ai-translate check
|
|
99
|
+
ai-translate check --locale de
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Exits non-zero if validation reports an error, if a sync would have work to do, or if audit provenance is missing or stale. The error message tells you which: run `ai-translate sync` for pending content, `ai-translate audit --refresh` for stale audits.
|
|
103
|
+
|
|
104
|
+
### `validate`
|
|
105
|
+
|
|
106
|
+
Structural and source-level validation with no provider calls. Reports source document counts, target locales, and any issues.
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
ai-translate validate
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### `audit`
|
|
113
|
+
|
|
114
|
+
Runs the configured semantic audits over existing translations.
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
ai-translate audit # audit anything not already covered
|
|
118
|
+
ai-translate audit --check # verify stored provenance only, no provider calls
|
|
119
|
+
ai-translate audit --refresh # re-run audits even where provenance exists
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### `new-locale <locale>`
|
|
123
|
+
|
|
124
|
+
Scaffolds the files for a new locale and translates it, in a single transaction.
|
|
125
|
+
|
|
126
|
+
```bash
|
|
127
|
+
ai-translate new-locale pt
|
|
128
|
+
ai-translate new-locale pt --from es --strategy copy-locale-and-retranslate
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
`--from` defaults to the source locale and `--strategy` to `copy-source`. Strategies are `copy-source`, `copy-locale`, `copy-locale-and-retranslate`, and `empty`; anything other than `copy-source` requires `--from` to be an already-translated locale. With `--dry-run`, only `--from <sourceLocale>` and `--strategy copy-source` are supported.
|
|
132
|
+
|
|
133
|
+
### `scaffold-locale <locale> --from <locale>`
|
|
134
|
+
|
|
135
|
+
Creates the files for a locale without translating anything. Defaults to `--strategy copy-locale`.
|
|
136
|
+
|
|
137
|
+
### `adopt`
|
|
138
|
+
|
|
139
|
+
One-time migration from whatever tool translated your catalogs before this one. It reads the catalogs themselves and records every existing translation as state, so the first `sync` afterwards only translates what is genuinely missing instead of redoing the whole corpus.
|
|
140
|
+
|
|
141
|
+
Because catalogs carry no evidence of who wrote their text, every adopted entry is recorded with an origin of `legacy-unknown`. What happens to those entries later is up to `legacyOriginPolicy` in your config, and the choice matters more than it looks:
|
|
142
|
+
|
|
143
|
+
| Policy | First sync after adopting | Once English changes |
|
|
144
|
+
| --- | --- | --- |
|
|
145
|
+
| `preserve` (default) | Nothing. | The entry is reported as stale rather than retranslated, so `check` fails and a human decides. |
|
|
146
|
+
| `validate-existing` | Runs the deterministic validators over the existing text, no model calls, and promotes surviving entries to `generated`. | Retranslated automatically, like any other entry. |
|
|
147
|
+
| `retranslate` | Regenerates every adopted entry under the current contract. | Retranslated automatically. |
|
|
148
|
+
|
|
149
|
+
`validate-existing` is the one you want for a migration. It costs no model calls, it checks the inherited text instead of trusting it, and it graduates the corpus into normal behaviour in a single pass — otherwise every adopted entry keeps acting like a hand-written override and your next copy edit turns into a CI failure instead of a translation. Expect that first pass to surface a punch list of pre-existing problems; that is the validators doing their job.
|
|
150
|
+
|
|
151
|
+
A source string with no target text, or an empty one, is left out of state entirely so the next sync still picks it up.
|
|
152
|
+
|
|
153
|
+
Target text byte-identical to the source is ambiguous — it is either a correct translation that happens to match (`Excel`, or `Status` in German) or a placeholder from a pipeline that backfilled missing keys with English. `--identical-to-source adopt` is the default and keeps it; `skip` leaves those entries to the next sync. The command reports the count either way, so start with `--dry-run` and decide from the number.
|
|
154
|
+
|
|
155
|
+
```bash
|
|
156
|
+
ai-translate adopt --dry-run
|
|
157
|
+
ai-translate adopt
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## Flags
|
|
161
|
+
|
|
162
|
+
| Flag | Commands | Meaning |
|
|
163
|
+
| --- | --- | --- |
|
|
164
|
+
| `--config <path>` | all | Path to the config file instead of auto-discovery. |
|
|
165
|
+
| `--locale <locale>` | sync, check, audit, validate | Limit to a locale. Repeatable. |
|
|
166
|
+
| `--catalog <id>` | sync, check, audit, validate | Limit to a catalog. Repeatable. |
|
|
167
|
+
| `--unit <id>` | sync, check, audit, validate | Limit to a document unit. Repeatable. |
|
|
168
|
+
| `--include-path <pointer>` | sync, check, audit, validate | Limit to exact JSON pointers; everything else is left untouched. Repeatable. |
|
|
169
|
+
| `--dry-run` | sync, new-locale, adopt | Plan the work and report it without writing. |
|
|
170
|
+
| `--force-retranslate` | sync | Retranslate the selected scope even when state is current. |
|
|
171
|
+
| `--force-retranslate-path <pointer>` | sync | Force retranslation of specific pointers. Repeatable. |
|
|
172
|
+
| `--max-pending-translations <n>` | sync, check | Abort before any provider call if the scope would translate more than `n` entries. |
|
|
173
|
+
| `--check` | audit | Verify stored provenance without calling the provider. |
|
|
174
|
+
| `--refresh` | audit | Re-run audits even where provenance already exists. |
|
|
175
|
+
| `--from <locale>` | new-locale, scaffold-locale | Locale to seed from. |
|
|
176
|
+
| `--strategy <strategy>` | new-locale, scaffold-locale | Scaffolding strategy. |
|
|
177
|
+
| `--identical-to-source <adopt\|skip>` | adopt | What to do with target text identical to its source. Defaults to `adopt`. |
|
|
178
|
+
| `--help`, `-h` | | Print usage. |
|
|
179
|
+
| `--version`, `-v` | | Print the version. |
|
|
180
|
+
|
|
181
|
+
Flags accept both `--locale de` and `--locale=de`.
|
|
182
|
+
|
|
183
|
+
## Output and exit codes
|
|
184
|
+
|
|
185
|
+
Every command prints a JSON report to stdout — sync metrics, validation issues, audit counts — and errors to stderr. Exit code is `0` on success and `1` on any failure, which makes `ai-translate check` usable directly as a CI step.
|
|
186
|
+
|
|
187
|
+
A dry-run sync also reports `pendingTranslationReasons`, a count of why each entry was selected. That is the fastest way to understand an unexpectedly large run.
|
|
188
|
+
|
|
189
|
+
## Programmatic use
|
|
190
|
+
|
|
191
|
+
```ts
|
|
192
|
+
import { runCli, loadConfig, findConfigPath, defineConfig } from "@ai-translate/cli";
|
|
193
|
+
|
|
194
|
+
const exitCode = await runCli(["sync", "--dry-run"], process.cwd());
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
## License
|
|
198
|
+
|
|
199
|
+
MIT © Thiago Peres
|
package/dist/bin.d.mts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {}
|
package/dist/bin.mjs
ADDED
package/dist/bin.mjs.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bin.mjs","names":[],"sources":["../src/bin.ts"],"sourcesContent":["#!/usr/bin/env node\n\nimport { runCli } from \"./index\";\n\nconst exitCode = await runCli();\nprocess.exit(exitCode);\n"],"mappings":";;;AAIA,MAAM,WAAW,MAAM,OAAO;AAC9B,QAAQ,KAAK,QAAQ"}
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { defineConfig } from "@ai-translate/core";
|
|
2
|
+
import { AiTranslateConfig } from "@ai-translate/core/types";
|
|
3
|
+
//#region src/config.d.ts
|
|
4
|
+
declare function findConfigPath(cwd: string, explicitPath?: string): string;
|
|
5
|
+
declare function loadEnvFiles(cwd: string): Promise<Record<string, string>>;
|
|
6
|
+
declare function loadConfig(cwd: string, explicitPath?: string): Promise<{
|
|
7
|
+
config: AiTranslateConfig;
|
|
8
|
+
configPath: string;
|
|
9
|
+
}>;
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region src/index.d.ts
|
|
12
|
+
declare function runCli(argv?: readonly string[], cwd?: string): Promise<number>;
|
|
13
|
+
//#endregion
|
|
14
|
+
export { defineConfig, findConfigPath, loadConfig, loadEnvFiles, runCli };
|
|
15
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/config.ts","../src/index.ts"],"mappings":";;;iBA4BgB,eAAe,aAAa;iBAiBtB,aAAa,cAAc,QAAQ;iBA+BnC,WACpB,aACA,wBACC;EAAU,QAAQ;EAAmB;;;;iBCkflB,OACpB,0BACA,eACC"}
|
package/dist/index.mjs
ADDED