-
-
Notifications
You must be signed in to change notification settings - Fork 277
Expand file tree
/
Copy pathgeneric.ts
More file actions
86 lines (83 loc) · 3 KB
/
Copy pathgeneric.ts
File metadata and controls
86 lines (83 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import type { CommandRequestResult } from '../../agent-device-client.ts';
import { runCliCommandWithOutput } from '../../commands/cli-runner.ts';
import type { CommandName } from '../../commands/command-metadata.ts';
import type { CliOutput } from '../../commands/command-contract.ts';
import type { ReplaySuiteResult } from '@agent-device/contracts/replay';
import type { CliFlags } from '@agent-device/contracts/command';
import { readCommandMessage } from '@agent-device/kernel/success-text';
import { isNonDefaultResponseLevel } from '@agent-device/kernel/contracts';
import { writeCommandOutput } from './shared.ts';
import type { ClientBackedCliCommandName } from './client-backed.ts';
import type { ClientCommandParams } from './router-types.ts';
export async function runGenericClientBackedCommand({
command,
positionals,
flags,
client,
debug,
replayTestReporterRuntime,
commandProgress,
}: ClientCommandParams & { command: ClientBackedCliCommandName }): Promise<boolean> {
const { result, cliOutput } = await runCliCommandWithOutput({
client,
command: command as CommandName,
positionals,
flags,
commandProgress,
});
// A non-default responseLevel returns a leveled payload (e.g. the snapshot
// digest { nodeCount, refs }) that the per-command CLI formatters assume away —
// they serialize the default shape and drop the digest fields. Emit the leveled
// payload verbatim instead.
if (isNonDefaultResponseLevel(flags.responseLevel)) {
await writeCommandOutput(flags, result, () => JSON.stringify(result, null, 2));
return true;
}
if (cliOutput) {
await writeCliOutput(flags, cliOutput);
} else {
const exitCode = await writeGenericCliOutput(command, flags, result, {
debug,
replayTestReporterRuntime,
});
if (exitCode !== 0) {
process.exit(exitCode);
}
}
return true;
}
async function writeGenericCliOutput(
command: ClientBackedCliCommandName,
flags: CliFlags,
data: CommandRequestResult,
options: Pick<ClientCommandParams, 'debug' | 'replayTestReporterRuntime'> = {},
): Promise<number> {
if (command === 'test') {
// Lazy: keeps the replay test reporting runtime off every other command's path.
return import('../replay-test/reporting.ts').then(({ renderReplayTestResponse }) =>
renderReplayTestResponse({
suite: data as ReplaySuiteResult,
debug: options.debug,
verbose: flags.verbose,
json: flags.json,
reporter: flags.reporter,
reportJunit: flags.reportJunit,
reporterRuntime: options.replayTestReporterRuntime,
}),
);
}
await writeCommandOutput(flags, data, () =>
readCommandMessage(data as Record<string, unknown> | undefined),
);
return 0;
}
async function writeCliOutput(flags: CliFlags, output: CliOutput): Promise<void> {
if (!flags.json && output.stderr) {
process.stderr.write(output.stderr);
}
await writeCommandOutput(
flags,
flags.json ? (output.jsonData ?? output.data) : output.data,
() => output.text,
);
}