@canonical/summon 0.36.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/components/App.js +24 -2
- package/dist/src/components/App.js.map +1 -1
- package/dist/src/index.d.ts +15 -0
- package/dist/src/index.js +16 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/registration/registerFromBarrel.js +13 -0
- package/dist/src/registration/registerFromBarrel.js.map +1 -1
- package/package.json +22 -14
- 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,558 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PromptSequence Component
|
|
3
|
+
*
|
|
4
|
+
* Handles interactive prompts for generators using React Ink.
|
|
5
|
+
* Features:
|
|
6
|
+
* - Progress indicator (step X of Y)
|
|
7
|
+
* - Navigate back to previous answers
|
|
8
|
+
* - Show completed answers
|
|
9
|
+
* - Group headers
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import type { PromptDefinition } from "@canonical/summon-core";
|
|
13
|
+
import { Box, Text, useInput } from "ink";
|
|
14
|
+
import SelectInput from "ink-select-input";
|
|
15
|
+
import TextInput from "ink-text-input";
|
|
16
|
+
import { useCallback, useEffect, useMemo, useState } from "react";
|
|
17
|
+
|
|
18
|
+
export interface PromptSequenceProps {
|
|
19
|
+
/** List of prompts to display */
|
|
20
|
+
prompts: PromptDefinition[];
|
|
21
|
+
/** Called when all prompts are answered */
|
|
22
|
+
onComplete: (answers: Record<string, unknown>) => void;
|
|
23
|
+
/** Called when user cancels */
|
|
24
|
+
onCancel?: () => void;
|
|
25
|
+
/** Initial answers (for resuming/editing) */
|
|
26
|
+
initialAnswers?: Record<string, unknown>;
|
|
27
|
+
/**
|
|
28
|
+
* Answers provided EXPLICITLY (CLI flags / the positional): their prompts
|
|
29
|
+
* are never asked — they show as completed — so the sequence asks exactly
|
|
30
|
+
* `pendingPrompts(prompts, provided)`. An empty pending set completes
|
|
31
|
+
* immediately. Unlike `initialAnswers` (which pre-fill but still ask, e.g.
|
|
32
|
+
* when navigating back from the confirm gate), provided answers are final.
|
|
33
|
+
*/
|
|
34
|
+
provided?: Record<string, unknown>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// =============================================================================
|
|
38
|
+
// Helper to format answer values for display
|
|
39
|
+
// =============================================================================
|
|
40
|
+
|
|
41
|
+
const formatAnswerValue = (
|
|
42
|
+
value: unknown,
|
|
43
|
+
prompt: PromptDefinition,
|
|
44
|
+
): string => {
|
|
45
|
+
if (value === undefined || value === null) return "";
|
|
46
|
+
|
|
47
|
+
if (prompt.type === "confirm") {
|
|
48
|
+
return value ? "Yes" : "No";
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (prompt.type === "select" && prompt.choices) {
|
|
52
|
+
const choice = prompt.choices.find((c) => c.value === value);
|
|
53
|
+
return choice?.label ?? String(value);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (prompt.type === "multiselect" && Array.isArray(value)) {
|
|
57
|
+
if (value.length === 0) return "None";
|
|
58
|
+
if (prompt.choices) {
|
|
59
|
+
return value
|
|
60
|
+
.map((v) => prompt.choices?.find((c) => c.value === v)?.label ?? v)
|
|
61
|
+
.join(", ");
|
|
62
|
+
}
|
|
63
|
+
return value.join(", ");
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return String(value);
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
// =============================================================================
|
|
70
|
+
// Completed Answers Table Display
|
|
71
|
+
// =============================================================================
|
|
72
|
+
|
|
73
|
+
interface CompletedAnswersTableProps {
|
|
74
|
+
prompts: PromptDefinition[];
|
|
75
|
+
answers: Record<string, unknown>;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Display completed answers in a borderless table format with aligned columns.
|
|
80
|
+
*/
|
|
81
|
+
const CompletedAnswersTable = ({
|
|
82
|
+
prompts,
|
|
83
|
+
answers,
|
|
84
|
+
}: CompletedAnswersTableProps) => {
|
|
85
|
+
if (prompts.length === 0) {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Calculate max width for the question column (for alignment)
|
|
90
|
+
const maxQuestionWidth = Math.max(...prompts.map((p) => p.message.length));
|
|
91
|
+
|
|
92
|
+
return (
|
|
93
|
+
<Box flexDirection="column" marginBottom={1}>
|
|
94
|
+
{prompts.map((prompt) => {
|
|
95
|
+
const value = answers[prompt.name];
|
|
96
|
+
const displayValue = formatAnswerValue(value, prompt);
|
|
97
|
+
|
|
98
|
+
return (
|
|
99
|
+
<Box key={prompt.name}>
|
|
100
|
+
<Text color="green">✔ </Text>
|
|
101
|
+
<Text dimColor>{prompt.message.padEnd(maxQuestionWidth)}</Text>
|
|
102
|
+
<Text dimColor> </Text>
|
|
103
|
+
<Text color="cyan">{displayValue}</Text>
|
|
104
|
+
</Box>
|
|
105
|
+
);
|
|
106
|
+
})}
|
|
107
|
+
</Box>
|
|
108
|
+
);
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
// =============================================================================
|
|
112
|
+
// Text Prompt
|
|
113
|
+
// =============================================================================
|
|
114
|
+
|
|
115
|
+
interface TextPromptProps {
|
|
116
|
+
prompt: PromptDefinition;
|
|
117
|
+
initialValue?: string;
|
|
118
|
+
onSubmit: (value: string) => void;
|
|
119
|
+
onBack?: () => void;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const TextPrompt = ({
|
|
123
|
+
prompt,
|
|
124
|
+
initialValue,
|
|
125
|
+
onSubmit,
|
|
126
|
+
onBack,
|
|
127
|
+
}: TextPromptProps) => {
|
|
128
|
+
const [value, setValue] = useState(
|
|
129
|
+
initialValue ?? String(prompt.default ?? ""),
|
|
130
|
+
);
|
|
131
|
+
const [error, setError] = useState<string | null>(null);
|
|
132
|
+
|
|
133
|
+
// Reset value when prompt changes
|
|
134
|
+
useEffect(() => {
|
|
135
|
+
setValue(initialValue ?? String(prompt.default ?? ""));
|
|
136
|
+
setError(null);
|
|
137
|
+
}, [initialValue, prompt.default]);
|
|
138
|
+
|
|
139
|
+
const handleSubmit = useCallback(
|
|
140
|
+
(val: string) => {
|
|
141
|
+
if (prompt.validate) {
|
|
142
|
+
const result = prompt.validate(val);
|
|
143
|
+
if (result !== true) {
|
|
144
|
+
setError(typeof result === "string" ? result : "Invalid input");
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
onSubmit(val);
|
|
149
|
+
},
|
|
150
|
+
[prompt, onSubmit],
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
// Handle back navigation with Escape
|
|
154
|
+
useInput((_input, key) => {
|
|
155
|
+
if (key.escape && onBack) {
|
|
156
|
+
onBack();
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
return (
|
|
161
|
+
<Box flexDirection="column">
|
|
162
|
+
<Box>
|
|
163
|
+
<Text color="magenta">› </Text>
|
|
164
|
+
<Text bold>{prompt.message}</Text>
|
|
165
|
+
</Box>
|
|
166
|
+
<Box marginLeft={2}>
|
|
167
|
+
<TextInput value={value} onChange={setValue} onSubmit={handleSubmit} />
|
|
168
|
+
</Box>
|
|
169
|
+
{error && (
|
|
170
|
+
<Box marginLeft={2}>
|
|
171
|
+
<Text color="red">✘ {error}</Text>
|
|
172
|
+
</Box>
|
|
173
|
+
)}
|
|
174
|
+
</Box>
|
|
175
|
+
);
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
// =============================================================================
|
|
179
|
+
// Confirm Prompt
|
|
180
|
+
// =============================================================================
|
|
181
|
+
|
|
182
|
+
interface ConfirmPromptProps {
|
|
183
|
+
prompt: PromptDefinition;
|
|
184
|
+
/** An already-collected answer (e.g. a flag value re-asked after esc). */
|
|
185
|
+
initialValue?: boolean;
|
|
186
|
+
onSubmit: (value: boolean) => void;
|
|
187
|
+
onBack?: () => void;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const ConfirmPrompt = ({
|
|
191
|
+
prompt,
|
|
192
|
+
initialValue,
|
|
193
|
+
onSubmit,
|
|
194
|
+
onBack,
|
|
195
|
+
}: ConfirmPromptProps) => {
|
|
196
|
+
// Enter re-submits the EXISTING answer when there is one — after esc from
|
|
197
|
+
// the confirm gate, an explicit `--no-with-stories` must survive an Enter,
|
|
198
|
+
// not be overwritten by the declared default.
|
|
199
|
+
const defaultValue = initialValue ?? Boolean(prompt.default);
|
|
200
|
+
|
|
201
|
+
useInput((input, key) => {
|
|
202
|
+
if (input.toLowerCase() === "y") {
|
|
203
|
+
onSubmit(true);
|
|
204
|
+
} else if (input.toLowerCase() === "n") {
|
|
205
|
+
onSubmit(false);
|
|
206
|
+
} else if (key.return) {
|
|
207
|
+
onSubmit(defaultValue);
|
|
208
|
+
} else if (key.escape && onBack) {
|
|
209
|
+
onBack();
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
const hint = defaultValue ? "Y/n" : "y/N";
|
|
214
|
+
|
|
215
|
+
return (
|
|
216
|
+
<Box>
|
|
217
|
+
<Text color="magenta">› </Text>
|
|
218
|
+
<Text bold>{prompt.message} </Text>
|
|
219
|
+
<Text dimColor>({hint})</Text>
|
|
220
|
+
</Box>
|
|
221
|
+
);
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
// =============================================================================
|
|
225
|
+
// Select Prompt
|
|
226
|
+
// =============================================================================
|
|
227
|
+
|
|
228
|
+
interface SelectPromptProps {
|
|
229
|
+
prompt: PromptDefinition;
|
|
230
|
+
/** An already-collected answer (e.g. a flag value re-asked after esc). */
|
|
231
|
+
initialValue?: string;
|
|
232
|
+
onSubmit: (value: string) => void;
|
|
233
|
+
onBack?: () => void;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const SelectPrompt = ({
|
|
237
|
+
prompt,
|
|
238
|
+
initialValue,
|
|
239
|
+
onSubmit,
|
|
240
|
+
onBack,
|
|
241
|
+
}: SelectPromptProps) => {
|
|
242
|
+
const items =
|
|
243
|
+
prompt.choices?.map((choice) => ({
|
|
244
|
+
label: choice.label,
|
|
245
|
+
value: choice.value,
|
|
246
|
+
})) ?? [];
|
|
247
|
+
|
|
248
|
+
// Highlight the existing answer when there is one, the default otherwise.
|
|
249
|
+
const preferred = initialValue ?? prompt.default;
|
|
250
|
+
const initialIndex = preferred
|
|
251
|
+
? items.findIndex((item) => item.value === preferred)
|
|
252
|
+
: 0;
|
|
253
|
+
|
|
254
|
+
useInput((_input, key) => {
|
|
255
|
+
if (key.escape && onBack) {
|
|
256
|
+
onBack();
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
return (
|
|
261
|
+
<Box flexDirection="column">
|
|
262
|
+
<Box>
|
|
263
|
+
<Text color="magenta">› </Text>
|
|
264
|
+
<Text bold>{prompt.message}</Text>
|
|
265
|
+
<Text dimColor> (↑↓ to select, enter to confirm)</Text>
|
|
266
|
+
</Box>
|
|
267
|
+
<Box marginLeft={2}>
|
|
268
|
+
<SelectInput
|
|
269
|
+
items={items}
|
|
270
|
+
initialIndex={initialIndex >= 0 ? initialIndex : 0}
|
|
271
|
+
onSelect={(item) => onSubmit(item.value)}
|
|
272
|
+
/>
|
|
273
|
+
</Box>
|
|
274
|
+
</Box>
|
|
275
|
+
);
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
// =============================================================================
|
|
279
|
+
// Multiselect Prompt
|
|
280
|
+
// =============================================================================
|
|
281
|
+
|
|
282
|
+
interface MultiselectPromptProps {
|
|
283
|
+
prompt: PromptDefinition;
|
|
284
|
+
/** An already-collected answer (e.g. a flag value re-asked after esc). */
|
|
285
|
+
initialValue?: string[];
|
|
286
|
+
onSubmit: (values: string[]) => void;
|
|
287
|
+
onBack?: () => void;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
const MultiselectPrompt = ({
|
|
291
|
+
prompt,
|
|
292
|
+
initialValue,
|
|
293
|
+
onSubmit,
|
|
294
|
+
onBack,
|
|
295
|
+
}: MultiselectPromptProps) => {
|
|
296
|
+
const [selected, setSelected] = useState<Set<string>>(
|
|
297
|
+
new Set(initialValue ?? (prompt.default as string[] | undefined)),
|
|
298
|
+
);
|
|
299
|
+
const [highlightedIndex, setHighlightedIndex] = useState(0);
|
|
300
|
+
|
|
301
|
+
const choices = prompt.choices ?? [];
|
|
302
|
+
|
|
303
|
+
useInput((input, key) => {
|
|
304
|
+
if (key.escape && onBack) {
|
|
305
|
+
onBack();
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
if (key.upArrow) {
|
|
309
|
+
setHighlightedIndex((prev) => (prev > 0 ? prev - 1 : choices.length - 1));
|
|
310
|
+
} else if (key.downArrow) {
|
|
311
|
+
setHighlightedIndex((prev) => (prev < choices.length - 1 ? prev + 1 : 0));
|
|
312
|
+
} else if (input === " ") {
|
|
313
|
+
const choice = choices[highlightedIndex];
|
|
314
|
+
if (choice) {
|
|
315
|
+
setSelected((prev) => {
|
|
316
|
+
const newSet = new Set(prev);
|
|
317
|
+
if (newSet.has(choice.value)) {
|
|
318
|
+
newSet.delete(choice.value);
|
|
319
|
+
} else {
|
|
320
|
+
newSet.add(choice.value);
|
|
321
|
+
}
|
|
322
|
+
return newSet;
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
} else if (key.return) {
|
|
326
|
+
onSubmit(Array.from(selected));
|
|
327
|
+
}
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
return (
|
|
331
|
+
<Box flexDirection="column">
|
|
332
|
+
<Box>
|
|
333
|
+
<Text color="magenta">› </Text>
|
|
334
|
+
<Text bold>{prompt.message}</Text>
|
|
335
|
+
<Text dimColor> (space to toggle, enter to confirm)</Text>
|
|
336
|
+
</Box>
|
|
337
|
+
<Box marginLeft={2} flexDirection="column">
|
|
338
|
+
{choices.map((choice, i) => {
|
|
339
|
+
const isHighlighted = i === highlightedIndex;
|
|
340
|
+
const isSelected = selected.has(choice.value);
|
|
341
|
+
const pointer = isHighlighted ? "› " : " ";
|
|
342
|
+
const checkbox = isSelected ? "◉ " : "○ ";
|
|
343
|
+
|
|
344
|
+
return (
|
|
345
|
+
<Box key={choice.value}>
|
|
346
|
+
<Text color={isHighlighted ? "magenta" : undefined}>
|
|
347
|
+
{pointer}
|
|
348
|
+
</Text>
|
|
349
|
+
<Text color={isSelected ? "green" : "gray"}>{checkbox}</Text>
|
|
350
|
+
<Text color={isSelected ? undefined : "gray"}>
|
|
351
|
+
{choice.label}
|
|
352
|
+
</Text>
|
|
353
|
+
</Box>
|
|
354
|
+
);
|
|
355
|
+
})}
|
|
356
|
+
</Box>
|
|
357
|
+
</Box>
|
|
358
|
+
);
|
|
359
|
+
};
|
|
360
|
+
|
|
361
|
+
// =============================================================================
|
|
362
|
+
// Progress Header
|
|
363
|
+
// =============================================================================
|
|
364
|
+
|
|
365
|
+
interface ProgressHeaderProps {
|
|
366
|
+
current: number;
|
|
367
|
+
total: number;
|
|
368
|
+
group?: string;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
const ProgressHeader = ({ current, total, group }: ProgressHeaderProps) => {
|
|
372
|
+
return (
|
|
373
|
+
<Box marginBottom={1}>
|
|
374
|
+
<Text dimColor>
|
|
375
|
+
{group ? `${group} · ` : ""}Step {current} of {total}
|
|
376
|
+
{" · "}
|
|
377
|
+
<Text dimColor italic>
|
|
378
|
+
esc to go back
|
|
379
|
+
</Text>
|
|
380
|
+
</Text>
|
|
381
|
+
</Box>
|
|
382
|
+
);
|
|
383
|
+
};
|
|
384
|
+
|
|
385
|
+
// =============================================================================
|
|
386
|
+
// Main PromptSequence Component
|
|
387
|
+
// =============================================================================
|
|
388
|
+
|
|
389
|
+
export const PromptSequence = ({
|
|
390
|
+
prompts,
|
|
391
|
+
onComplete,
|
|
392
|
+
onCancel,
|
|
393
|
+
initialAnswers,
|
|
394
|
+
provided,
|
|
395
|
+
}: PromptSequenceProps) => {
|
|
396
|
+
// Always start at index 0, but preserve answers for display/editing
|
|
397
|
+
const [currentIndex, setCurrentIndex] = useState(0);
|
|
398
|
+
const [answers, setAnswers] = useState<Record<string, unknown>>(
|
|
399
|
+
initialAnswers ?? {},
|
|
400
|
+
);
|
|
401
|
+
// Track the history of prompt indices for back navigation
|
|
402
|
+
const [history, setHistory] = useState<number[]>([]);
|
|
403
|
+
|
|
404
|
+
const providedNames = useMemo(
|
|
405
|
+
() => new Set(Object.keys(provided ?? {})),
|
|
406
|
+
[provided],
|
|
407
|
+
);
|
|
408
|
+
|
|
409
|
+
// The asking list: skip explicitly provided prompts, and evaluate `when`
|
|
410
|
+
// against the given answer set (the answers collected so far by default).
|
|
411
|
+
const askable = useCallback(
|
|
412
|
+
(against: Record<string, unknown>) =>
|
|
413
|
+
prompts.filter((prompt) => {
|
|
414
|
+
if (providedNames.has(prompt.name)) {
|
|
415
|
+
return false;
|
|
416
|
+
}
|
|
417
|
+
if (prompt.when) {
|
|
418
|
+
return prompt.when(against);
|
|
419
|
+
}
|
|
420
|
+
return true;
|
|
421
|
+
}),
|
|
422
|
+
[prompts, providedNames],
|
|
423
|
+
);
|
|
424
|
+
|
|
425
|
+
const activePrompts = askable(answers);
|
|
426
|
+
|
|
427
|
+
const currentPrompt = activePrompts[currentIndex];
|
|
428
|
+
|
|
429
|
+
const handleAnswer = useCallback(
|
|
430
|
+
(value: unknown) => {
|
|
431
|
+
if (!currentPrompt) return;
|
|
432
|
+
|
|
433
|
+
const newAnswers = { ...answers, [currentPrompt.name]: value };
|
|
434
|
+
setAnswers(newAnswers);
|
|
435
|
+
|
|
436
|
+
// Recompute the asking list against the NEW answers: this answer may
|
|
437
|
+
// have just made a later conditional prompt applicable (or not) — e.g.
|
|
438
|
+
// "Include stories? yes" unlocking "Use TypeScript stories?".
|
|
439
|
+
const nextActive = askable(newAnswers);
|
|
440
|
+
const nextIndex = nextActive.indexOf(currentPrompt) + 1;
|
|
441
|
+
if (nextIndex > 0 && nextIndex < nextActive.length) {
|
|
442
|
+
setHistory((prev) => [...prev, currentIndex]);
|
|
443
|
+
setCurrentIndex(nextIndex);
|
|
444
|
+
} else {
|
|
445
|
+
onComplete(newAnswers);
|
|
446
|
+
}
|
|
447
|
+
},
|
|
448
|
+
[answers, currentPrompt, currentIndex, askable, onComplete],
|
|
449
|
+
);
|
|
450
|
+
|
|
451
|
+
const handleBack = useCallback(() => {
|
|
452
|
+
if (history.length > 0) {
|
|
453
|
+
const prevIndex = history[history.length - 1];
|
|
454
|
+
setHistory((prev) => prev.slice(0, -1));
|
|
455
|
+
setCurrentIndex(prevIndex);
|
|
456
|
+
} else if (onCancel) {
|
|
457
|
+
onCancel();
|
|
458
|
+
}
|
|
459
|
+
}, [history, onCancel]);
|
|
460
|
+
|
|
461
|
+
// Nothing to ask (every prompt explicitly provided): complete immediately —
|
|
462
|
+
// the wizard proceeds straight to its preview/confirm.
|
|
463
|
+
useEffect(() => {
|
|
464
|
+
if (currentPrompt === undefined) {
|
|
465
|
+
onComplete(answers);
|
|
466
|
+
}
|
|
467
|
+
}, [currentPrompt, onComplete, answers]);
|
|
468
|
+
|
|
469
|
+
if (!currentPrompt) {
|
|
470
|
+
return null;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
// Previously completed prompts to display: the explicitly provided ones,
|
|
474
|
+
// then everything answered so far — in declared order.
|
|
475
|
+
const answeredSoFar = new Set(
|
|
476
|
+
activePrompts.slice(0, currentIndex).map((prompt) => prompt.name),
|
|
477
|
+
);
|
|
478
|
+
const completedPrompts = prompts.filter(
|
|
479
|
+
(prompt) =>
|
|
480
|
+
providedNames.has(prompt.name) || answeredSoFar.has(prompt.name),
|
|
481
|
+
);
|
|
482
|
+
const completedAnswers = { ...provided, ...answers };
|
|
483
|
+
|
|
484
|
+
// Render the appropriate prompt type
|
|
485
|
+
const renderCurrentPrompt = () => {
|
|
486
|
+
const existingValue = answers[currentPrompt.name];
|
|
487
|
+
|
|
488
|
+
switch (currentPrompt.type) {
|
|
489
|
+
case "text":
|
|
490
|
+
return (
|
|
491
|
+
<TextPrompt
|
|
492
|
+
prompt={currentPrompt}
|
|
493
|
+
initialValue={existingValue as string | undefined}
|
|
494
|
+
onSubmit={handleAnswer}
|
|
495
|
+
onBack={handleBack}
|
|
496
|
+
/>
|
|
497
|
+
);
|
|
498
|
+
case "confirm":
|
|
499
|
+
return (
|
|
500
|
+
<ConfirmPrompt
|
|
501
|
+
key={currentPrompt.name}
|
|
502
|
+
prompt={currentPrompt}
|
|
503
|
+
initialValue={existingValue as boolean | undefined}
|
|
504
|
+
onSubmit={handleAnswer}
|
|
505
|
+
onBack={handleBack}
|
|
506
|
+
/>
|
|
507
|
+
);
|
|
508
|
+
case "select":
|
|
509
|
+
return (
|
|
510
|
+
<SelectPrompt
|
|
511
|
+
key={currentPrompt.name}
|
|
512
|
+
prompt={currentPrompt}
|
|
513
|
+
initialValue={existingValue as string | undefined}
|
|
514
|
+
onSubmit={handleAnswer}
|
|
515
|
+
onBack={handleBack}
|
|
516
|
+
/>
|
|
517
|
+
);
|
|
518
|
+
case "multiselect":
|
|
519
|
+
return (
|
|
520
|
+
<MultiselectPrompt
|
|
521
|
+
key={currentPrompt.name}
|
|
522
|
+
prompt={currentPrompt}
|
|
523
|
+
initialValue={existingValue as string[] | undefined}
|
|
524
|
+
onSubmit={handleAnswer}
|
|
525
|
+
onBack={handleBack}
|
|
526
|
+
/>
|
|
527
|
+
);
|
|
528
|
+
default:
|
|
529
|
+
return (
|
|
530
|
+
<Text color="red">
|
|
531
|
+
Unknown prompt type: {(currentPrompt as PromptDefinition).type}
|
|
532
|
+
</Text>
|
|
533
|
+
);
|
|
534
|
+
}
|
|
535
|
+
};
|
|
536
|
+
|
|
537
|
+
return (
|
|
538
|
+
<Box flexDirection="column">
|
|
539
|
+
{/* Progress indicator */}
|
|
540
|
+
<ProgressHeader
|
|
541
|
+
current={currentIndex + 1}
|
|
542
|
+
total={activePrompts.length}
|
|
543
|
+
group={currentPrompt.group}
|
|
544
|
+
/>
|
|
545
|
+
|
|
546
|
+
{/* Show completed answers in table format */}
|
|
547
|
+
{completedPrompts.length > 0 && (
|
|
548
|
+
<CompletedAnswersTable
|
|
549
|
+
prompts={completedPrompts}
|
|
550
|
+
answers={completedAnswers}
|
|
551
|
+
/>
|
|
552
|
+
)}
|
|
553
|
+
|
|
554
|
+
{/* Current prompt */}
|
|
555
|
+
{renderCurrentPrompt()}
|
|
556
|
+
</Box>
|
|
557
|
+
);
|
|
558
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Spinner Component
|
|
3
|
+
*
|
|
4
|
+
* A simple loading spinner for the CLI.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { Box, Text } from "ink";
|
|
8
|
+
import { useEffect, useState } from "react";
|
|
9
|
+
|
|
10
|
+
const frames = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
|
11
|
+
|
|
12
|
+
export interface SpinnerProps {
|
|
13
|
+
/** Color of the spinner */
|
|
14
|
+
color?: string;
|
|
15
|
+
/** Label to show next to the spinner */
|
|
16
|
+
label?: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const Spinner = ({ color = "cyan", label }: SpinnerProps) => {
|
|
20
|
+
const [frameIndex, setFrameIndex] = useState(0);
|
|
21
|
+
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
const timer = setInterval(() => {
|
|
24
|
+
setFrameIndex((prev) => (prev + 1) % frames.length);
|
|
25
|
+
}, 80);
|
|
26
|
+
|
|
27
|
+
return () => clearInterval(timer);
|
|
28
|
+
}, []);
|
|
29
|
+
|
|
30
|
+
return (
|
|
31
|
+
<Box>
|
|
32
|
+
<Text color={color}>{frames[frameIndex]}</Text>
|
|
33
|
+
{label && <Text> {label}</Text>}
|
|
34
|
+
</Box>
|
|
35
|
+
);
|
|
36
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* React Ink Components
|
|
3
|
+
*
|
|
4
|
+
* This module exports all React Ink components for the CLI.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export type { AppProps, AppState } from "./App.js";
|
|
8
|
+
export { App } from "./App.js";
|
|
9
|
+
export type { ExecutionProgressProps } from "./ExecutionProgress.js";
|
|
10
|
+
export { ExecutionProgress } from "./ExecutionProgress.js";
|
|
11
|
+
export type { FileTreePreviewProps } from "./FileTreePreview.js";
|
|
12
|
+
export { FileTreePreview } from "./FileTreePreview.js";
|
|
13
|
+
export type { PromptSequenceProps } from "./PromptSequence.js";
|
|
14
|
+
export { PromptSequence } from "./PromptSequence.js";
|
|
15
|
+
export type { SpinnerProps } from "./Spinner.js";
|
|
16
|
+
export { Spinner } from "./Spinner.js";
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import {
|
|
2
|
+
deleteFile,
|
|
3
|
+
type Effect,
|
|
4
|
+
execEffect,
|
|
5
|
+
flatMap,
|
|
6
|
+
pure,
|
|
7
|
+
readFile,
|
|
8
|
+
type Task,
|
|
9
|
+
} from "@canonical/task";
|
|
10
|
+
import { describe, expect, it } from "vitest";
|
|
11
|
+
import {
|
|
12
|
+
describeUndoSteps,
|
|
13
|
+
isUnreversibleExec,
|
|
14
|
+
shouldSkipUndoGate,
|
|
15
|
+
} from "./undoPlan.js";
|
|
16
|
+
|
|
17
|
+
describe("describeUndoSteps", () => {
|
|
18
|
+
it("lists the plan in execution order (reverse of collection order)", () => {
|
|
19
|
+
const undos: Task<void>[] = [
|
|
20
|
+
deleteFile("first-created.ts"),
|
|
21
|
+
deleteFile("second-created.ts"),
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
const plan = describeUndoSteps(undos);
|
|
25
|
+
|
|
26
|
+
expect(plan.map((effect) => "path" in effect && effect.path)).toEqual([
|
|
27
|
+
"second-created.ts",
|
|
28
|
+
"first-created.ts",
|
|
29
|
+
]);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("shows a revert line for an undo whose dry-run yields only reads", () => {
|
|
33
|
+
// Shaped like removeLineFromFile's undo: reads the file, and because
|
|
34
|
+
// dry-run mocks reads the target line is absent, so no write surfaces.
|
|
35
|
+
const removeLineShaped: Task<void> = flatMap(
|
|
36
|
+
readFile("parent/index.ts"),
|
|
37
|
+
() => pure(undefined),
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
const plan = describeUndoSteps([removeLineShaped]);
|
|
41
|
+
|
|
42
|
+
expect(plan).toHaveLength(1);
|
|
43
|
+
expect(plan[0]).toMatchObject({
|
|
44
|
+
_tag: "Log",
|
|
45
|
+
message: "Revert changes in parent/index.ts",
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("shows one entry per step, mixing visible and revert lines", () => {
|
|
50
|
+
const removeLineShaped: Task<void> = flatMap(
|
|
51
|
+
readFile("parent/index.ts"),
|
|
52
|
+
() => pure(undefined),
|
|
53
|
+
);
|
|
54
|
+
const plan = describeUndoSteps([
|
|
55
|
+
deleteFile("Button/index.ts"),
|
|
56
|
+
removeLineShaped,
|
|
57
|
+
]);
|
|
58
|
+
|
|
59
|
+
expect(plan).toHaveLength(2);
|
|
60
|
+
expect(plan[0]?._tag).toBe("Log");
|
|
61
|
+
expect(plan[1]).toMatchObject({ _tag: "DeleteFile" });
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
describe("isUnreversibleExec", () => {
|
|
66
|
+
it("flags an exec without an undo as residue", () => {
|
|
67
|
+
expect(isUnreversibleExec(execEffect("bun", ["install"]))).toBe(true);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("does not flag an exec that carries its own undo", () => {
|
|
71
|
+
const reversible = execEffect("git", ["init"], undefined, {
|
|
72
|
+
undo: pure(undefined),
|
|
73
|
+
});
|
|
74
|
+
expect(isUnreversibleExec(reversible)).toBe(false);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("ignores non-exec effects", () => {
|
|
78
|
+
const write: Effect = {
|
|
79
|
+
_tag: "WriteFile",
|
|
80
|
+
path: "a.ts",
|
|
81
|
+
content: "",
|
|
82
|
+
};
|
|
83
|
+
expect(isUnreversibleExec(write)).toBe(false);
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
describe("shouldSkipUndoGate", () => {
|
|
88
|
+
it("gates when answers came from flags alone", () => {
|
|
89
|
+
expect(shouldSkipUndoGate({ yes: false, preview: true })).toBe(false);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("skips the gate with --yes", () => {
|
|
93
|
+
expect(shouldSkipUndoGate({ yes: true, preview: true })).toBe(true);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("skips the gate with --no-preview", () => {
|
|
97
|
+
expect(shouldSkipUndoGate({ yes: false, preview: false })).toBe(true);
|
|
98
|
+
});
|
|
99
|
+
});
|