@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.
- package/README.md +14 -26
- package/{src/index.ts → dist/esm/index.js} +5 -8
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/lit/generator.js +152 -0
- package/dist/esm/lit/generator.js.map +1 -0
- package/dist/esm/lit/index.js +5 -0
- package/dist/esm/lit/index.js.map +1 -0
- package/dist/esm/lit/types.js +5 -0
- package/dist/esm/lit/types.js.map +1 -0
- package/dist/esm/react/generator.js +129 -0
- package/dist/esm/react/generator.js.map +1 -0
- package/dist/esm/react/index.js +5 -0
- package/dist/esm/react/index.js.map +1 -0
- package/dist/esm/react/types.js +5 -0
- package/dist/esm/react/types.js.map +1 -0
- package/dist/esm/shared/createComponentPathPrompt.js +23 -0
- package/dist/esm/shared/createComponentPathPrompt.js.map +1 -0
- package/dist/esm/shared/createTemplateContext.js +20 -0
- package/dist/esm/shared/createTemplateContext.js.map +1 -0
- package/dist/esm/shared/file-operations/appendExportToParentIndex.js +25 -0
- package/dist/esm/shared/file-operations/appendExportToParentIndex.js.map +1 -0
- package/dist/esm/shared/file-operations/dryRunWithFileState.js +25 -0
- package/dist/esm/shared/file-operations/dryRunWithFileState.js.map +1 -0
- package/{src/shared/file-operations/index.ts → dist/esm/shared/file-operations/index.js} +1 -0
- package/dist/esm/shared/file-operations/index.js.map +1 -0
- package/dist/esm/shared/file-operations/removeExportFromParentIndex.js +13 -0
- package/dist/esm/shared/file-operations/removeExportFromParentIndex.js.map +1 -0
- package/dist/esm/shared/file-operations/removeLineFromFile.js +19 -0
- package/dist/esm/shared/file-operations/removeLineFromFile.js.map +1 -0
- package/dist/esm/shared/index.js +11 -0
- package/dist/esm/shared/index.js.map +1 -0
- package/dist/esm/shared/loadTemplate.js +56 -0
- package/dist/esm/shared/loadTemplate.js.map +1 -0
- package/dist/esm/shared/packageName.js +11 -0
- package/dist/esm/shared/packageName.js.map +1 -0
- package/dist/esm/shared/sharedPrompts.js +28 -0
- package/dist/esm/shared/sharedPrompts.js.map +1 -0
- package/dist/esm/shared/string-helpers/getComponentName.js +8 -0
- package/dist/esm/shared/string-helpers/getComponentName.js.map +1 -0
- package/dist/esm/shared/string-helpers/getParentDir.js +8 -0
- package/dist/esm/shared/string-helpers/getParentDir.js.map +1 -0
- package/{src/shared/string-helpers/index.ts → dist/esm/shared/string-helpers/index.js} +1 -0
- package/dist/esm/shared/string-helpers/index.js.map +1 -0
- package/dist/esm/shared/types.js +5 -0
- package/dist/esm/shared/types.js.map +1 -0
- package/dist/esm/shared/validateComponentPath.js +27 -0
- package/dist/esm/shared/validateComponentPath.js.map +1 -0
- package/dist/esm/svelte/generator.js +165 -0
- package/dist/esm/svelte/generator.js.map +1 -0
- package/dist/esm/svelte/index.js +5 -0
- package/dist/esm/svelte/index.js.map +1 -0
- package/dist/esm/svelte/types.js +5 -0
- package/dist/esm/svelte/types.js.map +1 -0
- package/dist/types/index.d.ts +87 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/lit/generator.d.ts +52 -0
- package/dist/types/lit/generator.d.ts.map +1 -0
- package/{src/lit/index.ts → dist/types/lit/index.d.ts} +1 -1
- package/dist/types/lit/index.d.ts.map +1 -0
- package/{src/lit/types.ts → dist/types/lit/types.d.ts} +1 -2
- package/dist/types/lit/types.d.ts.map +1 -0
- package/dist/types/react/generator.d.ts +28 -0
- package/dist/types/react/generator.d.ts.map +1 -0
- package/{src/react/index.ts → dist/types/react/index.d.ts} +1 -1
- package/dist/types/react/index.d.ts.map +1 -0
- package/{src/react/types.ts → dist/types/react/types.d.ts} +1 -2
- package/dist/types/react/types.d.ts.map +1 -0
- package/dist/types/shared/createComponentPathPrompt.d.ts +12 -0
- package/dist/types/shared/createComponentPathPrompt.d.ts.map +1 -0
- package/dist/types/shared/createTemplateContext.d.ts +9 -0
- package/dist/types/shared/createTemplateContext.d.ts.map +1 -0
- package/dist/types/shared/file-operations/appendExportToParentIndex.d.ts +8 -0
- package/dist/types/shared/file-operations/appendExportToParentIndex.d.ts.map +1 -0
- package/dist/types/shared/file-operations/dryRunWithFileState.d.ts +7 -0
- package/dist/types/shared/file-operations/dryRunWithFileState.d.ts.map +1 -0
- package/dist/types/shared/file-operations/index.d.ts +4 -0
- package/dist/types/shared/file-operations/index.d.ts.map +1 -0
- package/dist/types/shared/file-operations/removeExportFromParentIndex.d.ts +7 -0
- package/dist/types/shared/file-operations/removeExportFromParentIndex.d.ts.map +1 -0
- package/dist/types/shared/file-operations/removeLineFromFile.d.ts +7 -0
- package/dist/types/shared/file-operations/removeLineFromFile.d.ts.map +1 -0
- package/{src/shared/index.ts → dist/types/shared/index.d.ts} +2 -1
- package/dist/types/shared/index.d.ts.map +1 -0
- package/dist/types/shared/loadTemplate.d.ts +20 -0
- package/dist/types/shared/loadTemplate.d.ts.map +1 -0
- package/dist/types/shared/packageName.d.ts +11 -0
- package/dist/types/shared/packageName.d.ts.map +1 -0
- package/dist/types/shared/sharedPrompts.d.ts +7 -0
- package/dist/types/shared/sharedPrompts.d.ts.map +1 -0
- package/{src/shared/string-helpers/getComponentName.ts → dist/types/shared/string-helpers/getComponentName.d.ts} +2 -5
- package/dist/types/shared/string-helpers/getComponentName.d.ts.map +1 -0
- package/{src/shared/string-helpers/getParentDir.ts → dist/types/shared/string-helpers/getParentDir.d.ts} +2 -5
- package/dist/types/shared/string-helpers/getParentDir.d.ts.map +1 -0
- package/dist/types/shared/string-helpers/index.d.ts +4 -0
- package/dist/types/shared/string-helpers/index.d.ts.map +1 -0
- package/dist/types/shared/types.d.ts +34 -0
- package/dist/types/shared/types.d.ts.map +1 -0
- package/dist/types/shared/validateComponentPath.d.ts +9 -0
- package/dist/types/shared/validateComponentPath.d.ts.map +1 -0
- package/dist/types/svelte/generator.d.ts +52 -0
- package/dist/types/svelte/generator.d.ts.map +1 -0
- package/{src/svelte/index.ts → dist/types/svelte/index.d.ts} +1 -1
- package/dist/types/svelte/index.d.ts.map +1 -0
- package/{src/svelte/types.ts → dist/types/svelte/types.d.ts} +3 -4
- package/dist/types/svelte/types.d.ts.map +1 -0
- package/package.json +21 -10
- package/CHANGELOG.md +0 -245
- package/CONVENTIONS.md +0 -184
- package/biome.json +0 -6
- package/src/index.test.ts +0 -514
- package/src/lit/generator.ts +0 -190
- package/src/react/generator.ts +0 -176
- package/src/shared/createComponentPathPrompt.ts +0 -28
- package/src/shared/createTemplateContext.ts +0 -29
- package/src/shared/file-operations/appendExportToParentIndex.test.ts +0 -82
- package/src/shared/file-operations/appendExportToParentIndex.ts +0 -40
- package/src/shared/file-operations/dryRunWithFileState.ts +0 -28
- package/src/shared/file-operations/removeExportFromParentIndex.test.ts +0 -52
- package/src/shared/file-operations/removeExportFromParentIndex.ts +0 -21
- package/src/shared/file-operations/removeLineFromFile.test.ts +0 -66
- package/src/shared/file-operations/removeLineFromFile.ts +0 -29
- package/src/shared/loadTemplate.test.ts +0 -100
- package/src/shared/loadTemplate.ts +0 -70
- package/src/shared/sharedPrompts.ts +0 -30
- package/src/shared/string-helpers/getComponentName.test.ts +0 -18
- package/src/shared/string-helpers/getParentDir.test.ts +0 -16
- package/src/shared/types.ts +0 -36
- package/src/shared/validateComponentPath.test.ts +0 -40
- package/src/shared/validateComponentPath.ts +0 -16
- package/src/svelte/generator.ts +0 -224
- package/src/testing/integration/generators-undo.test.ts +0 -200
- package/tsconfig.json +0 -9
- package/vitest.config.ts +0 -18
- /package/{src → dist/esm}/templates/lit/component.ts.ejs +0 -0
- /package/{src → dist/esm}/templates/lit/index.ts.ejs +0 -0
- /package/{src → dist/esm}/templates/lit/stories.ts.ejs +0 -0
- /package/{src → dist/esm}/templates/lit/styles.css.ejs +0 -0
- /package/{src → dist/esm}/templates/lit/tests.ts.ejs +0 -0
- /package/{src → dist/esm}/templates/lit/types.ts.ejs +0 -0
- /package/{src → dist/esm}/templates/react/component.tsx.ejs +0 -0
- /package/{src → dist/esm}/templates/react/index.ts.ejs +0 -0
- /package/{src → dist/esm}/templates/react/ssr.test.tsx.ejs +0 -0
- /package/{src → dist/esm}/templates/react/stories.tsx.ejs +0 -0
- /package/{src → dist/esm}/templates/react/test.tsx.ejs +0 -0
- /package/{src → dist/esm}/templates/react/types.ts.ejs +0 -0
- /package/{src → dist/esm}/templates/shared/styles.css.ejs +0 -0
- /package/{src → dist/esm}/templates/svelte/component.svelte.ejs +0 -0
- /package/{src → dist/esm}/templates/svelte/index.ts.ejs +0 -0
- /package/{src → dist/esm}/templates/svelte/ssr.test.ts.ejs +0 -0
- /package/{src → dist/esm}/templates/svelte/stories.svelte.ejs +0 -0
- /package/{src → dist/esm}/templates/svelte/stories.ts.ejs +0 -0
- /package/{src → dist/esm}/templates/svelte/test.ts.ejs +0 -0
- /package/{src → dist/esm}/templates/svelte/types.ts.ejs +0 -0
|
@@ -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
|
-
});
|
package/src/shared/types.ts
DELETED
|
@@ -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
|
-
}
|
package/src/svelte/generator.ts
DELETED
|
@@ -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;
|
|
@@ -1,200 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Undo integration tests for component generators
|
|
3
|
-
*
|
|
4
|
-
* Verifies that collectUndos produces correct undo plans for each generator.
|
|
5
|
-
* Each template() call produces 2 undos: mkdir(destDir) + writeFile(dest).
|
|
6
|
-
* Plus the generator's own mkdir(componentDir) and appendExportToParentIndex.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { collectUndos, dryRun } from "@canonical/task";
|
|
10
|
-
import { describe, expect, it } from "vitest";
|
|
11
|
-
import { generators } from "../../index.js";
|
|
12
|
-
|
|
13
|
-
// =============================================================================
|
|
14
|
-
// Helpers
|
|
15
|
-
// =============================================================================
|
|
16
|
-
|
|
17
|
-
/** Collect all undo effect tags from a generator task */
|
|
18
|
-
const getUndoEffectTags = (undos: ReturnType<typeof collectUndos>) =>
|
|
19
|
-
undos.flatMap((undo) => dryRun(undo).effects.map((e) => e._tag));
|
|
20
|
-
|
|
21
|
-
// =============================================================================
|
|
22
|
-
// React
|
|
23
|
-
// =============================================================================
|
|
24
|
-
|
|
25
|
-
describe("component/react undo plan", () => {
|
|
26
|
-
const generator = generators["component/react"];
|
|
27
|
-
|
|
28
|
-
it("produces undos for full component (all options)", () => {
|
|
29
|
-
const task = generator.generate({
|
|
30
|
-
componentPath: "src/components/Button",
|
|
31
|
-
withStyles: true,
|
|
32
|
-
withStories: true,
|
|
33
|
-
withSsrTests: true,
|
|
34
|
-
});
|
|
35
|
-
const undos = collectUndos(task);
|
|
36
|
-
|
|
37
|
-
// 1 mkdir(componentDir) + 7 templates × 2 + 1 appendExport(writeFile) = 16
|
|
38
|
-
expect(undos.length).toBeGreaterThan(0);
|
|
39
|
-
expect(undos.length).toBe(16);
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
it("produces fewer undos for minimal component", () => {
|
|
43
|
-
const fullTask = generator.generate({
|
|
44
|
-
componentPath: "src/components/Button",
|
|
45
|
-
withStyles: true,
|
|
46
|
-
withStories: true,
|
|
47
|
-
withSsrTests: true,
|
|
48
|
-
});
|
|
49
|
-
const minTask = generator.generate({
|
|
50
|
-
componentPath: "src/components/Button",
|
|
51
|
-
withStyles: false,
|
|
52
|
-
withStories: false,
|
|
53
|
-
withSsrTests: false,
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
const fullUndos = collectUndos(fullTask);
|
|
57
|
-
const minUndos = collectUndos(minTask);
|
|
58
|
-
|
|
59
|
-
expect(minUndos.length).toBeLessThan(fullUndos.length);
|
|
60
|
-
// 1 mkdir + 4 templates × 2 + 1 appendExport = 10
|
|
61
|
-
expect(minUndos.length).toBe(10);
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
it("undo effects are only DeleteFile and DeleteDirectory", () => {
|
|
65
|
-
const task = generator.generate({
|
|
66
|
-
componentPath: "src/components/Button",
|
|
67
|
-
withStyles: true,
|
|
68
|
-
withStories: true,
|
|
69
|
-
withSsrTests: true,
|
|
70
|
-
});
|
|
71
|
-
const undos = collectUndos(task);
|
|
72
|
-
const tags = getUndoEffectTags(undos);
|
|
73
|
-
|
|
74
|
-
for (const tag of tags) {
|
|
75
|
-
expect(["DeleteFile", "DeleteDirectory"]).toContain(tag);
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
it("undo includes DeleteDirectory for component dir", () => {
|
|
80
|
-
const task = generator.generate({
|
|
81
|
-
componentPath: "src/components/Card",
|
|
82
|
-
withStyles: false,
|
|
83
|
-
withStories: false,
|
|
84
|
-
withSsrTests: false,
|
|
85
|
-
});
|
|
86
|
-
const undos = collectUndos(task);
|
|
87
|
-
|
|
88
|
-
// First undo should be the mkdir(componentDir) → DeleteDirectory
|
|
89
|
-
const firstUndoEffects = dryRun(undos[0]).effects;
|
|
90
|
-
expect(firstUndoEffects[0]._tag).toBe("DeleteDirectory");
|
|
91
|
-
expect((firstUndoEffects[0] as { path: string }).path).toBe(
|
|
92
|
-
"src/components/Card",
|
|
93
|
-
);
|
|
94
|
-
});
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
// =============================================================================
|
|
98
|
-
// Svelte
|
|
99
|
-
// =============================================================================
|
|
100
|
-
|
|
101
|
-
describe("component/svelte undo plan", () => {
|
|
102
|
-
const generator = generators["component/svelte"];
|
|
103
|
-
|
|
104
|
-
it("produces undos for full component (all options)", () => {
|
|
105
|
-
const task = generator.generate({
|
|
106
|
-
componentPath: "src/lib/components/Button",
|
|
107
|
-
withStyles: true,
|
|
108
|
-
withStories: true,
|
|
109
|
-
useTsStories: false,
|
|
110
|
-
withSsrTests: true,
|
|
111
|
-
});
|
|
112
|
-
const undos = collectUndos(task);
|
|
113
|
-
|
|
114
|
-
// 1 mkdir + 7 templates × 2 + 1 appendExport = 16
|
|
115
|
-
expect(undos.length).toBe(16);
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
it("TS stories and Svelte CSF stories produce same undo count", () => {
|
|
119
|
-
const svelteCSF = generator.generate({
|
|
120
|
-
componentPath: "src/lib/components/Button",
|
|
121
|
-
withStyles: false,
|
|
122
|
-
withStories: true,
|
|
123
|
-
useTsStories: false,
|
|
124
|
-
withSsrTests: false,
|
|
125
|
-
});
|
|
126
|
-
const tsStories = generator.generate({
|
|
127
|
-
componentPath: "src/lib/components/Button",
|
|
128
|
-
withStyles: false,
|
|
129
|
-
withStories: true,
|
|
130
|
-
useTsStories: true,
|
|
131
|
-
withSsrTests: false,
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
expect(collectUndos(svelteCSF).length).toBe(collectUndos(tsStories).length);
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
it("produces fewer undos for minimal component", () => {
|
|
138
|
-
const minTask = generator.generate({
|
|
139
|
-
componentPath: "src/lib/components/Button",
|
|
140
|
-
withStyles: false,
|
|
141
|
-
withStories: false,
|
|
142
|
-
useTsStories: false,
|
|
143
|
-
withSsrTests: false,
|
|
144
|
-
});
|
|
145
|
-
const minUndos = collectUndos(minTask);
|
|
146
|
-
|
|
147
|
-
// 1 mkdir + 4 templates × 2 + 1 appendExport = 10
|
|
148
|
-
expect(minUndos.length).toBe(10);
|
|
149
|
-
});
|
|
150
|
-
});
|
|
151
|
-
|
|
152
|
-
// =============================================================================
|
|
153
|
-
// Lit
|
|
154
|
-
// =============================================================================
|
|
155
|
-
|
|
156
|
-
describe("component/lit undo plan", () => {
|
|
157
|
-
const generator = generators["component/lit"];
|
|
158
|
-
|
|
159
|
-
it("produces undos for full component (all options)", () => {
|
|
160
|
-
const task = generator.generate({
|
|
161
|
-
componentPath: "src/lib/components/Button",
|
|
162
|
-
withStyles: true,
|
|
163
|
-
withStories: true,
|
|
164
|
-
withSsrTests: false,
|
|
165
|
-
});
|
|
166
|
-
const undos = collectUndos(task);
|
|
167
|
-
|
|
168
|
-
// 1 mkdir + 6 templates × 2 + 1 appendExport = 14
|
|
169
|
-
// (lit has: component, index, types, tests, stories, styles = 6 templates)
|
|
170
|
-
expect(undos.length).toBe(14);
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
it("produces fewer undos for minimal component", () => {
|
|
174
|
-
const minTask = generator.generate({
|
|
175
|
-
componentPath: "src/lib/components/Button",
|
|
176
|
-
withStyles: false,
|
|
177
|
-
withStories: false,
|
|
178
|
-
withSsrTests: false,
|
|
179
|
-
});
|
|
180
|
-
const minUndos = collectUndos(minTask);
|
|
181
|
-
|
|
182
|
-
// 1 mkdir + 4 templates × 2 + 1 appendExport = 10
|
|
183
|
-
expect(minUndos.length).toBe(10);
|
|
184
|
-
});
|
|
185
|
-
|
|
186
|
-
it("undo effects are only DeleteFile and DeleteDirectory", () => {
|
|
187
|
-
const task = generator.generate({
|
|
188
|
-
componentPath: "src/lib/components/Widget",
|
|
189
|
-
withStyles: true,
|
|
190
|
-
withStories: true,
|
|
191
|
-
withSsrTests: false,
|
|
192
|
-
});
|
|
193
|
-
const undos = collectUndos(task);
|
|
194
|
-
const tags = getUndoEffectTags(undos);
|
|
195
|
-
|
|
196
|
-
for (const tag of tags) {
|
|
197
|
-
expect(["DeleteFile", "DeleteDirectory"]).toContain(tag);
|
|
198
|
-
}
|
|
199
|
-
});
|
|
200
|
-
});
|
package/tsconfig.json
DELETED
package/vitest.config.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { defineConfig } from "vitest/config";
|
|
2
|
-
|
|
3
|
-
export default defineConfig({
|
|
4
|
-
test: {
|
|
5
|
-
include: ["src/**/*.test.ts"],
|
|
6
|
-
coverage: {
|
|
7
|
-
provider: "v8",
|
|
8
|
-
include: ["src/**/*.ts"],
|
|
9
|
-
exclude: ["**/index.ts", "**/*.test.ts", "**/*.d.ts", "**/types.ts"],
|
|
10
|
-
thresholds: {
|
|
11
|
-
statements: 100,
|
|
12
|
-
branches: 100,
|
|
13
|
-
functions: 100,
|
|
14
|
-
lines: 100,
|
|
15
|
-
},
|
|
16
|
-
},
|
|
17
|
-
},
|
|
18
|
-
});
|
|
File without changes
|
|
File without changes
|