@canonical/summon-component 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.
Files changed (153) hide show
  1. package/README.md +14 -26
  2. package/{src/index.ts → dist/esm/index.js} +5 -8
  3. package/dist/esm/index.js.map +1 -0
  4. package/dist/esm/lit/generator.js +152 -0
  5. package/dist/esm/lit/generator.js.map +1 -0
  6. package/dist/esm/lit/index.js +5 -0
  7. package/dist/esm/lit/index.js.map +1 -0
  8. package/dist/esm/lit/types.js +5 -0
  9. package/dist/esm/lit/types.js.map +1 -0
  10. package/dist/esm/react/generator.js +129 -0
  11. package/dist/esm/react/generator.js.map +1 -0
  12. package/dist/esm/react/index.js +5 -0
  13. package/dist/esm/react/index.js.map +1 -0
  14. package/dist/esm/react/types.js +5 -0
  15. package/dist/esm/react/types.js.map +1 -0
  16. package/dist/esm/shared/createComponentPathPrompt.js +23 -0
  17. package/dist/esm/shared/createComponentPathPrompt.js.map +1 -0
  18. package/dist/esm/shared/createTemplateContext.js +20 -0
  19. package/dist/esm/shared/createTemplateContext.js.map +1 -0
  20. package/dist/esm/shared/file-operations/appendExportToParentIndex.js +25 -0
  21. package/dist/esm/shared/file-operations/appendExportToParentIndex.js.map +1 -0
  22. package/dist/esm/shared/file-operations/dryRunWithFileState.js +25 -0
  23. package/dist/esm/shared/file-operations/dryRunWithFileState.js.map +1 -0
  24. package/{src/shared/file-operations/index.ts → dist/esm/shared/file-operations/index.js} +1 -0
  25. package/dist/esm/shared/file-operations/index.js.map +1 -0
  26. package/dist/esm/shared/file-operations/removeExportFromParentIndex.js +13 -0
  27. package/dist/esm/shared/file-operations/removeExportFromParentIndex.js.map +1 -0
  28. package/dist/esm/shared/file-operations/removeLineFromFile.js +19 -0
  29. package/dist/esm/shared/file-operations/removeLineFromFile.js.map +1 -0
  30. package/dist/esm/shared/index.js +11 -0
  31. package/dist/esm/shared/index.js.map +1 -0
  32. package/dist/esm/shared/loadTemplate.js +56 -0
  33. package/dist/esm/shared/loadTemplate.js.map +1 -0
  34. package/dist/esm/shared/packageName.js +11 -0
  35. package/dist/esm/shared/packageName.js.map +1 -0
  36. package/dist/esm/shared/sharedPrompts.js +28 -0
  37. package/dist/esm/shared/sharedPrompts.js.map +1 -0
  38. package/dist/esm/shared/string-helpers/getComponentName.js +8 -0
  39. package/dist/esm/shared/string-helpers/getComponentName.js.map +1 -0
  40. package/dist/esm/shared/string-helpers/getParentDir.js +8 -0
  41. package/dist/esm/shared/string-helpers/getParentDir.js.map +1 -0
  42. package/{src/shared/string-helpers/index.ts → dist/esm/shared/string-helpers/index.js} +1 -0
  43. package/dist/esm/shared/string-helpers/index.js.map +1 -0
  44. package/dist/esm/shared/types.js +5 -0
  45. package/dist/esm/shared/types.js.map +1 -0
  46. package/dist/esm/shared/validateComponentPath.js +27 -0
  47. package/dist/esm/shared/validateComponentPath.js.map +1 -0
  48. package/dist/esm/svelte/generator.js +165 -0
  49. package/dist/esm/svelte/generator.js.map +1 -0
  50. package/dist/esm/svelte/index.js +5 -0
  51. package/dist/esm/svelte/index.js.map +1 -0
  52. package/dist/esm/svelte/types.js +5 -0
  53. package/dist/esm/svelte/types.js.map +1 -0
  54. package/dist/types/index.d.ts +87 -0
  55. package/dist/types/index.d.ts.map +1 -0
  56. package/dist/types/lit/generator.d.ts +52 -0
  57. package/dist/types/lit/generator.d.ts.map +1 -0
  58. package/{src/lit/index.ts → dist/types/lit/index.d.ts} +1 -1
  59. package/dist/types/lit/index.d.ts.map +1 -0
  60. package/{src/lit/types.ts → dist/types/lit/types.d.ts} +1 -2
  61. package/dist/types/lit/types.d.ts.map +1 -0
  62. package/dist/types/react/generator.d.ts +28 -0
  63. package/dist/types/react/generator.d.ts.map +1 -0
  64. package/{src/react/index.ts → dist/types/react/index.d.ts} +1 -1
  65. package/dist/types/react/index.d.ts.map +1 -0
  66. package/{src/react/types.ts → dist/types/react/types.d.ts} +1 -2
  67. package/dist/types/react/types.d.ts.map +1 -0
  68. package/dist/types/shared/createComponentPathPrompt.d.ts +12 -0
  69. package/dist/types/shared/createComponentPathPrompt.d.ts.map +1 -0
  70. package/dist/types/shared/createTemplateContext.d.ts +9 -0
  71. package/dist/types/shared/createTemplateContext.d.ts.map +1 -0
  72. package/dist/types/shared/file-operations/appendExportToParentIndex.d.ts +8 -0
  73. package/dist/types/shared/file-operations/appendExportToParentIndex.d.ts.map +1 -0
  74. package/dist/types/shared/file-operations/dryRunWithFileState.d.ts +7 -0
  75. package/dist/types/shared/file-operations/dryRunWithFileState.d.ts.map +1 -0
  76. package/dist/types/shared/file-operations/index.d.ts +4 -0
  77. package/dist/types/shared/file-operations/index.d.ts.map +1 -0
  78. package/dist/types/shared/file-operations/removeExportFromParentIndex.d.ts +7 -0
  79. package/dist/types/shared/file-operations/removeExportFromParentIndex.d.ts.map +1 -0
  80. package/dist/types/shared/file-operations/removeLineFromFile.d.ts +7 -0
  81. package/dist/types/shared/file-operations/removeLineFromFile.d.ts.map +1 -0
  82. package/{src/shared/index.ts → dist/types/shared/index.d.ts} +2 -1
  83. package/dist/types/shared/index.d.ts.map +1 -0
  84. package/dist/types/shared/loadTemplate.d.ts +20 -0
  85. package/dist/types/shared/loadTemplate.d.ts.map +1 -0
  86. package/dist/types/shared/packageName.d.ts +11 -0
  87. package/dist/types/shared/packageName.d.ts.map +1 -0
  88. package/dist/types/shared/sharedPrompts.d.ts +7 -0
  89. package/dist/types/shared/sharedPrompts.d.ts.map +1 -0
  90. package/{src/shared/string-helpers/getComponentName.ts → dist/types/shared/string-helpers/getComponentName.d.ts} +2 -5
  91. package/dist/types/shared/string-helpers/getComponentName.d.ts.map +1 -0
  92. package/{src/shared/string-helpers/getParentDir.ts → dist/types/shared/string-helpers/getParentDir.d.ts} +2 -5
  93. package/dist/types/shared/string-helpers/getParentDir.d.ts.map +1 -0
  94. package/dist/types/shared/string-helpers/index.d.ts +4 -0
  95. package/dist/types/shared/string-helpers/index.d.ts.map +1 -0
  96. package/dist/types/shared/types.d.ts +34 -0
  97. package/dist/types/shared/types.d.ts.map +1 -0
  98. package/dist/types/shared/validateComponentPath.d.ts +9 -0
  99. package/dist/types/shared/validateComponentPath.d.ts.map +1 -0
  100. package/dist/types/svelte/generator.d.ts +52 -0
  101. package/dist/types/svelte/generator.d.ts.map +1 -0
  102. package/{src/svelte/index.ts → dist/types/svelte/index.d.ts} +1 -1
  103. package/dist/types/svelte/index.d.ts.map +1 -0
  104. package/{src/svelte/types.ts → dist/types/svelte/types.d.ts} +3 -4
  105. package/dist/types/svelte/types.d.ts.map +1 -0
  106. package/package.json +21 -10
  107. package/CHANGELOG.md +0 -245
  108. package/CONVENTIONS.md +0 -184
  109. package/biome.json +0 -6
  110. package/src/index.test.ts +0 -514
  111. package/src/lit/generator.ts +0 -190
  112. package/src/react/generator.ts +0 -176
  113. package/src/shared/createComponentPathPrompt.ts +0 -28
  114. package/src/shared/createTemplateContext.ts +0 -29
  115. package/src/shared/file-operations/appendExportToParentIndex.test.ts +0 -82
  116. package/src/shared/file-operations/appendExportToParentIndex.ts +0 -40
  117. package/src/shared/file-operations/dryRunWithFileState.ts +0 -28
  118. package/src/shared/file-operations/removeExportFromParentIndex.test.ts +0 -52
  119. package/src/shared/file-operations/removeExportFromParentIndex.ts +0 -21
  120. package/src/shared/file-operations/removeLineFromFile.test.ts +0 -66
  121. package/src/shared/file-operations/removeLineFromFile.ts +0 -29
  122. package/src/shared/loadTemplate.test.ts +0 -100
  123. package/src/shared/loadTemplate.ts +0 -70
  124. package/src/shared/sharedPrompts.ts +0 -30
  125. package/src/shared/string-helpers/getComponentName.test.ts +0 -18
  126. package/src/shared/string-helpers/getParentDir.test.ts +0 -16
  127. package/src/shared/types.ts +0 -36
  128. package/src/shared/validateComponentPath.test.ts +0 -40
  129. package/src/shared/validateComponentPath.ts +0 -16
  130. package/src/svelte/generator.ts +0 -224
  131. package/src/testing/integration/generators-undo.test.ts +0 -200
  132. package/tsconfig.json +0 -9
  133. package/vitest.config.ts +0 -18
  134. /package/{src → dist/esm}/templates/lit/component.ts.ejs +0 -0
  135. /package/{src → dist/esm}/templates/lit/index.ts.ejs +0 -0
  136. /package/{src → dist/esm}/templates/lit/stories.ts.ejs +0 -0
  137. /package/{src → dist/esm}/templates/lit/styles.css.ejs +0 -0
  138. /package/{src → dist/esm}/templates/lit/tests.ts.ejs +0 -0
  139. /package/{src → dist/esm}/templates/lit/types.ts.ejs +0 -0
  140. /package/{src → dist/esm}/templates/react/component.tsx.ejs +0 -0
  141. /package/{src → dist/esm}/templates/react/index.ts.ejs +0 -0
  142. /package/{src → dist/esm}/templates/react/ssr.test.tsx.ejs +0 -0
  143. /package/{src → dist/esm}/templates/react/stories.tsx.ejs +0 -0
  144. /package/{src → dist/esm}/templates/react/test.tsx.ejs +0 -0
  145. /package/{src → dist/esm}/templates/react/types.ts.ejs +0 -0
  146. /package/{src → dist/esm}/templates/shared/styles.css.ejs +0 -0
  147. /package/{src → dist/esm}/templates/svelte/component.svelte.ejs +0 -0
  148. /package/{src → dist/esm}/templates/svelte/index.ts.ejs +0 -0
  149. /package/{src → dist/esm}/templates/svelte/ssr.test.ts.ejs +0 -0
  150. /package/{src → dist/esm}/templates/svelte/stories.svelte.ejs +0 -0
  151. /package/{src → dist/esm}/templates/svelte/stories.ts.ejs +0 -0
  152. /package/{src → dist/esm}/templates/svelte/test.ts.ejs +0 -0
  153. /package/{src → dist/esm}/templates/svelte/types.ts.ejs +0 -0
package/src/index.test.ts DELETED
@@ -1,514 +0,0 @@
1
- /**
2
- * Tests for @canonical/summon-component generators
3
- *
4
- * Uses dry-run to verify generator output without writing files.
5
- * Uses dryRunWith with ReadFile mock to test template rendering.
6
- */
7
-
8
- import * as fs from "node:fs";
9
- import { dryRun, dryRunWith, type Effect, type Task } from "@canonical/task";
10
- import { describe, expect, it } from "vitest";
11
- import { generators } from "./index.js";
12
-
13
- /**
14
- * Helper: dry-run with actual file reading for templates.
15
- * This allows testing template output without writing files.
16
- */
17
- const dryRunWithTemplates = <A>(task: Task<A>) => {
18
- const mocks = new Map<string, (effect: Effect) => unknown>([
19
- [
20
- "ReadFile",
21
- (effect) => {
22
- const e = effect as { path: string };
23
- // Actually read template files
24
- if (e.path.endsWith(".ejs")) {
25
- return fs.readFileSync(e.path, "utf-8");
26
- }
27
- return `[mock content of ${e.path}]`;
28
- },
29
- ],
30
- ]);
31
- return dryRunWith(task, mocks);
32
- };
33
-
34
- describe("generators barrel", () => {
35
- it("exports component/react generator", () => {
36
- expect(generators["component/react"]).toBeDefined();
37
- expect(generators["component/react"].meta.name).toBe("component/react");
38
- });
39
-
40
- it("exports component/svelte generator", () => {
41
- expect(generators["component/svelte"]).toBeDefined();
42
- expect(generators["component/svelte"].meta.name).toBe("component/svelte");
43
- });
44
-
45
- it("exports component/lit generator", () => {
46
- expect(generators["component/lit"]).toBeDefined();
47
- expect(generators["component/lit"].meta.name).toBe("component/lit");
48
- });
49
- });
50
-
51
- describe("component/react generator", () => {
52
- const generator = generators["component/react"];
53
-
54
- describe("meta", () => {
55
- it("has correct name", () => {
56
- expect(generator.meta.name).toBe("component/react");
57
- });
58
-
59
- it("has description", () => {
60
- expect(generator.meta.description).toContain("React component");
61
- });
62
-
63
- it("has version", () => {
64
- expect(generator.meta.version).toBe("0.1.0");
65
- });
66
-
67
- it("has help text", () => {
68
- expect(generator.meta.help).toBeDefined();
69
- expect(generator.meta.help).toContain("TypeScript");
70
- });
71
-
72
- it("has examples", () => {
73
- expect(generator.meta.examples).toBeDefined();
74
- expect(generator.meta.examples?.length).toBeGreaterThan(0);
75
- });
76
- });
77
-
78
- describe("prompts", () => {
79
- it("has componentPath prompt", () => {
80
- const prompt = generator.prompts.find((p) => p.name === "componentPath");
81
- expect(prompt).toBeDefined();
82
- expect(prompt?.type).toBe("text");
83
- expect(prompt?.default).toBe("src/components/MyComponent");
84
- });
85
-
86
- it("has withStyles prompt", () => {
87
- const prompt = generator.prompts.find((p) => p.name === "withStyles");
88
- expect(prompt).toBeDefined();
89
- expect(prompt?.type).toBe("confirm");
90
- expect(prompt?.default).toBe(true);
91
- });
92
-
93
- it("has withStories prompt", () => {
94
- const prompt = generator.prompts.find((p) => p.name === "withStories");
95
- expect(prompt).toBeDefined();
96
- expect(prompt?.type).toBe("confirm");
97
- expect(prompt?.default).toBe(true);
98
- });
99
-
100
- it("has withSsrTests prompt", () => {
101
- const prompt = generator.prompts.find((p) => p.name === "withSsrTests");
102
- expect(prompt).toBeDefined();
103
- expect(prompt?.type).toBe("confirm");
104
- expect(prompt?.default).toBe(true);
105
- });
106
- });
107
-
108
- describe("generate", () => {
109
- it("creates component files with all options enabled", () => {
110
- const task = generator.generate({
111
- componentPath: "src/components/Button",
112
- withStyles: true,
113
- withStories: true,
114
- withSsrTests: true,
115
- });
116
-
117
- const result = dryRun(task);
118
- const paths = result.effects
119
- .filter((e) => e._tag === "WriteFile")
120
- .map((e) => (e as { path: string }).path);
121
-
122
- expect(paths).toContain("src/components/Button/Button.tsx");
123
- expect(paths).toContain("src/components/Button/types.ts");
124
- expect(paths).toContain("src/components/Button/index.ts");
125
- expect(paths).toContain("src/components/Button/Button.tests.tsx");
126
- expect(paths).toContain("src/components/Button/Button.ssr.tests.tsx");
127
- expect(paths).toContain("src/components/Button/Button.stories.tsx");
128
- expect(paths).toContain("src/components/Button/styles.css");
129
- });
130
-
131
- it("creates minimal component without optional files", () => {
132
- const task = generator.generate({
133
- componentPath: "src/components/Icon",
134
- withStyles: false,
135
- withStories: false,
136
- withSsrTests: false,
137
- });
138
-
139
- const result = dryRun(task);
140
- const paths = result.effects
141
- .filter((e) => e._tag === "WriteFile")
142
- .map((e) => (e as { path: string }).path);
143
-
144
- expect(paths).toContain("src/components/Icon/Icon.tsx");
145
- expect(paths).toContain("src/components/Icon/types.ts");
146
- expect(paths).toContain("src/components/Icon/index.ts");
147
- expect(paths).toContain("src/components/Icon/Icon.tests.tsx");
148
- expect(paths).not.toContain("src/components/Icon/Icon.ssr.tests.tsx");
149
- expect(paths).not.toContain("src/components/Icon/Icon.stories.tsx");
150
- expect(paths).not.toContain("src/components/Icon/styles.css");
151
- });
152
-
153
- it("extracts component name from path", () => {
154
- const task = generator.generate({
155
- componentPath: "src/components/MyButton",
156
- withStyles: false,
157
- withStories: false,
158
- withSsrTests: false,
159
- });
160
-
161
- const result = dryRunWithTemplates(task);
162
- const mainFile = result.effects.find(
163
- (e) =>
164
- e._tag === "WriteFile" &&
165
- (e as { path: string }).path.endsWith("MyButton.tsx"),
166
- );
167
-
168
- expect(mainFile).toBeDefined();
169
- const content = (mainFile as { content: string }).content;
170
- expect(content).toContain("MyButton");
171
- expect(content).toContain("MyButtonProps");
172
- });
173
-
174
- it("generates kebab-case class name", () => {
175
- const task = generator.generate({
176
- componentPath: "src/components/MyButton",
177
- withStyles: false,
178
- withStories: false,
179
- withSsrTests: false,
180
- });
181
-
182
- const result = dryRunWithTemplates(task);
183
- const mainFile = result.effects.find(
184
- (e) =>
185
- e._tag === "WriteFile" &&
186
- (e as { path: string }).path.endsWith("MyButton.tsx"),
187
- );
188
-
189
- const content = (mainFile as { content: string }).content;
190
- expect(content).toContain("my-button");
191
- });
192
-
193
- it("appends export to parent index", () => {
194
- const task = generator.generate({
195
- componentPath: "src/components/Card",
196
- withStyles: false,
197
- withStories: false,
198
- withSsrTests: false,
199
- });
200
-
201
- const result = dryRun(task);
202
- const parentIndex = result.effects.find(
203
- (e) =>
204
- e._tag === "WriteFile" &&
205
- (e as { path: string }).path === "src/components/index.ts",
206
- );
207
-
208
- expect(parentIndex).toBeDefined();
209
- const content = (parentIndex as { content: string }).content;
210
- expect(content).toContain('export * from "./Card/index.js"');
211
- });
212
- });
213
- });
214
-
215
- describe("component/svelte generator", () => {
216
- const generator = generators["component/svelte"];
217
-
218
- describe("meta", () => {
219
- it("has correct name", () => {
220
- expect(generator.meta.name).toBe("component/svelte");
221
- });
222
-
223
- it("has description", () => {
224
- expect(generator.meta.description).toContain("Svelte");
225
- });
226
-
227
- it("has version", () => {
228
- expect(generator.meta.version).toBe("0.1.0");
229
- });
230
- });
231
-
232
- describe("prompts", () => {
233
- it("has componentPath prompt with svelte default", () => {
234
- const prompt = generator.prompts.find((p) => p.name === "componentPath");
235
- expect(prompt).toBeDefined();
236
- expect(prompt?.default).toBe("src/lib/components/MyComponent");
237
- });
238
-
239
- it("has useTsStories prompt", () => {
240
- const prompt = generator.prompts.find((p) => p.name === "useTsStories");
241
- expect(prompt).toBeDefined();
242
- expect(prompt?.type).toBe("confirm");
243
- expect(prompt?.default).toBe(false);
244
- });
245
-
246
- it("useTsStories prompt when condition checks withStories", () => {
247
- const prompt = generator.prompts.find((p) => p.name === "useTsStories");
248
- expect(
249
- prompt?.when?.({ withStories: true } as Record<string, unknown>),
250
- ).toBe(true);
251
- expect(
252
- prompt?.when?.({ withStories: false } as Record<string, unknown>),
253
- ).toBe(false);
254
- });
255
- });
256
-
257
- describe("generate", () => {
258
- it("creates svelte component files with all options", () => {
259
- const task = generator.generate({
260
- componentPath: "src/lib/components/Button",
261
- withStyles: true,
262
- withStories: true,
263
- useTsStories: false,
264
- withSsrTests: true,
265
- });
266
-
267
- const result = dryRun(task);
268
- const paths = result.effects
269
- .filter((e) => e._tag === "WriteFile")
270
- .map((e) => (e as { path: string }).path);
271
-
272
- expect(paths).toContain("src/lib/components/Button/Button.svelte");
273
- expect(paths).toContain("src/lib/components/Button/types.ts");
274
- expect(paths).toContain("src/lib/components/Button/index.ts");
275
- expect(paths).toContain(
276
- "src/lib/components/Button/Button.svelte.test.ts",
277
- );
278
- expect(paths).toContain("src/lib/components/Button/Button.ssr.test.ts");
279
- expect(paths).toContain(
280
- "src/lib/components/Button/Button.stories.svelte",
281
- );
282
- expect(paths).toContain("src/lib/components/Button/styles.css");
283
- });
284
-
285
- it("creates typescript stories when useTsStories is true", () => {
286
- const task = generator.generate({
287
- componentPath: "src/lib/components/Card",
288
- withStyles: false,
289
- withStories: true,
290
- useTsStories: true,
291
- withSsrTests: false,
292
- });
293
-
294
- const result = dryRun(task);
295
- const paths = result.effects
296
- .filter((e) => e._tag === "WriteFile")
297
- .map((e) => (e as { path: string }).path);
298
-
299
- expect(paths).toContain("src/lib/components/Card/Card.stories.ts");
300
- expect(paths).not.toContain(
301
- "src/lib/components/Card/Card.stories.svelte",
302
- );
303
- });
304
-
305
- it("generates svelte 5 runes syntax", () => {
306
- const task = generator.generate({
307
- componentPath: "src/lib/components/Toggle",
308
- withStyles: false,
309
- withStories: false,
310
- useTsStories: false,
311
- withSsrTests: false,
312
- });
313
-
314
- const result = dryRunWithTemplates(task);
315
- const mainFile = result.effects.find(
316
- (e) =>
317
- e._tag === "WriteFile" &&
318
- (e as { path: string }).path.endsWith("Toggle.svelte"),
319
- );
320
-
321
- const content = (mainFile as { content: string }).content;
322
- expect(content).toContain("$props()");
323
- expect(content).toContain("@render children");
324
- });
325
-
326
- it("imports styles.css when withStyles is enabled", () => {
327
- const task = generator.generate({
328
- componentPath: "src/lib/components/Banner",
329
- withStyles: true,
330
- withStories: false,
331
- useTsStories: false,
332
- withSsrTests: false,
333
- });
334
-
335
- const result = dryRunWithTemplates(task);
336
- const mainFile = result.effects.find(
337
- (e) =>
338
- e._tag === "WriteFile" &&
339
- (e as { path: string }).path.endsWith("Banner.svelte"),
340
- );
341
-
342
- const content = (mainFile as { content: string }).content;
343
- expect(content).toContain('import "./styles.css";');
344
- expect(content).not.toContain("<style>");
345
- });
346
- });
347
- });
348
-
349
- // =============================================================================
350
- // Lit Generator
351
- // =============================================================================
352
-
353
- describe("component/lit generator", () => {
354
- const generator = generators["component/lit"];
355
-
356
- describe("meta", () => {
357
- it("has correct name", () => {
358
- expect(generator.meta.name).toBe("component/lit");
359
- });
360
-
361
- it("has description containing Lit", () => {
362
- expect(generator.meta.description).toContain("Lit");
363
- });
364
-
365
- it("has version", () => {
366
- expect(generator.meta.version).toBe("0.1.0");
367
- });
368
-
369
- it("has help text mentioning decorators", () => {
370
- expect(generator.meta.help).toBeDefined();
371
- expect(generator.meta.help).toContain("decorators");
372
- });
373
-
374
- it("has examples", () => {
375
- expect(generator.meta.examples).toBeDefined();
376
- expect(generator.meta.examples?.length).toBeGreaterThan(0);
377
- });
378
- });
379
-
380
- describe("prompts", () => {
381
- it("has componentPath prompt with lib default path", () => {
382
- const prompt = generator.prompts.find((p) => p.name === "componentPath");
383
- expect(prompt).toBeDefined();
384
- expect(prompt?.default).toBe("src/lib/components/MyComponent");
385
- });
386
-
387
- it("has withStyles prompt", () => {
388
- const prompt = generator.prompts.find((p) => p.name === "withStyles");
389
- expect(prompt).toBeDefined();
390
- expect(prompt?.type).toBe("confirm");
391
- expect(prompt?.default).toBe(true);
392
- });
393
-
394
- it("has withStories prompt", () => {
395
- const prompt = generator.prompts.find((p) => p.name === "withStories");
396
- expect(prompt).toBeDefined();
397
- expect(prompt?.type).toBe("confirm");
398
- expect(prompt?.default).toBe(true);
399
- });
400
-
401
- it("does not have withSsrTests prompt", () => {
402
- const prompt = generator.prompts.find((p) => p.name === "withSsrTests");
403
- expect(prompt).toBeUndefined();
404
- });
405
-
406
- it("does not have useTsStories prompt", () => {
407
- const prompt = generator.prompts.find((p) => p.name === "useTsStories");
408
- expect(prompt).toBeUndefined();
409
- });
410
- });
411
-
412
- describe("generate", () => {
413
- it("creates lit component files with all options enabled", () => {
414
- const task = generator.generate({
415
- componentPath: "src/lib/components/Button",
416
- withStyles: true,
417
- withStories: true,
418
- withSsrTests: false,
419
- });
420
-
421
- const result = dryRun(task);
422
- const paths = result.effects
423
- .filter((e) => e._tag === "WriteFile")
424
- .map((e) => (e as { path: string }).path);
425
-
426
- expect(paths).toContain("src/lib/components/Button/Button.ts");
427
- expect(paths).toContain("src/lib/components/Button/index.ts");
428
- expect(paths).toContain("src/lib/components/Button/types.ts");
429
- expect(paths).toContain("src/lib/components/Button/Button.tests.ts");
430
- expect(paths).toContain("src/lib/components/Button/Button.stories.ts");
431
- expect(paths).toContain("src/lib/components/Button/styles.css");
432
- });
433
-
434
- it("creates minimal component without optional files", () => {
435
- const task = generator.generate({
436
- componentPath: "src/lib/components/Icon",
437
- withStyles: false,
438
- withStories: false,
439
- withSsrTests: false,
440
- });
441
-
442
- const result = dryRun(task);
443
- const paths = result.effects
444
- .filter((e) => e._tag === "WriteFile")
445
- .map((e) => (e as { path: string }).path);
446
-
447
- expect(paths).toContain("src/lib/components/Icon/Icon.ts");
448
- expect(paths).toContain("src/lib/components/Icon/index.ts");
449
- expect(paths).toContain("src/lib/components/Icon/types.ts");
450
- expect(paths).toContain("src/lib/components/Icon/Icon.tests.ts");
451
- expect(paths).not.toContain("src/lib/components/Icon/Icon.stories.ts");
452
- expect(paths).not.toContain("src/lib/components/Icon/styles.css");
453
- });
454
-
455
- it("extracts component name from path", () => {
456
- const task = generator.generate({
457
- componentPath: "src/lib/components/MyButton",
458
- withStyles: false,
459
- withStories: false,
460
- withSsrTests: false,
461
- });
462
-
463
- const result = dryRunWithTemplates(task);
464
- const mainFile = result.effects.find(
465
- (e) =>
466
- e._tag === "WriteFile" &&
467
- (e as { path: string }).path.endsWith("MyButton.ts"),
468
- );
469
-
470
- expect(mainFile).toBeDefined();
471
- const content = (mainFile as { content: string }).content;
472
- expect(content).toContain("MyButton");
473
- });
474
-
475
- it("generates kebab-case tag name", () => {
476
- const task = generator.generate({
477
- componentPath: "src/lib/components/MyButton",
478
- withStyles: false,
479
- withStories: false,
480
- withSsrTests: false,
481
- });
482
-
483
- const result = dryRunWithTemplates(task);
484
- const mainFile = result.effects.find(
485
- (e) =>
486
- e._tag === "WriteFile" &&
487
- (e as { path: string }).path.endsWith("MyButton.ts"),
488
- );
489
-
490
- const content = (mainFile as { content: string }).content;
491
- expect(content).toContain("my-button");
492
- });
493
-
494
- it("appends export to parent index", () => {
495
- const task = generator.generate({
496
- componentPath: "src/lib/components/Card",
497
- withStyles: false,
498
- withStories: false,
499
- withSsrTests: false,
500
- });
501
-
502
- const result = dryRun(task);
503
- const parentIndex = result.effects.find(
504
- (e) =>
505
- e._tag === "WriteFile" &&
506
- (e as { path: string }).path === "src/lib/components/index.ts",
507
- );
508
-
509
- expect(parentIndex).toBeDefined();
510
- const content = (parentIndex as { content: string }).content;
511
- expect(content).toContain('export * from "./Card/index.js"');
512
- });
513
- });
514
- });
@@ -1,190 +0,0 @@
1
- /**
2
- * Web Components Generator
3
- *
4
- * Generates a Lit web component with a consistent file structure:
5
- * - `ComponentName.ts` - Main component file (Lit element)
6
- * - `index.ts` - Barrel export
7
- * - `ComponentName.tests.ts` - Unit tests with Vitest
8
- * - `ComponentName.stories.ts` - Storybook stories (optional)
9
- * - `styles.css` - CSS styles (optional)
10
- *
11
- * @example
12
- * ```bash
13
- * summon component lit --component-path=src/lib/components/Button
14
- * summon component lit --component-path=src/lib/components/Card --with-styles --with-stories
15
- * ```
16
- *
17
- * @module
18
- */
19
-
20
- import * as path from "node:path";
21
- import { fileURLToPath } from "node:url";
22
- import { type GeneratorDefinition, template } from "@canonical/summon-core";
23
- import { debug, info, mkdir, sequence_, when } from "@canonical/task";
24
- import pkg from "../../package.json" with { type: "json" };
25
-
26
- import {
27
- appendExportToParentIndex,
28
- createComponentPathPrompt,
29
- createTemplateContext,
30
- getComponentName,
31
- getParentDir,
32
- } from "../shared/index.js";
33
- import loadTemplate from "../shared/loadTemplate.js";
34
- import type { LitAnswers } from "./types.js";
35
-
36
- // =============================================================================
37
- // Template Paths & Content (loaded eagerly via top-level await)
38
- // =============================================================================
39
-
40
- const __dirname = path.dirname(fileURLToPath(import.meta.url));
41
- const templatesDir = path.join(__dirname, "..", "templates");
42
-
43
- const litTemplates = {
44
- component: await loadTemplate(
45
- path.join(templatesDir, "lit", "component.ts.ejs"),
46
- ),
47
- index: await loadTemplate(path.join(templatesDir, "lit", "index.ts.ejs")),
48
- types: await loadTemplate(path.join(templatesDir, "lit", "types.ts.ejs")),
49
- tests: await loadTemplate(path.join(templatesDir, "lit", "tests.ts.ejs")),
50
- stories: await loadTemplate(path.join(templatesDir, "lit", "stories.ts.ejs")),
51
- styles: await loadTemplate(path.join(templatesDir, "lit", "styles.css.ejs")),
52
- };
53
-
54
- // =============================================================================
55
- // Custom Prompts (without SSR tests)
56
- // =============================================================================
57
-
58
- const litPrompts = [
59
- {
60
- name: "withStyles",
61
- type: "confirm",
62
- message: "Include styles?",
63
- default: true,
64
- group: "Options",
65
- },
66
- {
67
- name: "withStories",
68
- type: "confirm",
69
- message: "Include Storybook stories?",
70
- default: true,
71
- group: "Options",
72
- },
73
- ] as const;
74
-
75
- // =============================================================================
76
- // Generator Definition
77
- // =============================================================================
78
-
79
- /**
80
- * Web components generator definition.
81
- *
82
- * This generator creates a complete Lit web component with TypeScript support,
83
- * testing infrastructure, and optional Storybook stories and CSS styles.
84
- *
85
- * @see {@link https://summon.dev/generators/component/lit} for documentation
86
- */
87
- const generator = {
88
- meta: {
89
- name: "component/lit",
90
- displayName: `${pkg.name}:lit`,
91
- description:
92
- "Generate a Lit web component with TypeScript, tests, stories, and styles",
93
- version: "0.1.0",
94
- help: `Generate a Lit web component with TypeScript, tests, stories, and styles.
95
-
96
- FEATURES:
97
- - Lit element with TypeScript decorators
98
- - Unit tests with Vitest and shadow DOM testing
99
- - Storybook integration with @storybook/web-components-vite (optional)
100
- - CSS styles (optional)
101
- - Auto-export from parent index.ts
102
-
103
- The component name is extracted from the path and must be PascalCase.
104
- For example, 'src/lib/components/Button' creates a 'Button' component
105
- with the custom element tag 'button'.`,
106
- examples: [
107
- "summon component lit --component-path=src/lib/components/Button",
108
- "summon component lit --component-path=src/lib/components/Card --with-styles --with-stories",
109
- "summon component lit --component-path=src/lib/components/Modal --no-with-styles",
110
- "summon component lit --component-path=src/lib/components/Button --dry-run",
111
- ],
112
- },
113
-
114
- prompts: [createComponentPathPrompt("lit"), ...litPrompts],
115
-
116
- generate: (answers) => {
117
- const componentName = getComponentName(answers.componentPath);
118
- const componentDir = answers.componentPath;
119
- const parentDir = getParentDir(answers.componentPath);
120
- const ctx = createTemplateContext(answers, "lit");
121
-
122
- return sequence_([
123
- info(`Generating Lit web component: ${componentName}`),
124
-
125
- debug("Creating component directory"),
126
- mkdir(componentDir),
127
-
128
- debug("Creating main component file"),
129
- template({
130
- source: litTemplates.component.source,
131
- content: litTemplates.component.content,
132
- dest: path.join(componentDir, `${componentName}.ts`),
133
- vars: ctx,
134
- }),
135
-
136
- debug("Creating index barrel file"),
137
- template({
138
- source: litTemplates.index.source,
139
- content: litTemplates.index.content,
140
- dest: path.join(componentDir, "index.ts"),
141
- vars: ctx,
142
- }),
143
-
144
- debug("Creating types file"),
145
- template({
146
- source: litTemplates.types.source,
147
- content: litTemplates.types.content,
148
- dest: path.join(componentDir, "types.ts"),
149
- vars: ctx,
150
- }),
151
-
152
- debug("Creating test file"),
153
- template({
154
- source: litTemplates.tests.source,
155
- content: litTemplates.tests.content,
156
- dest: path.join(componentDir, `${componentName}.tests.ts`),
157
- vars: ctx,
158
- }),
159
-
160
- when(answers.withStories, debug("Creating stories file")),
161
- when(
162
- answers.withStories,
163
- template({
164
- source: litTemplates.stories.source,
165
- content: litTemplates.stories.content,
166
- dest: path.join(componentDir, `${componentName}.stories.ts`),
167
- vars: ctx,
168
- }),
169
- ),
170
-
171
- when(answers.withStyles, debug("Creating styles file")),
172
- when(
173
- answers.withStyles,
174
- template({
175
- source: litTemplates.styles.source,
176
- content: litTemplates.styles.content,
177
- dest: path.join(componentDir, "styles.css"),
178
- vars: ctx,
179
- }),
180
- ),
181
-
182
- debug("Updating parent index.ts with export"),
183
- appendExportToParentIndex(parentDir, componentName),
184
-
185
- info(`Created ${componentName} component at ${componentDir}`),
186
- ]);
187
- },
188
- } as const satisfies GeneratorDefinition<LitAnswers>;
189
-
190
- export default generator;