@canonical/summon 0.17.1 → 0.21.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.
Files changed (41) hide show
  1. package/generators/example/hello/index.ts +3 -9
  2. package/generators/example/webapp/index.ts +7 -5
  3. package/generators/init/index.ts +3 -8
  4. package/generators/init/templates/generator.ts.ejs +3 -9
  5. package/package.json +25 -27
  6. package/src/bin.tsx +199 -0
  7. package/src/completion.test.ts +202 -0
  8. package/src/completion.ts +4 -1
  9. package/src/components/App.tsx +108 -15
  10. package/src/components/ExecutionProgress.tsx +8 -4
  11. package/src/components/FileTreePreview.tsx +1 -1
  12. package/src/components/PromptSequence.tsx +1 -1
  13. package/src/discovery/index.ts +1 -0
  14. package/src/discovery/loadGenerator.ts +37 -0
  15. package/src/display/index.ts +2 -0
  16. package/src/display/printGeneratorHelp.ts +128 -0
  17. package/src/display/printNode.ts +76 -0
  18. package/src/registration/buildCommandBarrel.ts +57 -0
  19. package/src/registration/index.ts +3 -0
  20. package/src/registration/registerFromBarrel.tsx +548 -0
  21. package/src/registration/types.ts +28 -0
  22. package/README.md +0 -446
  23. package/src/__tests__/combinators.test.ts +0 -895
  24. package/src/__tests__/dry-run.test.ts +0 -927
  25. package/src/__tests__/effect.test.ts +0 -816
  26. package/src/__tests__/interpreter.test.ts +0 -673
  27. package/src/__tests__/primitives.test.ts +0 -970
  28. package/src/__tests__/task.test.ts +0 -929
  29. package/src/__tests__/template.test.ts +0 -666
  30. package/src/cli-format.ts +0 -236
  31. package/src/cli-types.ts +0 -53
  32. package/src/cli.tsx +0 -1366
  33. package/src/combinators.ts +0 -294
  34. package/src/dry-run.ts +0 -434
  35. package/src/effect.ts +0 -224
  36. package/src/index.ts +0 -266
  37. package/src/interpreter.ts +0 -463
  38. package/src/primitives.ts +0 -442
  39. package/src/task.ts +0 -245
  40. package/src/template.ts +0 -537
  41. package/src/types.ts +0 -453
@@ -8,15 +8,9 @@
8
8
  */
9
9
 
10
10
  import * as path from "node:path";
11
- import {
12
- info,
13
- mkdir,
14
- sequence_,
15
- template,
16
- when,
17
- withHelpers,
18
- } from "../../../src/index.js";
19
- import type { GeneratorDefinition } from "../../../src/types.js";
11
+ import type { GeneratorDefinition } from "@canonical/summon-core";
12
+ import { template, withHelpers } from "@canonical/summon-core";
13
+ import { info, mkdir, sequence_, when } from "@canonical/task";
20
14
 
21
15
  // =============================================================================
22
16
  // Types
@@ -9,29 +9,31 @@
9
9
  */
10
10
 
11
11
  import * as path from "node:path";
12
+ import type { GeneratorDefinition } from "@canonical/summon-core";
13
+ import { template, withHelpers } from "@canonical/summon-core";
14
+ import type { Task } from "@canonical/task";
12
15
  import {
13
16
  appendFile,
14
17
  debug,
15
18
  exec,
16
19
  exists,
20
+ flatMap,
17
21
  getContext,
18
22
  ifElse,
19
23
  info,
24
+ map,
20
25
  mkdir,
21
26
  orElse,
22
27
  parallel,
28
+ pure,
23
29
  readFile,
24
30
  sequence_,
25
31
  setContext,
26
- template,
27
32
  traverse_,
28
33
  warn,
29
34
  when,
30
- withHelpers,
31
35
  writeFile,
32
- } from "../../../src/index.js";
33
- import { flatMap, map, pure } from "../../../src/task.js";
34
- import type { GeneratorDefinition, Task } from "../../../src/types.js";
36
+ } from "@canonical/task";
35
37
 
36
38
  // =============================================================================
37
39
  // Types
@@ -6,14 +6,9 @@
6
6
 
7
7
  import * as path from "node:path";
8
8
  import { fileURLToPath } from "node:url";
9
- import type { GeneratorDefinition } from "../../src/index.js";
10
- import {
11
- info,
12
- mkdir,
13
- sequence_,
14
- template,
15
- withHelpers,
16
- } from "../../src/index.js";
9
+ import type { GeneratorDefinition } from "@canonical/summon-core";
10
+ import { template, withHelpers } from "@canonical/summon-core";
11
+ import { info, mkdir, sequence_ } from "@canonical/task";
17
12
 
18
13
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
19
14
 
@@ -6,15 +6,9 @@
6
6
 
7
7
  import * as path from "node:path";
8
8
  import { fileURLToPath } from "node:url";
9
- import type { GeneratorDefinition } from "@canonical/summon";
10
- import {
11
- info,
12
- mkdir,
13
- sequence_,
14
- template,
15
- when,
16
- withHelpers,
17
- } from "@canonical/summon";
9
+ import type { GeneratorDefinition } from "@canonical/summon-core";
10
+ import { template, withHelpers } from "@canonical/summon-core";
11
+ import { info, mkdir, sequence_, when } from "@canonical/task";
18
12
 
19
13
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
20
14
 
package/package.json CHANGED
@@ -1,12 +1,11 @@
1
1
  {
2
2
  "name": "@canonical/summon",
3
- "description": "A monadic task-centric code generator framework with React Ink CLI.",
4
- "version": "0.17.1",
3
+ "description": "Summon CLI binary: interactive code generator with Ink UI, shell completion, and Commander registration.",
4
+ "version": "0.21.0",
5
5
  "type": "module",
6
- "module": "src/index.ts",
7
- "types": "src/index.ts",
6
+ "module": "src/bin.tsx",
8
7
  "bin": {
9
- "summon": "src/cli.tsx"
8
+ "summon": "src/bin.tsx"
10
9
  },
11
10
  "files": [
12
11
  "src",
@@ -28,40 +27,39 @@
28
27
  "scripts": {
29
28
  "build": "echo 'No build needed - runs directly from TypeScript'",
30
29
  "build:all": "bun run build",
31
- "check": "bun run check:biome && bun run check:ts && bun run check:webarchitect",
30
+ "check": "bun run check:biome && bun run check:ts",
32
31
  "check:fix": "bun run check:biome:fix && bun run check:ts",
33
32
  "check:biome": "biome check",
34
33
  "check:biome:fix": "biome check --write",
35
34
  "check:ts": "tsc --noEmit",
36
- "check:webarchitect": "webarchitect tool-ts",
37
- "test": "vitest run",
38
- "test:watch": "vitest",
39
- "test:coverage": "vitest run --coverage"
40
- },
41
- "devDependencies": {
42
- "@biomejs/biome": "2.4.5",
43
- "@canonical/biome-config": "^0.17.1",
44
- "@canonical/typescript-config": "^0.17.1",
45
- "@canonical/webarchitect": "^0.17.1",
46
- "@types/bun": "^1.3.8",
47
- "@types/ejs": "^3.1.5",
48
- "@types/node": "^24.10.13",
49
- "@types/omelette": "^0.4.5",
50
- "@types/react": "^19.2.13",
51
- "ink-testing-library": "^4.0.0",
52
- "typescript": "^5.9.3",
53
- "vitest": "^4.0.18"
35
+ "test": "vitest run --passWithNoTests",
36
+ "test:watch": "vitest"
54
37
  },
55
38
  "dependencies": {
39
+ "@canonical/cli-core": "^0.21.0",
40
+ "@canonical/summon-core": "^0.21.0",
41
+ "@canonical/task": "^0.21.0",
42
+ "@canonical/utils": "^0.21.0",
56
43
  "chalk": "^5.6.2",
57
44
  "commander": "^14.0.3",
58
- "ejs": "^4.0.1",
59
- "ink": "^6.7.0",
45
+ "ink": "^6.8.0",
60
46
  "ink-select-input": "^6.2.0",
61
47
  "ink-spinner": "^5.0.0",
62
48
  "ink-text-input": "^6.0.0",
63
49
  "omelette": "^0.4.17",
64
50
  "react": "^19.2.4"
65
51
  },
66
- "gitHead": "e3eaaa9efdb58fa72acb8fdd3c7dffa25a1341c4"
52
+ "devDependencies": {
53
+ "@biomejs/biome": "2.4.9",
54
+ "@canonical/biome-config": "^0.21.0",
55
+ "@canonical/typescript-config": "^0.21.0",
56
+ "@types/bun": "^1.3.10",
57
+ "@types/node": "^24.12.0",
58
+ "@types/omelette": "^0.4.5",
59
+ "@types/react": "^19.2.14",
60
+ "ink-testing-library": "^4.0.0",
61
+ "typescript": "^5.9.3",
62
+ "vitest": "^4.0.18"
63
+ },
64
+ "gitHead": "4a39c954e7bbad4903397f8371f801982034a26d"
67
65
  }
package/src/bin.tsx ADDED
@@ -0,0 +1,199 @@
1
+ #!/usr/bin/env bun
2
+
3
+ /**
4
+ * Summon CLI — thin orchestrator.
5
+ *
6
+ * Wires together generator discovery, display, registration, and completion.
7
+ * All domain logic lives in the corresponding modules.
8
+ *
9
+ * @note Impure — CLI entry point.
10
+ */
11
+
12
+ import * as path from "node:path";
13
+ import {
14
+ discoverGeneratorTree,
15
+ type GeneratorNode,
16
+ } from "@canonical/summon-core";
17
+ import chalk from "chalk";
18
+ import { Command } from "commander";
19
+ import {
20
+ type CompletionNode,
21
+ handleSetupRequest,
22
+ initCompletion,
23
+ isCompletionRequest,
24
+ isSetupRequest,
25
+ } from "./completion.js";
26
+ import { loadGenerator } from "./discovery/index.js";
27
+ import { printGeneratorHelp, printNode } from "./display/index.js";
28
+ import {
29
+ buildCommandBarrel,
30
+ registerFromBarrel,
31
+ } from "./registration/index.js";
32
+
33
+ // =============================================================================
34
+ // Helpers
35
+ // =============================================================================
36
+
37
+ /**
38
+ * Navigate the generator tree by path segments.
39
+ * Returns [node, remainingSegments] where node is as deep as we could go.
40
+ */
41
+ const navigateTree = (
42
+ root: GeneratorNode,
43
+ segments: string[],
44
+ ): [GeneratorNode, string[]] => {
45
+ let current = root;
46
+ let i = 0;
47
+
48
+ for (; i < segments.length; i++) {
49
+ const child = current.children.get(segments[i]);
50
+ if (!child) break;
51
+ current = child;
52
+ }
53
+
54
+ return [current, segments.slice(i)];
55
+ };
56
+
57
+ /**
58
+ * Convert GeneratorNode to CompletionNode for the completion module.
59
+ */
60
+ const toCompletionNode = (node: GeneratorNode): CompletionNode => ({
61
+ name: node.name,
62
+ indexPath: node.indexPath,
63
+ children: new Map(
64
+ [...node.children.entries()].map(([name, child]) => [
65
+ name,
66
+ toCompletionNode(child),
67
+ ]),
68
+ ),
69
+ });
70
+
71
+ /**
72
+ * Register all generator commands from the tree.
73
+ * Uses the barrel pattern: build flat list → sort by depth → register in order.
74
+ */
75
+ const registerGeneratorCommands = async (
76
+ parentCmd: Command,
77
+ node: GeneratorNode,
78
+ ): Promise<void> => {
79
+ const barrel = await buildCommandBarrel(node);
80
+ registerFromBarrel(parentCmd, barrel);
81
+ };
82
+
83
+ // =============================================================================
84
+ // Main
85
+ // =============================================================================
86
+
87
+ const program = new Command();
88
+
89
+ const main = async () => {
90
+ // Get generators path from args early (before full parse)
91
+ const generatorsIdx = process.argv.indexOf("--generators");
92
+ const gIdx = process.argv.indexOf("-g");
93
+ const generatorsPath =
94
+ generatorsIdx !== -1
95
+ ? process.argv[generatorsIdx + 1]
96
+ : gIdx !== -1
97
+ ? process.argv[gIdx + 1]
98
+ : undefined;
99
+
100
+ // Discover generators (needed for both completion and normal CLI)
101
+ const builtinDir = path.join(__dirname, "..", "generators");
102
+ const root = await discoverGeneratorTree(
103
+ generatorsPath ? { explicitPath: generatorsPath } : { builtinDir },
104
+ );
105
+
106
+ // Initialize shell completion (must happen before commander parsing)
107
+ if (isCompletionRequest() || isSetupRequest()) {
108
+ const completionTree = toCompletionNode(root);
109
+ const complete = await initCompletion(completionTree, loadGenerator);
110
+
111
+ // Handle setup/cleanup requests
112
+ if (isSetupRequest()) {
113
+ handleSetupRequest(complete);
114
+ return;
115
+ }
116
+
117
+ // For completion requests, omelette handles everything via init()
118
+ return;
119
+ }
120
+
121
+ program
122
+ .name("summon")
123
+ .description("A monadic task-centric code generator framework")
124
+ .version("0.1.0")
125
+ .option(
126
+ "-g, --generators <path>",
127
+ "Load generators ONLY from this path (for testing)",
128
+ )
129
+ .option("--setup-completion", "Install shell autocompletion")
130
+ .option("--cleanup-completion", "Remove shell autocompletion");
131
+
132
+ // If no arguments, show available topics
133
+ if (process.argv.length === 2) {
134
+ printNode(root, []);
135
+ console.log(
136
+ chalk.dim(
137
+ "Tip: Run 'summon --setup-completion' to enable TAB completion\n",
138
+ ),
139
+ );
140
+ return;
141
+ }
142
+
143
+ // Intercept --help --llm (or --help -l) for structured markdown help
144
+ const hasHelp =
145
+ process.argv.includes("--help") || process.argv.includes("-h");
146
+ const hasLlm =
147
+ process.argv.includes("--llm") ||
148
+ process.argv.includes("-l") ||
149
+ process.env.SUMMON_LLM === "1";
150
+ if (hasHelp && hasLlm) {
151
+ // Extract command path segments (skip flags and flag arguments)
152
+ const skipFlags = new Set([
153
+ "--help",
154
+ "-h",
155
+ "--llm",
156
+ "-l",
157
+ "--generators",
158
+ "-g",
159
+ "--format",
160
+ ]);
161
+ const args = process.argv.slice(2);
162
+ const segments: string[] = [];
163
+ let skipNext = false;
164
+ for (const arg of args) {
165
+ if (skipNext) {
166
+ skipNext = false;
167
+ continue;
168
+ }
169
+ if (skipFlags.has(arg)) {
170
+ if (arg === "--generators" || arg === "-g" || arg === "--format")
171
+ skipNext = true;
172
+ continue;
173
+ }
174
+ if (arg.startsWith("-")) continue;
175
+ segments.push(arg);
176
+ }
177
+
178
+ if (segments.length > 0) {
179
+ const [node] = navigateTree(root, segments);
180
+ if (node.indexPath) {
181
+ await printGeneratorHelp(node, segments, true);
182
+ return;
183
+ }
184
+ }
185
+ // Fall through to normal help if we can't find the generator
186
+ }
187
+
188
+ // Register generator commands dynamically
189
+ await registerGeneratorCommands(program, root);
190
+
191
+ // Parse and execute
192
+ program.parse();
193
+ };
194
+
195
+ // Run main
196
+ main().catch((err) => {
197
+ console.error(chalk.red("Error:"), err.message);
198
+ process.exit(1);
199
+ });
@@ -0,0 +1,202 @@
1
+ /**
2
+ * Tests for shell autocompletion utilities
3
+ */
4
+
5
+ import type { PromptDefinition } from "@canonical/summon-core";
6
+ import { describe, expect, it } from "vitest";
7
+ import { getArgumentCompletions, isPathPrompt } from "./completion.js";
8
+
9
+ // =============================================================================
10
+ // isPathPrompt
11
+ // =============================================================================
12
+
13
+ describe("isPathPrompt", () => {
14
+ it("detects path in prompt name", () => {
15
+ expect(
16
+ isPathPrompt({ name: "componentPath", type: "text", message: "Enter:" }),
17
+ ).toBe(true);
18
+ });
19
+
20
+ it("detects dir in prompt name", () => {
21
+ expect(
22
+ isPathPrompt({ name: "outputDir", type: "text", message: "Enter:" }),
23
+ ).toBe(true);
24
+ });
25
+
26
+ it("detects directory in prompt name", () => {
27
+ expect(
28
+ isPathPrompt({
29
+ name: "outputDirectory",
30
+ type: "text",
31
+ message: "Enter:",
32
+ }),
33
+ ).toBe(true);
34
+ });
35
+
36
+ it("detects file in prompt name", () => {
37
+ expect(
38
+ isPathPrompt({ name: "configFile", type: "text", message: "Enter:" }),
39
+ ).toBe(true);
40
+ });
41
+
42
+ it("detects folder in prompt message", () => {
43
+ expect(
44
+ isPathPrompt({
45
+ name: "output",
46
+ type: "text",
47
+ message: "Choose output folder:",
48
+ }),
49
+ ).toBe(true);
50
+ });
51
+
52
+ it("detects location in prompt message", () => {
53
+ expect(
54
+ isPathPrompt({
55
+ name: "output",
56
+ type: "text",
57
+ message: "File location:",
58
+ }),
59
+ ).toBe(true);
60
+ });
61
+
62
+ it("returns false for non-path prompts", () => {
63
+ expect(
64
+ isPathPrompt({ name: "name", type: "text", message: "Package name:" }),
65
+ ).toBe(false);
66
+ });
67
+
68
+ it("returns false for confirm prompts", () => {
69
+ expect(
70
+ isPathPrompt({
71
+ name: "withStyles",
72
+ type: "confirm",
73
+ message: "Include styles?",
74
+ }),
75
+ ).toBe(false);
76
+ });
77
+
78
+ it("is case-insensitive", () => {
79
+ expect(
80
+ isPathPrompt({ name: "MyPath", type: "text", message: "Enter:" }),
81
+ ).toBe(true);
82
+ });
83
+ });
84
+
85
+ // =============================================================================
86
+ // getArgumentCompletions
87
+ // =============================================================================
88
+
89
+ describe("getArgumentCompletions", () => {
90
+ const prompts: PromptDefinition[] = [
91
+ { name: "componentPath", type: "text", message: "Component path:" },
92
+ {
93
+ name: "withStyles",
94
+ type: "confirm",
95
+ message: "Include styles?",
96
+ default: true,
97
+ },
98
+ {
99
+ name: "withStories",
100
+ type: "confirm",
101
+ message: "Include stories?",
102
+ default: true,
103
+ },
104
+ {
105
+ name: "type",
106
+ type: "select",
107
+ message: "Package type:",
108
+ choices: [
109
+ { value: "library", label: "Library" },
110
+ { value: "tool-ts", label: "Tool" },
111
+ { value: "css", label: "CSS" },
112
+ ],
113
+ },
114
+ ];
115
+
116
+ it("returns flag completions when completing a flag name", () => {
117
+ const completions = getArgumentCompletions(
118
+ prompts,
119
+ "summon test --comp",
120
+ "--comp",
121
+ );
122
+
123
+ expect(completions).toContain("--component-path");
124
+ });
125
+
126
+ it("shows --no-X for confirm prompts with default true", () => {
127
+ const completions = getArgumentCompletions(
128
+ prompts,
129
+ "summon test --no-",
130
+ "--no-",
131
+ );
132
+
133
+ expect(completions).toContain("--no-with-styles");
134
+ expect(completions).toContain("--no-with-stories");
135
+ });
136
+
137
+ it("returns select choices when completing a flag value", () => {
138
+ const completions = getArgumentCompletions(
139
+ prompts,
140
+ "summon test --type=",
141
+ "",
142
+ );
143
+
144
+ expect(completions).toContain("library");
145
+ expect(completions).toContain("tool-ts");
146
+ expect(completions).toContain("css");
147
+ });
148
+
149
+ it("filters select choices by partial value", () => {
150
+ const completions = getArgumentCompletions(
151
+ prompts,
152
+ "summon test --type=lib",
153
+ "",
154
+ );
155
+
156
+ expect(completions).toContain("library");
157
+ expect(completions).not.toContain("tool-ts");
158
+ expect(completions).not.toContain("css");
159
+ });
160
+
161
+ it("includes built-in flags", () => {
162
+ const completions = getArgumentCompletions(
163
+ prompts,
164
+ "summon test --dry",
165
+ "--dry",
166
+ );
167
+
168
+ expect(completions).toContain("--dry-run");
169
+ });
170
+
171
+ it("shows all flags when showAll is true", () => {
172
+ const completions = getArgumentCompletions(
173
+ prompts,
174
+ "summon test",
175
+ "",
176
+ true,
177
+ );
178
+
179
+ expect(completions).toContain("--component-path");
180
+ expect(completions).toContain("--dry-run");
181
+ expect(completions).toContain("--help");
182
+ });
183
+
184
+ it("returns empty array when no matches", () => {
185
+ const completions = getArgumentCompletions(
186
+ prompts,
187
+ "summon test --zzz",
188
+ "--zzz",
189
+ );
190
+
191
+ expect(completions).toHaveLength(0);
192
+ });
193
+
194
+ it("includes --yes and --verbose in built-in flags", () => {
195
+ const completions = getArgumentCompletions([], "summon test --", "--");
196
+
197
+ expect(completions).toContain("--yes");
198
+ expect(completions).toContain("--verbose");
199
+ expect(completions).toContain("--llm");
200
+ expect(completions).toContain("--help");
201
+ });
202
+ });
package/src/completion.ts CHANGED
@@ -11,8 +11,11 @@
11
11
 
12
12
  import * as fs from "node:fs";
13
13
  import * as path from "node:path";
14
+ import type {
15
+ GeneratorDefinition,
16
+ PromptDefinition,
17
+ } from "@canonical/summon-core";
14
18
  import omelette from "omelette";
15
- import type { GeneratorDefinition, PromptDefinition } from "./types.js";
16
19
 
17
20
  // =============================================================================
18
21
  // Types