@canonical/summon-component 0.29.0 → 0.30.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (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 +16 -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 +5 -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
@@ -1,66 +0,0 @@
1
- import { collectUndos } from "@canonical/task";
2
- import { describe, expect, it } from "vitest";
3
- import dryRunWithFileState from "./dryRunWithFileState.js";
4
- import removeLineFromFile from "./removeLineFromFile.js";
5
-
6
- describe("removeLineFromFile", () => {
7
- it("removes a matching line from multi-line content", () => {
8
- const result = dryRunWithFileState(
9
- removeLineFromFile("/src/index.ts", "line2"),
10
- { "/src/index.ts": "line1\nline2\nline3" },
11
- );
12
-
13
- const writeEffect = result.effects.find((e) => e._tag === "WriteFile");
14
- expect(writeEffect).toBeDefined();
15
- expect((writeEffect as { content: string }).content).toBe("line1\nline3");
16
- });
17
-
18
- it("writes back content when line is not present", () => {
19
- const result = dryRunWithFileState(
20
- removeLineFromFile("/src/index.ts", "nonexistent"),
21
- { "/src/index.ts": "line1\nline3" },
22
- );
23
-
24
- const writeEffect = result.effects.find((e) => e._tag === "WriteFile");
25
- expect(writeEffect).toBeDefined();
26
- expect((writeEffect as { content: string }).content).toBe("line1\nline3");
27
- });
28
-
29
- it("deletes file when content becomes empty after removal", () => {
30
- const result = dryRunWithFileState(
31
- removeLineFromFile("/src/index.ts", "only-line"),
32
- { "/src/index.ts": "only-line\n" },
33
- );
34
-
35
- const deleteEffect = result.effects.find((e) => e._tag === "DeleteFile");
36
- expect(deleteEffect).toBeDefined();
37
- expect((deleteEffect as { path: string }).path).toBe("/src/index.ts");
38
- });
39
-
40
- it("trims whitespace when matching lines", () => {
41
- const result = dryRunWithFileState(
42
- removeLineFromFile("/src/index.ts", " target "),
43
- { "/src/index.ts": "before\n target \nafter" },
44
- );
45
-
46
- const writeEffect = result.effects.find((e) => e._tag === "WriteFile");
47
- expect(writeEffect).toBeDefined();
48
- expect((writeEffect as { content: string }).content).toBe("before\nafter");
49
- });
50
-
51
- it("removes all matching lines", () => {
52
- const result = dryRunWithFileState(
53
- removeLineFromFile("/src/index.ts", "dup"),
54
- { "/src/index.ts": "dup\nkeep\ndup" },
55
- );
56
-
57
- const writeEffect = result.effects.find((e) => e._tag === "WriteFile");
58
- expect(writeEffect).toBeDefined();
59
- expect((writeEffect as { content: string }).content).toBe("keep");
60
- });
61
-
62
- it("uses { undo: null } on internal effects to prevent undo chains", () => {
63
- const undos = collectUndos(removeLineFromFile("/src/index.ts", "line"));
64
- expect(undos).toHaveLength(0);
65
- });
66
- });
@@ -1,29 +0,0 @@
1
- import {
2
- deleteFile,
3
- flatMap,
4
- readFile,
5
- type Task,
6
- writeFile,
7
- } from "@canonical/task";
8
-
9
- /**
10
- * Remove a specific line from a file. If the file becomes empty,
11
- * delete it entirely. Idempotent — no-op if the line is not present.
12
- */
13
- export default function removeLineFromFile(
14
- filePath: string,
15
- line: string,
16
- ): Task<void> {
17
- return flatMap(readFile(filePath), (content) => {
18
- const trimmedLine = line.trim();
19
- const filtered = content
20
- .split("\n")
21
- .filter((l) => l.trim() !== trimmedLine)
22
- .join("\n");
23
-
24
- if (filtered.trim() === "") {
25
- return deleteFile(filePath, { undo: null });
26
- }
27
- return writeFile(filePath, filtered, { undo: null });
28
- });
29
- }
@@ -1,100 +0,0 @@
1
- import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
2
- import { tmpdir } from "node:os";
3
- import { join } from "node:path";
4
- import { afterEach, beforeEach, describe, expect, it } from "vitest";
5
- import loadTemplate from "./loadTemplate.js";
6
-
7
- describe("loadTemplate", () => {
8
- let dir: string;
9
-
10
- beforeEach(() => {
11
- dir = mkdtempSync(join(tmpdir(), "pragma-load-template-"));
12
- });
13
-
14
- afterEach(() => {
15
- rmSync(dir, { recursive: true, force: true });
16
- // Ensure no embedded-files stub leaks between tests.
17
- delete (globalThis as { Bun?: unknown }).Bun;
18
- });
19
-
20
- it("reads template content from the filesystem", async () => {
21
- const path = join(dir, "component.tsx.ejs");
22
- writeFileSync(path, "export const <%= name %> = () => null;");
23
-
24
- const result = await loadTemplate(path);
25
-
26
- expect(result.source).toBe(path);
27
- expect(result.content).toBe("export const <%= name %> = () => null;");
28
- });
29
-
30
- it("falls back to Bun.embeddedFiles when the file is not on disk", async () => {
31
- // No file written to disk → fs read fails → embedded lookup runs.
32
- // Blob names are content-hashed (e.g. "component-a1b2c3.tsx.ejs"); the
33
- // loader dehashes by stripping the "-<hash>." segment.
34
- const missingPath = join(dir, "component.tsx.ejs");
35
- (globalThis as { Bun?: unknown }).Bun = {
36
- embeddedFiles: [
37
- Object.assign(new Blob(["EMBEDDED CONTENT"]), {
38
- name: "component-a1b2c3d4.tsx.ejs",
39
- }),
40
- ],
41
- };
42
-
43
- const result = await loadTemplate(missingPath);
44
-
45
- expect(result.content).toBe("EMBEDDED CONTENT");
46
- expect(result.source).toBe(missingPath);
47
- });
48
-
49
- it("returns the disk content even when an embedded blob also exists", async () => {
50
- const path = join(dir, "types.ts.ejs");
51
- writeFileSync(path, "DISK WINS");
52
- (globalThis as { Bun?: unknown }).Bun = {
53
- embeddedFiles: [
54
- Object.assign(new Blob(["EMBEDDED"]), {
55
- name: "types-deadbeef.ts.ejs",
56
- }),
57
- ],
58
- };
59
-
60
- const result = await loadTemplate(path);
61
-
62
- expect(result.content).toBe("DISK WINS");
63
- });
64
-
65
- // A total miss (no disk file, no matching embedded blob) must fail loud:
66
- // callers await this for required templates and do not guard against empty
67
- // content, so a silent "" would generate blank files (notably in a compiled
68
- // binary, where the disk read always fails).
69
- it("throws on a total miss (no disk, no embedded match)", async () => {
70
- const missingPath = join(dir, "nonexistent.ejs");
71
-
72
- await expect(loadTemplate(missingPath)).rejects.toThrow(
73
- /Template not found/,
74
- );
75
- });
76
-
77
- it("throws when an embedded blob exists but none matches the basename", async () => {
78
- const missingPath = join(dir, "wanted.ejs");
79
- (globalThis as { Bun?: unknown }).Bun = {
80
- embeddedFiles: [
81
- Object.assign(new Blob(["x"]), { name: "other-cafef00d.ejs" }),
82
- ],
83
- };
84
-
85
- await expect(loadTemplate(missingPath)).rejects.toThrow(
86
- /Template not found/,
87
- );
88
- });
89
-
90
- it("throws when Bun is present but exposes no embeddedFiles", async () => {
91
- // Running under the Bun runtime (not a compiled binary): `Bun` exists but
92
- // `embeddedFiles` is undefined, so the embedded lookup is skipped.
93
- const missingPath = join(dir, "wanted.ejs");
94
- (globalThis as { Bun?: unknown }).Bun = {};
95
-
96
- await expect(loadTemplate(missingPath)).rejects.toThrow(
97
- /Template not found/,
98
- );
99
- });
100
- });
@@ -1,70 +0,0 @@
1
- /**
2
- * Load a template file eagerly, with compiled binary support.
3
- *
4
- * In interpreted mode, reads from the filesystem. In a compiled binary,
5
- * falls back to `Bun.embeddedFiles` blobs (async, requires top-level await).
6
- */
7
-
8
- import { readFileSync } from "node:fs";
9
-
10
- export interface LoadedTemplate {
11
- /** Original source path (for diagnostics and dry-run display). */
12
- readonly source: string;
13
- /** Template content string. */
14
- readonly content: string;
15
- }
16
-
17
- /**
18
- * Load a template from disk or embedded files.
19
- *
20
- * @param source - Absolute path to the template file.
21
- * @returns Loaded template with path and content.
22
- */
23
- export default async function loadTemplate(
24
- source: string,
25
- ): Promise<LoadedTemplate> {
26
- // Try filesystem first (works in interpreted mode)
27
- try {
28
- return { source, content: readFileSync(source, "utf-8") };
29
- } catch {
30
- // Fall through to embedded lookup
31
- }
32
-
33
- // Try Bun.embeddedFiles (works in compiled binary)
34
- if (typeof globalThis.Bun !== "undefined") {
35
- const blobs = (globalThis.Bun as { embeddedFiles?: readonly Blob[] })
36
- .embeddedFiles as ReadonlyArray<Blob & { name: string }> | undefined;
37
-
38
- if (blobs) {
39
- // KNOWN LIMITATION (basename collision): we match embedded blobs by
40
- // basename only, because Bun flattens embedded-asset names to the
41
- // filename (the source directory is not preserved). Several templates
42
- // share a basename across frameworks — types.ts.ejs, index.ts.ejs,
43
- // styles.css.ejs and stories.ts.ejs each exist in lit/, react/ and/or
44
- // svelte/. In a compiled binary this can resolve to the WRONG
45
- // framework's template (whichever blob is encountered first), producing
46
- // a silently incorrect generated file. The proper fix is build-side:
47
- // embed templates under directory-qualified unique names and match on
48
- // those. Deferred — needs verification against a real compiled binary
49
- // (see pragma-adrs/session/J.CLI_MCP_FINISH.md). Interpreted mode (the
50
- // common path) reads from disk above and is unaffected.
51
- const basename = source.slice(source.lastIndexOf("/") + 1);
52
- for (const blob of blobs) {
53
- const dehashed = blob.name.replace(/-[a-z0-9]+\./, ".");
54
- if (dehashed === basename) {
55
- const content = await blob.text();
56
- return { source, content };
57
- }
58
- }
59
- }
60
- }
61
-
62
- // Neither the filesystem nor embedded files yielded the template. Fail loud
63
- // rather than returning empty content: callers await this for required
64
- // templates and do not guard against blanks, so a silent "" would generate
65
- // empty files (notably in a compiled binary, where the disk read always
66
- // fails and a missing embed is the only failure mode).
67
- throw new Error(
68
- `Template not found: ${source} (not on disk, and no matching embedded file).`,
69
- );
70
- }
@@ -1,30 +0,0 @@
1
- import type { PromptDefinition } from "@canonical/summon-core";
2
-
3
- /**
4
- * Shared prompts for component generators (styles, stories, SSR tests)
5
- */
6
- const sharedPrompts: PromptDefinition[] = [
7
- {
8
- name: "withStyles",
9
- type: "confirm",
10
- message: "Include styles?",
11
- default: true,
12
- group: "Options",
13
- },
14
- {
15
- name: "withStories",
16
- type: "confirm",
17
- message: "Include Storybook stories?",
18
- default: true,
19
- group: "Options",
20
- },
21
- {
22
- name: "withSsrTests",
23
- type: "confirm",
24
- message: "Include SSR tests?",
25
- default: true,
26
- group: "Options",
27
- },
28
- ];
29
-
30
- export default sharedPrompts;
@@ -1,18 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import getComponentName from "./getComponentName.js";
3
-
4
- describe("getComponentName", () => {
5
- it("extracts name from simple path", () => {
6
- expect(getComponentName("src/components/Button")).toBe("Button");
7
- });
8
-
9
- it("extracts name from nested path", () => {
10
- expect(getComponentName("src/lib/components/deep/MyWidget")).toBe(
11
- "MyWidget",
12
- );
13
- });
14
-
15
- it("returns name when given just a name", () => {
16
- expect(getComponentName("Button")).toBe("Button");
17
- });
18
- });
@@ -1,16 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import getParentDir from "./getParentDir.js";
3
-
4
- describe("getParentDir", () => {
5
- it("extracts parent from normal path", () => {
6
- expect(getParentDir("src/components/Button")).toBe("src/components");
7
- });
8
-
9
- it("returns '.' for root-level name", () => {
10
- expect(getParentDir("Button")).toBe(".");
11
- });
12
-
13
- it("extracts parent from deep path", () => {
14
- expect(getParentDir("src/lib/deep/Thing")).toBe("src/lib/deep");
15
- });
16
- });
@@ -1,36 +0,0 @@
1
- /**
2
- * Shared types for component generators
3
- */
4
-
5
- export type Framework = "react" | "svelte" | "lit";
6
-
7
- /** Base answers shared across component generators */
8
- export interface BaseComponentAnswers {
9
- /** Full path to the component directory (e.g., src/components/Button) */
10
- componentPath: string;
11
- /** Include CSS styles */
12
- withStyles: boolean;
13
- /** Include Storybook stories */
14
- withStories: boolean;
15
- /** Include SSR tests */
16
- withSsrTests: boolean;
17
- }
18
-
19
- export interface TemplateContext {
20
- /** Component name in PascalCase (e.g., MyComponent) */
21
- name: string;
22
- /** Component name in kebab-case (e.g., my-component) */
23
- kebabName: string;
24
- /** Generator name for comments */
25
- generatorName: string;
26
- /** Generator version */
27
- version: string;
28
- /** Include styles */
29
- withStyles: boolean;
30
- /** Include stories */
31
- withStories: boolean;
32
- /** Include SSR tests */
33
- withSsrTests: boolean;
34
- /** Index signature for compatibility with Record<string, unknown> */
35
- [key: string]: unknown;
36
- }
@@ -1,40 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import validateComponentPath from "./validateComponentPath.js";
3
-
4
- describe("validateComponentPath", () => {
5
- it("accepts valid path with PascalCase name", () => {
6
- expect(validateComponentPath("src/components/Button")).toBe(true);
7
- });
8
-
9
- it("accepts valid path with numbers in name", () => {
10
- expect(validateComponentPath("src/components/Button2")).toBe(true);
11
- });
12
-
13
- it("rejects empty string", () => {
14
- expect(validateComponentPath("")).not.toBe(true);
15
- expect(typeof validateComponentPath("")).toBe("string");
16
- });
17
-
18
- it("rejects undefined", () => {
19
- expect(validateComponentPath(undefined)).not.toBe(true);
20
- });
21
-
22
- it("rejects null", () => {
23
- expect(validateComponentPath(null)).not.toBe(true);
24
- });
25
-
26
- it("rejects non-string value", () => {
27
- expect(validateComponentPath(42)).not.toBe(true);
28
- });
29
-
30
- it("rejects non-PascalCase component name", () => {
31
- const result = validateComponentPath("src/components/my-button");
32
- expect(result).not.toBe(true);
33
- expect(result).toContain("PascalCase");
34
- });
35
-
36
- it("rejects camelCase component name", () => {
37
- const result = validateComponentPath("src/components/myButton");
38
- expect(result).not.toBe(true);
39
- });
40
- });
@@ -1,16 +0,0 @@
1
- import * as path from "node:path";
2
- import { isPascalCase } from "./string-helpers/index.js";
3
-
4
- /**
5
- * Validate component path
6
- */
7
- export default function validateComponentPath(value: unknown): true | string {
8
- if (!value || typeof value !== "string") {
9
- return "Component path is required";
10
- }
11
- const name = path.basename(value);
12
- if (!isPascalCase(name)) {
13
- return "Component name must be in PascalCase (e.g., MyComponent)";
14
- }
15
- return true;
16
- }
@@ -1,224 +0,0 @@
1
- /**
2
- * Svelte Component Generator
3
- *
4
- * Generates a Svelte 5 component with a consistent file structure:
5
- * - `ComponentName.svelte` - Main component file with Svelte 5 runes ($props)
6
- * - `types.ts` - TypeScript types and props interface
7
- * - `index.ts` - Barrel export
8
- * - `ComponentName.svelte.test.ts` - Unit tests with @testing-library/svelte
9
- * - `ComponentName.ssr.test.ts` - SSR tests (optional)
10
- * - `ComponentName.stories.svelte` - Svelte CSF stories (optional)
11
- * - `ComponentName.stories.ts` - TypeScript stories (optional, alternative to Svelte CSF)
12
- * - `styles.css` - CSS styles (optional)
13
- *
14
- * @example
15
- * ```bash
16
- * summon component svelte --component-path=src/lib/components/Button
17
- * summon component svelte --component-path=src/lib/components/Card --with-stories
18
- * summon component svelte --component-path=src/lib/components/Modal --with-stories --use-ts-stories
19
- * ```
20
- *
21
- * @module
22
- */
23
-
24
- import * as path from "node:path";
25
- import { fileURLToPath } from "node:url";
26
- import { type GeneratorDefinition, template } from "@canonical/summon-core";
27
- import { debug, info, mkdir, sequence_, when } from "@canonical/task";
28
- import pkg from "../../package.json" with { type: "json" };
29
-
30
- import {
31
- appendExportToParentIndex,
32
- createComponentPathPrompt,
33
- createTemplateContext,
34
- getComponentName,
35
- getParentDir,
36
- sharedPrompts,
37
- } from "../shared/index.js";
38
- import loadTemplate from "../shared/loadTemplate.js";
39
- import type { SvelteComponentAnswers } from "./types.js";
40
-
41
- // =============================================================================
42
- // Template Paths & Content (loaded eagerly via top-level await)
43
- // =============================================================================
44
-
45
- const __dirname = path.dirname(fileURLToPath(import.meta.url));
46
- const templatesDir = path.join(__dirname, "..", "templates");
47
-
48
- const svelteTemplates = {
49
- component: await loadTemplate(
50
- path.join(templatesDir, "svelte", "component.svelte.ejs"),
51
- ),
52
- types: await loadTemplate(path.join(templatesDir, "svelte", "types.ts.ejs")),
53
- index: await loadTemplate(path.join(templatesDir, "svelte", "index.ts.ejs")),
54
- test: await loadTemplate(path.join(templatesDir, "svelte", "test.ts.ejs")),
55
- ssrTest: await loadTemplate(
56
- path.join(templatesDir, "svelte", "ssr.test.ts.ejs"),
57
- ),
58
- storiesSvelte: await loadTemplate(
59
- path.join(templatesDir, "svelte", "stories.svelte.ejs"),
60
- ),
61
- storiesTs: await loadTemplate(
62
- path.join(templatesDir, "svelte", "stories.ts.ejs"),
63
- ),
64
- styles: await loadTemplate(
65
- path.join(templatesDir, "shared", "styles.css.ejs"),
66
- ),
67
- };
68
-
69
- // =============================================================================
70
- // Generator Definition
71
- // =============================================================================
72
-
73
- /**
74
- * Svelte component generator definition.
75
- *
76
- * This generator creates a complete Svelte 5 component with TypeScript support,
77
- * runes ($props), testing infrastructure, and optional Storybook stories.
78
- *
79
- * @see {@link https://summon.dev/generators/component/svelte} for documentation
80
- */
81
- const generator = {
82
- meta: {
83
- name: "component/svelte",
84
- displayName: `${pkg.name}:svelte`,
85
- description:
86
- "Generate a Svelte 5 component with TypeScript, tests, stories, and styles",
87
- version: "0.1.0",
88
- help: `Generate a Svelte 5 component with TypeScript, tests, stories, and styles.
89
-
90
- FEATURES:
91
- - Svelte 5 with runes ($props)
92
- - TypeScript types and props interface
93
- - Unit tests with @testing-library/svelte
94
- - Storybook integration (Svelte CSF or TypeScript)
95
- - CSS styles (optional)
96
- - SSR tests (optional)
97
- - Auto-export from parent index.ts
98
-
99
- The component name is extracted from the path and must be PascalCase.
100
- For example, 'src/lib/components/Button' creates a 'Button' component.`,
101
- examples: [
102
- "summon component svelte --component-path=src/lib/components/Button",
103
- "summon component svelte --component-path=src/lib/components/Card --with-styles --with-stories",
104
- "summon component svelte --component-path=src/lib/components/Modal --with-stories --use-ts-stories",
105
- "summon component svelte --component-path=src/lib/components/Button --dry-run",
106
- ],
107
- },
108
-
109
- prompts: [
110
- createComponentPathPrompt("svelte"),
111
- ...sharedPrompts,
112
- {
113
- name: "useTsStories",
114
- type: "confirm",
115
- message: "Use TypeScript stories format? (otherwise Svelte CSF)",
116
- default: false,
117
- when: (answers) => answers.withStories === true,
118
- group: "Options",
119
- },
120
- ],
121
-
122
- generate: (answers) => {
123
- const componentName = getComponentName(answers.componentPath);
124
- const componentDir = answers.componentPath;
125
- const parentDir = getParentDir(answers.componentPath);
126
- const ctx = createTemplateContext(answers, "svelte");
127
-
128
- return sequence_([
129
- info(`Generating Svelte component: ${componentName}`),
130
-
131
- debug("Creating component directory"),
132
- mkdir(componentDir),
133
-
134
- debug("Creating main component file"),
135
- template({
136
- source: svelteTemplates.component.source,
137
- content: svelteTemplates.component.content,
138
- dest: path.join(componentDir, `${componentName}.svelte`),
139
- vars: ctx,
140
- }),
141
-
142
- debug("Creating types file"),
143
- template({
144
- source: svelteTemplates.types.source,
145
- content: svelteTemplates.types.content,
146
- dest: path.join(componentDir, "types.ts"),
147
- vars: ctx,
148
- }),
149
-
150
- debug("Creating index barrel file"),
151
- template({
152
- source: svelteTemplates.index.source,
153
- content: svelteTemplates.index.content,
154
- dest: path.join(componentDir, "index.ts"),
155
- vars: ctx,
156
- }),
157
-
158
- debug("Creating test file"),
159
- template({
160
- source: svelteTemplates.test.source,
161
- content: svelteTemplates.test.content,
162
- dest: path.join(componentDir, `${componentName}.svelte.test.ts`),
163
- vars: ctx,
164
- }),
165
-
166
- when(answers.withSsrTests, debug("Creating SSR test file")),
167
- when(
168
- answers.withSsrTests,
169
- template({
170
- source: svelteTemplates.ssrTest.source,
171
- content: svelteTemplates.ssrTest.content,
172
- dest: path.join(componentDir, `${componentName}.ssr.test.ts`),
173
- vars: ctx,
174
- }),
175
- ),
176
-
177
- when(
178
- answers.withStories && !answers.useTsStories,
179
- debug("Creating Svelte CSF stories file"),
180
- ),
181
- when(
182
- answers.withStories && !answers.useTsStories,
183
- template({
184
- source: svelteTemplates.storiesSvelte.source,
185
- content: svelteTemplates.storiesSvelte.content,
186
- dest: path.join(componentDir, `${componentName}.stories.svelte`),
187
- vars: ctx,
188
- }),
189
- ),
190
-
191
- when(
192
- answers.withStories && answers.useTsStories,
193
- debug("Creating TypeScript stories file"),
194
- ),
195
- when(
196
- answers.withStories && answers.useTsStories,
197
- template({
198
- source: svelteTemplates.storiesTs.source,
199
- content: svelteTemplates.storiesTs.content,
200
- dest: path.join(componentDir, `${componentName}.stories.ts`),
201
- vars: ctx,
202
- }),
203
- ),
204
-
205
- when(answers.withStyles, debug("Creating styles file")),
206
- when(
207
- answers.withStyles,
208
- template({
209
- source: svelteTemplates.styles.source,
210
- content: svelteTemplates.styles.content,
211
- dest: path.join(componentDir, "styles.css"),
212
- vars: ctx,
213
- }),
214
- ),
215
-
216
- debug("Updating parent index.ts with export"),
217
- appendExportToParentIndex(parentDir, componentName),
218
-
219
- info(`Created ${componentName} component at ${componentDir}`),
220
- ]);
221
- },
222
- } as const satisfies GeneratorDefinition<SvelteComponentAnswers>;
223
-
224
- export default generator;