@canonical/summon 0.37.0 → 0.38.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/dist/src/index.d.ts +15 -0
- package/dist/src/index.js +16 -0
- package/dist/src/index.js.map +1 -0
- package/package.json +17 -12
- package/src/bin.tsx +238 -0
- package/src/completion.test.ts +202 -0
- package/src/completion.ts +492 -0
- package/src/components/App.askMissing.test.tsx +164 -0
- package/src/components/App.confirmGate.test.tsx +111 -0
- package/src/components/App.escBack.test.tsx +98 -0
- package/src/components/App.invalidAnswer.test.tsx +71 -0
- package/src/components/App.tsx +1305 -0
- package/src/components/ExecutionProgress.tsx +222 -0
- package/src/components/FileTreePreview.tsx +97 -0
- package/src/components/PromptSequence.tsx +558 -0
- package/src/components/Spinner.tsx +36 -0
- package/src/components/index.ts +16 -0
- package/src/components/undoPlan.test.ts +99 -0
- package/src/components/undoPlan.ts +70 -0
- package/src/discovery/index.ts +1 -0
- package/src/discovery/loadGenerator.ts +40 -0
- package/src/display/index.ts +2 -0
- package/src/display/printGeneratorHelp.ts +130 -0
- package/src/display/printNode.ts +76 -0
- package/src/index.ts +16 -0
- package/src/registration/buildCommandBarrel.test.ts +80 -0
- package/src/registration/buildCommandBarrel.ts +66 -0
- package/src/registration/conformance.subprocess.test.ts +192 -0
- package/src/registration/index.ts +2 -0
- package/src/registration/interaction.subprocess.test.ts +458 -0
- package/src/registration/registerFromBarrel.tsx +501 -0
- package/src/registration/resolveMode.test.ts +118 -0
- package/src/registration/resolveMode.ts +52 -0
- package/src/testing/globalSetup.test.ts +165 -0
- package/src/testing/globalSetup.ts +501 -0
- package/src/ui/index.tsx +31 -0
|
@@ -0,0 +1,1305 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* App Component
|
|
3
|
+
*
|
|
4
|
+
* Main CLI application component using React Ink.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
formatContentPreview,
|
|
9
|
+
GENERATOR_INVALID_ANSWER,
|
|
10
|
+
type GeneratorDefinition,
|
|
11
|
+
isInvalidAnswersError,
|
|
12
|
+
type PromptDefinition,
|
|
13
|
+
type StampConfig,
|
|
14
|
+
} from "@canonical/summon-core";
|
|
15
|
+
import {
|
|
16
|
+
collectUndos,
|
|
17
|
+
dryRun,
|
|
18
|
+
type Effect,
|
|
19
|
+
type Task,
|
|
20
|
+
type TaskError,
|
|
21
|
+
} from "@canonical/task";
|
|
22
|
+
import { hostExistsResolver, runCollectedUndos } from "@canonical/task/node";
|
|
23
|
+
import { Box, Text, useApp, useInput } from "ink";
|
|
24
|
+
import { useCallback, useEffect, useState } from "react";
|
|
25
|
+
import { ExecutionProgress, type TimedEffect } from "./ExecutionProgress.js";
|
|
26
|
+
import { PromptSequence } from "./PromptSequence.js";
|
|
27
|
+
import { Spinner } from "./Spinner.js";
|
|
28
|
+
import {
|
|
29
|
+
describeUndoSteps,
|
|
30
|
+
isUnreversibleExec,
|
|
31
|
+
shouldSkipUndoGate,
|
|
32
|
+
} from "./undoPlan.js";
|
|
33
|
+
|
|
34
|
+
// =============================================================================
|
|
35
|
+
// Effect Tree - Hierarchical display with action labels and tree connectors
|
|
36
|
+
// =============================================================================
|
|
37
|
+
|
|
38
|
+
interface GroupedEffects {
|
|
39
|
+
files: Effect[];
|
|
40
|
+
directories: Effect[];
|
|
41
|
+
commands: Effect[];
|
|
42
|
+
logs: Effect[];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Group effects by category for display.
|
|
47
|
+
* @param effects - The effects to group
|
|
48
|
+
* @param verbose - If true, include debug logs
|
|
49
|
+
*/
|
|
50
|
+
const groupEffects = (effects: Effect[], verbose = false): GroupedEffects => {
|
|
51
|
+
const groups: GroupedEffects = {
|
|
52
|
+
files: [],
|
|
53
|
+
directories: [],
|
|
54
|
+
commands: [],
|
|
55
|
+
logs: [],
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
for (const effect of effects) {
|
|
59
|
+
switch (effect._tag) {
|
|
60
|
+
case "WriteFile":
|
|
61
|
+
case "AppendFile":
|
|
62
|
+
case "CopyFile":
|
|
63
|
+
case "CopyDirectory":
|
|
64
|
+
case "DeleteFile":
|
|
65
|
+
groups.files.push(effect);
|
|
66
|
+
break;
|
|
67
|
+
case "MakeDir":
|
|
68
|
+
case "DeleteDirectory":
|
|
69
|
+
groups.directories.push(effect);
|
|
70
|
+
break;
|
|
71
|
+
case "Exec":
|
|
72
|
+
groups.commands.push(effect);
|
|
73
|
+
break;
|
|
74
|
+
case "Log":
|
|
75
|
+
// Filter out debug logs unless verbose is enabled
|
|
76
|
+
if (effect.level !== "debug" || verbose) {
|
|
77
|
+
groups.logs.push(effect);
|
|
78
|
+
}
|
|
79
|
+
break;
|
|
80
|
+
// Ignore internal effects: ReadFile, Exists, Glob, ReadContext, WriteContext, Parallel, Race
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return groups;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Get human-readable action label for an effect.
|
|
89
|
+
*/
|
|
90
|
+
const getActionLabel = (effect: Effect): string => {
|
|
91
|
+
switch (effect._tag) {
|
|
92
|
+
case "WriteFile":
|
|
93
|
+
return "Created file";
|
|
94
|
+
case "AppendFile":
|
|
95
|
+
return "Appended to";
|
|
96
|
+
case "MakeDir":
|
|
97
|
+
return "Created dir";
|
|
98
|
+
case "CopyFile":
|
|
99
|
+
return "Copied file";
|
|
100
|
+
case "CopyDirectory":
|
|
101
|
+
return "Copied dir";
|
|
102
|
+
case "DeleteFile":
|
|
103
|
+
return "Deleted file";
|
|
104
|
+
case "DeleteDirectory":
|
|
105
|
+
return "Deleted dir";
|
|
106
|
+
case "Exec":
|
|
107
|
+
return "Executed";
|
|
108
|
+
case "Log":
|
|
109
|
+
switch (effect.level) {
|
|
110
|
+
case "debug":
|
|
111
|
+
return "Debug";
|
|
112
|
+
case "info":
|
|
113
|
+
return "Info";
|
|
114
|
+
case "warn":
|
|
115
|
+
return "Warning";
|
|
116
|
+
case "error":
|
|
117
|
+
return "Error";
|
|
118
|
+
default:
|
|
119
|
+
return "Log";
|
|
120
|
+
}
|
|
121
|
+
default:
|
|
122
|
+
return effect._tag;
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Get color for action label based on effect type.
|
|
128
|
+
*/
|
|
129
|
+
const getActionColor = (
|
|
130
|
+
effect: Effect,
|
|
131
|
+
): "green" | "red" | "yellow" | "cyan" | "blue" | "magenta" | undefined => {
|
|
132
|
+
switch (effect._tag) {
|
|
133
|
+
case "WriteFile":
|
|
134
|
+
case "MakeDir":
|
|
135
|
+
return "green";
|
|
136
|
+
case "AppendFile":
|
|
137
|
+
return "magenta";
|
|
138
|
+
case "DeleteFile":
|
|
139
|
+
case "DeleteDirectory":
|
|
140
|
+
return "red";
|
|
141
|
+
case "CopyFile":
|
|
142
|
+
case "CopyDirectory":
|
|
143
|
+
return "cyan";
|
|
144
|
+
case "Exec":
|
|
145
|
+
return "yellow";
|
|
146
|
+
case "Log":
|
|
147
|
+
switch (effect.level) {
|
|
148
|
+
case "error":
|
|
149
|
+
return "red";
|
|
150
|
+
case "warn":
|
|
151
|
+
return "yellow";
|
|
152
|
+
case "debug":
|
|
153
|
+
return undefined; // dim by default
|
|
154
|
+
default:
|
|
155
|
+
return "blue";
|
|
156
|
+
}
|
|
157
|
+
default:
|
|
158
|
+
return undefined;
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Get the payload (description) for an effect.
|
|
164
|
+
*/
|
|
165
|
+
const getEffectPayload = (effect: Effect): string => {
|
|
166
|
+
switch (effect._tag) {
|
|
167
|
+
case "WriteFile":
|
|
168
|
+
return effect.path;
|
|
169
|
+
case "AppendFile":
|
|
170
|
+
return effect.path;
|
|
171
|
+
case "MakeDir":
|
|
172
|
+
return effect.path;
|
|
173
|
+
case "CopyFile":
|
|
174
|
+
return `${effect.source} → ${effect.dest}`;
|
|
175
|
+
case "CopyDirectory":
|
|
176
|
+
return `${effect.source}/ → ${effect.dest}/`;
|
|
177
|
+
case "DeleteFile":
|
|
178
|
+
case "DeleteDirectory":
|
|
179
|
+
return effect.path;
|
|
180
|
+
case "Exec":
|
|
181
|
+
return `${effect.command} ${effect.args.join(" ")}`;
|
|
182
|
+
case "Log":
|
|
183
|
+
return effect.message;
|
|
184
|
+
default:
|
|
185
|
+
return effect._tag;
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
// Fixed width for action label column (padded to align payloads)
|
|
190
|
+
const ACTION_LABEL_WIDTH = 14;
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Render a single effect as a tree row with action label and payload.
|
|
194
|
+
*/
|
|
195
|
+
const EffectTreeRow = ({
|
|
196
|
+
effect,
|
|
197
|
+
isLast,
|
|
198
|
+
}: {
|
|
199
|
+
effect: Effect;
|
|
200
|
+
isLast: boolean;
|
|
201
|
+
}) => {
|
|
202
|
+
const connector = isLast ? "└─" : "├─";
|
|
203
|
+
const actionLabel = getActionLabel(effect);
|
|
204
|
+
const color = getActionColor(effect);
|
|
205
|
+
const payload = getEffectPayload(effect);
|
|
206
|
+
|
|
207
|
+
return (
|
|
208
|
+
<Box>
|
|
209
|
+
<Text dimColor>{connector} </Text>
|
|
210
|
+
<Text color={color}>{actionLabel.padEnd(ACTION_LABEL_WIDTH)}</Text>
|
|
211
|
+
<Text>{payload}</Text>
|
|
212
|
+
</Box>
|
|
213
|
+
);
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Render a section of the effect tree (e.g., Files, Directories).
|
|
218
|
+
*/
|
|
219
|
+
const EffectTreeSection = ({
|
|
220
|
+
title,
|
|
221
|
+
effects,
|
|
222
|
+
}: {
|
|
223
|
+
title: string;
|
|
224
|
+
effects: Effect[];
|
|
225
|
+
}) => {
|
|
226
|
+
if (effects.length === 0) {
|
|
227
|
+
return null;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
return (
|
|
231
|
+
<Box flexDirection="column" marginTop={1}>
|
|
232
|
+
<Text bold dimColor>
|
|
233
|
+
{title}:
|
|
234
|
+
</Text>
|
|
235
|
+
<Box flexDirection="column" marginLeft={1}>
|
|
236
|
+
{effects.map((effect, index) => (
|
|
237
|
+
<EffectTreeRow
|
|
238
|
+
key={`${effect._tag}:${getEffectPayload(effect)}`}
|
|
239
|
+
effect={effect}
|
|
240
|
+
isLast={index === effects.length - 1}
|
|
241
|
+
/>
|
|
242
|
+
))}
|
|
243
|
+
</Box>
|
|
244
|
+
</Box>
|
|
245
|
+
);
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Render a tree view of completed effects, grouped by category.
|
|
250
|
+
*/
|
|
251
|
+
const _EffectTree = ({ effects }: { effects: Effect[] }) => {
|
|
252
|
+
const groups = groupEffects(effects);
|
|
253
|
+
const hasAnyEffects =
|
|
254
|
+
groups.files.length > 0 ||
|
|
255
|
+
groups.directories.length > 0 ||
|
|
256
|
+
groups.commands.length > 0 ||
|
|
257
|
+
groups.logs.length > 0;
|
|
258
|
+
|
|
259
|
+
if (!hasAnyEffects) {
|
|
260
|
+
return null;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
return (
|
|
264
|
+
<Box flexDirection="column">
|
|
265
|
+
<EffectTreeSection title="Files" effects={groups.files} />
|
|
266
|
+
<EffectTreeSection title="Directories" effects={groups.directories} />
|
|
267
|
+
<EffectTreeSection title="Commands" effects={groups.commands} />
|
|
268
|
+
<EffectTreeSection title="Logs" effects={groups.logs} />
|
|
269
|
+
</Box>
|
|
270
|
+
);
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
// =============================================================================
|
|
274
|
+
// Effect Timeline - Chronological display with timestamps
|
|
275
|
+
// =============================================================================
|
|
276
|
+
|
|
277
|
+
// Width for timestamp column (e.g., "+123ms")
|
|
278
|
+
const TIMESTAMP_WIDTH = 8;
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Filter effects to only show user-relevant ones (not internal effects).
|
|
282
|
+
* @param effect - The effect to check
|
|
283
|
+
* @param verbose - If true, include debug logs
|
|
284
|
+
*/
|
|
285
|
+
const isVisibleEffect = (effect: Effect, verbose = false): boolean => {
|
|
286
|
+
switch (effect._tag) {
|
|
287
|
+
case "WriteFile":
|
|
288
|
+
case "AppendFile":
|
|
289
|
+
case "MakeDir":
|
|
290
|
+
case "CopyFile":
|
|
291
|
+
case "CopyDirectory":
|
|
292
|
+
case "DeleteFile":
|
|
293
|
+
case "DeleteDirectory":
|
|
294
|
+
case "Exec":
|
|
295
|
+
return true;
|
|
296
|
+
case "Log":
|
|
297
|
+
// Filter out debug logs unless verbose is enabled
|
|
298
|
+
if (effect.level === "debug") {
|
|
299
|
+
return verbose;
|
|
300
|
+
}
|
|
301
|
+
return true;
|
|
302
|
+
// Internal effects are not shown
|
|
303
|
+
case "ReadFile":
|
|
304
|
+
case "Exists":
|
|
305
|
+
case "Glob":
|
|
306
|
+
case "ReadContext":
|
|
307
|
+
case "WriteContext":
|
|
308
|
+
case "Prompt":
|
|
309
|
+
case "Parallel":
|
|
310
|
+
case "Race":
|
|
311
|
+
return false;
|
|
312
|
+
default:
|
|
313
|
+
return false;
|
|
314
|
+
}
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Render a single timeline row with timestamp, action, and payload.
|
|
319
|
+
*/
|
|
320
|
+
const TimelineRow = ({
|
|
321
|
+
effect,
|
|
322
|
+
timestamp,
|
|
323
|
+
showTimestamp,
|
|
324
|
+
isLast,
|
|
325
|
+
}: {
|
|
326
|
+
effect: Effect;
|
|
327
|
+
timestamp: number;
|
|
328
|
+
showTimestamp: boolean;
|
|
329
|
+
isLast: boolean;
|
|
330
|
+
}) => {
|
|
331
|
+
const connector = isLast ? "└─" : "├─";
|
|
332
|
+
const actionLabel = getActionLabel(effect);
|
|
333
|
+
const color = getActionColor(effect);
|
|
334
|
+
const payload = getEffectPayload(effect);
|
|
335
|
+
const timestampStr = showTimestamp
|
|
336
|
+
? `+${Math.round(timestamp)}ms`.padEnd(TIMESTAMP_WIDTH)
|
|
337
|
+
: " ".repeat(TIMESTAMP_WIDTH);
|
|
338
|
+
|
|
339
|
+
return (
|
|
340
|
+
<Box>
|
|
341
|
+
<Text dimColor>{timestampStr}</Text>
|
|
342
|
+
<Text dimColor>{connector} </Text>
|
|
343
|
+
<Text color={color}>{actionLabel.padEnd(ACTION_LABEL_WIDTH)}</Text>
|
|
344
|
+
<Text>{payload}</Text>
|
|
345
|
+
</Box>
|
|
346
|
+
);
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* Render effects in chronological order with timestamps.
|
|
351
|
+
* Timestamps are only shown when they differ from the previous effect.
|
|
352
|
+
* Duplicate MakeDir effects (same path) are deduplicated.
|
|
353
|
+
*/
|
|
354
|
+
const EffectTimeline = ({
|
|
355
|
+
effects,
|
|
356
|
+
verbose = false,
|
|
357
|
+
}: {
|
|
358
|
+
effects: TimedEffect[];
|
|
359
|
+
verbose?: boolean;
|
|
360
|
+
}) => {
|
|
361
|
+
// Filter to visible effects only and deduplicate MakeDir by path
|
|
362
|
+
const seenDirPaths = new Set<string>();
|
|
363
|
+
const visibleEffects = effects.filter((e) => {
|
|
364
|
+
if (!isVisibleEffect(e.effect, verbose)) return false;
|
|
365
|
+
// Deduplicate MakeDir by path (keep only first occurrence)
|
|
366
|
+
if (e.effect._tag === "MakeDir") {
|
|
367
|
+
if (seenDirPaths.has(e.effect.path)) return false;
|
|
368
|
+
seenDirPaths.add(e.effect.path);
|
|
369
|
+
}
|
|
370
|
+
return true;
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
if (visibleEffects.length === 0) {
|
|
374
|
+
return null;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
// Track which timestamps to show (only when different from previous)
|
|
378
|
+
let lastShownTimestamp = -1;
|
|
379
|
+
|
|
380
|
+
return (
|
|
381
|
+
<Box flexDirection="column" marginTop={1}>
|
|
382
|
+
<Text bold dimColor>
|
|
383
|
+
Timeline:
|
|
384
|
+
</Text>
|
|
385
|
+
<Box flexDirection="column" marginLeft={1}>
|
|
386
|
+
{visibleEffects.map((item, index) => {
|
|
387
|
+
const roundedTimestamp = Math.round(item.timestamp);
|
|
388
|
+
const showTimestamp = roundedTimestamp !== lastShownTimestamp;
|
|
389
|
+
if (showTimestamp) {
|
|
390
|
+
lastShownTimestamp = roundedTimestamp;
|
|
391
|
+
}
|
|
392
|
+
// Effects are append-only, index is stable
|
|
393
|
+
const key = `${item.timestamp}-${item.effect._tag}-${index}`;
|
|
394
|
+
return (
|
|
395
|
+
<TimelineRow
|
|
396
|
+
key={key}
|
|
397
|
+
effect={item.effect}
|
|
398
|
+
timestamp={item.timestamp}
|
|
399
|
+
showTimestamp={showTimestamp}
|
|
400
|
+
isLast={index === visibleEffects.length - 1}
|
|
401
|
+
/>
|
|
402
|
+
);
|
|
403
|
+
})}
|
|
404
|
+
</Box>
|
|
405
|
+
</Box>
|
|
406
|
+
);
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
// =============================================================================
|
|
410
|
+
// Dry-Run Timeline - Preview without timestamps
|
|
411
|
+
// =============================================================================
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* Render a single dry-run row (no timestamp column).
|
|
415
|
+
*/
|
|
416
|
+
const DryRunRow = ({ effect, isLast }: { effect: Effect; isLast: boolean }) => {
|
|
417
|
+
const connector = isLast ? "└─" : "├─";
|
|
418
|
+
const actionLabel = getActionLabel(effect);
|
|
419
|
+
const color = getActionColor(effect);
|
|
420
|
+
const payload = getEffectPayload(effect);
|
|
421
|
+
|
|
422
|
+
return (
|
|
423
|
+
<Box>
|
|
424
|
+
<Text dimColor>{connector} </Text>
|
|
425
|
+
<Text color={color}>{actionLabel.padEnd(ACTION_LABEL_WIDTH)}</Text>
|
|
426
|
+
<Text>{payload}</Text>
|
|
427
|
+
</Box>
|
|
428
|
+
);
|
|
429
|
+
};
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* Render effects as a preview timeline (dry-run mode).
|
|
433
|
+
* Shows the same format as execution timeline but without timestamps.
|
|
434
|
+
*/
|
|
435
|
+
const DryRunTimeline = ({
|
|
436
|
+
effects,
|
|
437
|
+
title = "Plan:",
|
|
438
|
+
verbose = false,
|
|
439
|
+
}: {
|
|
440
|
+
effects: Effect[];
|
|
441
|
+
title?: string;
|
|
442
|
+
verbose?: boolean;
|
|
443
|
+
}) => {
|
|
444
|
+
// Filter to visible effects only and deduplicate MakeDir by path
|
|
445
|
+
const seenDirPaths = new Set<string>();
|
|
446
|
+
const visibleEffects = effects.filter((e) => {
|
|
447
|
+
if (!isVisibleEffect(e, verbose)) return false;
|
|
448
|
+
// Deduplicate MakeDir by path (keep only first occurrence)
|
|
449
|
+
if (e._tag === "MakeDir") {
|
|
450
|
+
if (seenDirPaths.has(e.path)) return false;
|
|
451
|
+
seenDirPaths.add(e.path);
|
|
452
|
+
}
|
|
453
|
+
return true;
|
|
454
|
+
});
|
|
455
|
+
|
|
456
|
+
if (visibleEffects.length === 0) {
|
|
457
|
+
return (
|
|
458
|
+
<Box>
|
|
459
|
+
<Text dimColor>No operations planned.</Text>
|
|
460
|
+
</Box>
|
|
461
|
+
);
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
return (
|
|
465
|
+
<Box flexDirection="column">
|
|
466
|
+
<Text bold dimColor>
|
|
467
|
+
{title}
|
|
468
|
+
</Text>
|
|
469
|
+
<Box flexDirection="column" marginLeft={1}>
|
|
470
|
+
{visibleEffects.map((effect, index) => {
|
|
471
|
+
const key = `${effect._tag}-${index}`;
|
|
472
|
+
return (
|
|
473
|
+
<DryRunRow
|
|
474
|
+
key={key}
|
|
475
|
+
effect={effect}
|
|
476
|
+
isLast={index === visibleEffects.length - 1}
|
|
477
|
+
/>
|
|
478
|
+
);
|
|
479
|
+
})}
|
|
480
|
+
</Box>
|
|
481
|
+
</Box>
|
|
482
|
+
);
|
|
483
|
+
};
|
|
484
|
+
|
|
485
|
+
// =============================================================================
|
|
486
|
+
// Completed Answers Display (for confirmation phase)
|
|
487
|
+
// =============================================================================
|
|
488
|
+
|
|
489
|
+
/**
|
|
490
|
+
* Format answer value for display based on prompt type.
|
|
491
|
+
*/
|
|
492
|
+
const formatAnswerValue = (
|
|
493
|
+
value: unknown,
|
|
494
|
+
prompt: PromptDefinition,
|
|
495
|
+
): string => {
|
|
496
|
+
if (value === undefined || value === null) return "";
|
|
497
|
+
|
|
498
|
+
if (prompt.type === "confirm") {
|
|
499
|
+
return value ? "Yes" : "No";
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
if (prompt.type === "select" && prompt.choices) {
|
|
503
|
+
const choice = prompt.choices.find((c) => c.value === value);
|
|
504
|
+
return choice?.label ?? String(value);
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
if (prompt.type === "multiselect" && Array.isArray(value)) {
|
|
508
|
+
if (value.length === 0) return "None";
|
|
509
|
+
if (prompt.choices) {
|
|
510
|
+
return value
|
|
511
|
+
.map((v) => prompt.choices?.find((c) => c.value === v)?.label ?? v)
|
|
512
|
+
.join(", ");
|
|
513
|
+
}
|
|
514
|
+
return value.join(", ");
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
return String(value);
|
|
518
|
+
};
|
|
519
|
+
|
|
520
|
+
/**
|
|
521
|
+
* Display all completed answers in a borderless table format for confirmation review.
|
|
522
|
+
*/
|
|
523
|
+
const CompletedAnswersTable = ({
|
|
524
|
+
prompts,
|
|
525
|
+
answers,
|
|
526
|
+
}: {
|
|
527
|
+
prompts: PromptDefinition[];
|
|
528
|
+
answers: Record<string, unknown>;
|
|
529
|
+
}) => {
|
|
530
|
+
// Filter to only show prompts that have answers and pass their `when` condition
|
|
531
|
+
const activePrompts = prompts.filter((prompt) => {
|
|
532
|
+
if (prompt.when && !prompt.when(answers)) {
|
|
533
|
+
return false;
|
|
534
|
+
}
|
|
535
|
+
return prompt.name in answers;
|
|
536
|
+
});
|
|
537
|
+
|
|
538
|
+
if (activePrompts.length === 0) {
|
|
539
|
+
return null;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
// Calculate max width for the question column (for alignment)
|
|
543
|
+
const maxQuestionWidth = Math.max(
|
|
544
|
+
...activePrompts.map((p) => p.message.length),
|
|
545
|
+
);
|
|
546
|
+
|
|
547
|
+
return (
|
|
548
|
+
<Box flexDirection="column" marginBottom={1}>
|
|
549
|
+
{activePrompts.map((prompt) => {
|
|
550
|
+
const value = answers[prompt.name];
|
|
551
|
+
const displayValue = formatAnswerValue(value, prompt);
|
|
552
|
+
|
|
553
|
+
return (
|
|
554
|
+
<Box key={prompt.name}>
|
|
555
|
+
<Text color="green">✔ </Text>
|
|
556
|
+
<Text dimColor>{prompt.message.padEnd(maxQuestionWidth)}</Text>
|
|
557
|
+
<Text dimColor> </Text>
|
|
558
|
+
<Text color="cyan">{displayValue}</Text>
|
|
559
|
+
</Box>
|
|
560
|
+
);
|
|
561
|
+
})}
|
|
562
|
+
</Box>
|
|
563
|
+
);
|
|
564
|
+
};
|
|
565
|
+
|
|
566
|
+
/**
|
|
567
|
+
* Display a summary of planned effects in a borderless table format.
|
|
568
|
+
*/
|
|
569
|
+
const EffectsSummaryTable = ({ effects }: { effects: Effect[] }) => {
|
|
570
|
+
const files = new Set<string>();
|
|
571
|
+
const directories = new Set<string>();
|
|
572
|
+
const copied = new Set<string>();
|
|
573
|
+
const deleted = new Set<string>();
|
|
574
|
+
let commands = 0;
|
|
575
|
+
|
|
576
|
+
for (const effect of effects) {
|
|
577
|
+
switch (effect._tag) {
|
|
578
|
+
case "WriteFile":
|
|
579
|
+
case "AppendFile":
|
|
580
|
+
files.add(effect.path);
|
|
581
|
+
break;
|
|
582
|
+
case "MakeDir":
|
|
583
|
+
directories.add(effect.path);
|
|
584
|
+
break;
|
|
585
|
+
case "CopyFile":
|
|
586
|
+
copied.add(effect.dest);
|
|
587
|
+
break;
|
|
588
|
+
case "CopyDirectory":
|
|
589
|
+
copied.add(effect.dest);
|
|
590
|
+
break;
|
|
591
|
+
case "DeleteFile":
|
|
592
|
+
case "DeleteDirectory":
|
|
593
|
+
deleted.add(effect.path);
|
|
594
|
+
break;
|
|
595
|
+
case "Exec":
|
|
596
|
+
commands++;
|
|
597
|
+
break;
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
// Build rows for non-zero counts
|
|
602
|
+
const rows: Array<{ label: string; count: number; color: string }> = [];
|
|
603
|
+
|
|
604
|
+
if (files.size > 0) {
|
|
605
|
+
rows.push({
|
|
606
|
+
label: `File${files.size > 1 ? "s" : ""} to create`,
|
|
607
|
+
count: files.size,
|
|
608
|
+
color: "green",
|
|
609
|
+
});
|
|
610
|
+
}
|
|
611
|
+
if (directories.size > 0) {
|
|
612
|
+
rows.push({
|
|
613
|
+
label: `Director${directories.size > 1 ? "ies" : "y"} to create`,
|
|
614
|
+
count: directories.size,
|
|
615
|
+
color: "green",
|
|
616
|
+
});
|
|
617
|
+
}
|
|
618
|
+
if (copied.size > 0) {
|
|
619
|
+
rows.push({
|
|
620
|
+
label: `Item${copied.size > 1 ? "s" : ""} to copy`,
|
|
621
|
+
count: copied.size,
|
|
622
|
+
color: "cyan",
|
|
623
|
+
});
|
|
624
|
+
}
|
|
625
|
+
if (deleted.size > 0) {
|
|
626
|
+
rows.push({
|
|
627
|
+
label: `Item${deleted.size > 1 ? "s" : ""} to delete`,
|
|
628
|
+
count: deleted.size,
|
|
629
|
+
color: "red",
|
|
630
|
+
});
|
|
631
|
+
}
|
|
632
|
+
if (commands > 0) {
|
|
633
|
+
rows.push({
|
|
634
|
+
label: `Command${commands > 1 ? "s" : ""} to run`,
|
|
635
|
+
count: commands,
|
|
636
|
+
color: "yellow",
|
|
637
|
+
});
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
if (rows.length === 0) {
|
|
641
|
+
return (
|
|
642
|
+
<Box marginBottom={1}>
|
|
643
|
+
<Text dimColor>No operations planned.</Text>
|
|
644
|
+
</Box>
|
|
645
|
+
);
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
// Calculate max label width for alignment
|
|
649
|
+
const maxLabelWidth = Math.max(...rows.map((r) => r.label.length));
|
|
650
|
+
|
|
651
|
+
return (
|
|
652
|
+
<Box flexDirection="column" marginBottom={1}>
|
|
653
|
+
<Text bold dimColor>
|
|
654
|
+
Operations:
|
|
655
|
+
</Text>
|
|
656
|
+
{rows.map((row) => (
|
|
657
|
+
<Box key={row.label}>
|
|
658
|
+
<Text dimColor> {row.label.padEnd(maxLabelWidth)} </Text>
|
|
659
|
+
<Text color={row.color as "green" | "cyan" | "red" | "yellow"}>
|
|
660
|
+
{row.count}
|
|
661
|
+
</Text>
|
|
662
|
+
</Box>
|
|
663
|
+
))}
|
|
664
|
+
</Box>
|
|
665
|
+
);
|
|
666
|
+
};
|
|
667
|
+
|
|
668
|
+
/**
|
|
669
|
+
* Summarize effects into a human-readable string.
|
|
670
|
+
* Deduplicates paths to avoid counting the same directory multiple times.
|
|
671
|
+
*/
|
|
672
|
+
const summarizeEffects = (effects: TimedEffect[]): string => {
|
|
673
|
+
const files = new Set<string>();
|
|
674
|
+
const directories = new Set<string>();
|
|
675
|
+
const copied = new Set<string>();
|
|
676
|
+
const deleted = new Set<string>();
|
|
677
|
+
let commands = 0;
|
|
678
|
+
|
|
679
|
+
for (const { effect } of effects) {
|
|
680
|
+
switch (effect._tag) {
|
|
681
|
+
case "WriteFile":
|
|
682
|
+
files.add(effect.path);
|
|
683
|
+
break;
|
|
684
|
+
case "MakeDir":
|
|
685
|
+
directories.add(effect.path);
|
|
686
|
+
break;
|
|
687
|
+
case "CopyFile":
|
|
688
|
+
copied.add(effect.dest);
|
|
689
|
+
break;
|
|
690
|
+
case "DeleteFile":
|
|
691
|
+
case "DeleteDirectory":
|
|
692
|
+
deleted.add(effect.path);
|
|
693
|
+
break;
|
|
694
|
+
case "Exec":
|
|
695
|
+
commands++;
|
|
696
|
+
break;
|
|
697
|
+
// Log effects are not counted in summary
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
const parts: string[] = [];
|
|
702
|
+
|
|
703
|
+
if (files.size > 0) {
|
|
704
|
+
parts.push(`${files.size} file${files.size > 1 ? "s" : ""}`);
|
|
705
|
+
}
|
|
706
|
+
if (directories.size > 0) {
|
|
707
|
+
parts.push(
|
|
708
|
+
`${directories.size} director${directories.size > 1 ? "ies" : "y"}`,
|
|
709
|
+
);
|
|
710
|
+
}
|
|
711
|
+
if (copied.size > 0) {
|
|
712
|
+
parts.push(`${copied.size} copied`);
|
|
713
|
+
}
|
|
714
|
+
if (deleted.size > 0) {
|
|
715
|
+
parts.push(`${deleted.size} deleted`);
|
|
716
|
+
}
|
|
717
|
+
if (commands > 0) {
|
|
718
|
+
parts.push(`${commands} command${commands > 1 ? "s" : ""}`);
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
if (parts.length === 0) {
|
|
722
|
+
return "No changes made";
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
return `Created ${parts.join(", ")}`;
|
|
726
|
+
};
|
|
727
|
+
|
|
728
|
+
// =============================================================================
|
|
729
|
+
// File Content View - Full file content display (for Ctrl+O toggle)
|
|
730
|
+
// =============================================================================
|
|
731
|
+
|
|
732
|
+
/**
|
|
733
|
+
* Display the full content of all WriteFile/AppendFile effects.
|
|
734
|
+
* Used when the user toggles file content view with Ctrl+O.
|
|
735
|
+
*/
|
|
736
|
+
const FileContentView = ({ effects }: { effects: Effect[] }) => {
|
|
737
|
+
const writeEffects = effects.filter(
|
|
738
|
+
(e): e is Effect & { _tag: "WriteFile" | "AppendFile" } =>
|
|
739
|
+
e._tag === "WriteFile" || e._tag === "AppendFile",
|
|
740
|
+
);
|
|
741
|
+
|
|
742
|
+
if (writeEffects.length === 0) {
|
|
743
|
+
return (
|
|
744
|
+
<Box marginBottom={1}>
|
|
745
|
+
<Text dimColor>No file contents to display.</Text>
|
|
746
|
+
</Box>
|
|
747
|
+
);
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
return (
|
|
751
|
+
<Box flexDirection="column" marginBottom={1}>
|
|
752
|
+
<Text bold dimColor>
|
|
753
|
+
File contents:
|
|
754
|
+
</Text>
|
|
755
|
+
{writeEffects.map((effect, i) => (
|
|
756
|
+
// biome-ignore lint/suspicious/noArrayIndexKey: path may be duplicated across effects
|
|
757
|
+
<Box key={`${effect.path}-${i}`} flexDirection="column" marginTop={1}>
|
|
758
|
+
<Text color="green" bold>
|
|
759
|
+
{effect.path}
|
|
760
|
+
</Text>
|
|
761
|
+
<Box marginLeft={1}>
|
|
762
|
+
<Text>{formatContentPreview(effect.content)}</Text>
|
|
763
|
+
</Box>
|
|
764
|
+
</Box>
|
|
765
|
+
))}
|
|
766
|
+
</Box>
|
|
767
|
+
);
|
|
768
|
+
};
|
|
769
|
+
|
|
770
|
+
// =============================================================================
|
|
771
|
+
// App State and Component
|
|
772
|
+
// =============================================================================
|
|
773
|
+
|
|
774
|
+
export type AppState =
|
|
775
|
+
| { phase: "loading" }
|
|
776
|
+
| { phase: "prompting" }
|
|
777
|
+
| { phase: "preview"; effects: Effect[] }
|
|
778
|
+
| {
|
|
779
|
+
phase: "confirming";
|
|
780
|
+
effects: Effect[];
|
|
781
|
+
promptAnswers: Record<string, unknown>;
|
|
782
|
+
}
|
|
783
|
+
| { phase: "undoPreview"; planEffects: Effect[] }
|
|
784
|
+
| {
|
|
785
|
+
phase: "confirmingUndo";
|
|
786
|
+
undos: Task<void>[];
|
|
787
|
+
planEffects: Effect[];
|
|
788
|
+
unreversible: Effect[];
|
|
789
|
+
}
|
|
790
|
+
| { phase: "undone"; undoCount: number; unreversible: Effect[] }
|
|
791
|
+
| { phase: "executing"; task: Task<void> }
|
|
792
|
+
| { phase: "complete"; effects: TimedEffect[]; duration: number }
|
|
793
|
+
| { phase: "error"; error: TaskError; answers?: Record<string, unknown> };
|
|
794
|
+
|
|
795
|
+
export interface AppProps {
|
|
796
|
+
/** The generator to run */
|
|
797
|
+
generator: GeneratorDefinition;
|
|
798
|
+
/** Whether to show a preview before executing */
|
|
799
|
+
preview?: boolean;
|
|
800
|
+
/** Whether to run in dry-run mode only */
|
|
801
|
+
dryRunOnly?: boolean;
|
|
802
|
+
/** Whether to reverse a previously executed generator */
|
|
803
|
+
undo?: boolean;
|
|
804
|
+
/** Whether to show debug output */
|
|
805
|
+
verbose?: boolean;
|
|
806
|
+
/** Pre-filled answers (for non-interactive mode) */
|
|
807
|
+
answers?: Record<string, unknown>;
|
|
808
|
+
/**
|
|
809
|
+
* Wizard mode over a PARTIAL answer set: `answers` are the explicitly
|
|
810
|
+
* provided ones — never re-asked, shown as completed — and the wizard asks
|
|
811
|
+
* exactly the pending prompts (empty set ⇒ straight to preview/confirm).
|
|
812
|
+
* Without this flag, provided `answers` skip prompting entirely (a run).
|
|
813
|
+
*/
|
|
814
|
+
askMissing?: boolean;
|
|
815
|
+
/** Skip confirmation gates (`--yes`) */
|
|
816
|
+
yes?: boolean;
|
|
817
|
+
/** Stamp configuration for generated files (undefined = no stamps) */
|
|
818
|
+
stamp?: StampConfig;
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
export const App = ({
|
|
822
|
+
generator,
|
|
823
|
+
preview = true,
|
|
824
|
+
dryRunOnly = false,
|
|
825
|
+
undo = false,
|
|
826
|
+
verbose = false,
|
|
827
|
+
answers: prefilledAnswers,
|
|
828
|
+
askMissing = false,
|
|
829
|
+
yes = false,
|
|
830
|
+
stamp,
|
|
831
|
+
}: AppProps) => {
|
|
832
|
+
const { exit } = useApp();
|
|
833
|
+
const [state, setState] = useState<AppState>(
|
|
834
|
+
prefilledAnswers && !askMissing
|
|
835
|
+
? { phase: "loading" }
|
|
836
|
+
: { phase: "prompting" },
|
|
837
|
+
);
|
|
838
|
+
const [answers, setAnswers] = useState<Record<string, unknown>>(
|
|
839
|
+
prefilledAnswers ?? {},
|
|
840
|
+
);
|
|
841
|
+
const [showFiles, setShowFiles] = useState(false);
|
|
842
|
+
// Set once the user navigates BACK from the confirm gate: the re-entered
|
|
843
|
+
// wizard asks EVERY prompt again (previous values pre-filled) instead of
|
|
844
|
+
// seeding `provided` — with a fully-explicit invocation the pending set is
|
|
845
|
+
// empty, so a kept seed would auto-complete straight back to the gate and
|
|
846
|
+
// make the advertised `esc to go back` a no-op.
|
|
847
|
+
const [reasking, setReasking] = useState(false);
|
|
848
|
+
|
|
849
|
+
// Generate the task, entering the error phase on ANY throw — §3's exit
|
|
850
|
+
// contract: a rendered failure never exits 0. A generator-raised typed
|
|
851
|
+
// invalid answer (a cross-answer constraint its `generate` enforces — two
|
|
852
|
+
// answers only valid together; no shipped generator raises one today) is
|
|
853
|
+
// the usage class
|
|
854
|
+
// (GENERATOR_INVALID_ANSWER → the effect's exit 2); any OTHER throw is a
|
|
855
|
+
// generator bug rendered as GENERATE_ERROR — the run/wizard sibling of the
|
|
856
|
+
// batch arms' bare stderr line — carrying the runtime class (exit 1).
|
|
857
|
+
// Re-throwing it (the old behavior) reached Ink's error boundary, which
|
|
858
|
+
// renders a crash box but sets NO exit code: `summon … --yes` under bun
|
|
859
|
+
// exited 0 on a failed run.
|
|
860
|
+
//
|
|
861
|
+
// The SUCCESS path is validated too: `undefined` is this helper's
|
|
862
|
+
// "already handled, stop" sentinel, so a `generate()` that RETURNS
|
|
863
|
+
// undefined/null (a plain-JS generator that forgot its `return` — the
|
|
864
|
+
// `--generators` extension point is untyped) must not be forwarded as
|
|
865
|
+
// that sentinel — no state would be set, no exit code owned, and the
|
|
866
|
+
// App would sit in phase limbo with `waitUntilExit` pending FOREVER.
|
|
867
|
+
// It is the same generator-bug class as a throw: GENERATE_ERROR, named.
|
|
868
|
+
const generateTask = useCallback(
|
|
869
|
+
(promptAnswers: Record<string, unknown>): Task<void> | undefined => {
|
|
870
|
+
try {
|
|
871
|
+
// The wider type is honest: `--generators` loads unchecked JS, so
|
|
872
|
+
// the declared `Task<void>` is a promise the author may break.
|
|
873
|
+
const task: Task<void> | undefined | null =
|
|
874
|
+
generator.generate(promptAnswers);
|
|
875
|
+
if (task === undefined || task === null) {
|
|
876
|
+
setState({
|
|
877
|
+
phase: "error",
|
|
878
|
+
error: {
|
|
879
|
+
code: "GENERATE_ERROR",
|
|
880
|
+
message: `${generator.meta.name}'s generate returned no task`,
|
|
881
|
+
},
|
|
882
|
+
answers: promptAnswers,
|
|
883
|
+
});
|
|
884
|
+
return undefined;
|
|
885
|
+
}
|
|
886
|
+
return task;
|
|
887
|
+
} catch (error) {
|
|
888
|
+
setState({
|
|
889
|
+
phase: "error",
|
|
890
|
+
error: isInvalidAnswersError(error)
|
|
891
|
+
? { code: GENERATOR_INVALID_ANSWER, message: error.message }
|
|
892
|
+
: {
|
|
893
|
+
code: "GENERATE_ERROR",
|
|
894
|
+
message: error instanceof Error ? error.message : String(error),
|
|
895
|
+
},
|
|
896
|
+
answers: promptAnswers,
|
|
897
|
+
});
|
|
898
|
+
return undefined;
|
|
899
|
+
}
|
|
900
|
+
},
|
|
901
|
+
[generator],
|
|
902
|
+
);
|
|
903
|
+
|
|
904
|
+
const runUndoPlan = useCallback(
|
|
905
|
+
(
|
|
906
|
+
undos: Task<void>[],
|
|
907
|
+
unreversible: Effect[],
|
|
908
|
+
promptAnswers: Record<string, unknown>,
|
|
909
|
+
) => {
|
|
910
|
+
(async () => {
|
|
911
|
+
try {
|
|
912
|
+
const result = await runCollectedUndos(undos);
|
|
913
|
+
// Failure no longer aborts the pending undos — every step is
|
|
914
|
+
// attempted and reported in `outcomes` — so the verdict is read
|
|
915
|
+
// off the outcomes here: any failed step fails the undo, naming
|
|
916
|
+
// each cause, instead of a green "complete" over partial work.
|
|
917
|
+
const failed = result.outcomes.filter((o) => o.status === "failed");
|
|
918
|
+
if (failed.length > 0) {
|
|
919
|
+
setState({
|
|
920
|
+
phase: "error",
|
|
921
|
+
error: {
|
|
922
|
+
code: "UNDO_ERROR",
|
|
923
|
+
message: `${failed.length} of ${result.outcomes.length} undo step${result.outcomes.length === 1 ? "" : "s"} did not complete: ${failed
|
|
924
|
+
.map((o) => o.error.message)
|
|
925
|
+
.join("; ")}`,
|
|
926
|
+
},
|
|
927
|
+
answers: promptAnswers,
|
|
928
|
+
});
|
|
929
|
+
return;
|
|
930
|
+
}
|
|
931
|
+
setState({
|
|
932
|
+
phase: "undone",
|
|
933
|
+
undoCount: result.undoCount,
|
|
934
|
+
unreversible,
|
|
935
|
+
});
|
|
936
|
+
} catch (err) {
|
|
937
|
+
setState({
|
|
938
|
+
phase: "error",
|
|
939
|
+
error:
|
|
940
|
+
err instanceof Error
|
|
941
|
+
? { code: "UNDO_ERROR", message: err.message }
|
|
942
|
+
: { code: "UNKNOWN_ERROR", message: String(err) },
|
|
943
|
+
answers: promptAnswers,
|
|
944
|
+
});
|
|
945
|
+
}
|
|
946
|
+
})();
|
|
947
|
+
},
|
|
948
|
+
[],
|
|
949
|
+
);
|
|
950
|
+
|
|
951
|
+
const handlePromptsComplete = useCallback(
|
|
952
|
+
(promptAnswers: Record<string, unknown>) => {
|
|
953
|
+
setAnswers(promptAnswers);
|
|
954
|
+
|
|
955
|
+
const task = generateTask(promptAnswers);
|
|
956
|
+
if (task === undefined) return;
|
|
957
|
+
|
|
958
|
+
// Undo mode: collect the plan ONCE (host-backed Exists resolution, so
|
|
959
|
+
// branch selection matches the run being undone), show it, and execute
|
|
960
|
+
// exactly the collected undos — never re-walk the task in between.
|
|
961
|
+
if (undo) {
|
|
962
|
+
try {
|
|
963
|
+
const unreversible: Effect[] = [];
|
|
964
|
+
const undos = collectUndos(task, {
|
|
965
|
+
resolveExists: hostExistsResolver(),
|
|
966
|
+
onForwardEffect: (effect) => {
|
|
967
|
+
if (isUnreversibleExec(effect)) unreversible.push(effect);
|
|
968
|
+
},
|
|
969
|
+
});
|
|
970
|
+
if (undos.length === 0) {
|
|
971
|
+
setState({
|
|
972
|
+
phase: "error",
|
|
973
|
+
error: { code: "NOTHING_TO_UNDO", message: "Nothing to undo." },
|
|
974
|
+
});
|
|
975
|
+
return;
|
|
976
|
+
}
|
|
977
|
+
const planEffects = describeUndoSteps(undos);
|
|
978
|
+
if (dryRunOnly) {
|
|
979
|
+
setState({ phase: "undoPreview", planEffects });
|
|
980
|
+
return;
|
|
981
|
+
}
|
|
982
|
+
// Same gate contract as the forward run: flags only pre-fill
|
|
983
|
+
// answers; `--yes` skips the gate, and so does `--no-preview` —
|
|
984
|
+
// the forward flow goes straight to executing in both cases.
|
|
985
|
+
if (shouldSkipUndoGate({ yes, preview })) {
|
|
986
|
+
runUndoPlan(undos, unreversible, promptAnswers);
|
|
987
|
+
return;
|
|
988
|
+
}
|
|
989
|
+
setState({
|
|
990
|
+
phase: "confirmingUndo",
|
|
991
|
+
undos,
|
|
992
|
+
planEffects,
|
|
993
|
+
unreversible,
|
|
994
|
+
});
|
|
995
|
+
} catch (err) {
|
|
996
|
+
setState({
|
|
997
|
+
phase: "error",
|
|
998
|
+
error:
|
|
999
|
+
err instanceof Error
|
|
1000
|
+
? { code: "UNDO_ERROR", message: err.message }
|
|
1001
|
+
: { code: "UNKNOWN_ERROR", message: String(err) },
|
|
1002
|
+
answers: promptAnswers,
|
|
1003
|
+
});
|
|
1004
|
+
}
|
|
1005
|
+
return;
|
|
1006
|
+
}
|
|
1007
|
+
|
|
1008
|
+
if (dryRunOnly || preview) {
|
|
1009
|
+
// Run dry-run to collect effects
|
|
1010
|
+
try {
|
|
1011
|
+
const result = dryRun(task);
|
|
1012
|
+
if (dryRunOnly) {
|
|
1013
|
+
setState({ phase: "preview", effects: result.effects });
|
|
1014
|
+
} else {
|
|
1015
|
+
setState({
|
|
1016
|
+
phase: "confirming",
|
|
1017
|
+
effects: result.effects,
|
|
1018
|
+
promptAnswers,
|
|
1019
|
+
});
|
|
1020
|
+
}
|
|
1021
|
+
} catch (err) {
|
|
1022
|
+
setState({
|
|
1023
|
+
phase: "error",
|
|
1024
|
+
error:
|
|
1025
|
+
err instanceof Error
|
|
1026
|
+
? { code: "DRY_RUN_ERROR", message: err.message }
|
|
1027
|
+
: { code: "UNKNOWN_ERROR", message: String(err) },
|
|
1028
|
+
answers: promptAnswers,
|
|
1029
|
+
});
|
|
1030
|
+
}
|
|
1031
|
+
} else {
|
|
1032
|
+
setState({ phase: "executing", task });
|
|
1033
|
+
}
|
|
1034
|
+
},
|
|
1035
|
+
[generateTask, preview, dryRunOnly, undo, yes, runUndoPlan],
|
|
1036
|
+
);
|
|
1037
|
+
|
|
1038
|
+
const handleConfirm = useCallback(() => {
|
|
1039
|
+
// The same catch as the preview's generate: a re-generate at the confirm
|
|
1040
|
+
// gate normally re-runs what already succeeded, but a stateful generator
|
|
1041
|
+
// throwing HERE must land in the error phase too, not in Ink's boundary.
|
|
1042
|
+
const task = generateTask(answers);
|
|
1043
|
+
if (task === undefined) return;
|
|
1044
|
+
setState({ phase: "executing", task });
|
|
1045
|
+
}, [generateTask, answers]);
|
|
1046
|
+
|
|
1047
|
+
const handleCancel = useCallback(() => {
|
|
1048
|
+
exit();
|
|
1049
|
+
}, [exit]);
|
|
1050
|
+
|
|
1051
|
+
const handleExecutionComplete = useCallback(
|
|
1052
|
+
(effects: TimedEffect[], duration: number) => {
|
|
1053
|
+
setState({ phase: "complete", effects, duration });
|
|
1054
|
+
},
|
|
1055
|
+
[],
|
|
1056
|
+
);
|
|
1057
|
+
|
|
1058
|
+
const handleExecutionError = useCallback(
|
|
1059
|
+
(error: TaskError) => {
|
|
1060
|
+
setState({ phase: "error", error, answers });
|
|
1061
|
+
},
|
|
1062
|
+
[answers],
|
|
1063
|
+
);
|
|
1064
|
+
|
|
1065
|
+
// Handle pre-filled answers (run mode only — askMissing starts prompting)
|
|
1066
|
+
useEffect(() => {
|
|
1067
|
+
if (prefilledAnswers && !askMissing && state.phase === "loading") {
|
|
1068
|
+
handlePromptsComplete(prefilledAnswers);
|
|
1069
|
+
}
|
|
1070
|
+
}, [prefilledAnswers, askMissing, state.phase, handlePromptsComplete]);
|
|
1071
|
+
|
|
1072
|
+
// The error phase owns the process exit code (the cross-CLI matrix): a
|
|
1073
|
+
// rendered failure must not exit 0 — pragma routes the same failures
|
|
1074
|
+
// through mapExitCode (usage → 2, everything else → 1). The typed invalid
|
|
1075
|
+
// answer (a generator's cross-answer guard) is the usage class; every
|
|
1076
|
+
// other rendered failure — execution, dry-run, undo — is a runtime
|
|
1077
|
+
// failure. Only the exit code is owned here: the rendering above it stays
|
|
1078
|
+
// host UI, and a deliberate cancel (n at the confirm gate) keeps exit 0.
|
|
1079
|
+
useEffect(() => {
|
|
1080
|
+
if (state.phase !== "error") return;
|
|
1081
|
+
process.exitCode = state.error.code === GENERATOR_INVALID_ANSWER ? 2 : 1;
|
|
1082
|
+
}, [state]);
|
|
1083
|
+
|
|
1084
|
+
// Handle going back from confirmation to prompting. Clearing the provided
|
|
1085
|
+
// seed (via `reasking`) is what keeps esc meaningful when the wizard had
|
|
1086
|
+
// nothing left to ask — the flag-given answers are exactly what the user
|
|
1087
|
+
// may want to change.
|
|
1088
|
+
const handleGoBack = useCallback(() => {
|
|
1089
|
+
setShowFiles(false);
|
|
1090
|
+
setReasking(true);
|
|
1091
|
+
setState({ phase: "prompting" });
|
|
1092
|
+
}, []);
|
|
1093
|
+
|
|
1094
|
+
const handleUndoConfirm = useCallback(() => {
|
|
1095
|
+
if (state.phase !== "confirmingUndo") return;
|
|
1096
|
+
runUndoPlan(state.undos, state.unreversible, answers);
|
|
1097
|
+
}, [state, runUndoPlan, answers]);
|
|
1098
|
+
|
|
1099
|
+
// Handle confirm/cancel/back/show-files input when in a confirming state
|
|
1100
|
+
useInput(
|
|
1101
|
+
(input, key) => {
|
|
1102
|
+
if (state.phase === "confirming") {
|
|
1103
|
+
// Ctrl+O toggles file content view
|
|
1104
|
+
if (key.ctrl && input === "o") {
|
|
1105
|
+
setShowFiles((prev) => !prev);
|
|
1106
|
+
} else if (key.escape) {
|
|
1107
|
+
handleGoBack();
|
|
1108
|
+
} else if (key.return || input.toLowerCase() === "y") {
|
|
1109
|
+
// Enter or Y confirms
|
|
1110
|
+
handleConfirm();
|
|
1111
|
+
} else if (input.toLowerCase() === "n") {
|
|
1112
|
+
handleCancel();
|
|
1113
|
+
}
|
|
1114
|
+
} else if (state.phase === "confirmingUndo") {
|
|
1115
|
+
if (key.return || input.toLowerCase() === "y") {
|
|
1116
|
+
handleUndoConfirm();
|
|
1117
|
+
} else if (key.escape || input.toLowerCase() === "n") {
|
|
1118
|
+
handleCancel();
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
},
|
|
1122
|
+
{
|
|
1123
|
+
isActive:
|
|
1124
|
+
state.phase === "confirming" || state.phase === "confirmingUndo",
|
|
1125
|
+
},
|
|
1126
|
+
);
|
|
1127
|
+
|
|
1128
|
+
return (
|
|
1129
|
+
<Box flexDirection="column" padding={1}>
|
|
1130
|
+
{/* Header */}
|
|
1131
|
+
<Box marginBottom={1}>
|
|
1132
|
+
<Text bold color="magenta">
|
|
1133
|
+
{generator.meta.name}
|
|
1134
|
+
</Text>
|
|
1135
|
+
<Text dimColor> v{generator.meta.version}</Text>
|
|
1136
|
+
</Box>
|
|
1137
|
+
<Text dimColor>{generator.meta.description}</Text>
|
|
1138
|
+
<Box marginBottom={1} />
|
|
1139
|
+
|
|
1140
|
+
{/* Content based on state */}
|
|
1141
|
+
{state.phase === "loading" && <Spinner label="Loading..." />}
|
|
1142
|
+
|
|
1143
|
+
{state.phase === "prompting" && (
|
|
1144
|
+
<PromptSequence
|
|
1145
|
+
prompts={generator.prompts}
|
|
1146
|
+
onComplete={handlePromptsComplete}
|
|
1147
|
+
onCancel={handleCancel}
|
|
1148
|
+
initialAnswers={answers}
|
|
1149
|
+
provided={askMissing && !reasking ? prefilledAnswers : undefined}
|
|
1150
|
+
/>
|
|
1151
|
+
)}
|
|
1152
|
+
|
|
1153
|
+
{state.phase === "preview" && (
|
|
1154
|
+
<Box flexDirection="column">
|
|
1155
|
+
<DryRunTimeline
|
|
1156
|
+
effects={state.effects}
|
|
1157
|
+
title="Plan (dry-run):"
|
|
1158
|
+
verbose={verbose}
|
|
1159
|
+
/>
|
|
1160
|
+
<Box marginTop={1}>
|
|
1161
|
+
<Text dimColor>Dry-run complete. No files were modified.</Text>
|
|
1162
|
+
</Box>
|
|
1163
|
+
</Box>
|
|
1164
|
+
)}
|
|
1165
|
+
|
|
1166
|
+
{state.phase === "undoPreview" && (
|
|
1167
|
+
<Box flexDirection="column">
|
|
1168
|
+
<DryRunTimeline
|
|
1169
|
+
effects={state.planEffects}
|
|
1170
|
+
title="Undo plan (dry-run):"
|
|
1171
|
+
verbose={verbose}
|
|
1172
|
+
/>
|
|
1173
|
+
<Box marginTop={1}>
|
|
1174
|
+
<Text dimColor>Dry-run complete. No files were modified.</Text>
|
|
1175
|
+
</Box>
|
|
1176
|
+
</Box>
|
|
1177
|
+
)}
|
|
1178
|
+
|
|
1179
|
+
{state.phase === "confirmingUndo" && (
|
|
1180
|
+
<Box flexDirection="column">
|
|
1181
|
+
<DryRunTimeline
|
|
1182
|
+
effects={state.planEffects}
|
|
1183
|
+
title={`Undo will reverse ${state.undos.length} step${state.undos.length === 1 ? "" : "s"}:`}
|
|
1184
|
+
verbose={verbose}
|
|
1185
|
+
/>
|
|
1186
|
+
{state.unreversible.length > 0 && (
|
|
1187
|
+
<Box marginTop={1}>
|
|
1188
|
+
<Text color="yellow">
|
|
1189
|
+
{state.unreversible.length} exec step
|
|
1190
|
+
{state.unreversible.length === 1 ? "" : "s"} (e.g. installs)
|
|
1191
|
+
cannot be reversed; artifacts may remain.
|
|
1192
|
+
</Text>
|
|
1193
|
+
</Box>
|
|
1194
|
+
)}
|
|
1195
|
+
<Box marginTop={1}>
|
|
1196
|
+
<Text color="magenta">› </Text>
|
|
1197
|
+
<Text bold>Reverse these steps? </Text>
|
|
1198
|
+
<Text dimColor>(y/N) </Text>
|
|
1199
|
+
<Text dimColor italic>
|
|
1200
|
+
esc to cancel
|
|
1201
|
+
</Text>
|
|
1202
|
+
</Box>
|
|
1203
|
+
</Box>
|
|
1204
|
+
)}
|
|
1205
|
+
|
|
1206
|
+
{state.phase === "undone" && (
|
|
1207
|
+
<Box flexDirection="column">
|
|
1208
|
+
<Box>
|
|
1209
|
+
<Text color="green">
|
|
1210
|
+
✓ Undo complete ({state.undoCount} step
|
|
1211
|
+
{state.undoCount === 1 ? "" : "s"} reversed).
|
|
1212
|
+
</Text>
|
|
1213
|
+
</Box>
|
|
1214
|
+
{state.unreversible.length > 0 && (
|
|
1215
|
+
<Box marginTop={1}>
|
|
1216
|
+
<Text dimColor>
|
|
1217
|
+
Note: {state.unreversible.length} exec step
|
|
1218
|
+
{state.unreversible.length === 1 ? "" : "s"} were not reversed;
|
|
1219
|
+
their artifacts may remain.
|
|
1220
|
+
</Text>
|
|
1221
|
+
</Box>
|
|
1222
|
+
)}
|
|
1223
|
+
</Box>
|
|
1224
|
+
)}
|
|
1225
|
+
|
|
1226
|
+
{state.phase === "confirming" && (
|
|
1227
|
+
<Box flexDirection="column">
|
|
1228
|
+
{/* Show completed answers in table format */}
|
|
1229
|
+
<CompletedAnswersTable
|
|
1230
|
+
prompts={generator.prompts}
|
|
1231
|
+
answers={state.promptAnswers}
|
|
1232
|
+
/>
|
|
1233
|
+
{/* Show effects summary or file contents based on toggle */}
|
|
1234
|
+
{showFiles ? (
|
|
1235
|
+
<FileContentView effects={state.effects} />
|
|
1236
|
+
) : (
|
|
1237
|
+
<EffectsSummaryTable effects={state.effects} />
|
|
1238
|
+
)}
|
|
1239
|
+
{/* Confirmation prompt with escape hint and Ctrl+O hint */}
|
|
1240
|
+
<Box>
|
|
1241
|
+
<Text color="magenta">› </Text>
|
|
1242
|
+
<Text bold>Proceed? </Text>
|
|
1243
|
+
<Text dimColor>(Y/n) </Text>
|
|
1244
|
+
<Text dimColor italic>
|
|
1245
|
+
esc to go back · ctrl+o {showFiles ? "hide" : "show"} file
|
|
1246
|
+
contents
|
|
1247
|
+
</Text>
|
|
1248
|
+
</Box>
|
|
1249
|
+
</Box>
|
|
1250
|
+
)}
|
|
1251
|
+
|
|
1252
|
+
{state.phase === "executing" && (
|
|
1253
|
+
<ExecutionProgress
|
|
1254
|
+
task={state.task}
|
|
1255
|
+
onComplete={handleExecutionComplete}
|
|
1256
|
+
onError={handleExecutionError}
|
|
1257
|
+
stamp={stamp}
|
|
1258
|
+
/>
|
|
1259
|
+
)}
|
|
1260
|
+
|
|
1261
|
+
{state.phase === "complete" && (
|
|
1262
|
+
<Box flexDirection="column">
|
|
1263
|
+
<Box>
|
|
1264
|
+
<Text color="green">✓ Generation complete!</Text>
|
|
1265
|
+
</Box>
|
|
1266
|
+
<EffectTimeline effects={state.effects} verbose={verbose} />
|
|
1267
|
+
<Box marginTop={1}>
|
|
1268
|
+
<Text dimColor>
|
|
1269
|
+
{summarizeEffects(state.effects)} in {state.duration.toFixed(0)}ms
|
|
1270
|
+
</Text>
|
|
1271
|
+
</Box>
|
|
1272
|
+
</Box>
|
|
1273
|
+
)}
|
|
1274
|
+
|
|
1275
|
+
{state.phase === "error" && (
|
|
1276
|
+
<Box flexDirection="column">
|
|
1277
|
+
<Box>
|
|
1278
|
+
<Text color="red">✗ Error: {state.error.message}</Text>
|
|
1279
|
+
</Box>
|
|
1280
|
+
{state.error.code && (
|
|
1281
|
+
<Box marginTop={1}>
|
|
1282
|
+
<Text dimColor>Code: {state.error.code}</Text>
|
|
1283
|
+
</Box>
|
|
1284
|
+
)}
|
|
1285
|
+
{state.answers && Object.keys(state.answers).length > 0 && (
|
|
1286
|
+
<Box flexDirection="column" marginTop={1}>
|
|
1287
|
+
<Text dimColor bold>
|
|
1288
|
+
Arguments:
|
|
1289
|
+
</Text>
|
|
1290
|
+
<Box flexDirection="column" marginLeft={1}>
|
|
1291
|
+
{Object.entries(state.answers).map(([key, value]) => (
|
|
1292
|
+
<Box key={key}>
|
|
1293
|
+
<Text dimColor>
|
|
1294
|
+
{key}: {JSON.stringify(value)}
|
|
1295
|
+
</Text>
|
|
1296
|
+
</Box>
|
|
1297
|
+
))}
|
|
1298
|
+
</Box>
|
|
1299
|
+
</Box>
|
|
1300
|
+
)}
|
|
1301
|
+
</Box>
|
|
1302
|
+
)}
|
|
1303
|
+
</Box>
|
|
1304
|
+
);
|
|
1305
|
+
};
|