@lionrapid/cli 0.2.2 → 0.3.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/CHANGELOG.md +49 -0
- package/README.md +15 -1
- package/dist/api-client.d.ts +119 -1
- package/dist/api-client.js +136 -2
- package/dist/cli.js +208 -2
- package/dist/commands/push.d.ts +34 -0
- package/dist/commands/push.js +23 -1
- package/dist/commands/status.d.ts +76 -0
- package/dist/commands/status.js +221 -0
- package/dist/job-history.d.ts +97 -0
- package/dist/job-history.js +175 -0
- package/package.json +2 -2
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Local record of the sync jobs this machine has queued — `~/.lionrapid/jobs.json`.
|
|
3
|
+
*
|
|
4
|
+
* Why a file at all: `sync/batch` answers 202 (queued), so the only proof a push
|
|
5
|
+
* actually landed is the job's later state. You push, close the terminal, and
|
|
6
|
+
* come back — without this file the job ids are gone with the scrollback and
|
|
7
|
+
* there is nothing left to ask the server about.
|
|
8
|
+
*
|
|
9
|
+
* Why NOT in `~/.lionrapid/credentials`: different lifetime (history is
|
|
10
|
+
* disposable, a key is not) and different sensitivity. A corrupt or truncated
|
|
11
|
+
* history must never cost someone their login, so the two files never share a
|
|
12
|
+
* parser or a write. Everything here degrades to "no history" instead of
|
|
13
|
+
* throwing — losing visibility is bad, but failing a push because a cache file
|
|
14
|
+
* is unreadable is worse.
|
|
15
|
+
*
|
|
16
|
+
* The server purges finished non-DLQ jobs after ~1h, so for anything older this
|
|
17
|
+
* file is the ONLY answer available: a terminal status is recorded when we see
|
|
18
|
+
* it, and later reads report it from disk without a network call.
|
|
19
|
+
*/
|
|
20
|
+
/**
|
|
21
|
+
* One queued job, as this machine knew it. `files`/`keys` describe what was in
|
|
22
|
+
* the batch, so `lionrapid status` can say *what* is at risk without a network
|
|
23
|
+
* call.
|
|
24
|
+
*/
|
|
25
|
+
export interface JobHistoryEntry {
|
|
26
|
+
/** Server-assigned job id — the key for `GET /sync/jobs/{id}`. */
|
|
27
|
+
id: string;
|
|
28
|
+
/** Server this job was queued on (a machine may push to more than one). */
|
|
29
|
+
server: string;
|
|
30
|
+
/** The `project:` label from lionrapid.yml. */
|
|
31
|
+
project: string;
|
|
32
|
+
namespace: string;
|
|
33
|
+
/** Source locale of the batch. */
|
|
34
|
+
locale: string;
|
|
35
|
+
/** Files whose keys were in this batch (one, unless a file was chunked). */
|
|
36
|
+
files: string[];
|
|
37
|
+
/** Keys in the batch. */
|
|
38
|
+
keys: number;
|
|
39
|
+
/** ISO timestamp of the push that queued it. */
|
|
40
|
+
pushedAt: string;
|
|
41
|
+
/**
|
|
42
|
+
* Last status seen: the server's own vocabulary (`pending`, `processing`,
|
|
43
|
+
* `retry_pending`, `completed`, `failed`), or `gone` when the server no
|
|
44
|
+
* longer knows the job.
|
|
45
|
+
*/
|
|
46
|
+
status: string;
|
|
47
|
+
/** ISO timestamp of the last status check against the server. */
|
|
48
|
+
checkedAt?: string;
|
|
49
|
+
/** Last error the server reported for this job. */
|
|
50
|
+
error?: string;
|
|
51
|
+
/** Why the backend gave up, when it did. */
|
|
52
|
+
deadLetterReason?: string;
|
|
53
|
+
}
|
|
54
|
+
/** Newest first — the order the file is stored in and the order callers get. */
|
|
55
|
+
export type JobHistory = JobHistoryEntry[];
|
|
56
|
+
/**
|
|
57
|
+
* Pruning. Both bounds apply on every write, so the file cannot grow forever:
|
|
58
|
+
*
|
|
59
|
+
* - 500 entries: a single big push is one job per file per 500-key chunk, so a
|
|
60
|
+
* large repo can queue dozens at once. 500 keeps weeks of real use while
|
|
61
|
+
* staying a ~100KB file.
|
|
62
|
+
* - 30 days: the server purges finished jobs after ~1h, so an entry older than
|
|
63
|
+
* this can never be re-checked — it is a receipt, and a month-old receipt for
|
|
64
|
+
* a push you have long since repeated is not worth carrying.
|
|
65
|
+
*
|
|
66
|
+
* Whichever bites first wins; entries are dropped oldest-first.
|
|
67
|
+
*/
|
|
68
|
+
export declare const JOB_HISTORY_MAX_ENTRIES = 500;
|
|
69
|
+
export declare const JOB_HISTORY_MAX_AGE_DAYS = 30;
|
|
70
|
+
/**
|
|
71
|
+
* True when there is no point asking the server again. `failed` counts: the
|
|
72
|
+
* server only sets it from its dead-letter path, so a failed job is one it gave
|
|
73
|
+
* up on. `gone` is ours — the job 404s (unknown id, or purged after retention).
|
|
74
|
+
*/
|
|
75
|
+
export declare function isTerminalJobStatus(status: string): boolean;
|
|
76
|
+
/** Where the history lives: `~/.lionrapid/jobs.json`, beside the credentials file. */
|
|
77
|
+
export declare function defaultJobHistoryPath(): string;
|
|
78
|
+
/**
|
|
79
|
+
* Read the history, newest first. A missing, unreadable, or corrupt file is
|
|
80
|
+
* simply "no history" — never an exception, and the next write replaces it.
|
|
81
|
+
*/
|
|
82
|
+
export declare function readJobHistory(path?: string): JobHistory;
|
|
83
|
+
/**
|
|
84
|
+
* Upsert `entries` (by job id) and write the pruned result. Returns what is now
|
|
85
|
+
* on disk, newest first. A failed write warns and returns the merged list — a
|
|
86
|
+
* push must not fail because its receipt could not be saved.
|
|
87
|
+
*/
|
|
88
|
+
export declare function recordJobs(entries: JobHistoryEntry[], path?: string): JobHistory;
|
|
89
|
+
/** A status refresh for one job: everything except its identity may change. */
|
|
90
|
+
export type JobStatusPatch = Pick<JobHistoryEntry, 'id'> & Partial<Pick<JobHistoryEntry, 'status' | 'checkedAt' | 'error' | 'deadLetterReason'>>;
|
|
91
|
+
/**
|
|
92
|
+
* Fold status updates into the stored entries in a single read-modify-write.
|
|
93
|
+
* Unknown ids are ignored (the history is not a job database — it only tracks
|
|
94
|
+
* what this machine pushed).
|
|
95
|
+
*/
|
|
96
|
+
export declare function updateJobStatuses(patches: JobStatusPatch[], path?: string): JobHistory;
|
|
97
|
+
//# sourceMappingURL=job-history.d.ts.map
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Local record of the sync jobs this machine has queued — `~/.lionrapid/jobs.json`.
|
|
3
|
+
*
|
|
4
|
+
* Why a file at all: `sync/batch` answers 202 (queued), so the only proof a push
|
|
5
|
+
* actually landed is the job's later state. You push, close the terminal, and
|
|
6
|
+
* come back — without this file the job ids are gone with the scrollback and
|
|
7
|
+
* there is nothing left to ask the server about.
|
|
8
|
+
*
|
|
9
|
+
* Why NOT in `~/.lionrapid/credentials`: different lifetime (history is
|
|
10
|
+
* disposable, a key is not) and different sensitivity. A corrupt or truncated
|
|
11
|
+
* history must never cost someone their login, so the two files never share a
|
|
12
|
+
* parser or a write. Everything here degrades to "no history" instead of
|
|
13
|
+
* throwing — losing visibility is bad, but failing a push because a cache file
|
|
14
|
+
* is unreadable is worse.
|
|
15
|
+
*
|
|
16
|
+
* The server purges finished non-DLQ jobs after ~1h, so for anything older this
|
|
17
|
+
* file is the ONLY answer available: a terminal status is recorded when we see
|
|
18
|
+
* it, and later reads report it from disk without a network call.
|
|
19
|
+
*/
|
|
20
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
|
|
21
|
+
import { homedir } from 'node:os';
|
|
22
|
+
import { dirname, join } from 'node:path';
|
|
23
|
+
/**
|
|
24
|
+
* Pruning. Both bounds apply on every write, so the file cannot grow forever:
|
|
25
|
+
*
|
|
26
|
+
* - 500 entries: a single big push is one job per file per 500-key chunk, so a
|
|
27
|
+
* large repo can queue dozens at once. 500 keeps weeks of real use while
|
|
28
|
+
* staying a ~100KB file.
|
|
29
|
+
* - 30 days: the server purges finished jobs after ~1h, so an entry older than
|
|
30
|
+
* this can never be re-checked — it is a receipt, and a month-old receipt for
|
|
31
|
+
* a push you have long since repeated is not worth carrying.
|
|
32
|
+
*
|
|
33
|
+
* Whichever bites first wins; entries are dropped oldest-first.
|
|
34
|
+
*/
|
|
35
|
+
export const JOB_HISTORY_MAX_ENTRIES = 500;
|
|
36
|
+
export const JOB_HISTORY_MAX_AGE_DAYS = 30;
|
|
37
|
+
/** Statuses the server never leaves, plus the CLI's own `gone`. */
|
|
38
|
+
const TERMINAL_STATUSES = new Set(['completed', 'failed', 'gone']);
|
|
39
|
+
/**
|
|
40
|
+
* True when there is no point asking the server again. `failed` counts: the
|
|
41
|
+
* server only sets it from its dead-letter path, so a failed job is one it gave
|
|
42
|
+
* up on. `gone` is ours — the job 404s (unknown id, or purged after retention).
|
|
43
|
+
*/
|
|
44
|
+
export function isTerminalJobStatus(status) {
|
|
45
|
+
return TERMINAL_STATUSES.has(status);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* The user's home directory, `$HOME` first.
|
|
49
|
+
*
|
|
50
|
+
* Identical to `os.homedir()` for real users on POSIX (it reads `$HOME` too),
|
|
51
|
+
* but a JS-level `process.env.HOME` assignment is invisible to `homedir()`'s
|
|
52
|
+
* native `getenv` inside a worker thread — which is where vitest runs. Reading
|
|
53
|
+
* the variable directly means a relocated `$HOME` is honoured everywhere, so
|
|
54
|
+
* the test suite cannot write into the developer's real `~/.lionrapid`.
|
|
55
|
+
*/
|
|
56
|
+
function homeDir() {
|
|
57
|
+
return process.env['HOME'] || process.env['USERPROFILE'] || homedir();
|
|
58
|
+
}
|
|
59
|
+
/** Where the history lives: `~/.lionrapid/jobs.json`, beside the credentials file. */
|
|
60
|
+
export function defaultJobHistoryPath() {
|
|
61
|
+
return join(homeDir(), '.lionrapid', 'jobs.json');
|
|
62
|
+
}
|
|
63
|
+
/** Paths already warned about this process — an unreadable history warns once. */
|
|
64
|
+
const warned = new Set();
|
|
65
|
+
function warnOnce(path, detail) {
|
|
66
|
+
if (warned.has(path))
|
|
67
|
+
return;
|
|
68
|
+
warned.add(path);
|
|
69
|
+
console.error(`warning: could not use the push history at ${path} (${detail})`);
|
|
70
|
+
}
|
|
71
|
+
function isEntry(value) {
|
|
72
|
+
if (value === null || typeof value !== 'object')
|
|
73
|
+
return false;
|
|
74
|
+
const e = value;
|
|
75
|
+
return typeof e['id'] === 'string' && e['id'].length > 0 && typeof e['pushedAt'] === 'string';
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Read the history, newest first. A missing, unreadable, or corrupt file is
|
|
79
|
+
* simply "no history" — never an exception, and the next write replaces it.
|
|
80
|
+
*/
|
|
81
|
+
export function readJobHistory(path = defaultJobHistoryPath()) {
|
|
82
|
+
if (!existsSync(path))
|
|
83
|
+
return [];
|
|
84
|
+
let parsed;
|
|
85
|
+
try {
|
|
86
|
+
parsed = JSON.parse(readFileSync(path, 'utf8'));
|
|
87
|
+
}
|
|
88
|
+
catch (err) {
|
|
89
|
+
warnOnce(path, err instanceof Error ? err.message : String(err));
|
|
90
|
+
return [];
|
|
91
|
+
}
|
|
92
|
+
const jobs = parsed !== null && typeof parsed === 'object' && Array.isArray(parsed.jobs)
|
|
93
|
+
? parsed.jobs
|
|
94
|
+
: [];
|
|
95
|
+
return sortNewestFirst(jobs.filter(isEntry));
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Upsert `entries` (by job id) and write the pruned result. Returns what is now
|
|
99
|
+
* on disk, newest first. A failed write warns and returns the merged list — a
|
|
100
|
+
* push must not fail because its receipt could not be saved.
|
|
101
|
+
*/
|
|
102
|
+
export function recordJobs(entries, path = defaultJobHistoryPath()) {
|
|
103
|
+
if (entries.length === 0)
|
|
104
|
+
return readJobHistory(path);
|
|
105
|
+
return mutate(path, (current) => [...entries, ...current]);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Fold status updates into the stored entries in a single read-modify-write.
|
|
109
|
+
* Unknown ids are ignored (the history is not a job database — it only tracks
|
|
110
|
+
* what this machine pushed).
|
|
111
|
+
*/
|
|
112
|
+
export function updateJobStatuses(patches, path = defaultJobHistoryPath()) {
|
|
113
|
+
if (patches.length === 0)
|
|
114
|
+
return readJobHistory(path);
|
|
115
|
+
const byId = new Map(patches.map((p) => [p.id, p]));
|
|
116
|
+
return mutate(path, (current) => current.map((entry) => {
|
|
117
|
+
const patch = byId.get(entry.id);
|
|
118
|
+
return patch ? { ...entry, ...stripUndefined(patch), id: entry.id } : entry;
|
|
119
|
+
}));
|
|
120
|
+
}
|
|
121
|
+
/** Read → transform → dedupe → prune → write. The one place that touches the file. */
|
|
122
|
+
function mutate(path, transform) {
|
|
123
|
+
// Re-read immediately before writing: two pushes in different terminals
|
|
124
|
+
// otherwise clobber each other's ids.
|
|
125
|
+
const next = prune(dedupe(transform(readJobHistory(path))));
|
|
126
|
+
try {
|
|
127
|
+
write(path, next);
|
|
128
|
+
}
|
|
129
|
+
catch (err) {
|
|
130
|
+
warnOnce(path, err instanceof Error ? err.message : String(err));
|
|
131
|
+
}
|
|
132
|
+
return next;
|
|
133
|
+
}
|
|
134
|
+
/** First occurrence of an id wins — callers pass their fresher entries first. */
|
|
135
|
+
function dedupe(entries) {
|
|
136
|
+
const seen = new Set();
|
|
137
|
+
const out = [];
|
|
138
|
+
for (const entry of entries) {
|
|
139
|
+
if (seen.has(entry.id))
|
|
140
|
+
continue;
|
|
141
|
+
seen.add(entry.id);
|
|
142
|
+
out.push(entry);
|
|
143
|
+
}
|
|
144
|
+
return out;
|
|
145
|
+
}
|
|
146
|
+
function prune(entries) {
|
|
147
|
+
const cutoff = Date.now() - JOB_HISTORY_MAX_AGE_DAYS * 24 * 60 * 60 * 1000;
|
|
148
|
+
const fresh = sortNewestFirst(entries).filter((e) => {
|
|
149
|
+
const at = Date.parse(e.pushedAt);
|
|
150
|
+
// An unparseable timestamp is kept: dropping a receipt because we cannot
|
|
151
|
+
// read its clock loses exactly the information the file exists to hold.
|
|
152
|
+
return Number.isNaN(at) || at >= cutoff;
|
|
153
|
+
});
|
|
154
|
+
return fresh.slice(0, JOB_HISTORY_MAX_ENTRIES);
|
|
155
|
+
}
|
|
156
|
+
function sortNewestFirst(entries) {
|
|
157
|
+
return [...entries].sort((a, b) => (a.pushedAt < b.pushedAt ? 1 : a.pushedAt > b.pushedAt ? -1 : 0));
|
|
158
|
+
}
|
|
159
|
+
function stripUndefined(value) {
|
|
160
|
+
return Object.fromEntries(Object.entries(value).filter(([, v]) => v !== undefined));
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Write via a temp file + rename so a crash mid-write leaves the previous
|
|
164
|
+
* history intact rather than a half-written one. Same 0700/0600 permissions as
|
|
165
|
+
* the credentials file: this records project names and file paths.
|
|
166
|
+
*/
|
|
167
|
+
function write(path, entries) {
|
|
168
|
+
const dir = dirname(path);
|
|
169
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
170
|
+
const tmp = join(dir, `.jobs.${process.pid}.tmp`);
|
|
171
|
+
const body = `${JSON.stringify({ version: 1, jobs: entries }, null, 2)}\n`;
|
|
172
|
+
writeFileSync(tmp, body, { mode: 0o600 });
|
|
173
|
+
renameSync(tmp, path);
|
|
174
|
+
}
|
|
175
|
+
//# sourceMappingURL=job-history.js.map
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lionrapid/cli",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "LionRapid CLI
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "LionRapid CLI \u2014 sync translation files (iOS, Android, Flutter, JSON, YAML, PO) with the LionRapid backend",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
7
7
|
"types": "./dist/index.d.ts",
|