@tolkit/cli 0.1.0 → 0.2.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/commands/pull.js +76 -6
- package/dist/commands/sync.js +1 -1
- package/dist/index.js +4 -2
- package/dist/plan.js +1 -0
- package/package.json +1 -1
package/dist/commands/pull.js
CHANGED
|
@@ -1,17 +1,61 @@
|
|
|
1
|
-
import { mkdir, writeFile } from "node:fs/promises";
|
|
1
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
2
2
|
import { dirname, join } from "node:path";
|
|
3
3
|
import { TolkitClient, extFor } from "../client.js";
|
|
4
|
-
import {
|
|
4
|
+
import { flatten } from "../flatten.js";
|
|
5
|
+
import { isLocalePattern, localePattern, parseLocaleList, planPush } from "../plan.js";
|
|
5
6
|
/**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
7
|
+
* Patch only the specified keys inside a JSON object, preserving original key
|
|
8
|
+
* order, `$schema`, indentation, and trailing newline. Supports both flat keys
|
|
9
|
+
* (`"a.b"` or `"a_b"`) and nested JSON objects.
|
|
10
|
+
*/
|
|
11
|
+
export function patchJsonInPlace(raw, updates) {
|
|
12
|
+
const obj = JSON.parse(raw);
|
|
13
|
+
if (!obj || typeof obj !== "object" || Array.isArray(obj)) {
|
|
14
|
+
throw new Error("Source file must be a JSON object");
|
|
15
|
+
}
|
|
16
|
+
const indent = raw.match(/^(\t+| +)"/m)?.[1] ?? " ";
|
|
17
|
+
const eol = raw.endsWith("\n") ? "\n" : "";
|
|
18
|
+
let updated = 0;
|
|
19
|
+
for (const [key, value] of Object.entries(updates)) {
|
|
20
|
+
if (Object.prototype.hasOwnProperty.call(obj, key) && typeof obj[key] === "string") {
|
|
21
|
+
if (obj[key] !== value) {
|
|
22
|
+
obj[key] = value;
|
|
23
|
+
updated++;
|
|
24
|
+
}
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
// Nested object fallback (dot-separated path)
|
|
28
|
+
const parts = key.split(".");
|
|
29
|
+
let cur = obj;
|
|
30
|
+
let ok = true;
|
|
31
|
+
for (let i = 0; i < parts.length - 1; i++) {
|
|
32
|
+
const next = cur[parts[i]];
|
|
33
|
+
if (!next || typeof next !== "object" || Array.isArray(next)) {
|
|
34
|
+
ok = false;
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
cur = next;
|
|
38
|
+
}
|
|
39
|
+
const leaf = parts[parts.length - 1];
|
|
40
|
+
if (ok && Object.prototype.hasOwnProperty.call(cur, leaf) && typeof cur[leaf] === "string" && cur[leaf] !== value) {
|
|
41
|
+
cur[leaf] = value;
|
|
42
|
+
updated++;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return { text: `${JSON.stringify(obj, null, indent)}${eol}`, updated };
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Download target locale files. By default skips `sourceLocale` (pass
|
|
49
|
+
* `--locale <code >` or `--locales a,b` to choose explicitly). Pass `--source <file>`
|
|
50
|
+
* to patch Tolkit-only source edits in-place in the existing source JSON file
|
|
51
|
+
* without reordering keys or reverting git-side changes.
|
|
9
52
|
*/
|
|
10
53
|
export async function pull(cfg, opts, client = new TolkitClient(cfg)) {
|
|
11
54
|
const me = await client.me();
|
|
12
55
|
const format = opts.format ?? "json";
|
|
13
56
|
const out = opts.out ?? ".";
|
|
14
|
-
const
|
|
57
|
+
const requested = opts.locale ? [opts.locale] : parseLocaleList(opts.locales);
|
|
58
|
+
const codes = requested ?? me.locales.map((l) => l.code).filter((c) => c !== me.sourceLocale);
|
|
15
59
|
const unique = [...new Set(codes)];
|
|
16
60
|
const ext = extFor(format);
|
|
17
61
|
const pattern = isLocalePattern(out) ? localePattern(out) : null;
|
|
@@ -22,4 +66,30 @@ export async function pull(cfg, opts, client = new TolkitClient(cfg)) {
|
|
|
22
66
|
await writeFile(file, text, "utf8");
|
|
23
67
|
console.log(`pulled ${file}`);
|
|
24
68
|
}
|
|
69
|
+
if (opts.source) {
|
|
70
|
+
const raw = await readFile(opts.source, "utf8");
|
|
71
|
+
const local = flatten(JSON.parse(raw));
|
|
72
|
+
delete local.$schema;
|
|
73
|
+
const serverKeys = await client.allKeys(me.projectId);
|
|
74
|
+
const serverByName = new Map(serverKeys.map((k) => [k.name, k.values[me.sourceLocale]?.value ?? ""]));
|
|
75
|
+
const plan = planPush(local, serverKeys.map((k) => ({ name: k.name, source: k.values[me.sourceLocale]?.value, synced: k.sourceSynced })));
|
|
76
|
+
if (plan.edited.length === 0) {
|
|
77
|
+
console.log(`source ${opts.source}: up to date`);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
const updates = {};
|
|
81
|
+
for (const name of plan.edited) {
|
|
82
|
+
const value = serverByName.get(name);
|
|
83
|
+
if (value !== undefined)
|
|
84
|
+
updates[name] = value;
|
|
85
|
+
}
|
|
86
|
+
const { text, updated } = patchJsonInPlace(raw, updates);
|
|
87
|
+
if (updated > 0) {
|
|
88
|
+
await writeFile(opts.source, text, "utf8");
|
|
89
|
+
console.log(`patched ${opts.source}: ${updated} source edit(s) from Tolkit`);
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
console.log(`source ${opts.source}: up to date`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
25
95
|
}
|
package/dist/commands/sync.js
CHANGED
|
@@ -47,7 +47,7 @@ export function resolveSync(cfg, flags) {
|
|
|
47
47
|
root: flags.root,
|
|
48
48
|
};
|
|
49
49
|
const out = flags.out ?? cfg.out ?? (translations && isLocalePattern(translations) ? translations : ".");
|
|
50
|
-
return { push: pushOpts, pull: { format: flags.format ?? cfg.format, out } };
|
|
50
|
+
return { push: pushOpts, pull: { format: flags.format ?? cfg.format, out, source: pushOpts.from } };
|
|
51
51
|
}
|
|
52
52
|
/** `tolkit sync`: push (per the tolkit.json `sync` block + flags), then pull every locale. */
|
|
53
53
|
export async function sync(cfg, flags) {
|
package/dist/index.js
CHANGED
|
@@ -10,7 +10,7 @@ import { runMcp } from "./mcp.js";
|
|
|
10
10
|
const USAGE = `tolkit — translation CLI
|
|
11
11
|
|
|
12
12
|
Usage:
|
|
13
|
-
tolkit pull [--locale <code>] [--format <id>] [--out <dir | "locales/{locale}.json">]
|
|
13
|
+
tolkit pull [--locale <code> | --locales a,b] [--source <file.json>] [--format <id>] [--out <dir | "locales/{locale}.json">]
|
|
14
14
|
tolkit push [--from <file.json>] [--translations <pattern>] [--locales a,b] [--update]
|
|
15
15
|
[--remove-missing [--yes]] [--fail-on-conflict] [--dry-run] [--scan] [--root <dir>] [--plurals i18next]
|
|
16
16
|
tolkit sync [push flags] [--force] [--format <id>] [--out <dir | pattern>]
|
|
@@ -18,6 +18,8 @@ Usage:
|
|
|
18
18
|
tolkit check [--from <file.json>] [--complete] [--reviewed[=<pct>]] [--qa[=errors|warnings]]
|
|
19
19
|
tolkit mcp
|
|
20
20
|
|
|
21
|
+
pull downloads target locales (skips the source locale by default); --source patches Tolkit-only source edits in-place.
|
|
22
|
+
|
|
21
23
|
push --update overwrites differing source values (default: only empty ones are filled).
|
|
22
24
|
push --translations "locales/{locale}.json" pushes target locales (fills empty cells; --update overwrites).
|
|
23
25
|
push --remove-missing deletes server keys absent from --from (asks on a TTY; --yes in CI).
|
|
@@ -38,7 +40,7 @@ async function main() {
|
|
|
38
40
|
args: normalizeArgv(process.argv.slice(2)),
|
|
39
41
|
allowPositionals: true,
|
|
40
42
|
options: {
|
|
41
|
-
locale: { type: "string" }, format: { type: "string" }, out: { type: "string" },
|
|
43
|
+
locale: { type: "string" }, format: { type: "string" }, out: { type: "string" }, source: { type: "string" },
|
|
42
44
|
from: { type: "string" }, complete: { type: "boolean" }, reviewed: { type: "string" }, qa: { type: "string" },
|
|
43
45
|
scan: { type: "boolean" }, root: { type: "string" }, "dry-run": { type: "boolean" }, plurals: { type: "string" },
|
|
44
46
|
update: { type: "boolean" }, translations: { type: "string" }, locales: { type: "string" },
|
package/dist/plan.js
CHANGED
|
@@ -122,6 +122,7 @@ export function formatSourceConflicts(names, local, server) {
|
|
|
122
122
|
});
|
|
123
123
|
if (names.length > SAMPLE)
|
|
124
124
|
lines.push(` … (+${names.length - SAMPLE} more)`);
|
|
125
|
+
lines.push(" resolve: edit your source file to match Tolkit, or run `tolkit push --from <file> --update` to keep the repo value");
|
|
125
126
|
return lines.join("\n");
|
|
126
127
|
}
|
|
127
128
|
export function formatTranslationPlan(locale, path, plan) {
|