@wringer/gunshi 0.0.0-stage → 0.0.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 +55 -2
- package/dist/build-failure-envelope.d.ts +2 -0
- package/dist/build-gunshi-args.d.ts +4 -0
- package/dist/build-gunshi-commands.d.ts +22 -0
- package/dist/build-positional-args.d.ts +4 -0
- package/dist/build-raw-args.d.ts +17 -0
- package/dist/collect-boolean-flags-with-value.d.ts +3 -0
- package/dist/collect-flags-without-value.d.ts +3 -0
- package/dist/find-signed-value-flag.d.ts +6 -0
- package/dist/index.d.ts +3 -0
- package/dist/keep-flag-text.d.ts +1 -0
- package/dist/parse-arg-properties.d.ts +22 -0
- package/dist/pick-cli-outcome.d.ts +23 -0
- package/dist/pick-command-name.d.ts +1 -0
- package/dist/pick-dispatch-plan.d.ts +13 -0
- package/dist/pick-output-mode.d.ts +8 -0
- package/dist/read-repo-root.d.ts +2 -0
- package/dist/render-envelope.d.ts +3 -0
- package/dist/render-human-envelope.d.ts +2 -0
- package/dist/render-human-event.d.ts +1 -0
- package/dist/run-cli.d.ts +19 -0
- package/dist/run-verify.d.ts +2 -0
- package/package.json +46 -4
- package/src/build-failure-envelope.ts +19 -0
- package/src/build-gunshi-args.ts +66 -0
- package/src/build-gunshi-commands.ts +217 -0
- package/src/build-positional-args.ts +83 -0
- package/src/build-raw-args.ts +132 -0
- package/src/collect-boolean-flags-with-value.ts +22 -0
- package/src/collect-flags-without-value.ts +26 -0
- package/src/find-signed-value-flag.ts +35 -0
- package/src/index.ts +3 -0
- package/src/keep-flag-text.ts +6 -0
- package/src/parse-arg-properties.ts +117 -0
- package/src/pick-cli-outcome.ts +32 -0
- package/src/pick-command-name.ts +9 -0
- package/src/pick-dispatch-plan.ts +24 -0
- package/src/pick-output-mode.ts +31 -0
- package/src/read-repo-root.ts +35 -0
- package/src/render-envelope.ts +7 -0
- package/src/render-human-envelope.ts +29 -0
- package/src/render-human-event.ts +8 -0
- package/src/run-cli.ts +186 -0
- package/src/run-verify.ts +35 -0
package/src/run-cli.ts
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { WringerError, dispatch, supervisorCommandName } from '@wringer/core';
|
|
2
|
+
import type { RepoHost, StatePort, SupervisorPort, Wringer } from '@wringer/core';
|
|
3
|
+
import { cli, parseArgs } from 'gunshi';
|
|
4
|
+
import type { ArgToken } from 'gunshi';
|
|
5
|
+
import { buildGunshiCommands } from './build-gunshi-commands.ts';
|
|
6
|
+
import type { Invocation, InvokeResult } from './build-gunshi-commands.ts';
|
|
7
|
+
import { findSignedValueFlag } from './find-signed-value-flag.ts';
|
|
8
|
+
import type { SignedValueFlag } from './find-signed-value-flag.ts';
|
|
9
|
+
import { pickCLIOutcome } from './pick-cli-outcome.ts';
|
|
10
|
+
import type { ParsedInvocation } from './pick-cli-outcome.ts';
|
|
11
|
+
import { pickCommandName } from './pick-command-name.ts';
|
|
12
|
+
import { pickDispatchPlan } from './pick-dispatch-plan.ts';
|
|
13
|
+
import { pickOutputMode } from './pick-output-mode.ts';
|
|
14
|
+
import type { OutputMode } from './pick-output-mode.ts';
|
|
15
|
+
import { renderEnvelope } from './render-envelope.ts';
|
|
16
|
+
import { renderHumanEvent } from './render-human-event.ts';
|
|
17
|
+
|
|
18
|
+
interface RepoPorts {
|
|
19
|
+
readonly state: StatePort;
|
|
20
|
+
readonly repoHost: RepoHost;
|
|
21
|
+
readonly supervisor: SupervisorPort;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface CLIPorts {
|
|
25
|
+
readonly argv: readonly string[];
|
|
26
|
+
readonly cwd: string;
|
|
27
|
+
readonly isTTY: boolean;
|
|
28
|
+
readonly isAgent: boolean;
|
|
29
|
+
readonly writeStdout: (text: string) => void;
|
|
30
|
+
readonly readRepoRoot: (cwd: string) => Promise<string | WringerError>;
|
|
31
|
+
readonly openRepo: (root: string) => RepoPorts;
|
|
32
|
+
readonly runSupervisor: (wringer: Wringer, argv: readonly string[]) => Promise<void>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Every invocation ends in one envelope and an exit code, whatever failed: gunshi's parse, the
|
|
36
|
+
// repo root, or the command. The hidden supervisor command is the exception: it writes no envelope.
|
|
37
|
+
export async function runCLI(wringer: Wringer, ports: CLIPorts): Promise<number> {
|
|
38
|
+
const [first, ...rest] = ports.argv;
|
|
39
|
+
|
|
40
|
+
if (first === supervisorCommandName) {
|
|
41
|
+
await ports.runSupervisor(wringer, rest);
|
|
42
|
+
|
|
43
|
+
return 0;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const tokens = parseArgs([...ports.argv]);
|
|
47
|
+
const mode = pickOutputMode({ tokens, isTTY: ports.isTTY, isAgent: ports.isAgent });
|
|
48
|
+
|
|
49
|
+
const parsed = await readInvocation(wringer, ports, tokens);
|
|
50
|
+
|
|
51
|
+
const outcome = pickCLIOutcome(parsed, mode);
|
|
52
|
+
|
|
53
|
+
if (outcome.kind === 'dispatch') {
|
|
54
|
+
return dispatchInvocation(wringer, ports, mode, outcome.invocation);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
ports.writeStdout(outcome.text);
|
|
58
|
+
|
|
59
|
+
return outcome.exitCode;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// gunshi only records the invocation; the command runs after `cli` returns, so a gunshi parse error
|
|
63
|
+
// and a command error never mix. gunshi resolves the command path before it checks the flags, so a
|
|
64
|
+
// flag error still names the command.
|
|
65
|
+
async function readInvocation(
|
|
66
|
+
wringer: Wringer,
|
|
67
|
+
ports: CLIPorts,
|
|
68
|
+
tokens: readonly ArgToken[],
|
|
69
|
+
): Promise<ParsedInvocation> {
|
|
70
|
+
let invoked: InvokeResult | null = null;
|
|
71
|
+
let commandPath: readonly string[] = [];
|
|
72
|
+
|
|
73
|
+
const subCommands = buildGunshiCommands(wringer, (result) => {
|
|
74
|
+
invoked = result;
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
try {
|
|
78
|
+
const printed = await cli(
|
|
79
|
+
[...ports.argv],
|
|
80
|
+
{
|
|
81
|
+
name: wringer.name,
|
|
82
|
+
run: () => {
|
|
83
|
+
throw new WringerError('usage', {
|
|
84
|
+
message: 'Name a command.',
|
|
85
|
+
hint: `Run \`${wringer.name} --help\` to list them.`,
|
|
86
|
+
});
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
name: wringer.name,
|
|
91
|
+
subCommands,
|
|
92
|
+
strict: true,
|
|
93
|
+
usageSilent: true,
|
|
94
|
+
renderHeader: null,
|
|
95
|
+
renderValidationErrors: null,
|
|
96
|
+
onBeforeCommand: (ctx) => {
|
|
97
|
+
commandPath = ctx.commandPath;
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
return pickParsedInvocation(invoked, printed, pickCommandName(commandPath, ports.argv));
|
|
103
|
+
} catch (error) {
|
|
104
|
+
return {
|
|
105
|
+
kind: 'failed',
|
|
106
|
+
command: pickCommandName(commandPath, ports.argv),
|
|
107
|
+
error: toUsageError(error, findSignedValueFlag(tokens, ports.argv)),
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// gunshi ran no command when it printed help or a version.
|
|
113
|
+
function pickParsedInvocation(
|
|
114
|
+
invoked: InvokeResult | null,
|
|
115
|
+
printed: string | undefined,
|
|
116
|
+
command: string,
|
|
117
|
+
): ParsedInvocation {
|
|
118
|
+
if (invoked === null) {
|
|
119
|
+
return { kind: 'printed', text: `${printed ?? ''}\n` };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (invoked.kind === 'refused') {
|
|
123
|
+
return { kind: 'failed', command, error: invoked.error };
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return { kind: 'invoked', invocation: invoked.invocation };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// gunshi throws an AggregateError for a parse failure, with one error per problem.
|
|
130
|
+
function toUsageError(error: unknown, signed: SignedValueFlag | undefined): WringerError {
|
|
131
|
+
if (error instanceof WringerError) {
|
|
132
|
+
return error;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const problems = error instanceof AggregateError ? error.errors.map(String) : [String(error)];
|
|
136
|
+
|
|
137
|
+
const hint =
|
|
138
|
+
signed === undefined
|
|
139
|
+
? 'Add `--help` to the command to see its arguments.'
|
|
140
|
+
: `Join a negative value to its flag with \`=\`: \`${signed.flag}=${signed.value}\`.`;
|
|
141
|
+
|
|
142
|
+
return new WringerError('usage', {
|
|
143
|
+
message: problems.map((problem) => problem.replace(/^\w*Error: /u, '')).join(' '),
|
|
144
|
+
hint,
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
async function dispatchInvocation(
|
|
149
|
+
wringer: Wringer,
|
|
150
|
+
ports: CLIPorts,
|
|
151
|
+
mode: OutputMode,
|
|
152
|
+
invocation: Invocation,
|
|
153
|
+
): Promise<number> {
|
|
154
|
+
const root = await ports.readRepoRoot(ports.cwd);
|
|
155
|
+
|
|
156
|
+
const plan = pickDispatchPlan(invocation, root);
|
|
157
|
+
|
|
158
|
+
if (plan.kind === 'refuse') {
|
|
159
|
+
ports.writeStdout(renderEnvelope(plan.envelope, mode));
|
|
160
|
+
|
|
161
|
+
return plan.exitCode;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const repo = ports.openRepo(plan.root);
|
|
165
|
+
|
|
166
|
+
const result = await dispatch(wringer, invocation.path, invocation.raw, {
|
|
167
|
+
root: plan.root,
|
|
168
|
+
state: repo.state,
|
|
169
|
+
repoHost: repo.repoHost,
|
|
170
|
+
supervisor: repo.supervisor,
|
|
171
|
+
target: invocation.globals.target,
|
|
172
|
+
feature: invocation.globals.feature,
|
|
173
|
+
timeoutMs: invocation.globals.timeoutMs,
|
|
174
|
+
output: {
|
|
175
|
+
writeLine: (line) => {
|
|
176
|
+
const text = mode === 'json' ? `${line}\n` : renderHumanEvent(line);
|
|
177
|
+
|
|
178
|
+
ports.writeStdout(text);
|
|
179
|
+
},
|
|
180
|
+
},
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
ports.writeStdout(renderEnvelope(result.envelope, mode));
|
|
184
|
+
|
|
185
|
+
return result.exitCode;
|
|
186
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createNodeRepoHost,
|
|
3
|
+
createRepoState,
|
|
4
|
+
makeProcessSupervisorPort,
|
|
5
|
+
runSupervisorProcess,
|
|
6
|
+
} from '@wringer/core';
|
|
7
|
+
import type { Wringer } from '@wringer/core';
|
|
8
|
+
import { getAgentProfile } from 'gunshi/agent';
|
|
9
|
+
import { readRepoRoot } from './read-repo-root.ts';
|
|
10
|
+
import { runCLI } from './run-cli.ts';
|
|
11
|
+
|
|
12
|
+
// The repo's verify script calls this with its registry. The script restarts itself as the run's
|
|
13
|
+
// supervisor, so the supervisor's command is the runtime and the script, without the user's
|
|
14
|
+
// arguments.
|
|
15
|
+
export async function runVerify(wringer: Wringer): Promise<void> {
|
|
16
|
+
const [, script] = process.argv;
|
|
17
|
+
const selfCommand = script === undefined ? [process.execPath] : [process.execPath, script];
|
|
18
|
+
|
|
19
|
+
process.exitCode = await runCLI(wringer, {
|
|
20
|
+
argv: process.argv.slice(2),
|
|
21
|
+
cwd: process.cwd(),
|
|
22
|
+
isTTY: process.stdout.isTTY,
|
|
23
|
+
isAgent: getAgentProfile().isAgent,
|
|
24
|
+
writeStdout: (text) => {
|
|
25
|
+
process.stdout.write(text);
|
|
26
|
+
},
|
|
27
|
+
readRepoRoot,
|
|
28
|
+
openRepo: (root) => ({
|
|
29
|
+
state: createRepoState(root, wringer.stateDir),
|
|
30
|
+
repoHost: createNodeRepoHost(),
|
|
31
|
+
supervisor: makeProcessSupervisorPort({ selfCommand, root, stateDir: wringer.stateDir }),
|
|
32
|
+
}),
|
|
33
|
+
runSupervisor: runSupervisorProcess,
|
|
34
|
+
});
|
|
35
|
+
}
|