@canonical/summon 0.37.0 → 0.40.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,492 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shell Autocompletion Support for Summon CLI
|
|
3
|
+
*
|
|
4
|
+
* Uses omelette to provide TAB completion for:
|
|
5
|
+
* - Generator names (navigating the command tree)
|
|
6
|
+
* - Generator arguments (based on prompt definitions)
|
|
7
|
+
* - File/folder paths for path-related prompts
|
|
8
|
+
*
|
|
9
|
+
* Supports Bash, Zsh, and Fish shells.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import * as fs from "node:fs";
|
|
13
|
+
import * as path from "node:path";
|
|
14
|
+
import type {
|
|
15
|
+
GeneratorDefinition,
|
|
16
|
+
PromptDefinition,
|
|
17
|
+
} from "@canonical/summon-core";
|
|
18
|
+
import omelette from "omelette";
|
|
19
|
+
|
|
20
|
+
// =============================================================================
|
|
21
|
+
// Types
|
|
22
|
+
// =============================================================================
|
|
23
|
+
|
|
24
|
+
/** Represents a node in the generator tree (simplified for completion) */
|
|
25
|
+
export interface CompletionNode {
|
|
26
|
+
name: string;
|
|
27
|
+
indexPath?: string;
|
|
28
|
+
children: Map<string, CompletionNode>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Flattened generator info for completion */
|
|
32
|
+
interface GeneratorInfo {
|
|
33
|
+
path: string[];
|
|
34
|
+
prompts: PromptDefinition[];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// =============================================================================
|
|
38
|
+
// Path Detection
|
|
39
|
+
// =============================================================================
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Detect if a prompt expects a file/folder path based on its name or message.
|
|
43
|
+
*/
|
|
44
|
+
export const isPathPrompt = (prompt: PromptDefinition): boolean => {
|
|
45
|
+
const pathIndicators = [
|
|
46
|
+
"path",
|
|
47
|
+
"dir",
|
|
48
|
+
"directory",
|
|
49
|
+
"file",
|
|
50
|
+
"folder",
|
|
51
|
+
"location",
|
|
52
|
+
];
|
|
53
|
+
const nameLower = prompt.name.toLowerCase();
|
|
54
|
+
const messageLower = prompt.message.toLowerCase();
|
|
55
|
+
|
|
56
|
+
return pathIndicators.some(
|
|
57
|
+
(ind) => nameLower.includes(ind) || messageLower.includes(ind),
|
|
58
|
+
);
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
// =============================================================================
|
|
62
|
+
// Filesystem Completion
|
|
63
|
+
// =============================================================================
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Get filesystem path completions for a partial path.
|
|
67
|
+
*/
|
|
68
|
+
export const getPathCompletions = (partial: string): string[] => {
|
|
69
|
+
try {
|
|
70
|
+
// Handle empty or relative paths
|
|
71
|
+
const searchPath = partial || ".";
|
|
72
|
+
const dir = path.dirname(searchPath);
|
|
73
|
+
const prefix = path.basename(searchPath);
|
|
74
|
+
|
|
75
|
+
// Try to read the directory
|
|
76
|
+
const dirToRead = partial.endsWith("/") ? searchPath : dir;
|
|
77
|
+
const entries = fs.readdirSync(dirToRead, { withFileTypes: true });
|
|
78
|
+
|
|
79
|
+
// Filter entries that match the prefix
|
|
80
|
+
const matches = entries
|
|
81
|
+
.filter((entry) => {
|
|
82
|
+
// If partial ends with /, show all entries
|
|
83
|
+
if (partial.endsWith("/")) return true;
|
|
84
|
+
// Otherwise filter by prefix
|
|
85
|
+
return entry.name.startsWith(prefix);
|
|
86
|
+
})
|
|
87
|
+
.map((entry) => {
|
|
88
|
+
const name = entry.name;
|
|
89
|
+
const basePath = partial.endsWith("/") ? searchPath : dir;
|
|
90
|
+
const fullPath = basePath === "." ? name : path.join(basePath, name);
|
|
91
|
+
|
|
92
|
+
// Append / for directories to enable continued completion
|
|
93
|
+
return entry.isDirectory() ? `${fullPath}/` : fullPath;
|
|
94
|
+
})
|
|
95
|
+
.slice(0, 50); // Limit results
|
|
96
|
+
|
|
97
|
+
return matches;
|
|
98
|
+
} catch {
|
|
99
|
+
// Directory doesn't exist or can't be read
|
|
100
|
+
return [];
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
// =============================================================================
|
|
105
|
+
// Argument Completion
|
|
106
|
+
// =============================================================================
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Convert camelCase to kebab-case for CLI flags.
|
|
110
|
+
*/
|
|
111
|
+
const toKebabCase = (str: string): string =>
|
|
112
|
+
str.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Get completions for generator arguments based on prompt definitions.
|
|
116
|
+
*
|
|
117
|
+
* @param prompts - The prompt definitions for the generator
|
|
118
|
+
* @param line - The current command line
|
|
119
|
+
* @param before - The word before the cursor
|
|
120
|
+
* @param showAll - If true, show all flags even if line doesn't contain --
|
|
121
|
+
*/
|
|
122
|
+
export const getArgumentCompletions = (
|
|
123
|
+
prompts: PromptDefinition[],
|
|
124
|
+
line: string,
|
|
125
|
+
before: string,
|
|
126
|
+
showAll = false,
|
|
127
|
+
): string[] => {
|
|
128
|
+
const completions: string[] = [];
|
|
129
|
+
|
|
130
|
+
// Check if we're completing a flag value (e.g., --type=<TAB> or --type <TAB>)
|
|
131
|
+
const flagValueMatch = line.match(/--([a-z-]+)(?:=|\s+)([^\s]*)$/i);
|
|
132
|
+
if (flagValueMatch) {
|
|
133
|
+
const flagName = flagValueMatch[1];
|
|
134
|
+
const partial = flagValueMatch[2] || "";
|
|
135
|
+
|
|
136
|
+
// Find the corresponding prompt
|
|
137
|
+
const prompt = prompts.find(
|
|
138
|
+
(p) => toKebabCase(p.name) === flagName || p.name === flagName,
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
if (prompt) {
|
|
142
|
+
// For select/multiselect, return choices
|
|
143
|
+
if (
|
|
144
|
+
(prompt.type === "select" || prompt.type === "multiselect") &&
|
|
145
|
+
prompt.choices
|
|
146
|
+
) {
|
|
147
|
+
return prompt.choices
|
|
148
|
+
.map((c) => c.value)
|
|
149
|
+
.filter((v) => v.startsWith(partial));
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// For text prompts that look like paths, return path completions
|
|
153
|
+
if (prompt.type === "text" && isPathPrompt(prompt)) {
|
|
154
|
+
return getPathCompletions(partial);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Check if we're completing a flag name (--<TAB>) or showing all flags
|
|
160
|
+
const flagNameMatch = line.match(/--([a-z-]*)$/i);
|
|
161
|
+
const shouldShowFlags =
|
|
162
|
+
showAll ||
|
|
163
|
+
flagNameMatch ||
|
|
164
|
+
before === "--" ||
|
|
165
|
+
before.startsWith("--") ||
|
|
166
|
+
before.startsWith("-");
|
|
167
|
+
|
|
168
|
+
if (shouldShowFlags) {
|
|
169
|
+
// Extract partial from the flag name match, or from 'before' if it starts with --
|
|
170
|
+
let partial = flagNameMatch?.[1] || "";
|
|
171
|
+
if (!partial && before.startsWith("--")) {
|
|
172
|
+
partial = before.slice(2);
|
|
173
|
+
} else if (!partial && before.startsWith("-")) {
|
|
174
|
+
partial = before.slice(1);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
for (const prompt of prompts) {
|
|
178
|
+
const kebabName = toKebabCase(prompt.name);
|
|
179
|
+
|
|
180
|
+
if (prompt.type === "confirm") {
|
|
181
|
+
// For confirm prompts, show --no-X if default is true, --X if default is false
|
|
182
|
+
if (prompt.default === true) {
|
|
183
|
+
const flag = `no-${kebabName}`;
|
|
184
|
+
if (flag.startsWith(partial)) {
|
|
185
|
+
completions.push(`--${flag}`);
|
|
186
|
+
}
|
|
187
|
+
} else {
|
|
188
|
+
if (kebabName.startsWith(partial)) {
|
|
189
|
+
completions.push(`--${kebabName}`);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
} else {
|
|
193
|
+
if (kebabName.startsWith(partial)) {
|
|
194
|
+
completions.push(`--${kebabName}`);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// Add built-in flags
|
|
200
|
+
const builtinFlags = [
|
|
201
|
+
"--dry-run",
|
|
202
|
+
"--yes",
|
|
203
|
+
"--verbose",
|
|
204
|
+
"--no-preview",
|
|
205
|
+
"--no-generated-stamp",
|
|
206
|
+
"--llm",
|
|
207
|
+
"--help",
|
|
208
|
+
];
|
|
209
|
+
for (const flag of builtinFlags) {
|
|
210
|
+
if (flag.startsWith(`--${partial}`)) {
|
|
211
|
+
completions.push(flag);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
return completions;
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
// =============================================================================
|
|
220
|
+
// Tree Building
|
|
221
|
+
// =============================================================================
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Generator loader function type - passed in to avoid circular dependencies.
|
|
225
|
+
*/
|
|
226
|
+
export type GeneratorLoader = (
|
|
227
|
+
indexPath: string,
|
|
228
|
+
) => Promise<GeneratorDefinition>;
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Get completions for a specific position in the command.
|
|
232
|
+
*/
|
|
233
|
+
const getCompletionsAtPosition = (
|
|
234
|
+
node: CompletionNode,
|
|
235
|
+
args: string[],
|
|
236
|
+
generators: Map<string, GeneratorInfo>,
|
|
237
|
+
): string[] => {
|
|
238
|
+
// Navigate to the correct position in the tree
|
|
239
|
+
let current = node;
|
|
240
|
+
const pathSoFar: string[] = [];
|
|
241
|
+
|
|
242
|
+
for (const arg of args) {
|
|
243
|
+
// Skip flags and empty args
|
|
244
|
+
if (arg.startsWith("-") || !arg) continue;
|
|
245
|
+
|
|
246
|
+
const child = current.children.get(arg);
|
|
247
|
+
if (child) {
|
|
248
|
+
current = child;
|
|
249
|
+
pathSoFar.push(arg);
|
|
250
|
+
} else {
|
|
251
|
+
// Partial match - don't add to path, keep current node
|
|
252
|
+
break;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// Check if we have subcommands available
|
|
257
|
+
const subcommands = [...current.children.keys()];
|
|
258
|
+
|
|
259
|
+
// Check if we're at a generator
|
|
260
|
+
const generatorKey = pathSoFar.join("/");
|
|
261
|
+
const generatorInfo = generators.get(generatorKey);
|
|
262
|
+
|
|
263
|
+
// Get the last arg for context
|
|
264
|
+
const lastArg = args[args.length - 1] || "";
|
|
265
|
+
const line = args.join(" ");
|
|
266
|
+
|
|
267
|
+
// If we're at a generator
|
|
268
|
+
if (generatorInfo) {
|
|
269
|
+
// If the last arg starts with -, provide flag completions
|
|
270
|
+
if (lastArg.startsWith("-")) {
|
|
271
|
+
return getArgumentCompletions(generatorInfo.prompts, line, lastArg);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// Check if this generator has a positional prompt
|
|
275
|
+
const positionalPrompt = generatorInfo.prompts.find((p) => p.positional);
|
|
276
|
+
|
|
277
|
+
// If there are no subcommands and this is a leaf generator
|
|
278
|
+
if (subcommands.length === 0) {
|
|
279
|
+
// Count non-flag args after the generator path
|
|
280
|
+
const nonFlagArgs = args.filter((a) => !a.startsWith("-"));
|
|
281
|
+
const argsAfterGenerator = nonFlagArgs.length - pathSoFar.length;
|
|
282
|
+
|
|
283
|
+
// If we have a positional prompt and haven't provided the positional arg yet
|
|
284
|
+
if (positionalPrompt && argsAfterGenerator === 0) {
|
|
285
|
+
// If the positional is a path, provide path completions
|
|
286
|
+
if (isPathPrompt(positionalPrompt)) {
|
|
287
|
+
return getPathCompletions(lastArg || "");
|
|
288
|
+
}
|
|
289
|
+
// Otherwise show flags (they can type the positional or use flags)
|
|
290
|
+
return getArgumentCompletions(generatorInfo.prompts, line, "", true);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// If positional already provided or no positional, show flags
|
|
294
|
+
return getArgumentCompletions(generatorInfo.prompts, line, "", true);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// If there are both subcommands and this is a generator,
|
|
298
|
+
// show subcommands (user might want to go deeper)
|
|
299
|
+
return subcommands;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// Not at a generator, show subcommands
|
|
303
|
+
// Only filter if the last arg is a partial (not in pathSoFar)
|
|
304
|
+
const lastPathSeg = pathSoFar[pathSoFar.length - 1];
|
|
305
|
+
const isPartialArg =
|
|
306
|
+
lastArg && !lastArg.startsWith("-") && lastArg !== lastPathSeg;
|
|
307
|
+
|
|
308
|
+
if (isPartialArg) {
|
|
309
|
+
return subcommands.filter((cmd) => cmd.startsWith(lastArg));
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
return subcommands;
|
|
313
|
+
};
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Load all generators and cache their prompts.
|
|
317
|
+
*/
|
|
318
|
+
const loadAllGenerators = async (
|
|
319
|
+
node: CompletionNode,
|
|
320
|
+
loadGenerator: GeneratorLoader,
|
|
321
|
+
currentPath: string[] = [],
|
|
322
|
+
cache: Map<string, GeneratorInfo> = new Map(),
|
|
323
|
+
): Promise<Map<string, GeneratorInfo>> => {
|
|
324
|
+
for (const [name, child] of node.children) {
|
|
325
|
+
const childPath = [...currentPath, name];
|
|
326
|
+
const pathKey = childPath.join("/");
|
|
327
|
+
|
|
328
|
+
if (child.indexPath) {
|
|
329
|
+
try {
|
|
330
|
+
const generator = await loadGenerator(child.indexPath);
|
|
331
|
+
cache.set(pathKey, {
|
|
332
|
+
path: childPath,
|
|
333
|
+
prompts: generator.prompts,
|
|
334
|
+
});
|
|
335
|
+
} catch {
|
|
336
|
+
// Failed to load generator, skip
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
if (child.children.size > 0) {
|
|
341
|
+
await loadAllGenerators(child, loadGenerator, childPath, cache);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
return cache;
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
// =============================================================================
|
|
349
|
+
// Completion Initialization
|
|
350
|
+
// =============================================================================
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Initialize shell completion for the summon CLI.
|
|
354
|
+
*
|
|
355
|
+
* This should be called early in the CLI lifecycle, before commander parsing,
|
|
356
|
+
* because omelette needs to intercept completion requests.
|
|
357
|
+
*
|
|
358
|
+
* @param generatorTree - The root of the generator tree
|
|
359
|
+
* @param loadGenerator - Function to load a generator from its path
|
|
360
|
+
* @returns The omelette instance (for setup/cleanup methods)
|
|
361
|
+
*/
|
|
362
|
+
export const initCompletion = async (
|
|
363
|
+
generatorTree: CompletionNode,
|
|
364
|
+
loadGenerator: GeneratorLoader,
|
|
365
|
+
): Promise<omelette.Instance> => {
|
|
366
|
+
// Load all generators to get their prompts
|
|
367
|
+
const generators = await loadAllGenerators(generatorTree, loadGenerator);
|
|
368
|
+
|
|
369
|
+
// Create the completion instance
|
|
370
|
+
// We use a template with multiple segments to handle deep hierarchies
|
|
371
|
+
// The "complete" event fires for all positions
|
|
372
|
+
const complete = omelette("summon <arg1> <arg2> <arg3> <arg4> <arg5>");
|
|
373
|
+
|
|
374
|
+
// Handle completion dynamically for all positions
|
|
375
|
+
// The "complete" event receives the fragment name (arg1, arg2, etc.) and context
|
|
376
|
+
complete.on("complete", (fragment, { line, reply, before }) => {
|
|
377
|
+
// Parse the current line to determine what to complete
|
|
378
|
+
// Remove 'summon' and filter out internal flags like --generators
|
|
379
|
+
let args = line.split(/\s+/).filter(Boolean).slice(1);
|
|
380
|
+
|
|
381
|
+
// Filter out internal CLI flags that shouldn't be part of completion context
|
|
382
|
+
const internalFlags = [
|
|
383
|
+
"--generators",
|
|
384
|
+
"-g",
|
|
385
|
+
"--compbash",
|
|
386
|
+
"--compzsh",
|
|
387
|
+
"--compfish",
|
|
388
|
+
"--compgen",
|
|
389
|
+
];
|
|
390
|
+
args = args.filter((arg, idx) => {
|
|
391
|
+
// Skip internal flags and their values
|
|
392
|
+
if (internalFlags.includes(arg)) return false;
|
|
393
|
+
// Skip the value after an internal flag
|
|
394
|
+
if (idx > 0 && internalFlags.includes(args[idx - 1])) return false;
|
|
395
|
+
return true;
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
// Debug logging (only when SUMMON_DEBUG is set)
|
|
399
|
+
if (process.env.SUMMON_DEBUG) {
|
|
400
|
+
console.error("[completion] fragment:", fragment);
|
|
401
|
+
console.error("[completion] line:", line);
|
|
402
|
+
console.error("[completion] before:", before);
|
|
403
|
+
console.error("[completion] args (filtered):", args);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
// If before is empty, we're at the start of a new argument
|
|
407
|
+
// If not, we're completing a partial argument
|
|
408
|
+
const completions = getCompletionsAtPosition(
|
|
409
|
+
generatorTree,
|
|
410
|
+
args,
|
|
411
|
+
generators,
|
|
412
|
+
);
|
|
413
|
+
|
|
414
|
+
if (process.env.SUMMON_DEBUG) {
|
|
415
|
+
console.error("[completion] completions:", completions);
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
reply(completions);
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
// Initialize omelette (this intercepts --completion args)
|
|
422
|
+
complete.init();
|
|
423
|
+
|
|
424
|
+
return complete;
|
|
425
|
+
};
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* Check if the current invocation is a completion request.
|
|
429
|
+
* This allows us to skip normal CLI processing for completion.
|
|
430
|
+
*/
|
|
431
|
+
export const isCompletionRequest = (): boolean => {
|
|
432
|
+
const args = process.argv;
|
|
433
|
+
return (
|
|
434
|
+
args.includes("--completion") ||
|
|
435
|
+
args.includes("--completion-fish") ||
|
|
436
|
+
args.includes("--compzsh") ||
|
|
437
|
+
args.includes("--compbash") ||
|
|
438
|
+
args.includes("--compfish") ||
|
|
439
|
+
// Omelette internal completion trigger
|
|
440
|
+
args.some((arg) => arg.startsWith("--compgen"))
|
|
441
|
+
);
|
|
442
|
+
};
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* Check if this is a setup or cleanup request.
|
|
446
|
+
*/
|
|
447
|
+
export const isSetupRequest = (): boolean => {
|
|
448
|
+
const args = process.argv;
|
|
449
|
+
return (
|
|
450
|
+
args.includes("--setup-completion") || args.includes("--cleanup-completion")
|
|
451
|
+
);
|
|
452
|
+
};
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* Handle setup/cleanup requests.
|
|
456
|
+
*/
|
|
457
|
+
export const handleSetupRequest = (complete: omelette.Instance): void => {
|
|
458
|
+
const args = process.argv;
|
|
459
|
+
|
|
460
|
+
if (args.includes("--setup-completion")) {
|
|
461
|
+
try {
|
|
462
|
+
complete.setupShellInitFile();
|
|
463
|
+
console.log("Shell completion installed successfully!");
|
|
464
|
+
console.log(
|
|
465
|
+
"Please restart your shell or run: source ~/.zshrc (or ~/.bashrc)",
|
|
466
|
+
);
|
|
467
|
+
} catch (err) {
|
|
468
|
+
console.error("Failed to install completion:", (err as Error).message);
|
|
469
|
+
console.error("\nManual installation:");
|
|
470
|
+
console.error(" Zsh: echo '. <(summon --completion)' >> ~/.zshrc");
|
|
471
|
+
console.error(
|
|
472
|
+
" Bash: summon --completion >> ~/.summon-completion.sh && echo 'source ~/.summon-completion.sh' >> ~/.bash_profile",
|
|
473
|
+
);
|
|
474
|
+
console.error(
|
|
475
|
+
" Fish: echo 'summon --completion-fish | source' >> ~/.config/fish/config.fish",
|
|
476
|
+
);
|
|
477
|
+
process.exit(1);
|
|
478
|
+
}
|
|
479
|
+
process.exit(0);
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
if (args.includes("--cleanup-completion")) {
|
|
483
|
+
try {
|
|
484
|
+
complete.cleanupShellInitFile();
|
|
485
|
+
console.log("Shell completion removed successfully!");
|
|
486
|
+
} catch (err) {
|
|
487
|
+
console.error("Failed to remove completion:", (err as Error).message);
|
|
488
|
+
process.exit(1);
|
|
489
|
+
}
|
|
490
|
+
process.exit(0);
|
|
491
|
+
}
|
|
492
|
+
};
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The wizard asks exactly `pendingPrompts(prompts, explicit)` (A9): with
|
|
3
|
+
* `askMissing`, explicitly provided answers are pre-seeded — shown as
|
|
4
|
+
* completed, never re-asked — and only the missing prompts are asked, in
|
|
5
|
+
* declared order, with conditional prompts unlocked by the answers as they
|
|
6
|
+
* land. Rendered with ink-testing-library against the real App.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { GeneratorDefinition } from "@canonical/summon-core";
|
|
10
|
+
import { pure, task } from "@canonical/task";
|
|
11
|
+
import { render } from "ink-testing-library";
|
|
12
|
+
import { describe, expect, it } from "vitest";
|
|
13
|
+
import { App } from "./App.js";
|
|
14
|
+
|
|
15
|
+
const generator: GeneratorDefinition = {
|
|
16
|
+
meta: {
|
|
17
|
+
name: "fixture/widget",
|
|
18
|
+
displayName: "widget",
|
|
19
|
+
description: "A fixture widget",
|
|
20
|
+
version: "0.0.1",
|
|
21
|
+
},
|
|
22
|
+
prompts: [
|
|
23
|
+
{ name: "title", type: "text", message: "Title:", default: "t" },
|
|
24
|
+
{
|
|
25
|
+
name: "withStories",
|
|
26
|
+
type: "confirm",
|
|
27
|
+
message: "Include stories?",
|
|
28
|
+
default: true,
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
name: "useTsStories",
|
|
32
|
+
type: "confirm",
|
|
33
|
+
message: "Use TypeScript stories?",
|
|
34
|
+
default: false,
|
|
35
|
+
when: (answers) => answers.withStories === true,
|
|
36
|
+
},
|
|
37
|
+
],
|
|
38
|
+
generate: () => task(pure(undefined)).unwrap(),
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const tick = (ms = 25) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Poll until a frame satisfies `check`, then return it.
|
|
45
|
+
*
|
|
46
|
+
* The confirm gate is NOT the mount commit — it is the tail of an async chain
|
|
47
|
+
* (`PromptSequence`'s `onComplete` -> `handlePromptsComplete` -> `generateTask`
|
|
48
|
+
* -> `setState({phase:"confirming"})`). A single fixed tick can therefore read a
|
|
49
|
+
* frame the gate has not painted yet, which is exactly what happened under
|
|
50
|
+
* suite parallelism: 2 failures in 84 loaded runs, 0 in 30 unloaded, and
|
|
51
|
+
* 216/216 when the file ran alone — i.e. visible only in the condition CI runs
|
|
52
|
+
* in. Lifted from `summon-core`'s `wizard.test.tsx`.
|
|
53
|
+
*/
|
|
54
|
+
const waitForFrame = async (
|
|
55
|
+
read: () => string | undefined,
|
|
56
|
+
check: (frame: string) => boolean,
|
|
57
|
+
timeout = 15_000,
|
|
58
|
+
): Promise<string> => {
|
|
59
|
+
const deadline = Date.now() + timeout;
|
|
60
|
+
let frame = read() ?? "";
|
|
61
|
+
while (!check(frame) && Date.now() < deadline) {
|
|
62
|
+
await tick(15);
|
|
63
|
+
frame = read() ?? "";
|
|
64
|
+
}
|
|
65
|
+
return frame;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
describe("App askMissing — the wizard asks only the pending prompts", () => {
|
|
69
|
+
it("shows provided answers as completed and asks the first missing prompt", async () => {
|
|
70
|
+
const { lastFrame, unmount } = render(
|
|
71
|
+
<App generator={generator} askMissing answers={{ title: "Widget" }} />,
|
|
72
|
+
);
|
|
73
|
+
await tick();
|
|
74
|
+
const frame = lastFrame() ?? "";
|
|
75
|
+
// The provided answer is in the completed table…
|
|
76
|
+
expect(frame).toContain("Title:");
|
|
77
|
+
expect(frame).toContain("Widget");
|
|
78
|
+
// …and the first PENDING prompt is being asked, not Title again. (The
|
|
79
|
+
// conditional third prompt joins the count only once stories are on.)
|
|
80
|
+
expect(frame).toContain("Include stories?");
|
|
81
|
+
expect(frame).toContain("Step 1 of 1");
|
|
82
|
+
unmount();
|
|
83
|
+
}, 20_000);
|
|
84
|
+
|
|
85
|
+
it("an answer unlocks a conditional prompt (collectAnswers parity)", async () => {
|
|
86
|
+
const { lastFrame, stdin, unmount } = render(
|
|
87
|
+
<App generator={generator} askMissing answers={{ title: "Widget" }} />,
|
|
88
|
+
);
|
|
89
|
+
await tick();
|
|
90
|
+
stdin.write("y"); // Include stories? -> yes
|
|
91
|
+
// The wizard's handler IS attached by the time this lands (measured: 0
|
|
92
|
+
// lost in 72 runs, idle and under load) — but the unlocked prompt can
|
|
93
|
+
// paint LATE, past one 25ms tick. So poll for the frame rather than
|
|
94
|
+
// guessing a tick, and do NOT re-write the `y`: unlike the esc below it
|
|
95
|
+
// is not idempotent — a second one would answer the prompt it unlocked.
|
|
96
|
+
const deadline = Date.now() + 15_000;
|
|
97
|
+
while (
|
|
98
|
+
!(lastFrame() ?? "").includes("Use TypeScript stories?") &&
|
|
99
|
+
Date.now() < deadline
|
|
100
|
+
) {
|
|
101
|
+
await tick();
|
|
102
|
+
}
|
|
103
|
+
expect(lastFrame()).toContain("Use TypeScript stories?");
|
|
104
|
+
unmount();
|
|
105
|
+
}, 20_000);
|
|
106
|
+
|
|
107
|
+
it("a fully-provided answer set goes straight to the confirm gate", async () => {
|
|
108
|
+
const { lastFrame, unmount } = render(
|
|
109
|
+
<App
|
|
110
|
+
generator={generator}
|
|
111
|
+
askMissing
|
|
112
|
+
answers={{ title: "Widget", withStories: false }}
|
|
113
|
+
/>,
|
|
114
|
+
);
|
|
115
|
+
const frame = await waitForFrame(lastFrame, (f) => f.includes("Proceed?"));
|
|
116
|
+
expect(frame).toContain("Proceed?");
|
|
117
|
+
expect(frame).not.toContain("Step 1");
|
|
118
|
+
unmount();
|
|
119
|
+
}, 20_000);
|
|
120
|
+
|
|
121
|
+
it("esc at the gate re-opens the wizard even when nothing was pending (regression)", async () => {
|
|
122
|
+
// A fully-explicit invocation goes straight to the gate; the gate
|
|
123
|
+
// advertises `esc to go back`, and going back must ASK — with the
|
|
124
|
+
// provided seed kept, the empty pending set would auto-complete straight
|
|
125
|
+
// back to the gate, a silent no-op. On esc the seed is cleared: every
|
|
126
|
+
// prompt is asked again, previous values pre-filled.
|
|
127
|
+
const { lastFrame, stdin, unmount } = render(
|
|
128
|
+
<App
|
|
129
|
+
generator={generator}
|
|
130
|
+
askMissing
|
|
131
|
+
answers={{ title: "Widget", withStories: false }}
|
|
132
|
+
/>,
|
|
133
|
+
);
|
|
134
|
+
expect(
|
|
135
|
+
await waitForFrame(lastFrame, (frame) => frame.includes("Proceed?")),
|
|
136
|
+
).toContain("Proceed?");
|
|
137
|
+
// Same rule as App.confirmGate.test.tsx:55-70, same shape: Ink attaches
|
|
138
|
+
// its stdin listener asynchronously in a fresh worker (measured here: a
|
|
139
|
+
// cold worker's first render loses the esc outright — 5 of 72 — and no
|
|
140
|
+
// amount of waiting recovers one), so the esc is re-written until the
|
|
141
|
+
// gate lets go. Extra escapes are harmless — measured: the re-opened
|
|
142
|
+
// wizard sits at step 1 with an empty history, so a redundant esc exits
|
|
143
|
+
// the App without disturbing the frame asserted below.
|
|
144
|
+
const deadline = Date.now() + 15_000;
|
|
145
|
+
while (
|
|
146
|
+
!(lastFrame() ?? "").includes("Step 1 of 2") &&
|
|
147
|
+
Date.now() < deadline
|
|
148
|
+
) {
|
|
149
|
+
stdin.write("\u001B"); // esc
|
|
150
|
+
await tick(50);
|
|
151
|
+
}
|
|
152
|
+
const frame = lastFrame() ?? "";
|
|
153
|
+
expect(frame).toContain("Step 1 of 2");
|
|
154
|
+
expect(frame).toContain("Title:");
|
|
155
|
+
// The PRE-FILL, which is the behaviour the comment above promises and
|
|
156
|
+
// nothing here pinned: `Title:` is the active prompt's MESSAGE and renders
|
|
157
|
+
// whether or not a value carried over. The carried value renders beneath
|
|
158
|
+
// it. The round-22 review proved this by break + control — setting
|
|
159
|
+
// `initialAnswers={undefined}` left this cell green until this line existed.
|
|
160
|
+
expect(frame).toContain("Widget");
|
|
161
|
+
expect(frame).not.toContain("Proceed?");
|
|
162
|
+
unmount();
|
|
163
|
+
}, 20_000);
|
|
164
|
+
});
|