@canonical/summon-core 0.18.0 → 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 (45) hide show
  1. package/LICENSE +166 -0
  2. package/package.json +16 -8
  3. package/src/discovery/discoverGeneratorTree.ts +1 -1
  4. package/src/discovery/generatorCache.ts +1 -1
  5. package/src/index.ts +13 -14
  6. package/src/stamp/applyStamp.test.ts +199 -0
  7. package/src/stamp/applyStamp.ts +90 -0
  8. package/src/stamp/index.ts +3 -0
  9. package/src/stamp/runTaskWithStamp.ts +31 -0
  10. package/src/stamp/types.ts +7 -0
  11. package/src/template/TemplatingEngine.ts +17 -0
  12. package/src/template/ejsEngine.ts +21 -0
  13. package/src/template/generateStamp.ts +27 -0
  14. package/src/template/generatorComment.test.ts +81 -0
  15. package/src/template/generatorComment.ts +32 -0
  16. package/src/template/getCommentStyle.ts +59 -0
  17. package/src/template/index.ts +13 -0
  18. package/src/template/prependStamp.ts +16 -0
  19. package/src/template/renderFile.ts +13 -0
  20. package/src/template/renderString.test.ts +107 -0
  21. package/src/template/renderString.ts +13 -0
  22. package/src/template/renderStringAsync.ts +13 -0
  23. package/src/template/tasks/index.ts +3 -0
  24. package/src/template/tasks/template.test.ts +66 -0
  25. package/src/template/tasks/template.ts +22 -0
  26. package/src/template/tasks/templateDir.ts +60 -0
  27. package/src/template/tasks/types.ts +29 -0
  28. package/src/template/templateHelpers.test.ts +227 -0
  29. package/src/template/templateHelpers.ts +35 -0
  30. package/src/template/withHelpers.test.ts +41 -0
  31. package/src/template/withHelpers.ts +10 -0
  32. package/src/testing/integration/template-rendering.test.ts +69 -0
  33. package/src/types/AnyGenerator.ts +9 -0
  34. package/src/types/ForbidReserved.ts +23 -0
  35. package/src/types/GeneratorDefinition.ts +19 -0
  36. package/src/types/GeneratorMeta.ts +29 -0
  37. package/src/types/PromptDefinition.ts +33 -0
  38. package/src/types/ReservedOption.ts +23 -0
  39. package/src/types/StampConfig.ts +6 -0
  40. package/src/types/index.ts +7 -0
  41. package/src/cli-types.ts +0 -59
  42. package/src/stamp.ts +0 -165
  43. package/src/template.test.ts +0 -666
  44. package/src/template.ts +0 -543
  45. package/src/types.ts +0 -97
package/LICENSE ADDED
@@ -0,0 +1,166 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
166
+
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@canonical/summon-core",
3
3
  "description": "Code generation framework: generator definitions, templates, stamps, discovery. Built on @canonical/task.",
4
- "version": "0.18.0",
4
+ "version": "0.21.0",
5
5
  "type": "module",
6
6
  "module": "src/index.ts",
7
7
  "types": "src/index.ts",
@@ -11,6 +11,12 @@
11
11
  "import": "./src/index.ts"
12
12
  }
13
13
  },
14
+ "imports": {
15
+ "#types": "./src/types/index.ts",
16
+ "#stamp": "./src/stamp/index.ts",
17
+ "#template": "./src/template/index.ts",
18
+ "#discovery": "./src/discovery/index.ts"
19
+ },
14
20
  "files": [
15
21
  "src"
16
22
  ],
@@ -41,10 +47,10 @@
41
47
  "test:coverage": "vitest run --coverage"
42
48
  },
43
49
  "devDependencies": {
44
- "@biomejs/biome": "2.4.6",
45
- "@canonical/biome-config": "^0.18.0",
46
- "@canonical/typescript-config": "^0.18.0",
47
- "@canonical/webarchitect": "^0.18.0",
50
+ "@biomejs/biome": "2.4.9",
51
+ "@canonical/biome-config": "^0.21.0",
52
+ "@canonical/typescript-config": "^0.21.0",
53
+ "@canonical/webarchitect": "^0.21.0",
48
54
  "@types/bun": "^1.3.10",
49
55
  "@types/ejs": "^3.1.5",
50
56
  "@types/node": "^24.12.0",
@@ -52,8 +58,10 @@
52
58
  "vitest": "^4.0.18"
53
59
  },
54
60
  "dependencies": {
55
- "@canonical/task": "^0.18.0",
61
+ "@canonical/task": "^0.21.0",
62
+ "@canonical/utils": "^0.21.0",
56
63
  "chalk": "^5.6.2",
57
- "ejs": "^4.0.1"
58
- }
64
+ "ejs": "^5.0.0"
65
+ },
66
+ "gitHead": "4a39c954e7bbad4903397f8371f801982034a26d"
59
67
  }
@@ -11,7 +11,7 @@
11
11
  import * as fs from "node:fs/promises";
12
12
  import * as path from "node:path";
13
13
  import chalk from "chalk";
14
- import type { GeneratorDefinition } from "../types.js";
14
+ import type { GeneratorDefinition } from "../types/index.js";
15
15
  import { generatorCache } from "./generatorCache.js";
16
16
  import type { GeneratorNode, GeneratorOrigin } from "./types.js";
17
17
 
@@ -6,7 +6,7 @@
6
6
  * without coupling library code to binary-only modules.
7
7
  */
8
8
 
9
- import type { GeneratorDefinition } from "../types.js";
9
+ import type { GeneratorDefinition } from "../types/index.js";
10
10
 
11
11
  /**
12
12
  * Key is the command path (e.g., "component/react").
package/src/index.ts CHANGED
@@ -9,38 +9,43 @@
9
9
  */
10
10
 
11
11
  // =============================================================================
12
- // Generator Types
12
+ // Types
13
13
  // =============================================================================
14
14
 
15
15
  export type {
16
16
  AnyGenerator,
17
+ ForbidReserved,
17
18
  GeneratorDefinition,
18
19
  GeneratorMeta,
19
20
  PromptDefinition,
20
- } from "./types.js";
21
+ ReservedOption,
22
+ StampConfig,
23
+ } from "./types/index.js";
21
24
 
22
25
  // =============================================================================
23
26
  // Stamps (generated file stamp comments)
24
27
  // =============================================================================
25
28
 
26
- export type { RunTaskWithStampOptions, StampConfig } from "./stamp.js";
27
- export { applyStamp, runTaskWithStamp } from "./stamp.js";
29
+ export type { RunTaskWithStampOptions } from "./stamp/index.js";
30
+ export {
31
+ applyStamp,
32
+ runTaskWithStamp,
33
+ } from "./stamp/index.js";
28
34
 
29
35
  // =============================================================================
30
36
  // Templates
31
37
  // =============================================================================
32
38
 
33
39
  export type {
34
- StampOptions,
35
40
  TemplateDirOptions,
36
41
  TemplateOptions,
37
42
  TemplatingEngine,
38
- } from "./template.js";
43
+ } from "./template/index.js";
39
44
  export {
40
45
  ejsEngine,
41
46
  generateStamp,
42
47
  generatorComment,
43
- generatorHtmlComment,
48
+ getCommentStyle,
44
49
  prependStamp,
45
50
  renderFile,
46
51
  renderString,
@@ -49,13 +54,7 @@ export {
49
54
  templateDir,
50
55
  templateHelpers,
51
56
  withHelpers,
52
- } from "./template.js";
53
-
54
- // =============================================================================
55
- // CLI Types (for generator authors)
56
- // =============================================================================
57
-
58
- export type { ForbidReserved, ReservedOption } from "./cli-types.js";
57
+ } from "./template/index.js";
59
58
 
60
59
  // =============================================================================
61
60
  // Discovery (generator tree algorithm + types)
@@ -0,0 +1,199 @@
1
+ import { pure } from "@canonical/task";
2
+ import { describe, expect, it } from "vitest";
3
+ import type StampConfig from "../types/StampConfig.js";
4
+ import applyStamp from "./applyStamp.js";
5
+ import runTaskWithStamp from "./runTaskWithStamp.js";
6
+
7
+ const stamp: StampConfig = { generator: "test-gen", version: "1.0.0" };
8
+
9
+ // =============================================================================
10
+ // applyStamp
11
+ // =============================================================================
12
+
13
+ describe("applyStamp", () => {
14
+ it("stamps TypeScript files with single-line comment", () => {
15
+ expect(applyStamp("file.ts", "const x = 1;", stamp)).toBe(
16
+ "// Generated by test-gen v1.0.0\n\nconst x = 1;",
17
+ );
18
+ });
19
+
20
+ it("stamps TSX files with single-line comment", () => {
21
+ expect(applyStamp("Component.tsx", "<div />", stamp)).toBe(
22
+ "// Generated by test-gen v1.0.0\n\n<div />",
23
+ );
24
+ });
25
+
26
+ it("stamps JavaScript files with single-line comment", () => {
27
+ expect(applyStamp("app.js", "console.log(1);", stamp)).toBe(
28
+ "// Generated by test-gen v1.0.0\n\nconsole.log(1);",
29
+ );
30
+ });
31
+
32
+ it("stamps CSS files with block comment (preferBlock)", () => {
33
+ expect(applyStamp("styles.css", "body {}", stamp)).toBe(
34
+ "/* Generated by test-gen v1.0.0 */\n\nbody {}",
35
+ );
36
+ });
37
+
38
+ it("stamps SCSS files with single-line comment", () => {
39
+ expect(applyStamp("styles.scss", "$color: red;", stamp)).toBe(
40
+ "// Generated by test-gen v1.0.0\n\n$color: red;",
41
+ );
42
+ });
43
+
44
+ it("stamps HTML files with HTML comment", () => {
45
+ expect(applyStamp("page.html", "<div></div>", stamp)).toBe(
46
+ "<!-- Generated by test-gen v1.0.0 -->\n\n<div></div>",
47
+ );
48
+ });
49
+
50
+ it("stamps SVG files with HTML comment", () => {
51
+ expect(applyStamp("icon.svg", "<svg></svg>", stamp)).toBe(
52
+ "<!-- Generated by test-gen v1.0.0 -->\n\n<svg></svg>",
53
+ );
54
+ });
55
+
56
+ it("stamps Svelte files with HTML comment", () => {
57
+ expect(applyStamp("App.svelte", "<script></script>", stamp)).toBe(
58
+ "<!-- Generated by test-gen v1.0.0 -->\n\n<script></script>",
59
+ );
60
+ });
61
+
62
+ it("stamps MDX files with JSX comment", () => {
63
+ expect(applyStamp("doc.mdx", "# Hello", stamp)).toBe(
64
+ "{/* Generated by test-gen v1.0.0 */}\n\n# Hello",
65
+ );
66
+ });
67
+
68
+ it("stamps SQL files with double-dash comment", () => {
69
+ expect(applyStamp("query.sql", "SELECT 1;", stamp)).toBe(
70
+ "-- Generated by test-gen v1.0.0\n\nSELECT 1;",
71
+ );
72
+ });
73
+
74
+ it("stamps YAML files with hash comment", () => {
75
+ expect(applyStamp("config.yaml", "key: value", stamp)).toBe(
76
+ "# Generated by test-gen v1.0.0\n\nkey: value",
77
+ );
78
+ });
79
+
80
+ it("stamps YML files with hash comment", () => {
81
+ expect(applyStamp("ci.yml", "name: CI", stamp)).toBe(
82
+ "# Generated by test-gen v1.0.0\n\nname: CI",
83
+ );
84
+ });
85
+
86
+ it("stamps shell scripts with hash comment", () => {
87
+ expect(applyStamp("setup.sh", "echo hi", stamp)).toBe(
88
+ "# Generated by test-gen v1.0.0\n\necho hi",
89
+ );
90
+ });
91
+
92
+ it("stamps Python files with hash comment", () => {
93
+ expect(applyStamp("main.py", "print(1)", stamp)).toBe(
94
+ "# Generated by test-gen v1.0.0\n\nprint(1)",
95
+ );
96
+ });
97
+
98
+ it("stamps Go files with single-line comment", () => {
99
+ expect(applyStamp("main.go", "package main", stamp)).toBe(
100
+ "// Generated by test-gen v1.0.0\n\npackage main",
101
+ );
102
+ });
103
+
104
+ it("stamps Rust files with single-line comment", () => {
105
+ expect(applyStamp("lib.rs", "fn main() {}", stamp)).toBe(
106
+ "// Generated by test-gen v1.0.0\n\nfn main() {}",
107
+ );
108
+ });
109
+
110
+ it("stamps Markdown files with HTML comment", () => {
111
+ expect(applyStamp("README.md", "# Title", stamp)).toBe(
112
+ "<!-- Generated by test-gen v1.0.0 -->\n\n# Title",
113
+ );
114
+ });
115
+
116
+ it("returns content unchanged for JSON (no comment syntax)", () => {
117
+ const content = '{ "key": "value" }';
118
+ expect(applyStamp("package.json", content, stamp)).toBe(content);
119
+ });
120
+
121
+ it("returns content unchanged for unknown extension", () => {
122
+ const content = "binary data";
123
+ expect(applyStamp("file.dat", content, stamp)).toBe(content);
124
+ });
125
+
126
+ it("returns content unchanged for extensionless files", () => {
127
+ const content = "some content";
128
+ expect(applyStamp("Makefile", content, stamp)).toBe(content);
129
+ });
130
+
131
+ it("handles shebang — places stamp after shebang line", () => {
132
+ const content = "#!/bin/bash\necho hello";
133
+ expect(applyStamp("script.sh", content, stamp)).toBe(
134
+ "#!/bin/bash\n# Generated by test-gen v1.0.0\n\necho hello",
135
+ );
136
+ });
137
+
138
+ it("handles shebang in .ts files", () => {
139
+ const content = "#!/usr/bin/env bun\nconsole.log('hi');";
140
+ expect(applyStamp("cli.ts", content, stamp)).toBe(
141
+ "#!/usr/bin/env bun\n// Generated by test-gen v1.0.0\n\nconsole.log('hi');",
142
+ );
143
+ });
144
+
145
+ it("handles shebang-only file (no newline) — stamps normally", () => {
146
+ // When there's no newline after shebang, indexOf("\n") returns -1
147
+ // so the code falls through to prepend the stamp normally
148
+ const content = "#!/bin/bash";
149
+ expect(applyStamp("script.sh", content, stamp)).toBe(
150
+ "# Generated by test-gen v1.0.0\n\n#!/bin/bash",
151
+ );
152
+ });
153
+
154
+ it("handles empty content", () => {
155
+ expect(applyStamp("file.ts", "", stamp)).toBe(
156
+ "// Generated by test-gen v1.0.0\n\n",
157
+ );
158
+ });
159
+
160
+ it("handles compound extension (.test.ts) — uses final extension", () => {
161
+ expect(applyStamp("app.test.ts", "test()", stamp)).toBe(
162
+ "// Generated by test-gen v1.0.0\n\ntest()",
163
+ );
164
+ });
165
+
166
+ it("handles uppercase extensions via lowercase normalization", () => {
167
+ expect(applyStamp("README.MD", "# Title", stamp)).toBe(
168
+ "<!-- Generated by test-gen v1.0.0 -->\n\n# Title",
169
+ );
170
+ });
171
+
172
+ it("includes generator name and version in stamp text", () => {
173
+ const customStamp: StampConfig = {
174
+ generator: "@canonical/summon-component",
175
+ version: "0.3.2",
176
+ };
177
+ expect(applyStamp("file.ts", "code", customStamp)).toBe(
178
+ "// Generated by @canonical/summon-component v0.3.2\n\ncode",
179
+ );
180
+ });
181
+ });
182
+
183
+ // =============================================================================
184
+ // runTaskWithStamp
185
+ // =============================================================================
186
+
187
+ describe("runTaskWithStamp", () => {
188
+ it("runs a pure task without stamp option", async () => {
189
+ const result = await runTaskWithStamp(pure(42));
190
+ expect(result).toBe(42);
191
+ });
192
+
193
+ it("runs a pure task with stamp option (no WriteFile effects to stamp)", async () => {
194
+ const result = await runTaskWithStamp(pure("done"), {
195
+ stamp,
196
+ });
197
+ expect(result).toBe("done");
198
+ });
199
+ });
@@ -0,0 +1,90 @@
1
+ import * as path from "node:path";
2
+ import type StampConfig from "../types/StampConfig.js";
3
+
4
+ const COMMENT_STYLES: Record<
5
+ string,
6
+ {
7
+ single?: string;
8
+ blockStart?: string;
9
+ blockEnd?: string;
10
+ preferBlock?: boolean;
11
+ }
12
+ > = {
13
+ ".ts": { single: "//" },
14
+ ".tsx": { single: "//" },
15
+ ".js": { single: "//" },
16
+ ".jsx": { single: "//" },
17
+ ".mjs": { single: "//" },
18
+ ".cjs": { single: "//" },
19
+ ".css": { blockStart: "/*", blockEnd: "*/", preferBlock: true },
20
+ ".scss": { single: "//" },
21
+ ".sass": { single: "//" },
22
+ ".less": { single: "//" },
23
+ ".html": { blockStart: "<!--", blockEnd: "-->" },
24
+ ".htm": { blockStart: "<!--", blockEnd: "-->" },
25
+ ".xml": { blockStart: "<!--", blockEnd: "-->" },
26
+ ".svg": { blockStart: "<!--", blockEnd: "-->" },
27
+ ".vue": { blockStart: "<!--", blockEnd: "-->" },
28
+ ".svelte": { blockStart: "<!--", blockEnd: "-->" },
29
+ ".yaml": { single: "#" },
30
+ ".yml": { single: "#" },
31
+ ".toml": { single: "#" },
32
+ ".sh": { single: "#" },
33
+ ".bash": { single: "#" },
34
+ ".zsh": { single: "#" },
35
+ ".py": { single: "#" },
36
+ ".rb": { single: "#" },
37
+ ".go": { single: "//" },
38
+ ".rs": { single: "//" },
39
+ ".java": { single: "//" },
40
+ ".kt": { single: "//" },
41
+ ".swift": { single: "//" },
42
+ ".c": { single: "//" },
43
+ ".cpp": { single: "//" },
44
+ ".h": { single: "//" },
45
+ ".php": { single: "//" },
46
+ ".md": { blockStart: "<!--", blockEnd: "-->" },
47
+ ".mdx": { blockStart: "{/*", blockEnd: "*/}" },
48
+ ".sql": { single: "--" },
49
+ };
50
+
51
+ /**
52
+ * Apply a stamp to file content based on file extension.
53
+ * Returns the original content if the file type doesn't support comments.
54
+ */
55
+ export default function applyStamp(
56
+ filePath: string,
57
+ content: string,
58
+ stamp: StampConfig,
59
+ ): string {
60
+ const ext = path.extname(filePath).toLowerCase();
61
+ const style = COMMENT_STYLES[ext];
62
+
63
+ if (!style) {
64
+ return content;
65
+ }
66
+
67
+ const stampText = `Generated by ${stamp.generator} v${stamp.version}`;
68
+ let stampLine: string;
69
+
70
+ if (style.preferBlock || (!style.single && style.blockStart)) {
71
+ if (!style.blockStart || !style.blockEnd) return content;
72
+ stampLine = `${style.blockStart} ${stampText} ${style.blockEnd}`;
73
+ } else if (style.single) {
74
+ stampLine = `${style.single} ${stampText}`;
75
+ } else {
76
+ return content;
77
+ }
78
+
79
+ // Handle shebang - place stamp after it
80
+ if (content.startsWith("#!")) {
81
+ const firstNewline = content.indexOf("\n");
82
+ if (firstNewline !== -1) {
83
+ const shebang = content.slice(0, firstNewline + 1);
84
+ const rest = content.slice(firstNewline + 1);
85
+ return `${shebang}${stampLine}\n\n${rest}`;
86
+ }
87
+ }
88
+
89
+ return `${stampLine}\n\n${content}`;
90
+ }
@@ -0,0 +1,3 @@
1
+ export { default as applyStamp } from "./applyStamp.js";
2
+ export { default as runTaskWithStamp } from "./runTaskWithStamp.js";
3
+ export type { RunTaskWithStampOptions } from "./types.js";
@@ -0,0 +1,31 @@
1
+ import { type Effect, runTask, type Task } from "@canonical/task";
2
+ import applyStamp from "./applyStamp.js";
3
+ import type { RunTaskWithStampOptions } from "./types.js";
4
+
5
+ /**
6
+ * Run a task with stamp support. Wraps `runTask` from @canonical/task,
7
+ * intercepting WriteFile effects to prepend generated-file stamps.
8
+ */
9
+ export default async function runTaskWithStamp<A>(
10
+ task: Task<A>,
11
+ options: RunTaskWithStampOptions = {},
12
+ ): Promise<A> {
13
+ const { stamp, onEffectStart: userOnEffectStart, ...restOptions } = options;
14
+
15
+ if (!stamp) {
16
+ return runTask(task, options);
17
+ }
18
+
19
+ const onEffectStart = (effect: Effect) => {
20
+ if (effect._tag === "WriteFile") {
21
+ (effect as { content: string }).content = applyStamp(
22
+ effect.path,
23
+ effect.content,
24
+ stamp,
25
+ );
26
+ }
27
+ userOnEffectStart?.(effect);
28
+ };
29
+
30
+ return runTask(task, { ...restOptions, onEffectStart });
31
+ }
@@ -0,0 +1,7 @@
1
+ import type { RunTaskOptions } from "@canonical/task";
2
+ import type StampConfig from "../types/StampConfig.js";
3
+
4
+ export interface RunTaskWithStampOptions extends RunTaskOptions {
5
+ /** Stamp configuration for generated files. If provided, all written files get a stamp comment. */
6
+ stamp?: StampConfig;
7
+ }
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Abstract interface for templating engines.
3
+ *
4
+ * Implement this interface to use alternative template engines
5
+ * (e.g., Handlebars, Mustache, Nunjucks) with the summon generator.
6
+ */
7
+ export default interface TemplatingEngine {
8
+ /** Render a template string with variables (synchronous). */
9
+ render(template: string, vars: Record<string, unknown>): string;
10
+ /** Render a template string with variables (asynchronous). */
11
+ renderAsync(template: string, vars: Record<string, unknown>): Promise<string>;
12
+ /** Render a template file with variables (asynchronous). */
13
+ renderFile(
14
+ templatePath: string,
15
+ vars: Record<string, unknown>,
16
+ ): Promise<string>;
17
+ }
@@ -0,0 +1,21 @@
1
+ import ejs from "ejs";
2
+ import type TemplatingEngine from "./TemplatingEngine.js";
3
+
4
+ /**
5
+ * Default EJS templating engine implementation.
6
+ */
7
+ const ejsEngine: TemplatingEngine = {
8
+ render(template, vars) {
9
+ return ejs.render(template, vars, { async: false });
10
+ },
11
+
12
+ async renderAsync(template, vars) {
13
+ return ejs.render(template, vars, { async: true }) as Promise<string>;
14
+ },
15
+
16
+ async renderFile(templatePath, vars) {
17
+ return ejs.renderFile(templatePath, vars, { async: true });
18
+ },
19
+ };
20
+
21
+ export default ejsEngine;