@canonical/summon-core 0.29.0 → 0.30.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 (174) hide show
  1. package/README.md +20 -6
  2. package/dist/esm/discovery/discoverGeneratorTree.js +383 -0
  3. package/dist/esm/discovery/discoverGeneratorTree.js.map +1 -0
  4. package/dist/esm/discovery/generatorCache.js +14 -0
  5. package/dist/esm/discovery/generatorCache.js.map +1 -0
  6. package/dist/esm/discovery/index.js +3 -0
  7. package/dist/esm/discovery/index.js.map +1 -0
  8. package/dist/esm/discovery/types.js +2 -0
  9. package/dist/esm/discovery/types.js.map +1 -0
  10. package/dist/esm/index.js +17 -0
  11. package/dist/esm/index.js.map +1 -0
  12. package/dist/esm/stamp/applyStamp.js +73 -0
  13. package/dist/esm/stamp/applyStamp.js.map +1 -0
  14. package/dist/esm/stamp/index.js +3 -0
  15. package/dist/esm/stamp/index.js.map +1 -0
  16. package/dist/esm/stamp/runTaskWithStamp.js +20 -0
  17. package/dist/esm/stamp/runTaskWithStamp.js.map +1 -0
  18. package/dist/esm/stamp/types.js +2 -0
  19. package/dist/esm/stamp/types.js.map +1 -0
  20. package/dist/esm/template/TemplatingEngine.js +2 -0
  21. package/dist/esm/template/TemplatingEngine.js.map +1 -0
  22. package/dist/esm/template/ejsEngine.js +17 -0
  23. package/dist/esm/template/ejsEngine.js.map +1 -0
  24. package/dist/esm/template/generateStamp.js +23 -0
  25. package/dist/esm/template/generateStamp.js.map +1 -0
  26. package/dist/esm/template/generatorComment.js +21 -0
  27. package/dist/esm/template/generatorComment.js.map +1 -0
  28. package/dist/esm/template/getCommentStyle.js +51 -0
  29. package/dist/esm/template/getCommentStyle.js.map +1 -0
  30. package/dist/esm/template/index.js +12 -0
  31. package/dist/esm/template/index.js.map +1 -0
  32. package/dist/esm/template/prependStamp.js +16 -0
  33. package/dist/esm/template/prependStamp.js.map +1 -0
  34. package/dist/esm/template/renderFile.js +8 -0
  35. package/dist/esm/template/renderFile.js.map +1 -0
  36. package/dist/esm/template/renderString.js +8 -0
  37. package/dist/esm/template/renderString.js.map +1 -0
  38. package/dist/esm/template/renderStringAsync.js +8 -0
  39. package/dist/esm/template/renderStringAsync.js.map +1 -0
  40. package/dist/esm/template/tasks/index.js +3 -0
  41. package/dist/esm/template/tasks/index.js.map +1 -0
  42. package/dist/esm/template/tasks/template.js +29 -0
  43. package/dist/esm/template/tasks/template.js.map +1 -0
  44. package/dist/esm/template/tasks/templateDir.js +51 -0
  45. package/dist/esm/template/tasks/templateDir.js.map +1 -0
  46. package/dist/esm/template/tasks/types.js +2 -0
  47. package/dist/esm/template/tasks/types.js.map +1 -0
  48. package/dist/esm/template/templateHelpers.js +21 -0
  49. package/dist/esm/template/templateHelpers.js.map +1 -0
  50. package/dist/esm/template/withHelpers.js +8 -0
  51. package/dist/esm/template/withHelpers.js.map +1 -0
  52. package/dist/esm/types/AnyGenerator.js +2 -0
  53. package/dist/esm/types/AnyGenerator.js.map +1 -0
  54. package/dist/esm/types/ForbidReserved.js +2 -0
  55. package/dist/esm/types/ForbidReserved.js.map +1 -0
  56. package/dist/esm/types/GeneratorDefinition.js +2 -0
  57. package/dist/esm/types/GeneratorDefinition.js.map +1 -0
  58. package/dist/esm/types/GeneratorMeta.js +2 -0
  59. package/dist/esm/types/GeneratorMeta.js.map +1 -0
  60. package/dist/esm/types/PromptDefinition.js +2 -0
  61. package/dist/esm/types/PromptDefinition.js.map +1 -0
  62. package/dist/esm/types/ReservedOption.js +2 -0
  63. package/dist/esm/types/ReservedOption.js.map +1 -0
  64. package/dist/esm/types/StampConfig.js +2 -0
  65. package/dist/esm/types/StampConfig.js.map +1 -0
  66. package/dist/esm/types/index.js +2 -0
  67. package/dist/esm/types/index.js.map +1 -0
  68. package/dist/types/discovery/discoverGeneratorTree.d.ts +31 -0
  69. package/dist/types/discovery/discoverGeneratorTree.d.ts.map +1 -0
  70. package/{src/discovery/generatorCache.ts → dist/types/discovery/generatorCache.d.ts} +2 -3
  71. package/dist/types/discovery/generatorCache.d.ts.map +1 -0
  72. package/{src/discovery/index.ts → dist/types/discovery/index.d.ts} +1 -0
  73. package/dist/types/discovery/index.d.ts.map +1 -0
  74. package/dist/types/discovery/types.d.ts +18 -0
  75. package/dist/types/discovery/types.d.ts.map +1 -0
  76. package/dist/types/index.d.ts +18 -0
  77. package/dist/types/index.d.ts.map +1 -0
  78. package/dist/types/stamp/applyStamp.d.ts +7 -0
  79. package/dist/types/stamp/applyStamp.d.ts.map +1 -0
  80. package/{src/stamp/index.ts → dist/types/stamp/index.d.ts} +1 -0
  81. package/dist/types/stamp/index.d.ts.map +1 -0
  82. package/dist/types/stamp/runTaskWithStamp.d.ts +8 -0
  83. package/dist/types/stamp/runTaskWithStamp.d.ts.map +1 -0
  84. package/dist/types/stamp/types.d.ts +7 -0
  85. package/dist/types/stamp/types.d.ts.map +1 -0
  86. package/dist/types/template/TemplatingEngine.d.ts +15 -0
  87. package/dist/types/template/TemplatingEngine.d.ts.map +1 -0
  88. package/dist/types/template/ejsEngine.d.ts +7 -0
  89. package/dist/types/template/ejsEngine.d.ts.map +1 -0
  90. package/dist/types/template/generateStamp.d.ts +7 -0
  91. package/dist/types/template/generateStamp.d.ts.map +1 -0
  92. package/dist/types/template/generatorComment.d.ts +15 -0
  93. package/dist/types/template/generatorComment.d.ts.map +1 -0
  94. package/dist/types/template/getCommentStyle.d.ts +12 -0
  95. package/dist/types/template/getCommentStyle.d.ts.map +1 -0
  96. package/{src/template/index.ts → dist/types/template/index.d.ts} +1 -0
  97. package/dist/types/template/index.d.ts.map +1 -0
  98. package/dist/types/template/prependStamp.d.ts +6 -0
  99. package/dist/types/template/prependStamp.d.ts.map +1 -0
  100. package/dist/types/template/renderFile.d.ts +6 -0
  101. package/dist/types/template/renderFile.d.ts.map +1 -0
  102. package/dist/types/template/renderString.d.ts +6 -0
  103. package/dist/types/template/renderString.d.ts.map +1 -0
  104. package/dist/types/template/renderStringAsync.d.ts +6 -0
  105. package/dist/types/template/renderStringAsync.d.ts.map +1 -0
  106. package/{src/template/tasks/index.ts → dist/types/template/tasks/index.d.ts} +1 -0
  107. package/dist/types/template/tasks/index.d.ts.map +1 -0
  108. package/dist/types/template/tasks/template.d.ts +11 -0
  109. package/dist/types/template/tasks/template.d.ts.map +1 -0
  110. package/dist/types/template/tasks/templateDir.d.ts +7 -0
  111. package/dist/types/template/tasks/templateDir.d.ts.map +1 -0
  112. package/dist/types/template/tasks/types.d.ts +33 -0
  113. package/dist/types/template/tasks/types.d.ts.map +1 -0
  114. package/dist/types/template/templateHelpers.d.ts +19 -0
  115. package/dist/types/template/templateHelpers.d.ts.map +1 -0
  116. package/dist/types/template/withHelpers.d.ts +5 -0
  117. package/dist/types/template/withHelpers.d.ts.map +1 -0
  118. package/{src/types/AnyGenerator.ts → dist/types/types/AnyGenerator.d.ts} +1 -3
  119. package/dist/types/types/AnyGenerator.d.ts.map +1 -0
  120. package/{src/types/ForbidReserved.ts → dist/types/types/ForbidReserved.d.ts} +2 -5
  121. package/dist/types/types/ForbidReserved.d.ts.map +1 -0
  122. package/dist/types/types/GeneratorDefinition.d.ts +17 -0
  123. package/dist/types/types/GeneratorDefinition.d.ts.map +1 -0
  124. package/dist/types/types/GeneratorMeta.d.ts +30 -0
  125. package/dist/types/types/GeneratorMeta.d.ts.map +1 -0
  126. package/dist/types/types/PromptDefinition.d.ts +37 -0
  127. package/dist/types/types/PromptDefinition.d.ts.map +1 -0
  128. package/dist/types/types/ReservedOption.d.ts +7 -0
  129. package/dist/types/types/ReservedOption.d.ts.map +1 -0
  130. package/dist/types/types/StampConfig.d.ts +7 -0
  131. package/dist/types/types/StampConfig.d.ts.map +1 -0
  132. package/{src/types/index.ts → dist/types/types/index.d.ts} +1 -0
  133. package/dist/types/types/index.d.ts.map +1 -0
  134. package/package.json +14 -20
  135. package/src/discovery/discoverGeneratorTree.test.ts +0 -926
  136. package/src/discovery/discoverGeneratorTree.ts +0 -460
  137. package/src/discovery/types.ts +0 -18
  138. package/src/index.ts +0 -65
  139. package/src/stamp/applyStamp.test.ts +0 -252
  140. package/src/stamp/applyStamp.ts +0 -90
  141. package/src/stamp/runTaskWithStamp.ts +0 -31
  142. package/src/stamp/types.ts +0 -7
  143. package/src/template/TemplatingEngine.ts +0 -17
  144. package/src/template/ejsEngine.test.ts +0 -30
  145. package/src/template/ejsEngine.ts +0 -21
  146. package/src/template/generateStamp.test.ts +0 -45
  147. package/src/template/generateStamp.ts +0 -29
  148. package/src/template/generatorComment.test.ts +0 -81
  149. package/src/template/generatorComment.ts +0 -32
  150. package/src/template/getCommentStyle.test.ts +0 -112
  151. package/src/template/getCommentStyle.ts +0 -59
  152. package/src/template/prependStamp.test.ts +0 -30
  153. package/src/template/prependStamp.ts +0 -16
  154. package/src/template/renderFile.test.ts +0 -27
  155. package/src/template/renderFile.ts +0 -13
  156. package/src/template/renderString.test.ts +0 -107
  157. package/src/template/renderString.ts +0 -13
  158. package/src/template/renderStringAsync.test.ts +0 -28
  159. package/src/template/renderStringAsync.ts +0 -13
  160. package/src/template/tasks/template.test.ts +0 -88
  161. package/src/template/tasks/template.ts +0 -41
  162. package/src/template/tasks/templateDir.test.ts +0 -126
  163. package/src/template/tasks/templateDir.ts +0 -60
  164. package/src/template/tasks/types.ts +0 -34
  165. package/src/template/templateHelpers.test.ts +0 -227
  166. package/src/template/templateHelpers.ts +0 -35
  167. package/src/template/withHelpers.test.ts +0 -41
  168. package/src/template/withHelpers.ts +0 -10
  169. package/src/testing/integration/template-rendering.test.ts +0 -69
  170. package/src/types/GeneratorDefinition.ts +0 -19
  171. package/src/types/GeneratorMeta.ts +0 -29
  172. package/src/types/PromptDefinition.ts +0 -33
  173. package/src/types/ReservedOption.ts +0 -23
  174. package/src/types/StampConfig.ts +0 -6
@@ -1,460 +0,0 @@
1
- /**
2
- * Discover available generators from the filesystem.
3
- *
4
- * Scans local directories, global package manager locations, and
5
- * project node_modules for summon-* packages, building a tree of
6
- * {@link GeneratorNode} entries.
7
- *
8
- * @note Impure — performs filesystem I/O (readdir, stat, access, import).
9
- */
10
-
11
- import * as fs from "node:fs/promises";
12
- import * as path from "node:path";
13
- import chalk from "chalk";
14
- import type { GeneratorDefinition } from "../types/index.js";
15
- import { generatorCache } from "./generatorCache.js";
16
- import type { GeneratorNode, GeneratorOrigin } from "./types.js";
17
-
18
- // =============================================================================
19
- // Internal helpers
20
- // =============================================================================
21
-
22
- /**
23
- * Merge a child node into a parent, combining children if the topic already exists.
24
- */
25
- const mergeIntoTree = (parent: GeneratorNode, child: GeneratorNode): void => {
26
- const existing = parent.children.get(child.name);
27
- /* v8 ignore start -- defensive: readdir never returns duplicate names, so existing is always undefined in practice */
28
- if (existing) {
29
- // Merge children - existing (local) takes precedence for indexPath
30
- for (const [name, grandchild] of child.children) {
31
- if (!existing.children.has(name)) {
32
- existing.children.set(name, grandchild);
33
- }
34
- }
35
- // Only set indexPath if existing doesn't have one (local takes precedence)
36
- if (!existing.indexPath && child.indexPath) {
37
- existing.indexPath = child.indexPath;
38
- }
39
- } else {
40
- /* v8 ignore stop */
41
- parent.children.set(child.name, child);
42
- }
43
- };
44
-
45
- /**
46
- * Build a tree of generators from a directory.
47
- * Supports nested structure like:
48
- * generators/component/react/index.ts -> summon component react
49
- * generators/component/svelte/index.ts -> summon component svelte
50
- * generators/util/index.ts -> summon util
51
- */
52
- const buildGeneratorTree = async (
53
- dir: string,
54
- node: GeneratorNode,
55
- origin: GeneratorOrigin = "local",
56
- ): Promise<void> => {
57
- try {
58
- const entries = await fs.readdir(dir, { withFileTypes: true });
59
-
60
- for (const entry of entries) {
61
- if (entry.isDirectory()) {
62
- const childDir = path.join(dir, entry.name);
63
- const indexPath = path.join(childDir, "index.ts");
64
-
65
- const childNode: GeneratorNode = {
66
- name: entry.name,
67
- path: childDir,
68
- children: new Map(),
69
- origin,
70
- };
71
-
72
- // Check if this directory has an index.ts (is a runnable generator)
73
- try {
74
- await fs.access(indexPath);
75
- childNode.indexPath = indexPath;
76
- } catch {
77
- // No index.ts, might still have children
78
- }
79
-
80
- // Recursively discover children
81
- await buildGeneratorTree(childDir, childNode, origin);
82
-
83
- // Only add node if it has an index.ts or has children with generators
84
- if (childNode.indexPath || childNode.children.size > 0) {
85
- mergeIntoTree(node, childNode);
86
- }
87
- }
88
- }
89
- } catch {
90
- // Directory doesn't exist
91
- }
92
- };
93
-
94
- /**
95
- * Check if a path is a directory (follows symlinks).
96
- */
97
- const isDirectory = async (filePath: string): Promise<boolean> => {
98
- try {
99
- const stat = await fs.stat(filePath); // stat follows symlinks
100
- return stat.isDirectory();
101
- } catch {
102
- return false;
103
- }
104
- };
105
-
106
- /**
107
- * Insert a generator into the tree at the given path.
108
- * Creates intermediate namespace nodes as needed.
109
- */
110
- const insertGeneratorAtPath = (
111
- root: GeneratorNode,
112
- pathStr: string,
113
- generator: GeneratorDefinition,
114
- origin: GeneratorOrigin = "package",
115
- ): void => {
116
- const segments = pathStr.split("/").filter(Boolean);
117
- let current = root;
118
-
119
- // Cache the generator for later lookup
120
- generatorCache.set(pathStr, generator);
121
-
122
- for (let i = 0; i < segments.length; i++) {
123
- const segment = segments[i];
124
- /* v8 ignore next — structurally guaranteed by loop bounds and filter(Boolean) */
125
- if (!segment) throw new Error(`Expected path segment at index ${i}`);
126
- const isLast = i === segments.length - 1;
127
-
128
- if (!current.children.has(segment)) {
129
- current.children.set(segment, {
130
- name: segment,
131
- path: "",
132
- children: new Map(),
133
- origin,
134
- });
135
- }
136
-
137
- const child = current.children.get(segment);
138
- /* v8 ignore next — child was just set in the block above */
139
- if (!child) throw new Error(`Expected child node for segment "${segment}"`);
140
-
141
- if (isLast) {
142
- // Mark as having a generator (use path as synthetic indexPath)
143
- child.indexPath = `cache:${pathStr}`;
144
- child.origin = origin;
145
- }
146
-
147
- current = child;
148
- }
149
- };
150
-
151
- /**
152
- * Process a summon-* package.
153
- *
154
- * Imports the package's main entry and looks for a `generators` export
155
- * mapping command paths to generator definitions.
156
- */
157
- const processPackage = async (
158
- pkgName: string,
159
- pkgDir: string,
160
- root: GeneratorNode,
161
- origin: GeneratorOrigin = "package",
162
- ): Promise<void> => {
163
- // Read package.json to get the main entry
164
- const pkgJsonPath = path.join(pkgDir, "package.json");
165
- let mainEntry: string | undefined;
166
-
167
- try {
168
- const pkgJson = JSON.parse(await fs.readFile(pkgJsonPath, "utf-8"));
169
- mainEntry = pkgJson.main;
170
- } catch {
171
- return; // Can't read package.json
172
- }
173
-
174
- if (!mainEntry) return;
175
-
176
- // Import the package's main entry
177
- const entryPath = path.join(pkgDir, mainEntry);
178
- try {
179
- const module = await import(entryPath);
180
- const generators =
181
- module.generators ?? module.default ?? ({} as Record<string, unknown>);
182
-
183
- // Insert each generator into the tree
184
- for (const [cmdPath, generator] of Object.entries(generators)) {
185
- if (generator && typeof generator === "object" && "meta" in generator) {
186
- insertGeneratorAtPath(
187
- root,
188
- cmdPath,
189
- generator as GeneratorDefinition,
190
- origin,
191
- );
192
- }
193
- }
194
- } catch (err) {
195
- console.error(
196
- chalk.yellow(`Warning: Could not load generators from '${pkgName}':`),
197
- (err as Error).message,
198
- );
199
- }
200
- };
201
-
202
- /**
203
- * Discover summon-* packages in node_modules.
204
- */
205
- const discoverNodeModulesPackages = async (
206
- nodeModulesDir: string,
207
- root: GeneratorNode,
208
- ): Promise<void> => {
209
- try {
210
- const entries = await fs.readdir(nodeModulesDir);
211
-
212
- for (const entry of entries) {
213
- const entryPath = path.join(nodeModulesDir, entry);
214
-
215
- if (entry.startsWith("@")) {
216
- // Scoped packages - look inside @scope/
217
- if (!(await isDirectory(entryPath))) continue;
218
- try {
219
- const scopedEntries = await fs.readdir(entryPath);
220
- for (const scopedEntry of scopedEntries) {
221
- if (scopedEntry.startsWith("summon-")) {
222
- const pkgDir = path.join(entryPath, scopedEntry);
223
- if (await isDirectory(pkgDir)) {
224
- await processPackage(`${entry}/${scopedEntry}`, pkgDir, root);
225
- }
226
- }
227
- }
228
- } catch {
229
- // Scope directory doesn't exist or can't be read
230
- }
231
- } else if (entry.startsWith("summon-")) {
232
- // Unscoped summon-* package
233
- if (await isDirectory(entryPath)) {
234
- await processPackage(entry, entryPath, root);
235
- }
236
- }
237
- }
238
- } catch {
239
- // node_modules doesn't exist
240
- }
241
- };
242
-
243
- /**
244
- * Get bun's global node_modules directory.
245
- * Default is ~/.bun/install/global/node_modules
246
- */
247
- const getBunGlobalNodeModules = (): string => {
248
- /* v8 ignore next 2 -- HOME is always set in Node.js environments */
249
- const bunInstallDir =
250
- process.env.BUN_INSTALL ?? path.join(process.env.HOME ?? "~", ".bun");
251
- return path.join(bunInstallDir, "install", "global", "node_modules");
252
- };
253
-
254
- /**
255
- * Get npm's global node_modules directory.
256
- * Uses `npm root -g` equivalent logic.
257
- */
258
- const getNpmGlobalNodeModules = async (): Promise<string | null> => {
259
- // Check common npm global locations
260
- const npmPrefix = process.env.NPM_CONFIG_PREFIX;
261
- if (npmPrefix) {
262
- return path.join(npmPrefix, "lib", "node_modules");
263
- }
264
-
265
- // Try NVM location
266
- const nvmDir = process.env.NVM_DIR;
267
- if (nvmDir) {
268
- // NVM stores globals in the current node version's lib/node_modules
269
- const nodeVersion = process.version;
270
- const nvmNodeModules = path.join(
271
- nvmDir,
272
- "versions",
273
- "node",
274
- nodeVersion,
275
- "lib",
276
- "node_modules",
277
- );
278
- try {
279
- await fs.access(nvmNodeModules);
280
- return nvmNodeModules;
281
- } catch {
282
- // NVM path doesn't exist
283
- }
284
- }
285
-
286
- // Fallback: check common system locations
287
- const commonPaths = [
288
- "/usr/local/lib/node_modules",
289
- "/usr/lib/node_modules",
290
- path.join(
291
- /* v8 ignore next */ process.env.HOME ?? "~",
292
- ".npm-global",
293
- "lib",
294
- "node_modules",
295
- ),
296
- ];
297
-
298
- for (const p of commonPaths) {
299
- try {
300
- await fs.access(p);
301
- return p;
302
- } catch {
303
- // Path doesn't exist
304
- }
305
- }
306
-
307
- return null;
308
- };
309
-
310
- /**
311
- * Scan a node_modules directory for summon-* packages.
312
- */
313
- const scanNodeModulesForSummonPackages = async (
314
- nodeModulesDir: string,
315
- root: GeneratorNode,
316
- origin: GeneratorOrigin = "global",
317
- ): Promise<void> => {
318
- try {
319
- await fs.access(nodeModulesDir);
320
- const entries = await fs.readdir(nodeModulesDir);
321
-
322
- for (const entry of entries) {
323
- const entryPath = path.join(nodeModulesDir, entry);
324
-
325
- if (entry.startsWith("@")) {
326
- // Scoped packages
327
- if (!(await isDirectory(entryPath))) continue;
328
- try {
329
- const scopedEntries = await fs.readdir(entryPath);
330
- for (const scopedEntry of scopedEntries) {
331
- if (scopedEntry.startsWith("summon-")) {
332
- const pkgDir = path.join(entryPath, scopedEntry);
333
- if (await isDirectory(pkgDir)) {
334
- await processPackage(
335
- `${entry}/${scopedEntry}`,
336
- pkgDir,
337
- root,
338
- origin,
339
- );
340
- }
341
- }
342
- }
343
- } catch {
344
- // Scope directory doesn't exist
345
- }
346
- } else if (entry.startsWith("summon-")) {
347
- if (await isDirectory(entryPath)) {
348
- await processPackage(entry, entryPath, root, origin);
349
- }
350
- }
351
- }
352
- } catch {
353
- // Directory doesn't exist - that's fine
354
- }
355
- };
356
-
357
- /**
358
- * Discover globally installed summon-* packages.
359
- * Looks in global package manager locations:
360
- * 1. Bun global packages (~/.bun/install/global/node_modules)
361
- * 2. NPM global packages (npm root -g)
362
- *
363
- * Users can link packages globally using:
364
- * bun link # from the package directory
365
- * npm link # from the package directory
366
- */
367
- const discoverGlobalPackages = async (root: GeneratorNode): Promise<void> => {
368
- // 1. Bun global packages
369
- const bunGlobalNodeModules = getBunGlobalNodeModules();
370
- await scanNodeModulesForSummonPackages(bunGlobalNodeModules, root, "global");
371
-
372
- // 2. NPM global packages
373
- const npmGlobalNodeModules = await getNpmGlobalNodeModules();
374
- if (npmGlobalNodeModules) {
375
- await scanNodeModulesForSummonPackages(
376
- npmGlobalNodeModules,
377
- root,
378
- "global",
379
- );
380
- }
381
- };
382
-
383
- // =============================================================================
384
- // Public API
385
- // =============================================================================
386
-
387
- interface DiscoverOptions {
388
- /** When set, ONLY load from this path (for testing). */
389
- explicitPath?: string;
390
- /** Directory containing built-in generators. Defaults to ../generators relative to this file. */
391
- builtinDir?: string;
392
- }
393
-
394
- /**
395
- * Create the root generator tree from all sources.
396
- *
397
- * When `explicitPath` is provided, ONLY load from that path (for testing).
398
- *
399
- * Otherwise, priority (highest to lowest, later overrides earlier):
400
- * 1. Built-in generators from \@canonical/summon
401
- * 2. Global packages (bun link / npm link locations)
402
- * 3. Project ./node_modules/summon-* packages (highest priority)
403
- *
404
- * @note Impure — scans the filesystem and imports generator modules.
405
- */
406
- export default async function discoverGeneratorTree(
407
- explicitPathOrOptions?: string | DiscoverOptions,
408
- ): Promise<GeneratorNode> {
409
- const options: DiscoverOptions =
410
- typeof explicitPathOrOptions === "string"
411
- ? { explicitPath: explicitPathOrOptions }
412
- : (explicitPathOrOptions ?? {});
413
- const { explicitPath, builtinDir } = options;
414
- const root: GeneratorNode = {
415
- name: "root",
416
- path: "",
417
- children: new Map(),
418
- };
419
-
420
- if (explicitPath) {
421
- // Explicit path mode: ONLY load from the specified path
422
- const absolutePath = path.isAbsolute(explicitPath)
423
- ? explicitPath
424
- : path.join(process.cwd(), explicitPath);
425
-
426
- // Check if it's a package with package.json (barrel export)
427
- const pkgJsonPath = path.join(absolutePath, "package.json");
428
- try {
429
- await fs.access(pkgJsonPath);
430
- // It's a package - use processPackage to load from barrel
431
- await processPackage(
432
- path.basename(absolutePath),
433
- absolutePath,
434
- root,
435
- "local",
436
- );
437
- } catch {
438
- // Not a package - scan directory for generators
439
- await buildGeneratorTree(absolutePath, root, "local");
440
- }
441
- return root;
442
- }
443
-
444
- // Normal discovery mode (order matters: later sources override earlier)
445
-
446
- // 1. Built-in generators (lowest priority)
447
- const defaultBuiltinDir = path.join(__dirname, "..", "generators");
448
- await buildGeneratorTree(builtinDir ?? defaultBuiltinDir, root, "builtin");
449
-
450
- // 2-3. Global packages and generators
451
- await discoverGlobalPackages(root);
452
-
453
- // 3. node_modules packages (project-level, highest priority)
454
- await discoverNodeModulesPackages(
455
- path.join(process.cwd(), "node_modules"),
456
- root,
457
- );
458
-
459
- return root;
460
- }
@@ -1,18 +0,0 @@
1
- /** Origin of a generator (for display purposes). */
2
- export type GeneratorOrigin = "local" | "global" | "package" | "builtin";
3
-
4
- /** A node in the generator discovery tree. */
5
- export interface GeneratorNode {
6
- name: string;
7
- /** Directory path */
8
- path: string;
9
- /** Path to index.ts if this is a runnable generator */
10
- indexPath?: string;
11
- children: Map<string, GeneratorNode>;
12
- origin?: GeneratorOrigin;
13
- meta?: {
14
- name: string;
15
- description: string;
16
- version: string;
17
- };
18
- }
package/src/index.ts DELETED
@@ -1,65 +0,0 @@
1
- /**
2
- * Summon - A Monadic Task-Centric Code Generator Framework
3
- *
4
- * The effect framework (Task, Effect, combinators, primitives, interpreter,
5
- * dry-run) lives in @canonical/task. This package provides the generation
6
- * layer on top: generator definitions, templates, stamps, and CLI types.
7
- *
8
- * @packageDocumentation
9
- */
10
-
11
- // =============================================================================
12
- // Types
13
- // =============================================================================
14
-
15
- export type {
16
- AnyGenerator,
17
- ForbidReserved,
18
- GeneratorDefinition,
19
- GeneratorMeta,
20
- PromptDefinition,
21
- ReservedOption,
22
- StampConfig,
23
- } from "./types/index.js";
24
-
25
- // =============================================================================
26
- // Stamps (generated file stamp comments)
27
- // =============================================================================
28
-
29
- export type { RunTaskWithStampOptions } from "./stamp/index.js";
30
- export {
31
- applyStamp,
32
- runTaskWithStamp,
33
- } from "./stamp/index.js";
34
-
35
- // =============================================================================
36
- // Templates
37
- // =============================================================================
38
-
39
- export type {
40
- TemplateDirOptions,
41
- TemplateOptions,
42
- TemplatingEngine,
43
- } from "./template/index.js";
44
- export {
45
- ejsEngine,
46
- generateStamp,
47
- generatorComment,
48
- getCommentStyle,
49
- prependStamp,
50
- renderFile,
51
- renderString,
52
- renderStringAsync,
53
- template,
54
- templateDir,
55
- templateHelpers,
56
- withHelpers,
57
- } from "./template/index.js";
58
-
59
- // =============================================================================
60
- // Discovery (generator tree algorithm + types)
61
- // =============================================================================
62
-
63
- export { default as discoverGeneratorTree } from "./discovery/discoverGeneratorTree.js";
64
- export { generatorCache } from "./discovery/generatorCache.js";
65
- export type { GeneratorNode, GeneratorOrigin } from "./discovery/types.js";