@henols/c64-re-tools 2.3.0-rc.5 → 2.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/package.json +1 -1
- package/skills/c64-assembler/SKILL.md +149 -78
- package/skills/c64-assembler/scripts/assembler.ts +18 -11
- package/skills/c64-basic/SKILL.md +90 -52
- package/skills/c64-basic/scripts/basic.ts +15 -6
- package/skills/c64-development/SKILL.md +46 -73
- package/skills/c64-disk/SKILL.md +145 -73
- package/skills/c64-disk/scripts/disk.ts +11 -6
- package/skills/c64-docs/SKILL.md +199 -118
- package/skills/c64-docs/references/page-types.md +3 -3
- package/skills/c64-docs/references/style.md +9 -13
- package/skills/c64-docs/scripts/docs.ts +39 -15
- package/skills/c64-docs/scripts/lib/links.ts +1 -1
- package/skills/c64-docs/scripts/lib/reference.ts +2 -2
- package/skills/c64-docs/scripts/lib/source-map.ts +2 -2
- package/skills/c64-export/SKILL.md +21 -45
- package/skills/c64-knowledge/SKILL.md +489 -233
- package/skills/c64-knowledge/scripts/knowledge.ts +31 -26
- package/skills/c64-memory-map/SKILL.md +131 -65
- package/skills/c64-memory-map/scripts/memory-map.ts +19 -11
- package/skills/c64-packer/SKILL.md +120 -72
- package/skills/c64-packer/scripts/packer.ts +10 -3
- package/skills/c64-provenance/SKILL.md +301 -173
- package/skills/c64-provenance/scripts/lib/library.ts +6 -6
- package/skills/c64-provenance/scripts/provenance.ts +85 -36
- package/skills/c64-reverse-engineering/SKILL.md +55 -72
- package/skills/c64-source/SKILL.md +316 -224
- package/skills/c64-source/scripts/lib/export.ts +9 -14
- package/skills/c64-source/scripts/lib/loader.ts +5 -5
- package/skills/c64-source/scripts/lib/source.ts +5 -5
- package/skills/c64-source/scripts/source.ts +106 -21
- package/skills/c64-static-analysis/SKILL.md +188 -112
- package/skills/c64-static-analysis/scripts/static-analysis.ts +29 -9
- package/skills/c64-testing/SKILL.md +272 -166
- package/skills/c64-testing/references/recording-format.md +11 -12
- package/skills/c64-testing/references/scenario-format.md +20 -5
- package/skills/c64-testing/scripts/lib/diff-files.ts +8 -8
- package/skills/c64-testing/scripts/lib/recording.ts +7 -7
- package/skills/c64-testing/scripts/lib/replay.ts +7 -7
- package/skills/c64-testing/scripts/lib/runs.ts +6 -7
- package/skills/c64-testing/scripts/lib/scenario.ts +12 -6
- package/skills/c64-testing/scripts/testing.ts +35 -15
- package/skills/c64-unpacker/SKILL.md +273 -127
- package/skills/c64-unpacker/scripts/lib/loads.ts +3 -3
- package/skills/c64-unpacker/scripts/unpacker.ts +70 -11
- package/skills/emulator/SKILL.md +12 -12
- package/src/errors.ts +22 -2
- package/src/host/vice/session.ts +1 -1
- package/src/host-client/entry-state.ts +1 -1
- package/src/host-client/image.ts +3 -3
- package/src/host-client/steps.ts +1 -1
- package/src/knowledge/expression.ts +1 -1
- package/src/knowledge/names.ts +2 -3
- package/src/knowledge/program.ts +2 -3
- package/src/knowledge/write.ts +2 -2
- package/src/mcp/inputs.ts +1 -1
- package/src/mcp/tools/input.ts +1 -1
- package/src/mcp/tools/media.ts +1 -1
- package/src/native/exomizer.ts +2 -2
- package/src/project.ts +2 -2
- package/src/script.ts +164 -75
package/src/script.ts
CHANGED
|
@@ -5,25 +5,70 @@
|
|
|
5
5
|
// The command word comes first. The subjects are the files, addresses, ranges or
|
|
6
6
|
// names that the command acts on. Everything else is a named option; an option
|
|
7
7
|
// with a name from OPTIONS has the same meaning and parser in every script.
|
|
8
|
-
// A script prints one JSON
|
|
9
|
-
//
|
|
10
|
-
//
|
|
8
|
+
// A script prints one JSON envelope on one line:
|
|
9
|
+
//
|
|
10
|
+
// {ok: true, command, body, hint?, nextAction?} exit status 0
|
|
11
|
+
// {ok: false, command?, code, error, hint?, nextAction?} exit status 2
|
|
12
|
+
//
|
|
13
|
+
// A verdict (PASS, FAIL, INCONCLUSIVE) is in the body: a check that fails is a
|
|
14
|
+
// command that worked. error is one sentence: what is wrong. hint tells what to
|
|
15
|
+
// do. nextAction is a full command line that runs from any directory. With no
|
|
16
|
+
// arguments or with --help, the command is "help" and the body is the usage.
|
|
17
|
+
// Nothing is thrown out of a script: every failure becomes an error envelope.
|
|
11
18
|
|
|
19
|
+
import { resolve } from "node:path";
|
|
12
20
|
import { parseArgs } from "node:util";
|
|
13
21
|
|
|
14
22
|
import { c64AddressOf, formatC64Address, SPACE_PATTERN, SYMBOL_NAME, type AddressRange } from "./c64.ts";
|
|
15
|
-
import { isRefusal, Refusal, type ErrorCode } from "./errors.ts";
|
|
23
|
+
import { isRefusal, Refusal, splitMessage, type ErrorCode } from "./errors.ts";
|
|
16
24
|
import { checkOutputPath, projectRoot, resolveProjectPath, useProject, type OutputKind } from "./project.ts";
|
|
17
|
-
import {
|
|
25
|
+
import { STOP_AND_ASK } from "./protocol/messages.ts";
|
|
18
26
|
import { elapsedMs, startTrace, trace, type TraceFields } from "./trace.ts";
|
|
19
27
|
|
|
20
28
|
/** A wrong command line: an invalid-input refusal that also shows the usage of the command. */
|
|
21
29
|
export class UsageError extends Refusal {
|
|
22
30
|
override name = "UsageError";
|
|
23
31
|
|
|
24
|
-
constructor(message: string) {
|
|
25
|
-
super("invalid-input", message);
|
|
32
|
+
constructor(message: string, options: { hint?: string } = {}) {
|
|
33
|
+
super("invalid-input", message, options);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** A result with a hint or a next command for the envelope. A command returns it in place of the bare body. */
|
|
38
|
+
export class Reply {
|
|
39
|
+
readonly body: unknown;
|
|
40
|
+
readonly hint?: string;
|
|
41
|
+
readonly nextAction?: string;
|
|
42
|
+
|
|
43
|
+
constructor(body: unknown, extra: { hint?: string | undefined; nextAction?: string | undefined }) {
|
|
44
|
+
this.body = body;
|
|
45
|
+
if (extra.hint !== undefined) this.hint = extra.hint;
|
|
46
|
+
if (extra.nextAction !== undefined) this.nextAction = extra.nextAction;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function reply(body: unknown, extra: { hint?: string | undefined; nextAction?: string | undefined }): Reply {
|
|
51
|
+
return new Reply(body, extra);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** A word for a POSIX shell: as it is when that is safe, else in single quotes. */
|
|
55
|
+
export function shellWord(word: string): string {
|
|
56
|
+
return /^[\w@%+=:,./-]+$/.test(word) ? word : `'${word.replaceAll("'", `'\\''`)}'`;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The full command line of this script with `args`: node, the absolute path of
|
|
61
|
+
* the script, the args, and --project with the absolute project folder when
|
|
62
|
+
* there is one. It runs from any directory.
|
|
63
|
+
*/
|
|
64
|
+
export function commandLine(args: readonly string[]): string {
|
|
65
|
+
let project: string | undefined;
|
|
66
|
+
try {
|
|
67
|
+
project = projectRoot();
|
|
68
|
+
} catch {
|
|
69
|
+
project = undefined;
|
|
26
70
|
}
|
|
71
|
+
return ["node", resolve(process.argv[1] ?? ""), ...args, ...(project === undefined ? [] : ["--project", project])].map(shellWord).join(" ");
|
|
27
72
|
}
|
|
28
73
|
|
|
29
74
|
/** A fault of the toolkit, not of the call: a script that declares its options wrong. */
|
|
@@ -83,7 +128,7 @@ export const OPTIONS = {
|
|
|
83
128
|
frames: { kind: "count", value: "<n>", help: "run for this number of video frames", min: 1, max: 1_000_000 },
|
|
84
129
|
"timeout-frames": { kind: "count", value: "<n>", help: "stop a wait after this number of video frames", min: 1, max: 1_000_000 },
|
|
85
130
|
"max-listed": { kind: "count", value: "<n>", help: "list at most n items of each list (default 50). The counts are always given", min: 0, max: 100_000, default: 50 },
|
|
86
|
-
symbols: { kind: "path", value: "<file>", help: "the symbols of a build: the
|
|
131
|
+
symbols: { kind: "path", value: "<file>", help: "the symbols of a build: the symbols file of the assembler, or a JSON map of name to $ address" },
|
|
87
132
|
origin: { kind: "text", value: "<origin>", help: "user, llm, dxa, ghidra, heuristic, semantic (user and llm) or analyzer (dxa, ghidra and heuristic), or a comma list" },
|
|
88
133
|
reason: { kind: "text", value: "<text>", help: "why: kept with the revision" },
|
|
89
134
|
evidence: { kind: "text", value: "<kind:ref>", help: "what the change rests on: coverage:<map>, test:<file>, file:<file> or note:<text>", multiple: true },
|
|
@@ -167,6 +212,11 @@ export class Call {
|
|
|
167
212
|
this.#names = names;
|
|
168
213
|
}
|
|
169
214
|
|
|
215
|
+
/** The full command line of `command` of this script with `args`, for nextAction. */
|
|
216
|
+
next(command: string, ...args: string[]): string {
|
|
217
|
+
return commandLine([command, ...args]);
|
|
218
|
+
}
|
|
219
|
+
|
|
170
220
|
/** The knowledge space of the command: --space, else c64. */
|
|
171
221
|
get space(): string {
|
|
172
222
|
return ("space" in this.#options ? this.string("space") : undefined) ?? "c64";
|
|
@@ -290,11 +340,10 @@ export function listed<T, U = T>(items: readonly T[], max: number, show: (item:
|
|
|
290
340
|
// ---------------------------------------------------------------------------
|
|
291
341
|
// Usage
|
|
292
342
|
|
|
293
|
-
/** The
|
|
294
|
-
export const
|
|
343
|
+
/** The conventions in the help of every script. */
|
|
344
|
+
export const CONVENTIONS = `Addresses are $ and 1 to 4 hex digits, for example '$c000' or '$fb', or a name: a symbol, else an analyzer name that only one analyzer gives. A range is <start>-<end>, for example '$c000-$cfff'. Quote them in a shell.
|
|
295
345
|
Paths are relative to the project folder. Every script takes --project <folder>: the project folder, relative to the working directory. Without it, the script uses the nearest folder at or above the working directory that holds .c64-re-tools/.
|
|
296
|
-
|
|
297
|
-
Exit status: 0 done or PASS, 1 FAIL, 2 error, 3 INCONCLUSIVE.`;
|
|
346
|
+
Each command prints one JSON envelope: {ok: true, command, body, hint?, nextAction?} with exit status 0, or {ok: false, command?, code, error, hint?, nextAction?} with exit status 2.`;
|
|
298
347
|
|
|
299
348
|
function optionLabel(name: string, spec: OptionSpec): string {
|
|
300
349
|
return spec.kind === "flag" ? `--${name}` : `--${name} ${spec.value ?? "<value>"}`;
|
|
@@ -313,32 +362,33 @@ function synopsis(script: string, command: CommandSpec): string {
|
|
|
313
362
|
return parts.join(" ");
|
|
314
363
|
}
|
|
315
364
|
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
return
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
365
|
+
/** The help body of one command: its synopsis, subjects and options. */
|
|
366
|
+
export function commandHelp(script: ScriptSpec, command: CommandSpec): Record<string, unknown> {
|
|
367
|
+
return {
|
|
368
|
+
command: command.name,
|
|
369
|
+
synopsis: synopsis(script.name, command),
|
|
370
|
+
summary: command.summary,
|
|
371
|
+
subjects: (command.subjects ?? []).map((subject) => ({ name: subject.list ? `${subject.name}...` : subject.name, help: subject.help, ...(subject.optional ? { optional: true } : {}) })),
|
|
372
|
+
options: Object.entries(command.options ?? {}).map(([name, spec]) => ({
|
|
373
|
+
flag: optionLabel(name, spec),
|
|
374
|
+
help: spec.help,
|
|
375
|
+
...(spec.required ? { required: true } : {}),
|
|
376
|
+
...(spec.multiple ? { repeatable: true } : {}),
|
|
377
|
+
...(spec.default === undefined ? {} : { default: spec.default }),
|
|
378
|
+
})),
|
|
379
|
+
...(command.notes === undefined ? {} : { notes: command.notes }),
|
|
380
|
+
conventions: CONVENTIONS,
|
|
381
|
+
};
|
|
327
382
|
}
|
|
328
383
|
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
script.summary,
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
"",
|
|
338
|
-
`Give ${script.name}.ts <command> --help for the subjects and options of one command.`,
|
|
339
|
-
"",
|
|
340
|
-
CONTRACT_USAGE,
|
|
341
|
-
].join("\n");
|
|
384
|
+
/** The help body of a script: its commands. */
|
|
385
|
+
export function scriptHelp(script: ScriptSpec): Record<string, unknown> {
|
|
386
|
+
return {
|
|
387
|
+
script: `${script.name}.ts`,
|
|
388
|
+
summary: script.summary,
|
|
389
|
+
commands: script.commands.map((command) => ({ name: command.name, summary: command.summary, synopsis: synopsis(script.name, command) })),
|
|
390
|
+
conventions: CONVENTIONS,
|
|
391
|
+
};
|
|
342
392
|
}
|
|
343
393
|
|
|
344
394
|
// ---------------------------------------------------------------------------
|
|
@@ -434,11 +484,16 @@ function refuseSplitRange(command: CommandSpec, options: Readonly<Record<string,
|
|
|
434
484
|
}
|
|
435
485
|
}
|
|
436
486
|
|
|
487
|
+
/** a, b and c. */
|
|
488
|
+
function wordList(words: readonly string[]): string {
|
|
489
|
+
return words.length === 1 ? words[0]! : `${words.slice(0, -1).join(", ")} and ${words.at(-1)}`;
|
|
490
|
+
}
|
|
491
|
+
|
|
437
492
|
/** The options of a command for a message: --a, --b and --c, or a word that it has none. */
|
|
438
493
|
function optionList(options: Readonly<Record<string, OptionSpec>>): string {
|
|
439
494
|
const names = Object.keys(options).map((name) => `--${name}`);
|
|
440
|
-
if (names.length === 0) return "
|
|
441
|
-
return `Its options are ${
|
|
495
|
+
if (names.length === 0) return "The command takes no options";
|
|
496
|
+
return `Its options are ${wordList(names)}`;
|
|
442
497
|
}
|
|
443
498
|
|
|
444
499
|
/** True for a word that the command line reads as an option: - and one or more characters. A lone - is a subject (standard input). */
|
|
@@ -450,7 +505,7 @@ const isOptionWord = (word: string) => word.length > 1 && word.startsWith("-");
|
|
|
450
505
|
* does not have, a flag with a value, an option without its value, a value
|
|
451
506
|
* that starts with -, and a range option in two parts.
|
|
452
507
|
*/
|
|
453
|
-
function refuseOptionWords(command: CommandSpec, options: Readonly<Record<string, OptionSpec>>, argv: readonly string[]): void {
|
|
508
|
+
function refuseOptionWords(script: ScriptSpec, command: CommandSpec, options: Readonly<Record<string, OptionSpec>>, argv: readonly string[]): void {
|
|
454
509
|
const end = argv.indexOf("--");
|
|
455
510
|
const words = end < 0 ? argv : argv.slice(0, end);
|
|
456
511
|
for (let index = 0; index < words.length; index++) {
|
|
@@ -462,7 +517,11 @@ function refuseOptionWords(command: CommandSpec, options: Readonly<Record<string
|
|
|
462
517
|
const equals = word.indexOf("=");
|
|
463
518
|
const name = equals < 0 ? word.slice(2) : word.slice(2, equals);
|
|
464
519
|
const spec = options[name];
|
|
465
|
-
if (spec === undefined)
|
|
520
|
+
if (spec === undefined) {
|
|
521
|
+
const takers = script.commands.filter((other) => other.options !== undefined && name in other.options).map((other) => other.name);
|
|
522
|
+
const where = takers.length === 0 ? `No command of ${script.name}.ts takes --${name}.` : `--${name} is an option of ${wordList(takers)}.`;
|
|
523
|
+
throw new UsageError(`${command.name} has no option --${name}.`, { hint: `${where} ${optionList(options)}.` });
|
|
524
|
+
}
|
|
466
525
|
if (spec.kind === "flag") {
|
|
467
526
|
if (equals >= 0) throw new UsageError(`--${name} is a flag: give --${name} without a value`);
|
|
468
527
|
continue;
|
|
@@ -509,7 +568,7 @@ function checkOutput(text: string, what: string, kind: OutputKind): void {
|
|
|
509
568
|
export function parseCall(script: ScriptSpec, command: CommandSpec, argv: readonly string[]): Call {
|
|
510
569
|
const options = commandOptions(command);
|
|
511
570
|
refuseSplitRange(command, options, argv);
|
|
512
|
-
refuseOptionWords(command, options, argv);
|
|
571
|
+
refuseOptionWords(script, command, options, argv);
|
|
513
572
|
let parsed: ReturnType<typeof parseArgs>;
|
|
514
573
|
try {
|
|
515
574
|
parsed = parseArgs({
|
|
@@ -531,7 +590,7 @@ export function parseCall(script: ScriptSpec, command: CommandSpec, argv: readon
|
|
|
531
590
|
const most = last?.list ? Number.POSITIVE_INFINITY : specs.length;
|
|
532
591
|
if (subjects.length < least || subjects.length > most) {
|
|
533
592
|
const named = specs.length === 0 ? "no subjects" : specs.map((subject) => (subject.list ? `${subject.name}...` : subject.name)).join(" ");
|
|
534
|
-
throw new UsageError(subjects.length < least ? `${command.name} needs ${named}` : `${command.name} takes ${named}
|
|
593
|
+
throw new UsageError(subjects.length < least ? sentence(`${command.name} needs ${named}`) : `${sentence(`${command.name} takes ${named}`)} ${JSON.stringify(subjects[most])} is one too many.`);
|
|
535
594
|
}
|
|
536
595
|
for (const [name, spec] of Object.entries(options)) {
|
|
537
596
|
const value = values[name];
|
|
@@ -561,27 +620,40 @@ export function parseCall(script: ScriptSpec, command: CommandSpec, argv: readon
|
|
|
561
620
|
// ---------------------------------------------------------------------------
|
|
562
621
|
// Running
|
|
563
622
|
|
|
564
|
-
/**
|
|
565
|
-
function
|
|
566
|
-
const
|
|
567
|
-
|
|
623
|
+
/** A hint for a body with a cut list: the rest of the first list that has one. */
|
|
624
|
+
function cutListHint(body: unknown): string | undefined {
|
|
625
|
+
const lists = (value: unknown, depth: number): string | undefined => {
|
|
626
|
+
if (typeof value !== "object" || value === null || depth > 2) return undefined;
|
|
627
|
+
const rest = (value as { rest?: unknown; count?: unknown }).rest;
|
|
628
|
+
if (typeof rest === "string" && typeof (value as { count?: unknown }).count === "number") return rest;
|
|
629
|
+
for (const item of Object.values(value)) {
|
|
630
|
+
const found = lists(item, depth + 1);
|
|
631
|
+
if (found !== undefined) return found;
|
|
632
|
+
}
|
|
633
|
+
return undefined;
|
|
634
|
+
};
|
|
635
|
+
const rest = lists(body, 0);
|
|
636
|
+
return rest === undefined ? undefined : `A list in the body is cut: count gives its full size. To get the rest, give ${rest}.`;
|
|
568
637
|
}
|
|
569
638
|
|
|
570
|
-
/**
|
|
639
|
+
/** The text with a full stop at its end, so that another sentence can follow it. */
|
|
640
|
+
function sentence(text: string): string {
|
|
641
|
+
return /[.!?]$/.test(text) ? text : `${text}.`;
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
/** What the envelope needs of a script. */
|
|
571
645
|
interface Envelope {
|
|
572
646
|
name: string;
|
|
573
|
-
/**
|
|
574
|
-
usage: (command: string | undefined) => string;
|
|
575
|
-
/** Runs with the command line without --project. Calls found() with the command once it is known. Returns the value to print; { text } for help. */
|
|
647
|
+
/** Runs with the command line without --project. Calls found() with the command once it is known. Returns the body, or a Reply. */
|
|
576
648
|
perform: (argv: string[], found: (command: string) => void) => unknown;
|
|
577
649
|
}
|
|
578
650
|
|
|
579
651
|
/**
|
|
580
652
|
* The envelope of every script. It takes --project and runs the script. It
|
|
581
|
-
* prints
|
|
582
|
-
*
|
|
583
|
-
*
|
|
584
|
-
*
|
|
653
|
+
* prints one JSON envelope line. Every failure, also one outside the command
|
|
654
|
+
* (an exception or a rejection that nothing caught), prints an ok:false
|
|
655
|
+
* envelope with exit status 2. A refusal keeps its code; a wrong command line
|
|
656
|
+
* is invalid-input with the help command as nextAction; any other failure is
|
|
585
657
|
* operation-failed, and its stack goes to the trace only.
|
|
586
658
|
*/
|
|
587
659
|
async function envelope(script: Envelope): Promise<void> {
|
|
@@ -590,7 +662,7 @@ async function envelope(script: Envelope): Promise<void> {
|
|
|
590
662
|
let printed = false;
|
|
591
663
|
let commandName: string | undefined;
|
|
592
664
|
|
|
593
|
-
const print = (value:
|
|
665
|
+
const print = (value: Record<string, unknown>, status: number) => {
|
|
594
666
|
if (printed) return;
|
|
595
667
|
printed = true;
|
|
596
668
|
process.stdout.write(`${JSON.stringify(value)}\n`);
|
|
@@ -599,20 +671,28 @@ async function envelope(script: Envelope): Promise<void> {
|
|
|
599
671
|
|
|
600
672
|
const fail = (error: unknown) => {
|
|
601
673
|
let code: ErrorCode;
|
|
602
|
-
let
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
message = `${error.message}\n\n${script.usage(commandName)}`;
|
|
606
|
-
} else if (isRefusal(error)) {
|
|
674
|
+
let shown: { error: string; hint?: string };
|
|
675
|
+
let nextAction: string | undefined;
|
|
676
|
+
if (isRefusal(error)) {
|
|
607
677
|
code = error.code;
|
|
608
|
-
|
|
678
|
+
const stop = code === "installation-incomplete";
|
|
679
|
+
const message = stop && error.message.endsWith(STOP_AND_ASK) ? error.message.slice(0, -STOP_AND_ASK.length).trim() : error.message;
|
|
680
|
+
shown = splitMessage(message, (error as { hint?: string }).hint);
|
|
681
|
+
if (stop) shown = { error: shown.error, hint: shown.hint === undefined ? STOP_AND_ASK : `${sentence(shown.hint)} ${STOP_AND_ASK}` };
|
|
682
|
+
if (error instanceof UsageError) {
|
|
683
|
+
const help = `To get the usage, run ${script.name}.ts${commandName === undefined ? "" : ` ${commandName}`} --help.`;
|
|
684
|
+
shown = { error: shown.error, hint: shown.hint === undefined ? help : `${sentence(shown.hint)} ${help}` };
|
|
685
|
+
nextAction = commandLine(commandName === undefined ? [] : [commandName, "--help"]);
|
|
686
|
+
}
|
|
609
687
|
} else {
|
|
610
688
|
code = "operation-failed";
|
|
611
689
|
const where = commandName === undefined ? `${script.name}.ts` : `${script.name}.ts ${commandName}`;
|
|
612
|
-
|
|
690
|
+
const text = (error instanceof Error ? error.message : String(error)).replace(/\.$/, "");
|
|
691
|
+
shown = { error: `${where} failed inside c64-re-tools: ${text}.`, hint: "This is a fault of the toolkit, not of the call. Give this message to the user. Set C64RT_TRACE to get its details in the trace." };
|
|
613
692
|
}
|
|
693
|
+
const message = shown.hint === undefined ? shown.error : `${shown.error} ${shown.hint}`;
|
|
614
694
|
trace().event("script.end", { ms: elapsedMs(started), status: 2, error: { code, message }, ...(isRefusal(error) ? {} : { stack: error instanceof Error ? error.stack : String(error) }) } as TraceFields);
|
|
615
|
-
print({
|
|
695
|
+
print({ ok: false, ...(commandName === undefined ? {} : { command: commandName }), code, error: shown.error, ...(shown.hint === undefined ? {} : { hint: shown.hint }), ...(nextAction === undefined ? {} : { nextAction }) }, 2);
|
|
616
696
|
};
|
|
617
697
|
|
|
618
698
|
// A failure that nothing caught, for example in a callback of a child process: one error line, then the end.
|
|
@@ -630,9 +710,11 @@ async function envelope(script: Envelope): Promise<void> {
|
|
|
630
710
|
const result = await script.perform(argv, (command) => {
|
|
631
711
|
commandName = command;
|
|
632
712
|
});
|
|
633
|
-
const
|
|
634
|
-
|
|
635
|
-
|
|
713
|
+
const { body, hint, nextAction } = result instanceof Reply ? result : { body: result ?? {}, hint: undefined, nextAction: undefined };
|
|
714
|
+
const cut = cutListHint(body);
|
|
715
|
+
const shownHint = hint === undefined ? cut : cut === undefined || hint.includes("--max-listed") ? hint : `${sentence(hint)} ${cut}`;
|
|
716
|
+
print({ ok: true, command: commandName ?? "help", body: body ?? {}, ...(shownHint === undefined ? {} : { hint: shownHint }), ...(nextAction === undefined ? {} : { nextAction }) }, 0);
|
|
717
|
+
trace().event("script.end", { ms: elapsedMs(started), status: 0, result: body });
|
|
636
718
|
} catch (error) {
|
|
637
719
|
fail(error);
|
|
638
720
|
}
|
|
@@ -642,20 +724,27 @@ async function envelope(script: Envelope): Promise<void> {
|
|
|
642
724
|
export async function runScript(script: ScriptSpec): Promise<void> {
|
|
643
725
|
await envelope({
|
|
644
726
|
name: script.name,
|
|
645
|
-
usage: (name) => {
|
|
646
|
-
const command = script.commands.find((item) => item.name === name);
|
|
647
|
-
return command === undefined ? scriptUsage(script) : commandUsage(script, command);
|
|
648
|
-
},
|
|
649
727
|
perform: (argv, found) => {
|
|
650
728
|
const [first, ...rest] = argv;
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
729
|
+
const help = `To get the subjects and the options of one command, run ${script.name}.ts <command> --help.`;
|
|
730
|
+
if (first === undefined) {
|
|
731
|
+
found("help");
|
|
732
|
+
return reply(scriptHelp(script), { hint: help });
|
|
733
|
+
}
|
|
734
|
+
if (first.startsWith("-")) {
|
|
735
|
+
if (asksHelp(argv)) {
|
|
736
|
+
found("help");
|
|
737
|
+
return reply(scriptHelp(script), { hint: help });
|
|
738
|
+
}
|
|
739
|
+
throw new UsageError(`${first} comes before the command.`, { hint: `Give a command first: ${wordList(script.commands.map((command) => command.name))}.` });
|
|
654
740
|
}
|
|
655
741
|
const command = script.commands.find((item) => item.name === first);
|
|
656
|
-
if (command === undefined) throw new UsageError(`${first} is not a command of ${script.name}.ts
|
|
742
|
+
if (command === undefined) throw new UsageError(`${first} is not a command of ${script.name}.ts.`, { hint: `The commands are ${wordList(script.commands.map((item) => item.name))}.` });
|
|
657
743
|
found(command.name);
|
|
658
|
-
if (asksHelp(rest))
|
|
744
|
+
if (asksHelp(rest)) {
|
|
745
|
+
found("help");
|
|
746
|
+
return commandHelp(script, command);
|
|
747
|
+
}
|
|
659
748
|
return command.run(parseCall(script, command, rest));
|
|
660
749
|
},
|
|
661
750
|
});
|