@canonical/summon 0.17.1 → 0.20.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/generators/example/hello/index.ts +3 -9
- package/generators/example/webapp/index.ts +7 -5
- package/generators/init/index.ts +3 -8
- package/generators/init/templates/generator.ts.ejs +3 -9
- package/package.json +24 -27
- package/src/bin.tsx +199 -0
- package/src/completion.ts +4 -1
- package/src/components/App.tsx +108 -15
- package/src/components/ExecutionProgress.tsx +8 -4
- package/src/components/FileTreePreview.tsx +1 -1
- package/src/components/PromptSequence.tsx +1 -1
- package/src/discovery/loadGenerator.ts +37 -0
- package/src/display/index.ts +2 -0
- package/src/display/printGeneratorHelp.ts +133 -0
- package/src/display/printNode.ts +76 -0
- package/src/registration/buildCommandBarrel.ts +57 -0
- package/src/registration/index.ts +3 -0
- package/src/registration/registerFromBarrel.tsx +548 -0
- package/src/registration/types.ts +28 -0
- package/README.md +0 -446
- package/src/__tests__/combinators.test.ts +0 -895
- package/src/__tests__/dry-run.test.ts +0 -927
- package/src/__tests__/effect.test.ts +0 -816
- package/src/__tests__/interpreter.test.ts +0 -673
- package/src/__tests__/primitives.test.ts +0 -970
- package/src/__tests__/task.test.ts +0 -929
- package/src/__tests__/template.test.ts +0 -666
- package/src/cli-format.ts +0 -236
- package/src/cli-types.ts +0 -53
- package/src/cli.tsx +0 -1366
- package/src/combinators.ts +0 -294
- package/src/dry-run.ts +0 -434
- package/src/effect.ts +0 -224
- package/src/index.ts +0 -266
- package/src/interpreter.ts +0 -463
- package/src/primitives.ts +0 -442
- package/src/task.ts +0 -245
- package/src/template.ts +0 -537
- package/src/types.ts +0 -453
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Print detailed help for a generator (meta.help and examples).
|
|
3
|
+
*
|
|
4
|
+
* When `llmMode` is true, outputs structured markdown help via formatLlmHelp.
|
|
5
|
+
*
|
|
6
|
+
* @note Impure — writes to stdout, loads generators via dynamic import.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { formatGeneratorLlmHelp } from "@canonical/cli-core";
|
|
10
|
+
import type { GeneratorNode } from "@canonical/summon-core";
|
|
11
|
+
import chalk from "chalk";
|
|
12
|
+
import loadGenerator from "../discovery/loadGenerator.js";
|
|
13
|
+
import printNode from "./printNode.js";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Convert camelCase to kebab-case.
|
|
17
|
+
*/
|
|
18
|
+
const toKebabCase = (str: string): string =>
|
|
19
|
+
str.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();
|
|
20
|
+
|
|
21
|
+
export default async function printGeneratorHelp(
|
|
22
|
+
node: GeneratorNode,
|
|
23
|
+
pathSegments: string[],
|
|
24
|
+
llmMode = false,
|
|
25
|
+
): Promise<void> {
|
|
26
|
+
if (!node.indexPath) return;
|
|
27
|
+
|
|
28
|
+
try {
|
|
29
|
+
const generator = await loadGenerator(node.indexPath);
|
|
30
|
+
const { meta, prompts } = generator;
|
|
31
|
+
const commandPath = pathSegments.join(" ");
|
|
32
|
+
|
|
33
|
+
// LLM mode: output structured markdown help and return
|
|
34
|
+
if (llmMode) {
|
|
35
|
+
process.stdout.write(formatGeneratorLlmHelp(generator, commandPath));
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
console.log();
|
|
40
|
+
console.log(chalk.bold.cyan(`summon ${commandPath}`));
|
|
41
|
+
console.log(chalk.dim(`v${meta.version}`));
|
|
42
|
+
console.log();
|
|
43
|
+
console.log(meta.description);
|
|
44
|
+
|
|
45
|
+
// Print extended help if available
|
|
46
|
+
if (meta.help) {
|
|
47
|
+
console.log();
|
|
48
|
+
console.log(meta.help);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Print available options (from prompts)
|
|
52
|
+
if (prompts.length > 0) {
|
|
53
|
+
console.log();
|
|
54
|
+
console.log(chalk.bold("Options:"));
|
|
55
|
+
console.log();
|
|
56
|
+
for (const prompt of prompts) {
|
|
57
|
+
const flagName = `--${toKebabCase(prompt.name)}`;
|
|
58
|
+
const typeHint =
|
|
59
|
+
prompt.type === "confirm"
|
|
60
|
+
? "[boolean]"
|
|
61
|
+
: prompt.type === "select"
|
|
62
|
+
? `[${prompt.choices?.map((c) => c.value).join("|")}]`
|
|
63
|
+
: prompt.type === "multiselect"
|
|
64
|
+
? "[value,value,...]"
|
|
65
|
+
: "<value>";
|
|
66
|
+
|
|
67
|
+
const defaultHint =
|
|
68
|
+
prompt.default !== undefined
|
|
69
|
+
? chalk.dim(` (default: ${JSON.stringify(prompt.default)})`)
|
|
70
|
+
: "";
|
|
71
|
+
|
|
72
|
+
console.log(` ${chalk.cyan(flagName)} ${typeHint}${defaultHint}`);
|
|
73
|
+
console.log(` ${prompt.message}`);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Print examples if available
|
|
78
|
+
if (meta.examples && meta.examples.length > 0) {
|
|
79
|
+
console.log();
|
|
80
|
+
console.log(chalk.bold("Examples:"));
|
|
81
|
+
console.log();
|
|
82
|
+
for (const example of meta.examples) {
|
|
83
|
+
console.log(` ${chalk.dim("$")} ${example}`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Print available subtopics if any
|
|
88
|
+
if (node.children.size > 0) {
|
|
89
|
+
console.log();
|
|
90
|
+
console.log(chalk.bold("Subtopics:"));
|
|
91
|
+
for (const [name, child] of node.children) {
|
|
92
|
+
const desc = child.meta?.description ?? "";
|
|
93
|
+
console.log(` ${chalk.cyan(name)}${desc ? ` - ${desc}` : ""}`);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// LLM Usage section
|
|
98
|
+
console.log();
|
|
99
|
+
console.log(chalk.bold("LLM Usage:"));
|
|
100
|
+
console.log(chalk.dim(" # Preview what will be generated (markdown):"));
|
|
101
|
+
console.log(
|
|
102
|
+
chalk.dim(
|
|
103
|
+
` summon ${commandPath} ${prompts
|
|
104
|
+
.filter((p) => !p.when && p.default === undefined)
|
|
105
|
+
.map((p) => `--${toKebabCase(p.name)} <value>`)
|
|
106
|
+
.join(" ")} --llm`,
|
|
107
|
+
),
|
|
108
|
+
);
|
|
109
|
+
console.log(chalk.dim(" # Preview (JSON):"));
|
|
110
|
+
console.log(
|
|
111
|
+
chalk.dim(
|
|
112
|
+
` summon ${commandPath} ${prompts
|
|
113
|
+
.filter((p) => !p.when && p.default === undefined)
|
|
114
|
+
.map((p) => `--${toKebabCase(p.name)} <value>`)
|
|
115
|
+
.join(" ")} --format json`,
|
|
116
|
+
),
|
|
117
|
+
);
|
|
118
|
+
console.log(chalk.dim(" # Execute directly:"));
|
|
119
|
+
console.log(
|
|
120
|
+
chalk.dim(
|
|
121
|
+
` summon ${commandPath} ${prompts
|
|
122
|
+
.filter((p) => !p.when && p.default === undefined)
|
|
123
|
+
.map((p) => `--${toKebabCase(p.name)} <value>`)
|
|
124
|
+
.join(" ")} --yes`,
|
|
125
|
+
),
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
console.log();
|
|
129
|
+
} catch {
|
|
130
|
+
// Couldn't load generator, fall back to basic node printing
|
|
131
|
+
printNode(node, pathSegments);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Print available generators/sub-generators at a tree node.
|
|
3
|
+
*
|
|
4
|
+
* @note Impure — writes to stdout.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { GeneratorNode, GeneratorOrigin } from "@canonical/summon-core";
|
|
8
|
+
import chalk from "chalk";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Format origin badge for display.
|
|
12
|
+
*/
|
|
13
|
+
const formatOrigin = (origin?: GeneratorOrigin): string => {
|
|
14
|
+
switch (origin) {
|
|
15
|
+
case "local":
|
|
16
|
+
return chalk.green("[local]");
|
|
17
|
+
case "global":
|
|
18
|
+
return chalk.blue("[global]");
|
|
19
|
+
case "package":
|
|
20
|
+
return chalk.magenta("[pkg]");
|
|
21
|
+
case "builtin":
|
|
22
|
+
return chalk.dim("[builtin]");
|
|
23
|
+
default:
|
|
24
|
+
return "";
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export default function printNode(
|
|
29
|
+
node: GeneratorNode,
|
|
30
|
+
pathSegments: string[],
|
|
31
|
+
): void {
|
|
32
|
+
const prefix = pathSegments.length > 0 ? `${pathSegments.join(" ")} ` : "";
|
|
33
|
+
const isRoot = pathSegments.length === 0;
|
|
34
|
+
|
|
35
|
+
if (node.children.size === 0) {
|
|
36
|
+
console.log(chalk.yellow("No generators found"));
|
|
37
|
+
console.log(chalk.dim("\nInstall a generator package:"));
|
|
38
|
+
console.log(chalk.dim(" bun add @scope/summon-<name>"));
|
|
39
|
+
console.log(chalk.dim("\nOr link globally (from the package directory):"));
|
|
40
|
+
console.log(chalk.dim(" bun link # for bun users"));
|
|
41
|
+
console.log(chalk.dim(" npm link # for npm users"));
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (isRoot) {
|
|
46
|
+
console.log(chalk.bold("\nAvailable topics:\n"));
|
|
47
|
+
} else {
|
|
48
|
+
console.log(chalk.bold(`\nAvailable under '${pathSegments.join(" ")}':\n`));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
for (const [name, child] of node.children) {
|
|
52
|
+
const hasChildren = child.children.size > 0;
|
|
53
|
+
const isRunnable = !!child.indexPath;
|
|
54
|
+
const originBadge = child.origin ? ` ${formatOrigin(child.origin)}` : "";
|
|
55
|
+
|
|
56
|
+
let suffix = "";
|
|
57
|
+
if (hasChildren && isRunnable) {
|
|
58
|
+
suffix = chalk.dim(" (runnable, has subtopics)");
|
|
59
|
+
} else if (hasChildren) {
|
|
60
|
+
suffix = chalk.dim(" (has subtopics)");
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
console.log(chalk.cyan(` ${name}`) + originBadge + suffix);
|
|
64
|
+
|
|
65
|
+
// Show immediate children as hints
|
|
66
|
+
if (hasChildren) {
|
|
67
|
+
const childNames = [...child.children.keys()].slice(0, 5);
|
|
68
|
+
const more =
|
|
69
|
+
child.children.size > 5 ? `, +${child.children.size - 5} more` : "";
|
|
70
|
+
console.log(chalk.dim(` └─ ${childNames.join(", ")}${more}`));
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
console.log(chalk.dim(`\nUsage: summon ${prefix}<topic>`));
|
|
75
|
+
console.log();
|
|
76
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Build a flat command list from a generator tree.
|
|
3
|
+
*
|
|
4
|
+
* Flattens the tree into a list of {@link CommandEntry} sorted by depth
|
|
5
|
+
* so that parents are registered before children.
|
|
6
|
+
*
|
|
7
|
+
* @note Impure — loads generators via dynamic import.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { GeneratorNode } from "@canonical/summon-core";
|
|
11
|
+
import chalk from "chalk";
|
|
12
|
+
import loadGenerator from "../discovery/loadGenerator.js";
|
|
13
|
+
import type { CommandEntry } from "./types.js";
|
|
14
|
+
|
|
15
|
+
export default async function buildCommandBarrel(
|
|
16
|
+
node: GeneratorNode,
|
|
17
|
+
pathSegments: string[] = [],
|
|
18
|
+
): Promise<CommandEntry[]> {
|
|
19
|
+
const entries: CommandEntry[] = [];
|
|
20
|
+
|
|
21
|
+
for (const [name, child] of node.children) {
|
|
22
|
+
const childPath = [...pathSegments, name];
|
|
23
|
+
|
|
24
|
+
if (child.indexPath) {
|
|
25
|
+
// Runnable generator
|
|
26
|
+
try {
|
|
27
|
+
const generator = await loadGenerator(child.indexPath);
|
|
28
|
+
entries.push({ path: childPath, generator });
|
|
29
|
+
|
|
30
|
+
// If it also has children, we need to ensure parent exists and recurse
|
|
31
|
+
if (child.children.size > 0) {
|
|
32
|
+
const childEntries = await buildCommandBarrel(child, childPath);
|
|
33
|
+
entries.push(...childEntries);
|
|
34
|
+
}
|
|
35
|
+
} catch (err) {
|
|
36
|
+
console.error(
|
|
37
|
+
chalk.yellow(`Warning: Could not load generator '${name}':`),
|
|
38
|
+
(err as Error).message,
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
} else if (child.children.size > 0) {
|
|
42
|
+
// Namespace-only (no indexPath but has children)
|
|
43
|
+
// Add a placeholder entry so we create the parent command
|
|
44
|
+
entries.push({
|
|
45
|
+
path: childPath,
|
|
46
|
+
description: `${name} generators`,
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// Recurse into children
|
|
50
|
+
const childEntries = await buildCommandBarrel(child, childPath);
|
|
51
|
+
entries.push(...childEntries);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Sort by path length so parents are registered before children
|
|
56
|
+
return entries.sort((a, b) => a.path.length - b.path.length);
|
|
57
|
+
}
|