@canonical/summon-core 0.29.0 → 0.31.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/README.md +20 -6
- package/dist/esm/discovery/discoverGeneratorTree.js +383 -0
- package/dist/esm/discovery/discoverGeneratorTree.js.map +1 -0
- package/dist/esm/discovery/generatorCache.js +14 -0
- package/dist/esm/discovery/generatorCache.js.map +1 -0
- package/dist/esm/discovery/index.js +3 -0
- package/dist/esm/discovery/index.js.map +1 -0
- package/dist/esm/discovery/types.js +2 -0
- package/dist/esm/discovery/types.js.map +1 -0
- package/dist/esm/index.js +17 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/stamp/applyStamp.js +73 -0
- package/dist/esm/stamp/applyStamp.js.map +1 -0
- package/dist/esm/stamp/index.js +3 -0
- package/dist/esm/stamp/index.js.map +1 -0
- package/dist/esm/stamp/runTaskWithStamp.js +20 -0
- package/dist/esm/stamp/runTaskWithStamp.js.map +1 -0
- package/dist/esm/stamp/types.js +2 -0
- package/dist/esm/stamp/types.js.map +1 -0
- package/dist/esm/template/TemplatingEngine.js +2 -0
- package/dist/esm/template/TemplatingEngine.js.map +1 -0
- package/dist/esm/template/ejsEngine.js +17 -0
- package/dist/esm/template/ejsEngine.js.map +1 -0
- package/dist/esm/template/generateStamp.js +23 -0
- package/dist/esm/template/generateStamp.js.map +1 -0
- package/dist/esm/template/generatorComment.js +21 -0
- package/dist/esm/template/generatorComment.js.map +1 -0
- package/dist/esm/template/getCommentStyle.js +51 -0
- package/dist/esm/template/getCommentStyle.js.map +1 -0
- package/dist/esm/template/index.js +12 -0
- package/dist/esm/template/index.js.map +1 -0
- package/dist/esm/template/prependStamp.js +16 -0
- package/dist/esm/template/prependStamp.js.map +1 -0
- package/dist/esm/template/renderFile.js +8 -0
- package/dist/esm/template/renderFile.js.map +1 -0
- package/dist/esm/template/renderString.js +8 -0
- package/dist/esm/template/renderString.js.map +1 -0
- package/dist/esm/template/renderStringAsync.js +8 -0
- package/dist/esm/template/renderStringAsync.js.map +1 -0
- package/dist/esm/template/tasks/index.js +3 -0
- package/dist/esm/template/tasks/index.js.map +1 -0
- package/dist/esm/template/tasks/template.js +29 -0
- package/dist/esm/template/tasks/template.js.map +1 -0
- package/dist/esm/template/tasks/templateDir.js +51 -0
- package/dist/esm/template/tasks/templateDir.js.map +1 -0
- package/dist/esm/template/tasks/types.js +2 -0
- package/dist/esm/template/tasks/types.js.map +1 -0
- package/dist/esm/template/templateHelpers.js +21 -0
- package/dist/esm/template/templateHelpers.js.map +1 -0
- package/dist/esm/template/withHelpers.js +8 -0
- package/dist/esm/template/withHelpers.js.map +1 -0
- package/dist/esm/types/AnyGenerator.js +2 -0
- package/dist/esm/types/AnyGenerator.js.map +1 -0
- package/dist/esm/types/ForbidReserved.js +2 -0
- package/dist/esm/types/ForbidReserved.js.map +1 -0
- package/dist/esm/types/GeneratorDefinition.js +2 -0
- package/dist/esm/types/GeneratorDefinition.js.map +1 -0
- package/dist/esm/types/GeneratorMeta.js +2 -0
- package/dist/esm/types/GeneratorMeta.js.map +1 -0
- package/dist/esm/types/PromptDefinition.js +2 -0
- package/dist/esm/types/PromptDefinition.js.map +1 -0
- package/dist/esm/types/ReservedOption.js +2 -0
- package/dist/esm/types/ReservedOption.js.map +1 -0
- package/dist/esm/types/StampConfig.js +2 -0
- package/dist/esm/types/StampConfig.js.map +1 -0
- package/dist/esm/types/index.js +2 -0
- package/dist/esm/types/index.js.map +1 -0
- package/dist/types/discovery/discoverGeneratorTree.d.ts +31 -0
- package/dist/types/discovery/discoverGeneratorTree.d.ts.map +1 -0
- package/{src/discovery/generatorCache.ts → dist/types/discovery/generatorCache.d.ts} +2 -3
- package/dist/types/discovery/generatorCache.d.ts.map +1 -0
- package/{src/discovery/index.ts → dist/types/discovery/index.d.ts} +1 -0
- package/dist/types/discovery/index.d.ts.map +1 -0
- package/dist/types/discovery/types.d.ts +18 -0
- package/dist/types/discovery/types.d.ts.map +1 -0
- package/dist/types/index.d.ts +18 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/stamp/applyStamp.d.ts +7 -0
- package/dist/types/stamp/applyStamp.d.ts.map +1 -0
- package/{src/stamp/index.ts → dist/types/stamp/index.d.ts} +1 -0
- package/dist/types/stamp/index.d.ts.map +1 -0
- package/dist/types/stamp/runTaskWithStamp.d.ts +8 -0
- package/dist/types/stamp/runTaskWithStamp.d.ts.map +1 -0
- package/dist/types/stamp/types.d.ts +7 -0
- package/dist/types/stamp/types.d.ts.map +1 -0
- package/dist/types/template/TemplatingEngine.d.ts +15 -0
- package/dist/types/template/TemplatingEngine.d.ts.map +1 -0
- package/dist/types/template/ejsEngine.d.ts +7 -0
- package/dist/types/template/ejsEngine.d.ts.map +1 -0
- package/dist/types/template/generateStamp.d.ts +7 -0
- package/dist/types/template/generateStamp.d.ts.map +1 -0
- package/dist/types/template/generatorComment.d.ts +15 -0
- package/dist/types/template/generatorComment.d.ts.map +1 -0
- package/dist/types/template/getCommentStyle.d.ts +12 -0
- package/dist/types/template/getCommentStyle.d.ts.map +1 -0
- package/{src/template/index.ts → dist/types/template/index.d.ts} +1 -0
- package/dist/types/template/index.d.ts.map +1 -0
- package/dist/types/template/prependStamp.d.ts +6 -0
- package/dist/types/template/prependStamp.d.ts.map +1 -0
- package/dist/types/template/renderFile.d.ts +6 -0
- package/dist/types/template/renderFile.d.ts.map +1 -0
- package/dist/types/template/renderString.d.ts +6 -0
- package/dist/types/template/renderString.d.ts.map +1 -0
- package/dist/types/template/renderStringAsync.d.ts +6 -0
- package/dist/types/template/renderStringAsync.d.ts.map +1 -0
- package/{src/template/tasks/index.ts → dist/types/template/tasks/index.d.ts} +1 -0
- package/dist/types/template/tasks/index.d.ts.map +1 -0
- package/dist/types/template/tasks/template.d.ts +11 -0
- package/dist/types/template/tasks/template.d.ts.map +1 -0
- package/dist/types/template/tasks/templateDir.d.ts +7 -0
- package/dist/types/template/tasks/templateDir.d.ts.map +1 -0
- package/dist/types/template/tasks/types.d.ts +33 -0
- package/dist/types/template/tasks/types.d.ts.map +1 -0
- package/dist/types/template/templateHelpers.d.ts +19 -0
- package/dist/types/template/templateHelpers.d.ts.map +1 -0
- package/dist/types/template/withHelpers.d.ts +5 -0
- package/dist/types/template/withHelpers.d.ts.map +1 -0
- package/{src/types/AnyGenerator.ts → dist/types/types/AnyGenerator.d.ts} +2 -4
- package/dist/types/types/AnyGenerator.d.ts.map +1 -0
- package/{src/types/ForbidReserved.ts → dist/types/types/ForbidReserved.d.ts} +3 -6
- package/dist/types/types/ForbidReserved.d.ts.map +1 -0
- package/dist/types/types/GeneratorDefinition.d.ts +17 -0
- package/dist/types/types/GeneratorDefinition.d.ts.map +1 -0
- package/dist/types/types/GeneratorMeta.d.ts +30 -0
- package/dist/types/types/GeneratorMeta.d.ts.map +1 -0
- package/dist/types/types/PromptDefinition.d.ts +37 -0
- package/dist/types/types/PromptDefinition.d.ts.map +1 -0
- package/dist/types/types/ReservedOption.d.ts +7 -0
- package/dist/types/types/ReservedOption.d.ts.map +1 -0
- package/dist/types/types/StampConfig.d.ts +7 -0
- package/dist/types/types/StampConfig.d.ts.map +1 -0
- package/{src/types/index.ts → dist/types/types/index.d.ts} +1 -0
- package/dist/types/types/index.d.ts.map +1 -0
- package/package.json +14 -20
- package/src/discovery/discoverGeneratorTree.test.ts +0 -926
- package/src/discovery/discoverGeneratorTree.ts +0 -460
- package/src/discovery/types.ts +0 -18
- package/src/index.ts +0 -65
- package/src/stamp/applyStamp.test.ts +0 -252
- package/src/stamp/applyStamp.ts +0 -90
- package/src/stamp/runTaskWithStamp.ts +0 -31
- package/src/stamp/types.ts +0 -7
- package/src/template/TemplatingEngine.ts +0 -17
- package/src/template/ejsEngine.test.ts +0 -30
- package/src/template/ejsEngine.ts +0 -21
- package/src/template/generateStamp.test.ts +0 -45
- package/src/template/generateStamp.ts +0 -29
- package/src/template/generatorComment.test.ts +0 -81
- package/src/template/generatorComment.ts +0 -32
- package/src/template/getCommentStyle.test.ts +0 -112
- package/src/template/getCommentStyle.ts +0 -59
- package/src/template/prependStamp.test.ts +0 -30
- package/src/template/prependStamp.ts +0 -16
- package/src/template/renderFile.test.ts +0 -27
- package/src/template/renderFile.ts +0 -13
- package/src/template/renderString.test.ts +0 -107
- package/src/template/renderString.ts +0 -13
- package/src/template/renderStringAsync.test.ts +0 -28
- package/src/template/renderStringAsync.ts +0 -13
- package/src/template/tasks/template.test.ts +0 -88
- package/src/template/tasks/template.ts +0 -41
- package/src/template/tasks/templateDir.test.ts +0 -126
- package/src/template/tasks/templateDir.ts +0 -60
- package/src/template/tasks/types.ts +0 -34
- package/src/template/templateHelpers.test.ts +0 -227
- package/src/template/templateHelpers.ts +0 -35
- package/src/template/withHelpers.test.ts +0 -41
- package/src/template/withHelpers.ts +0 -10
- package/src/testing/integration/template-rendering.test.ts +0 -69
- package/src/types/GeneratorDefinition.ts +0 -19
- package/src/types/GeneratorMeta.ts +0 -29
- package/src/types/PromptDefinition.ts +0 -33
- package/src/types/ReservedOption.ts +0 -23
- package/src/types/StampConfig.ts +0 -6
|
@@ -1,252 +0,0 @@
|
|
|
1
|
-
import { type Effect, mkdir, pure, task, writeFile } from "@canonical/task";
|
|
2
|
-
import { describe, expect, it, vi } 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
|
-
|
|
200
|
-
it("stamps WriteFile effect content when stamp is provided", async () => {
|
|
201
|
-
const captured: Effect[] = [];
|
|
202
|
-
const task = writeFile("/tmp/test-stamp-output.ts", "const x = 1;");
|
|
203
|
-
|
|
204
|
-
await runTaskWithStamp(task, {
|
|
205
|
-
stamp,
|
|
206
|
-
onEffectStart: (effect) => {
|
|
207
|
-
captured.push({ ...effect });
|
|
208
|
-
},
|
|
209
|
-
});
|
|
210
|
-
|
|
211
|
-
const writeEffect = captured.find((e) => e._tag === "WriteFile");
|
|
212
|
-
expect(writeEffect).toBeDefined();
|
|
213
|
-
// The content should have been mutated before our onEffectStart was called
|
|
214
|
-
expect((writeEffect as { content: string }).content).toContain(
|
|
215
|
-
"Generated by test-gen v1.0.0",
|
|
216
|
-
);
|
|
217
|
-
});
|
|
218
|
-
|
|
219
|
-
it("calls user-provided onEffectStart after stamping", async () => {
|
|
220
|
-
const userCallback = vi.fn();
|
|
221
|
-
const task = writeFile("/tmp/test-stamp-cb.ts", "hello");
|
|
222
|
-
|
|
223
|
-
await runTaskWithStamp(task, {
|
|
224
|
-
stamp,
|
|
225
|
-
onEffectStart: userCallback,
|
|
226
|
-
});
|
|
227
|
-
|
|
228
|
-
expect(userCallback).toHaveBeenCalled();
|
|
229
|
-
const callArg = userCallback.mock.calls[0][0];
|
|
230
|
-
expect(callArg._tag).toBe("WriteFile");
|
|
231
|
-
});
|
|
232
|
-
|
|
233
|
-
it("does not modify non-WriteFile effects", async () => {
|
|
234
|
-
const captured: Effect[] = [];
|
|
235
|
-
// mkdir produces a MakeDir effect, not WriteFile
|
|
236
|
-
const mkdirTask = task(mkdir("/tmp/test-stamp-dir"))
|
|
237
|
-
.chain(() => task(writeFile("/tmp/test-stamp-dir/out.ts", "code")))
|
|
238
|
-
.unwrap();
|
|
239
|
-
|
|
240
|
-
await runTaskWithStamp(mkdirTask, {
|
|
241
|
-
stamp,
|
|
242
|
-
onEffectStart: (effect) => {
|
|
243
|
-
captured.push({ ...effect });
|
|
244
|
-
},
|
|
245
|
-
});
|
|
246
|
-
|
|
247
|
-
const mkdirEffect = captured.find((e) => e._tag === "MakeDir");
|
|
248
|
-
expect(mkdirEffect).toBeDefined();
|
|
249
|
-
// MakeDir effect should pass through unchanged
|
|
250
|
-
expect(mkdirEffect?._tag).toBe("MakeDir");
|
|
251
|
-
});
|
|
252
|
-
});
|
package/src/stamp/applyStamp.ts
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
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
|
-
".json": {},
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Apply a stamp to file content based on file extension.
|
|
54
|
-
* Returns the original content if the file type doesn't support comments.
|
|
55
|
-
*/
|
|
56
|
-
export default function applyStamp(
|
|
57
|
-
filePath: string,
|
|
58
|
-
content: string,
|
|
59
|
-
stamp: StampConfig,
|
|
60
|
-
): string {
|
|
61
|
-
const ext = path.extname(filePath).toLowerCase();
|
|
62
|
-
const style = COMMENT_STYLES[ext];
|
|
63
|
-
|
|
64
|
-
if (!style) {
|
|
65
|
-
return content;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const stampText = `Generated by ${stamp.generator} v${stamp.version}`;
|
|
69
|
-
let stampLine: string;
|
|
70
|
-
|
|
71
|
-
if (style.single && !style.preferBlock) {
|
|
72
|
-
stampLine = `${style.single} ${stampText}`;
|
|
73
|
-
} else if (style.blockStart && style.blockEnd) {
|
|
74
|
-
stampLine = `${style.blockStart} ${stampText} ${style.blockEnd}`;
|
|
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
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
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
|
-
}
|
package/src/stamp/types.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import ejsEngine from "./ejsEngine.js";
|
|
3
|
-
|
|
4
|
-
describe("ejsEngine", () => {
|
|
5
|
-
describe("render (sync)", () => {
|
|
6
|
-
it("renders a template string synchronously", () => {
|
|
7
|
-
expect(ejsEngine.render("Hello, <%= name %>!", { name: "World" })).toBe(
|
|
8
|
-
"Hello, World!",
|
|
9
|
-
);
|
|
10
|
-
});
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
describe("renderAsync", () => {
|
|
14
|
-
it("renders a template string asynchronously", async () => {
|
|
15
|
-
const result = await ejsEngine.renderAsync("Hello, <%= name %>!", {
|
|
16
|
-
name: "Async",
|
|
17
|
-
});
|
|
18
|
-
expect(result).toBe("Hello, Async!");
|
|
19
|
-
});
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
describe("renderFile", () => {
|
|
23
|
-
it("renders a template file", async () => {
|
|
24
|
-
// Use a known fixture or just verify it throws for non-existent files
|
|
25
|
-
await expect(
|
|
26
|
-
ejsEngine.renderFile("/nonexistent-path.ejs", {}),
|
|
27
|
-
).rejects.toThrow();
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
});
|
|
@@ -1,21 +0,0 @@
|
|
|
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;
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import type StampConfig from "../types/StampConfig.js";
|
|
3
|
-
import generateStamp from "./generateStamp.js";
|
|
4
|
-
|
|
5
|
-
const stamp: StampConfig = { generator: "test-gen", version: "1.0.0" };
|
|
6
|
-
|
|
7
|
-
describe("generateStamp", () => {
|
|
8
|
-
it("generates single-line stamp for .ts files", () => {
|
|
9
|
-
expect(generateStamp("file.ts", stamp)).toBe(
|
|
10
|
-
"// Generated by test-gen v1.0.0",
|
|
11
|
-
);
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
it("generates block stamp for .css files (preferBlock)", () => {
|
|
15
|
-
expect(generateStamp("style.css", stamp)).toBe(
|
|
16
|
-
"/* Generated by test-gen v1.0.0 */",
|
|
17
|
-
);
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
it("generates block stamp for .html files (block only, no single)", () => {
|
|
21
|
-
expect(generateStamp("page.html", stamp)).toBe(
|
|
22
|
-
"<!-- Generated by test-gen v1.0.0 -->",
|
|
23
|
-
);
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
it("generates JSX stamp for .mdx files", () => {
|
|
27
|
-
expect(generateStamp("doc.mdx", stamp)).toBe(
|
|
28
|
-
"{/* Generated by test-gen v1.0.0 */}",
|
|
29
|
-
);
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
it("generates hash stamp for .py files", () => {
|
|
33
|
-
expect(generateStamp("main.py", stamp)).toBe(
|
|
34
|
-
"# Generated by test-gen v1.0.0",
|
|
35
|
-
);
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
it("returns null for .json (no single or block comment)", () => {
|
|
39
|
-
expect(generateStamp("data.json", stamp)).toBeNull();
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
it("returns null for unknown extensions", () => {
|
|
43
|
-
expect(generateStamp("file.dat", stamp)).toBeNull();
|
|
44
|
-
});
|
|
45
|
-
});
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import type StampConfig from "../types/StampConfig.js";
|
|
2
|
-
import getCommentStyle from "./getCommentStyle.js";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Generate a stamp comment for a generated file.
|
|
6
|
-
* Returns null if the file type doesn't support comments.
|
|
7
|
-
*/
|
|
8
|
-
export default function generateStamp(
|
|
9
|
-
filePath: string,
|
|
10
|
-
options: StampConfig,
|
|
11
|
-
): string | null {
|
|
12
|
-
const style = getCommentStyle(filePath);
|
|
13
|
-
if (!style) return null;
|
|
14
|
-
if (!style.single && !style.blockStart) return null;
|
|
15
|
-
|
|
16
|
-
const stampText = `Generated by ${options.generator} v${options.version}`;
|
|
17
|
-
|
|
18
|
-
if (style.preferBlock || (!style.single && style.blockStart)) {
|
|
19
|
-
return `${style.blockStart} ${stampText} ${style.blockEnd}`;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/* v8 ignore next -- unreachable false branch: if we reach here, style.single is always truthy */
|
|
23
|
-
if (style.single) {
|
|
24
|
-
return `${style.single} ${stampText}`;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/* v8 ignore next -- structurally unreachable: line 14 guards !style.single && !style.blockStart */
|
|
28
|
-
throw new Error(`No comment syntax for stamp in ${filePath}`);
|
|
29
|
-
}
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import generatorComment from "./generatorComment.js";
|
|
3
|
-
|
|
4
|
-
describe("generatorComment", () => {
|
|
5
|
-
it("generates basic comment", () => {
|
|
6
|
-
expect(generatorComment("ComponentGenerator")).toBe(
|
|
7
|
-
"// Generated by ComponentGenerator",
|
|
8
|
-
);
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
it("includes version when specified", () => {
|
|
12
|
-
expect(generatorComment("ComponentGenerator", { version: "1.0.0" })).toBe(
|
|
13
|
-
"// Generated by ComponentGenerator v1.0.0",
|
|
14
|
-
);
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
it("includes timestamp when specified", () => {
|
|
18
|
-
expect(generatorComment("ComponentGenerator", { timestamp: true })).toMatch(
|
|
19
|
-
/^\/\/ Generated by ComponentGenerator on \d{4}-\d{2}-\d{2}/,
|
|
20
|
-
);
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
it("includes both version and timestamp", () => {
|
|
24
|
-
expect(
|
|
25
|
-
generatorComment("ComponentGenerator", {
|
|
26
|
-
version: "2.0.0",
|
|
27
|
-
timestamp: true,
|
|
28
|
-
}),
|
|
29
|
-
).toMatch(/^\/\/ Generated by ComponentGenerator v2\.0\.0 on \d{4}/);
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
it("handles generator name with spaces", () => {
|
|
33
|
-
expect(generatorComment("My Generator")).toBe(
|
|
34
|
-
"// Generated by My Generator",
|
|
35
|
-
);
|
|
36
|
-
});
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
describe("generatorComment (html format)", () => {
|
|
40
|
-
it("generates basic HTML comment", () => {
|
|
41
|
-
expect(generatorComment("ComponentGenerator", { format: "html" })).toBe(
|
|
42
|
-
"<!-- Generated by ComponentGenerator -->",
|
|
43
|
-
);
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
it("includes version when specified", () => {
|
|
47
|
-
expect(
|
|
48
|
-
generatorComment("ComponentGenerator", {
|
|
49
|
-
version: "1.0.0",
|
|
50
|
-
format: "html",
|
|
51
|
-
}),
|
|
52
|
-
).toBe("<!-- Generated by ComponentGenerator v1.0.0 -->");
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
it("includes timestamp when specified", () => {
|
|
56
|
-
expect(
|
|
57
|
-
generatorComment("ComponentGenerator", {
|
|
58
|
-
timestamp: true,
|
|
59
|
-
format: "html",
|
|
60
|
-
}),
|
|
61
|
-
).toMatch(/^<!-- Generated by ComponentGenerator on \d{4}-\d{2}-\d{2}/);
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
it("includes both version and timestamp", () => {
|
|
65
|
-
expect(
|
|
66
|
-
generatorComment("ComponentGenerator", {
|
|
67
|
-
version: "2.0.0",
|
|
68
|
-
timestamp: true,
|
|
69
|
-
format: "html",
|
|
70
|
-
}),
|
|
71
|
-
).toMatch(/^<!-- Generated by ComponentGenerator v2\.0\.0 on \d{4}/);
|
|
72
|
-
});
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
describe("generatorComment (jsx format)", () => {
|
|
76
|
-
it("generates JSX comment", () => {
|
|
77
|
-
expect(generatorComment("ComponentGenerator", { format: "jsx" })).toBe(
|
|
78
|
-
"{/* Generated by ComponentGenerator */}",
|
|
79
|
-
);
|
|
80
|
-
});
|
|
81
|
-
});
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
type CommentFormat = "line" | "html" | "jsx";
|
|
2
|
-
|
|
3
|
-
interface GeneratorCommentOptions {
|
|
4
|
-
version?: string;
|
|
5
|
-
timestamp?: boolean;
|
|
6
|
-
/** Comment format. Defaults to "line" (`// ...`). */
|
|
7
|
-
format?: CommentFormat;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
const DELIMITERS: Record<CommentFormat, { start: string; end: string }> = {
|
|
11
|
-
line: { start: "// ", end: "" },
|
|
12
|
-
html: { start: "<!-- ", end: " -->" },
|
|
13
|
-
jsx: { start: "{/* ", end: " */}" },
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Generate a metadata comment for generated files.
|
|
18
|
-
*
|
|
19
|
-
* @param format - "line" for `// ...`, "html" for `<!-- ... -->`, "jsx" for `{/* ... * /}`
|
|
20
|
-
*/
|
|
21
|
-
export default function generatorComment(
|
|
22
|
-
generatorName: string,
|
|
23
|
-
options?: GeneratorCommentOptions,
|
|
24
|
-
): string {
|
|
25
|
-
const parts = [`Generated by ${generatorName}`];
|
|
26
|
-
if (options?.version) parts.push(`v${options.version}`);
|
|
27
|
-
if (options?.timestamp) parts.push(`on ${new Date().toISOString()}`);
|
|
28
|
-
|
|
29
|
-
const format = options?.format ?? "line";
|
|
30
|
-
const { start, end } = DELIMITERS[format];
|
|
31
|
-
return `${start}${parts.join(" ")}${end}`;
|
|
32
|
-
}
|