evalctl 0.0.0-stage → 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 +149 -2
- package/dist/cli.d.ts +13 -0
- package/dist/cli.js +294 -0
- package/dist/client.d.ts +53 -0
- package/dist/client.js +269 -0
- package/dist/commands/auth.d.ts +6 -0
- package/dist/commands/auth.js +341 -0
- package/dist/commands/common.d.ts +17 -0
- package/dist/commands/common.js +53 -0
- package/dist/commands/export.d.ts +2 -0
- package/dist/commands/export.js +54 -0
- package/dist/commands/index.d.ts +3 -0
- package/dist/commands/index.js +10 -0
- package/dist/commands/info.d.ts +2 -0
- package/dist/commands/info.js +208 -0
- package/dist/commands/runs.d.ts +2 -0
- package/dist/commands/runs.js +325 -0
- package/dist/commands/scenarios.d.ts +2 -0
- package/dist/commands/scenarios.js +585 -0
- package/dist/commands/screens.d.ts +2 -0
- package/dist/commands/screens.js +139 -0
- package/dist/config.d.ts +52 -0
- package/dist/config.js +281 -0
- package/dist/context.d.ts +64 -0
- package/dist/context.js +1 -0
- package/dist/diff.d.ts +7 -0
- package/dist/diff.js +122 -0
- package/dist/errors.d.ts +22 -0
- package/dist/errors.js +35 -0
- package/dist/fsutil.d.ts +34 -0
- package/dist/fsutil.js +245 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +18 -0
- package/dist/output.d.ts +38 -0
- package/dist/output.js +156 -0
- package/dist/prompt.d.ts +29 -0
- package/dist/prompt.js +195 -0
- package/dist/sidecar.d.ts +39 -0
- package/dist/sidecar.js +138 -0
- package/dist/state.d.ts +14 -0
- package/dist/state.js +48 -0
- package/dist/util.d.ts +21 -0
- package/dist/util.js +77 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +12 -0
- package/dist/yaml-text.d.ts +15 -0
- package/dist/yaml-text.js +103 -0
- package/package.json +62 -4
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { JsonObject } from "./util.js";
|
|
2
|
+
export declare const EXIT_OK = 0;
|
|
3
|
+
export declare const EXIT_RUNTIME = 1;
|
|
4
|
+
export declare const EXIT_VALIDATION = 2;
|
|
5
|
+
export declare const EXIT_AUTH = 3;
|
|
6
|
+
export declare const EXIT_CONFLICT = 4;
|
|
7
|
+
export declare const EXIT_RUN_FAILED = 5;
|
|
8
|
+
export interface CliErrorOptions {
|
|
9
|
+
exitCode?: number;
|
|
10
|
+
/** The structured body emitted under `--json`; defaults to a generic `cli_error`. */
|
|
11
|
+
payload?: JsonObject;
|
|
12
|
+
}
|
|
13
|
+
/** A failure the CLI reports on its own: the message goes to stderr (or JSON to stdout) and the exit code is one of 1..5. */
|
|
14
|
+
export declare class CliError extends Error {
|
|
15
|
+
readonly exitCode: number;
|
|
16
|
+
readonly payload: JsonObject;
|
|
17
|
+
constructor(message: string, options?: CliErrorOptions);
|
|
18
|
+
}
|
|
19
|
+
/** Maps an HTTP status to the CLI exit code: auth 3, conflict 4, bad input 2, anything else 1. */
|
|
20
|
+
export declare function exitCodeForStatus(status: number): number;
|
|
21
|
+
/** The `--json` error document: the server's own fields first, then the fields every consumer can rely on. */
|
|
22
|
+
export declare function errorToJson(err: CliError): JsonObject;
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export const EXIT_OK = 0;
|
|
2
|
+
export const EXIT_RUNTIME = 1;
|
|
3
|
+
export const EXIT_VALIDATION = 2;
|
|
4
|
+
export const EXIT_AUTH = 3;
|
|
5
|
+
export const EXIT_CONFLICT = 4;
|
|
6
|
+
export const EXIT_RUN_FAILED = 5;
|
|
7
|
+
/** A failure the CLI reports on its own: the message goes to stderr (or JSON to stdout) and the exit code is one of 1..5. */
|
|
8
|
+
export class CliError extends Error {
|
|
9
|
+
exitCode;
|
|
10
|
+
payload;
|
|
11
|
+
constructor(message, options = {}) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.name = "CliError";
|
|
14
|
+
this.exitCode = options.exitCode ?? EXIT_RUNTIME;
|
|
15
|
+
this.payload = options.payload ?? { ok: false, error: "cli_error", message };
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/** Maps an HTTP status to the CLI exit code: auth 3, conflict 4, bad input 2, anything else 1. */
|
|
19
|
+
export function exitCodeForStatus(status) {
|
|
20
|
+
if (status === 401 || status === 403) {
|
|
21
|
+
return EXIT_AUTH;
|
|
22
|
+
}
|
|
23
|
+
if (status === 409 || status === 428) {
|
|
24
|
+
return EXIT_CONFLICT;
|
|
25
|
+
}
|
|
26
|
+
if (status === 400 || status === 422) {
|
|
27
|
+
return EXIT_VALIDATION;
|
|
28
|
+
}
|
|
29
|
+
return EXIT_RUNTIME;
|
|
30
|
+
}
|
|
31
|
+
/** The `--json` error document: the server's own fields first, then the fields every consumer can rely on. */
|
|
32
|
+
export function errorToJson(err) {
|
|
33
|
+
const code = typeof err.payload["error"] === "string" ? err.payload["error"] : "cli_error";
|
|
34
|
+
return { ...err.payload, ok: false, error: code, message: err.message, exit_code: err.exitCode };
|
|
35
|
+
}
|
package/dist/fsutil.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type Stats } from "node:fs";
|
|
2
|
+
/** `lstat` that treats a missing path as `undefined` and rethrows every other failure. */
|
|
3
|
+
export declare function tryLstat(path: string): Stats | undefined;
|
|
4
|
+
export declare const WINDOWS_DEVICE_NAME: RegExp;
|
|
5
|
+
/** An explicit absolute destination is allowed. A relative destination stays inside its base directory. */
|
|
6
|
+
export declare function resolveOutputPath(base: string, name: string): string;
|
|
7
|
+
/**
|
|
8
|
+
* Checks the destination before a write and reports the existing regular file there, if any.
|
|
9
|
+
*
|
|
10
|
+
* `baseDir` is the directory the person is working in (default: the target's own directory). It and everything above
|
|
11
|
+
* it is trusted as the system resolved it, so `/var -> private/var` on macOS or a symlinked workspace do not matter.
|
|
12
|
+
* Every component below it, down to the target itself, must be a plain name (no `..`, reserved device name, trailing
|
|
13
|
+
* dot or space, or characters Windows refuses) and must not be a symbolic link.
|
|
14
|
+
*/
|
|
15
|
+
export declare function checkOutputPath(path: string, baseDir?: string): Stats | undefined;
|
|
16
|
+
/** Reads at most `maxBytes` of UTF-8 text, accepting a byte order mark and normalizing line endings. */
|
|
17
|
+
export declare function readUtf8File(path: string, maxBytes: number): string;
|
|
18
|
+
export interface AtomicWriteOptions {
|
|
19
|
+
/** Permission bits of the new file (ignored on Windows, which has ACLs instead). */
|
|
20
|
+
fileMode: number;
|
|
21
|
+
/** Permission bits for directories this call has to create. */
|
|
22
|
+
dirMode?: number;
|
|
23
|
+
/** Refuse when `path` already is a symbolic link. */
|
|
24
|
+
refuseSymlink?: boolean;
|
|
25
|
+
/** Install the finished file only when the destination is still absent. */
|
|
26
|
+
exclusive?: boolean;
|
|
27
|
+
/** What the file is called in error messages, e.g. "config file". */
|
|
28
|
+
label: string;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Writes `content` to a sibling temp file created exclusively with `fileMode`, then renames it over `path`.
|
|
32
|
+
* A reader never sees a half-written file, and a symlink planted at `path` is replaced rather than followed.
|
|
33
|
+
*/
|
|
34
|
+
export declare function writeFileAtomic(path: string, content: string, options: AtomicWriteOptions): void;
|
package/dist/fsutil.js
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { closeSync, constants, fchmodSync, fstatSync, fsyncSync, linkSync, lstatSync, mkdirSync, openSync, readSync, realpathSync, renameSync, unlinkSync, writeSync, } from "node:fs";
|
|
3
|
+
import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
4
|
+
import { CliError, EXIT_CONFLICT, EXIT_VALIDATION } from "./errors.js";
|
|
5
|
+
import { errorMessage, oneLine } from "./util.js";
|
|
6
|
+
/** `lstat` that treats a missing path as `undefined` and rethrows every other failure. */
|
|
7
|
+
export function tryLstat(path) {
|
|
8
|
+
try {
|
|
9
|
+
return lstatSync(path);
|
|
10
|
+
}
|
|
11
|
+
catch (err) {
|
|
12
|
+
if (typeof err === "object" && err !== null && "code" in err && err.code === "ENOENT") {
|
|
13
|
+
return undefined;
|
|
14
|
+
}
|
|
15
|
+
throw err;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export const WINDOWS_DEVICE_NAME = /^(?:con|prn|aux|nul|com[1-9]|lpt[1-9])(?:\.|$)/i;
|
|
19
|
+
/** An explicit absolute destination is allowed. A relative destination stays inside its base directory. */
|
|
20
|
+
export function resolveOutputPath(base, name) {
|
|
21
|
+
if (name === "" || name.includes("\0") || name.endsWith("/") || name.endsWith("\\")) {
|
|
22
|
+
throw new CliError(`Invalid output file path: ${JSON.stringify(name)}`, { exitCode: EXIT_VALIDATION });
|
|
23
|
+
}
|
|
24
|
+
const target = resolve(base, name);
|
|
25
|
+
if (!isAbsolute(name)) {
|
|
26
|
+
const fromBase = relative(base, target);
|
|
27
|
+
if (fromBase === "" || fromBase === ".." || fromBase.startsWith(`..${sep}`) || isAbsolute(fromBase)) {
|
|
28
|
+
throw new CliError(`Output path must stay within ${base}: ${JSON.stringify(name)}`, { exitCode: EXIT_VALIDATION });
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return target;
|
|
32
|
+
}
|
|
33
|
+
function isInside(base, target) {
|
|
34
|
+
const fromBase = relative(base, target);
|
|
35
|
+
return fromBase !== "" && fromBase !== ".." && !fromBase.startsWith(`..${sep}`) && !isAbsolute(fromBase);
|
|
36
|
+
}
|
|
37
|
+
/** The deepest existing ancestor of `path` (the path itself when it exists), or undefined when nothing on the way exists. */
|
|
38
|
+
function nearestExisting(path) {
|
|
39
|
+
let current = path;
|
|
40
|
+
for (;;) {
|
|
41
|
+
if (tryLstat(current) !== undefined) {
|
|
42
|
+
return current;
|
|
43
|
+
}
|
|
44
|
+
const parent = dirname(current);
|
|
45
|
+
if (parent === current) {
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
current = parent;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Checks the destination before a write and reports the existing regular file there, if any.
|
|
53
|
+
*
|
|
54
|
+
* `baseDir` is the directory the person is working in (default: the target's own directory). It and everything above
|
|
55
|
+
* it is trusted as the system resolved it, so `/var -> private/var` on macOS or a symlinked workspace do not matter.
|
|
56
|
+
* Every component below it, down to the target itself, must be a plain name (no `..`, reserved device name, trailing
|
|
57
|
+
* dot or space, or characters Windows refuses) and must not be a symbolic link.
|
|
58
|
+
*/
|
|
59
|
+
export function checkOutputPath(path, baseDir) {
|
|
60
|
+
const unsafe = () => new CliError(`Unsafe output path: ${JSON.stringify(path)}`, { exitCode: EXIT_VALIDATION });
|
|
61
|
+
if (path.split(/[\\/]/).includes("..")) {
|
|
62
|
+
throw unsafe();
|
|
63
|
+
}
|
|
64
|
+
const target = resolve(path);
|
|
65
|
+
let base = baseDir === undefined ? dirname(target) : resolve(baseDir);
|
|
66
|
+
if (!isInside(base, target)) {
|
|
67
|
+
base = dirname(target); // an explicit destination outside the working directory: only its own name is judged
|
|
68
|
+
}
|
|
69
|
+
try {
|
|
70
|
+
const anchor = nearestExisting(base);
|
|
71
|
+
if (anchor === undefined) {
|
|
72
|
+
throw new CliError(`Could not inspect output path ${path}: no existing parent directory`);
|
|
73
|
+
}
|
|
74
|
+
// Follow whatever the system and the person set up at and above the anchor; judge only what lies below it.
|
|
75
|
+
const trusted = realpathSync(anchor);
|
|
76
|
+
if (!tryLstat(trusted)?.isDirectory()) {
|
|
77
|
+
throw new CliError(`Output parent is not a directory: ${anchor}`, { exitCode: EXIT_VALIDATION });
|
|
78
|
+
}
|
|
79
|
+
const components = relative(anchor, target).split(sep);
|
|
80
|
+
let current = trusted;
|
|
81
|
+
for (const [index, name] of components.entries()) {
|
|
82
|
+
if (name === "" || WINDOWS_DEVICE_NAME.test(name) || /[<>:"|?*\\]/.test(name) || /[. ]$/.test(name) || oneLine(name) !== name) {
|
|
83
|
+
throw unsafe();
|
|
84
|
+
}
|
|
85
|
+
current = join(current, name);
|
|
86
|
+
const stat = tryLstat(current);
|
|
87
|
+
if (stat?.isSymbolicLink()) {
|
|
88
|
+
throw new CliError(`Refusing to write through a symbolic link: ${join(anchor, ...components.slice(0, index + 1))}`, {
|
|
89
|
+
exitCode: EXIT_VALIDATION,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
if (index === components.length - 1) {
|
|
93
|
+
if (stat === undefined || stat.isFile()) {
|
|
94
|
+
return stat;
|
|
95
|
+
}
|
|
96
|
+
throw new CliError(`Output path is not a regular file: ${target}`, { exitCode: EXIT_VALIDATION });
|
|
97
|
+
}
|
|
98
|
+
if (stat !== undefined && !stat.isDirectory()) {
|
|
99
|
+
throw new CliError(`Output parent is not a directory: ${join(anchor, ...components.slice(0, index + 1))}`, {
|
|
100
|
+
exitCode: EXIT_VALIDATION,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
catch (err) {
|
|
106
|
+
if (err instanceof CliError) {
|
|
107
|
+
throw err;
|
|
108
|
+
}
|
|
109
|
+
throw new CliError(`Could not inspect output path ${path}: ${errorMessage(err)}`);
|
|
110
|
+
}
|
|
111
|
+
return undefined;
|
|
112
|
+
}
|
|
113
|
+
/** Reads at most `maxBytes` of UTF-8 text, accepting a byte order mark and normalizing line endings. */
|
|
114
|
+
export function readUtf8File(path, maxBytes) {
|
|
115
|
+
const tooLarge = () => new CliError(`${path} exceeds the ${maxBytes}-byte limit.`, { exitCode: EXIT_VALIDATION });
|
|
116
|
+
let content;
|
|
117
|
+
try {
|
|
118
|
+
const fd = openSync(path, constants.O_RDONLY | constants.O_NONBLOCK);
|
|
119
|
+
try {
|
|
120
|
+
const stat = fstatSync(fd);
|
|
121
|
+
if (!stat.isFile()) {
|
|
122
|
+
throw new CliError(`Expected a regular file: ${path}`, { exitCode: EXIT_VALIDATION });
|
|
123
|
+
}
|
|
124
|
+
if (stat.size > maxBytes) {
|
|
125
|
+
throw tooLarge();
|
|
126
|
+
}
|
|
127
|
+
const buffer = Buffer.allocUnsafe(maxBytes + 1);
|
|
128
|
+
let offset = 0;
|
|
129
|
+
while (offset < buffer.length) {
|
|
130
|
+
const count = readSync(fd, buffer, offset, buffer.length - offset, null);
|
|
131
|
+
if (count === 0) {
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
offset += count;
|
|
135
|
+
}
|
|
136
|
+
if (offset > maxBytes) {
|
|
137
|
+
throw tooLarge();
|
|
138
|
+
}
|
|
139
|
+
content = buffer.subarray(0, offset);
|
|
140
|
+
}
|
|
141
|
+
finally {
|
|
142
|
+
closeSync(fd);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
catch (err) {
|
|
146
|
+
if (err instanceof CliError) {
|
|
147
|
+
throw err;
|
|
148
|
+
}
|
|
149
|
+
throw new CliError(`Could not read ${path}: ${errorMessage(err)}`);
|
|
150
|
+
}
|
|
151
|
+
try {
|
|
152
|
+
return new TextDecoder("utf-8", { fatal: true }).decode(content).replace(/\r\n?/g, "\n");
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
throw new CliError(`${path} is not valid UTF-8.`, { exitCode: EXIT_VALIDATION });
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
/** Error codes that mean "this filesystem cannot make hard links" (FAT, exFAT, some network and FUSE mounts). */
|
|
159
|
+
const NO_HARD_LINKS = new Set(["EPERM", "ENOTSUP", "EOPNOTSUPP", "ENOSYS", "EXDEV", "EMLINK"]);
|
|
160
|
+
function errorCode(err) {
|
|
161
|
+
return typeof err === "object" && err !== null && "code" in err && typeof err.code === "string" ? err.code : undefined;
|
|
162
|
+
}
|
|
163
|
+
function writeAll(fd, bytes, fileMode) {
|
|
164
|
+
let offset = 0;
|
|
165
|
+
while (offset < bytes.length) {
|
|
166
|
+
offset += writeSync(fd, bytes, offset);
|
|
167
|
+
}
|
|
168
|
+
if (process.platform !== "win32") {
|
|
169
|
+
fchmodSync(fd, fileMode);
|
|
170
|
+
}
|
|
171
|
+
fsyncSync(fd);
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Creates `path` only if nothing is there: it is installed with a hard link when the filesystem has them, else by
|
|
175
|
+
* creating it directly with `wx` (which also refuses a planted symbolic link). Either way an existing file is never replaced.
|
|
176
|
+
*/
|
|
177
|
+
function installExclusive(tmp, path, bytes, fileMode) {
|
|
178
|
+
try {
|
|
179
|
+
linkSync(tmp, path);
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
catch (err) {
|
|
183
|
+
const code = errorCode(err);
|
|
184
|
+
if (code === undefined || !NO_HARD_LINKS.has(code)) {
|
|
185
|
+
throw err;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
// `wx` fails with EEXIST for a file, a directory or a link (dangling or not) that is already there.
|
|
189
|
+
const fd = openSync(path, "wx", fileMode);
|
|
190
|
+
try {
|
|
191
|
+
writeAll(fd, bytes, fileMode);
|
|
192
|
+
}
|
|
193
|
+
finally {
|
|
194
|
+
closeSync(fd);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Writes `content` to a sibling temp file created exclusively with `fileMode`, then renames it over `path`.
|
|
199
|
+
* A reader never sees a half-written file, and a symlink planted at `path` is replaced rather than followed.
|
|
200
|
+
*/
|
|
201
|
+
export function writeFileAtomic(path, content, options) {
|
|
202
|
+
const dir = dirname(path);
|
|
203
|
+
let tmp;
|
|
204
|
+
try {
|
|
205
|
+
if (options.refuseSymlink === true && tryLstat(path)?.isSymbolicLink() === true) {
|
|
206
|
+
throw new CliError(`Refusing to write through a symbolic link: ${path}`);
|
|
207
|
+
}
|
|
208
|
+
mkdirSync(dir, options.dirMode === undefined ? { recursive: true } : { recursive: true, mode: options.dirMode });
|
|
209
|
+
tmp = join(dir, `.${basename(path)}.${randomBytes(6).toString("hex")}.tmp`);
|
|
210
|
+
const bytes = Buffer.from(content, "utf8");
|
|
211
|
+
const fd = openSync(tmp, "wx", options.fileMode);
|
|
212
|
+
try {
|
|
213
|
+
writeAll(fd, bytes, options.fileMode);
|
|
214
|
+
}
|
|
215
|
+
finally {
|
|
216
|
+
closeSync(fd);
|
|
217
|
+
}
|
|
218
|
+
if (options.exclusive === true) {
|
|
219
|
+
installExclusive(tmp, path, bytes, options.fileMode);
|
|
220
|
+
}
|
|
221
|
+
else {
|
|
222
|
+
renameSync(tmp, path);
|
|
223
|
+
tmp = undefined;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
catch (err) {
|
|
227
|
+
if (err instanceof CliError) {
|
|
228
|
+
throw err;
|
|
229
|
+
}
|
|
230
|
+
if (options.exclusive === true && errorCode(err) === "EEXIST") {
|
|
231
|
+
throw new CliError(`File ${path} already exists; pass --force to overwrite.`, { exitCode: EXIT_CONFLICT });
|
|
232
|
+
}
|
|
233
|
+
throw new CliError(`Could not save ${options.label} ${path}: ${errorMessage(err)}`);
|
|
234
|
+
}
|
|
235
|
+
finally {
|
|
236
|
+
if (tmp !== undefined) {
|
|
237
|
+
try {
|
|
238
|
+
unlinkSync(tmp);
|
|
239
|
+
}
|
|
240
|
+
catch {
|
|
241
|
+
// The temp file is already gone; nothing is left to clean up.
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { runCli } from "./cli.js";
|
|
3
|
+
import { EXIT_RUNTIME } from "./errors.js";
|
|
4
|
+
function fail(reason) {
|
|
5
|
+
const message = reason instanceof Error ? reason.message : String(reason);
|
|
6
|
+
process.stderr.write(`evalctl: unexpected error: ${message}\n`);
|
|
7
|
+
process.exit(EXIT_RUNTIME);
|
|
8
|
+
}
|
|
9
|
+
// A closed pipe (`evalctl runs | head`) is the reader's choice, not a failure.
|
|
10
|
+
process.stdout.on("error", (err) => {
|
|
11
|
+
if (err.code === "EPIPE") {
|
|
12
|
+
process.exit(0);
|
|
13
|
+
}
|
|
14
|
+
fail(err);
|
|
15
|
+
});
|
|
16
|
+
process.on("unhandledRejection", fail);
|
|
17
|
+
process.on("uncaughtException", fail);
|
|
18
|
+
process.exitCode = await runCli(process.argv.slice(2));
|
package/dist/output.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { type JsonObject } from "./util.js";
|
|
2
|
+
export interface TextSink {
|
|
3
|
+
write(chunk: string): unknown;
|
|
4
|
+
isTTY?: boolean;
|
|
5
|
+
}
|
|
6
|
+
interface Sinks {
|
|
7
|
+
stdout: TextSink;
|
|
8
|
+
stderr: TextSink;
|
|
9
|
+
}
|
|
10
|
+
/** Runs `fn` with `out`, `errOut`, `outRaw` and `emitJson` writing to `sinks`, for everything `fn` starts. */
|
|
11
|
+
export declare function withOutputSinks<T>(sinks: Sinks, fn: () => T): T;
|
|
12
|
+
export declare function stdoutIsTty(): boolean;
|
|
13
|
+
/** A line on stdout. */
|
|
14
|
+
export declare function out(text?: string): void;
|
|
15
|
+
/** A line on stderr. */
|
|
16
|
+
export declare function errOut(text?: string): void;
|
|
17
|
+
/** Text exactly as given, no newline added. Control characters are dropped when a terminal is reading. */
|
|
18
|
+
export declare function outRaw(text: string): void;
|
|
19
|
+
export declare function emitJson(data: unknown): void;
|
|
20
|
+
export declare function displayText(value: unknown): string;
|
|
21
|
+
/** Left-aligned columns separated by two spaces, a dashed rule under the headers, no trailing blanks. */
|
|
22
|
+
export declare function formatTable(headers: readonly string[], rows: readonly (readonly unknown[])[]): string;
|
|
23
|
+
/** `YYYY-MM-DD HH:MM:SSZ` for epoch seconds; anything else is shown as given, null as a dash. */
|
|
24
|
+
export declare function formatTimestamp(value: unknown): string;
|
|
25
|
+
/** Turns a server-relative link into an absolute one for display; absolute links and other text pass through. */
|
|
26
|
+
export declare function resolveDisplayUrl(serverUrl: string, rawUrl: unknown): string;
|
|
27
|
+
export declare function printValidationIssues(payload: Readonly<JsonObject>): void;
|
|
28
|
+
export type HashChanges = Record<string, readonly [string | null, string | null]>;
|
|
29
|
+
/**
|
|
30
|
+
* What a save did to the definition hashes, from the previous sidecar entry and the server's answer.
|
|
31
|
+
* Without a visible change the server's own `hash-change` warning, if any, becomes the notice.
|
|
32
|
+
*/
|
|
33
|
+
export declare function hashImpactChanges(previous: Readonly<JsonObject> | null | undefined, response: Readonly<JsonObject>): {
|
|
34
|
+
changes: HashChanges;
|
|
35
|
+
notice: string | null;
|
|
36
|
+
};
|
|
37
|
+
export declare function printHashImpactBanner(changes: Readonly<HashChanges>, notice?: string | null): void;
|
|
38
|
+
export {};
|
package/dist/output.js
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
import { cellText, isObject, oneLine, stripControls } from "./util.js";
|
|
3
|
+
// Commands print through the module-level functions below. The streams they print to belong to the run in
|
|
4
|
+
// progress (`withOutputSinks`), carried along its async calls, so concurrent runs never share or clobber them.
|
|
5
|
+
const storage = new AsyncLocalStorage();
|
|
6
|
+
function current() {
|
|
7
|
+
return storage.getStore() ?? { stdout: process.stdout, stderr: process.stderr };
|
|
8
|
+
}
|
|
9
|
+
/** Runs `fn` with `out`, `errOut`, `outRaw` and `emitJson` writing to `sinks`, for everything `fn` starts. */
|
|
10
|
+
export function withOutputSinks(sinks, fn) {
|
|
11
|
+
return storage.run(sinks, fn);
|
|
12
|
+
}
|
|
13
|
+
export function stdoutIsTty() {
|
|
14
|
+
return current().stdout.isTTY === true;
|
|
15
|
+
}
|
|
16
|
+
/** A line on stdout. */
|
|
17
|
+
export function out(text = "") {
|
|
18
|
+
current().stdout.write(`${text}\n`);
|
|
19
|
+
}
|
|
20
|
+
/** A line on stderr. */
|
|
21
|
+
export function errOut(text = "") {
|
|
22
|
+
current().stderr.write(`${text}\n`);
|
|
23
|
+
}
|
|
24
|
+
/** Text exactly as given, no newline added. Control characters are dropped when a terminal is reading. */
|
|
25
|
+
export function outRaw(text) {
|
|
26
|
+
current().stdout.write(stdoutIsTty() ? stripControls(text) : text);
|
|
27
|
+
}
|
|
28
|
+
export function emitJson(data) {
|
|
29
|
+
current().stdout.write(`${JSON.stringify(data, null, 2)}\n`);
|
|
30
|
+
}
|
|
31
|
+
export function displayText(value) {
|
|
32
|
+
return oneLine(cellText(value));
|
|
33
|
+
}
|
|
34
|
+
function width(text) {
|
|
35
|
+
return Array.from(text).length;
|
|
36
|
+
}
|
|
37
|
+
function pad(text, size) {
|
|
38
|
+
return text + " ".repeat(Math.max(0, size - width(text)));
|
|
39
|
+
}
|
|
40
|
+
/** Left-aligned columns separated by two spaces, a dashed rule under the headers, no trailing blanks. */
|
|
41
|
+
export function formatTable(headers, rows) {
|
|
42
|
+
const body = rows.map((row) => row.map(displayText));
|
|
43
|
+
const widths = headers.map((header) => width(header));
|
|
44
|
+
for (const row of body) {
|
|
45
|
+
row.forEach((text, index) => {
|
|
46
|
+
if (index < widths.length) {
|
|
47
|
+
widths[index] = Math.max(widths[index] ?? 0, width(text));
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
const render = (cells) => widths
|
|
52
|
+
.map((size, index) => pad(cells[index] ?? "", size))
|
|
53
|
+
.join(" ")
|
|
54
|
+
.trimEnd();
|
|
55
|
+
return [render(headers), render(widths.map((size) => "-".repeat(size))), ...body.map(render)].join("\n");
|
|
56
|
+
}
|
|
57
|
+
/** `YYYY-MM-DD HH:MM:SSZ` for epoch seconds; anything else is shown as given, null as a dash. */
|
|
58
|
+
export function formatTimestamp(value) {
|
|
59
|
+
if (typeof value === "number" && Number.isFinite(value)) {
|
|
60
|
+
const date = new Date(value * 1000);
|
|
61
|
+
if (!Number.isNaN(date.getTime())) {
|
|
62
|
+
return `${date.toISOString().slice(0, 19).replace("T", " ")}Z`;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (value === null || value === undefined) {
|
|
66
|
+
return "-";
|
|
67
|
+
}
|
|
68
|
+
return typeof value === "string" ? value : displayText(value);
|
|
69
|
+
}
|
|
70
|
+
/** Turns a server-relative link into an absolute one for display; absolute links and other text pass through. */
|
|
71
|
+
export function resolveDisplayUrl(serverUrl, rawUrl) {
|
|
72
|
+
if (rawUrl === null || rawUrl === undefined) {
|
|
73
|
+
return "";
|
|
74
|
+
}
|
|
75
|
+
const text = cellText(rawUrl, "").trim();
|
|
76
|
+
if (text === "") {
|
|
77
|
+
return "";
|
|
78
|
+
}
|
|
79
|
+
if (/^https?:\/\//i.test(text)) {
|
|
80
|
+
return text;
|
|
81
|
+
}
|
|
82
|
+
if (text.startsWith("/") && !text.startsWith("//")) {
|
|
83
|
+
const base = serverUrl.replace(/\/+$/, "");
|
|
84
|
+
return base === "" ? text : `${base}${text}`;
|
|
85
|
+
}
|
|
86
|
+
return text;
|
|
87
|
+
}
|
|
88
|
+
export function printValidationIssues(payload) {
|
|
89
|
+
const asList = (value) => (Array.isArray(value) ? value.filter(isObject) : []);
|
|
90
|
+
const errors = asList(payload["errors"]);
|
|
91
|
+
const warnings = asList(payload["warnings"]);
|
|
92
|
+
const unacked = asList(payload["unacked_warnings"]);
|
|
93
|
+
const unackedCodes = new Set(unacked.map((warning) => warning["code"]));
|
|
94
|
+
const line = (tag, item) => {
|
|
95
|
+
const field = item["field"] ? ` (${displayText(item["field"])})` : "";
|
|
96
|
+
errOut(`[${tag}] ${displayText(item["code"])}${field}: ${displayText(item["message"])}`);
|
|
97
|
+
if (item["hint"]) {
|
|
98
|
+
errOut(` hint: ${displayText(item["hint"])}`);
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
for (const item of errors) {
|
|
102
|
+
line("ERROR", item);
|
|
103
|
+
}
|
|
104
|
+
for (const item of warnings) {
|
|
105
|
+
line(unackedCodes.has(item["code"]) ? "WARN-UNACKED" : "WARN", item);
|
|
106
|
+
}
|
|
107
|
+
for (const item of unacked) {
|
|
108
|
+
if (!warnings.some((warning) => warning["code"] === item["code"])) {
|
|
109
|
+
line("WARN-UNACKED", item);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* What a save did to the definition hashes, from the previous sidecar entry and the server's answer.
|
|
115
|
+
* Without a visible change the server's own `hash-change` warning, if any, becomes the notice.
|
|
116
|
+
*/
|
|
117
|
+
export function hashImpactChanges(previous, response) {
|
|
118
|
+
const changes = {};
|
|
119
|
+
if (previous) {
|
|
120
|
+
for (const key of ["voice_hash", "text_hash"]) {
|
|
121
|
+
if (key in previous) {
|
|
122
|
+
const before = previous[key] ?? null;
|
|
123
|
+
const after = response[key] ?? null;
|
|
124
|
+
if (before !== after) {
|
|
125
|
+
changes[key] = [before === null ? null : cellText(before), after === null ? null : cellText(after)];
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
let notice = null;
|
|
131
|
+
if (Object.keys(changes).length === 0 && Array.isArray(response["warnings"])) {
|
|
132
|
+
for (const warning of response["warnings"]) {
|
|
133
|
+
if (isObject(warning) && warning["code"] === "hash-change") {
|
|
134
|
+
const message = warning["message"];
|
|
135
|
+
notice =
|
|
136
|
+
typeof message === "string" && message !== ""
|
|
137
|
+
? message
|
|
138
|
+
: "Definition hash changed; score history will start a new definition series.";
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return { changes, notice };
|
|
144
|
+
}
|
|
145
|
+
export function printHashImpactBanner(changes, notice = null) {
|
|
146
|
+
const entries = Object.entries(changes);
|
|
147
|
+
if (entries.length === 0 && notice === null) {
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
out("=== Hash Impact Banner ===");
|
|
151
|
+
for (const [key, [before, after]] of entries) {
|
|
152
|
+
out(` ${key}: ${before ?? "null"} -> ${after ?? "null"}`);
|
|
153
|
+
}
|
|
154
|
+
out(notice !== null ? ` ${oneLine(notice)}` : " Score history will start a new definition series for changed hashes.");
|
|
155
|
+
out("==========================");
|
|
156
|
+
}
|
package/dist/prompt.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export type InputStream = NodeJS.ReadableStream & {
|
|
2
|
+
isTTY?: boolean;
|
|
3
|
+
setRawMode?: (mode: boolean) => unknown;
|
|
4
|
+
};
|
|
5
|
+
export interface PromptIO {
|
|
6
|
+
input: InputStream;
|
|
7
|
+
/** Prompts go here, stderr in practice, so stdout stays clean for data and `--json`. */
|
|
8
|
+
output: NodeJS.WritableStream;
|
|
9
|
+
}
|
|
10
|
+
/** Asks one visible question and returns the typed line (empty at end of input). */
|
|
11
|
+
export declare function promptLine(question: string, io: PromptIO): Promise<string>;
|
|
12
|
+
/**
|
|
13
|
+
* Asks for a secret without echoing it: raw-mode terminal input handling Backspace, Enter, Ctrl-D and Ctrl-C.
|
|
14
|
+
* It refuses when input is not a terminal; piped secrets belong on `--password-stdin`.
|
|
15
|
+
*
|
|
16
|
+
* Whatever ends the prompt (Enter, Ctrl-C, end of input, a stream error), the terminal leaves raw mode and the
|
|
17
|
+
* listeners are removed. Bytes are decoded as UTF-8 across chunk boundaries; arrow keys and other escape sequences
|
|
18
|
+
* are skipped one by one; a bracketed paste keeps its text and loses its markers; control characters are ignored.
|
|
19
|
+
*/
|
|
20
|
+
export declare function promptPassword(question: string, io: PromptIO): Promise<string>;
|
|
21
|
+
/** The most `--password-stdin` reads: a password is a few bytes, and a stray file must not be buffered whole. */
|
|
22
|
+
export declare const MAX_STDIN_BYTES: number;
|
|
23
|
+
/** Everything on the stream (at most 64 KiB), with the trailing line break(s) removed: a password piped by `echo` or a file. */
|
|
24
|
+
export declare function readStdin(input: NodeJS.ReadableStream): Promise<string>;
|
|
25
|
+
/**
|
|
26
|
+
* True when the action may go ahead: `yes` was passed, or a person typed y/yes.
|
|
27
|
+
* Without a terminal and without `yes` the answer is no, so a script never hangs on a prompt or confirms by accident.
|
|
28
|
+
*/
|
|
29
|
+
export declare function confirmPrompt(message: string, yes: boolean, io: PromptIO): Promise<boolean>;
|