@canonical/summon-component 0.29.0-experimental.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.
- 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 +16 -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 +5 -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 -237
- 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
package/src/lit/generator.ts
DELETED
|
@@ -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;
|
package/src/react/generator.ts
DELETED
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* React Component Generator
|
|
3
|
-
*
|
|
4
|
-
* Generates a React component with a consistent file structure:
|
|
5
|
-
* - `ComponentName.tsx` - Main component file
|
|
6
|
-
* - `types.ts` - TypeScript types and props interface
|
|
7
|
-
* - `index.ts` - Barrel export
|
|
8
|
-
* - `ComponentName.test.tsx` - Unit tests with @testing-library/react
|
|
9
|
-
* - `ComponentName.ssr.test.tsx` - SSR tests (optional)
|
|
10
|
-
* - `ComponentName.stories.tsx` - Storybook stories (optional)
|
|
11
|
-
* - `styles.css` - CSS styles (optional)
|
|
12
|
-
*
|
|
13
|
-
* @module
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
import * as path from "node:path";
|
|
17
|
-
import { fileURLToPath } from "node:url";
|
|
18
|
-
import { type GeneratorDefinition, template } from "@canonical/summon-core";
|
|
19
|
-
import { debug, info, mkdir, sequence_, when } from "@canonical/task";
|
|
20
|
-
import pkg from "../../package.json" with { type: "json" };
|
|
21
|
-
|
|
22
|
-
import {
|
|
23
|
-
appendExportToParentIndex,
|
|
24
|
-
createComponentPathPrompt,
|
|
25
|
-
createTemplateContext,
|
|
26
|
-
getComponentName,
|
|
27
|
-
getParentDir,
|
|
28
|
-
sharedPrompts,
|
|
29
|
-
} from "../shared/index.js";
|
|
30
|
-
import loadTemplate from "../shared/loadTemplate.js";
|
|
31
|
-
import type { ReactComponentAnswers } from "./types.js";
|
|
32
|
-
|
|
33
|
-
// =============================================================================
|
|
34
|
-
// Template Paths & Content (loaded eagerly via top-level await)
|
|
35
|
-
// =============================================================================
|
|
36
|
-
|
|
37
|
-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
38
|
-
const templatesDir = path.join(__dirname, "..", "templates");
|
|
39
|
-
|
|
40
|
-
const reactTemplates = {
|
|
41
|
-
component: await loadTemplate(
|
|
42
|
-
path.join(templatesDir, "react", "component.tsx.ejs"),
|
|
43
|
-
),
|
|
44
|
-
types: await loadTemplate(path.join(templatesDir, "react", "types.ts.ejs")),
|
|
45
|
-
index: await loadTemplate(path.join(templatesDir, "react", "index.ts.ejs")),
|
|
46
|
-
test: await loadTemplate(path.join(templatesDir, "react", "test.tsx.ejs")),
|
|
47
|
-
ssrTest: await loadTemplate(
|
|
48
|
-
path.join(templatesDir, "react", "ssr.test.tsx.ejs"),
|
|
49
|
-
),
|
|
50
|
-
stories: await loadTemplate(
|
|
51
|
-
path.join(templatesDir, "react", "stories.tsx.ejs"),
|
|
52
|
-
),
|
|
53
|
-
styles: await loadTemplate(
|
|
54
|
-
path.join(templatesDir, "shared", "styles.css.ejs"),
|
|
55
|
-
),
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
// =============================================================================
|
|
59
|
-
// Generator Definition
|
|
60
|
-
// =============================================================================
|
|
61
|
-
|
|
62
|
-
const generator = {
|
|
63
|
-
meta: {
|
|
64
|
-
name: "component/react",
|
|
65
|
-
displayName: `${pkg.name}:react`,
|
|
66
|
-
description:
|
|
67
|
-
"Generate a React component with TypeScript, tests, stories, and styles",
|
|
68
|
-
version: "0.1.0",
|
|
69
|
-
help: `Generate a React component with TypeScript, tests, stories, and styles.
|
|
70
|
-
|
|
71
|
-
FEATURES:
|
|
72
|
-
- TypeScript types and props interface
|
|
73
|
-
- Unit tests with @testing-library/react
|
|
74
|
-
- Storybook integration (optional)
|
|
75
|
-
- CSS styles (optional)
|
|
76
|
-
- SSR tests (optional)
|
|
77
|
-
- Auto-export from parent index.ts
|
|
78
|
-
|
|
79
|
-
The component name is extracted from the path and must be PascalCase.
|
|
80
|
-
For example, 'src/components/Button' creates a 'Button' component.`,
|
|
81
|
-
examples: [
|
|
82
|
-
"summon component react --component-path=src/components/Button",
|
|
83
|
-
"summon component react --component-path=src/components/Card --with-styles --with-stories",
|
|
84
|
-
"summon component react --component-path=src/components/Modal --no-with-ssr-tests",
|
|
85
|
-
"summon component react --component-path=src/components/Button --dry-run",
|
|
86
|
-
],
|
|
87
|
-
},
|
|
88
|
-
|
|
89
|
-
prompts: [createComponentPathPrompt("react"), ...sharedPrompts],
|
|
90
|
-
|
|
91
|
-
generate: (answers) => {
|
|
92
|
-
const componentName = getComponentName(answers.componentPath);
|
|
93
|
-
const componentDir = answers.componentPath;
|
|
94
|
-
const parentDir = getParentDir(answers.componentPath);
|
|
95
|
-
const ctx = createTemplateContext(answers, "react");
|
|
96
|
-
|
|
97
|
-
return sequence_([
|
|
98
|
-
info(`Generating React component: ${componentName}`),
|
|
99
|
-
|
|
100
|
-
debug("Creating component directory"),
|
|
101
|
-
mkdir(componentDir),
|
|
102
|
-
|
|
103
|
-
debug("Creating main component file"),
|
|
104
|
-
template({
|
|
105
|
-
source: reactTemplates.component.source,
|
|
106
|
-
content: reactTemplates.component.content,
|
|
107
|
-
dest: path.join(componentDir, `${componentName}.tsx`),
|
|
108
|
-
vars: ctx,
|
|
109
|
-
}),
|
|
110
|
-
|
|
111
|
-
debug("Creating types file"),
|
|
112
|
-
template({
|
|
113
|
-
source: reactTemplates.types.source,
|
|
114
|
-
content: reactTemplates.types.content,
|
|
115
|
-
dest: path.join(componentDir, "types.ts"),
|
|
116
|
-
vars: ctx,
|
|
117
|
-
}),
|
|
118
|
-
|
|
119
|
-
debug("Creating index barrel file"),
|
|
120
|
-
template({
|
|
121
|
-
source: reactTemplates.index.source,
|
|
122
|
-
content: reactTemplates.index.content,
|
|
123
|
-
dest: path.join(componentDir, "index.ts"),
|
|
124
|
-
vars: ctx,
|
|
125
|
-
}),
|
|
126
|
-
|
|
127
|
-
debug("Creating test file"),
|
|
128
|
-
template({
|
|
129
|
-
source: reactTemplates.test.source,
|
|
130
|
-
content: reactTemplates.test.content,
|
|
131
|
-
dest: path.join(componentDir, `${componentName}.tests.tsx`),
|
|
132
|
-
vars: ctx,
|
|
133
|
-
}),
|
|
134
|
-
|
|
135
|
-
when(answers.withSsrTests, debug("Creating SSR test file")),
|
|
136
|
-
when(
|
|
137
|
-
answers.withSsrTests,
|
|
138
|
-
template({
|
|
139
|
-
source: reactTemplates.ssrTest.source,
|
|
140
|
-
content: reactTemplates.ssrTest.content,
|
|
141
|
-
dest: path.join(componentDir, `${componentName}.ssr.tests.tsx`),
|
|
142
|
-
vars: ctx,
|
|
143
|
-
}),
|
|
144
|
-
),
|
|
145
|
-
|
|
146
|
-
when(answers.withStories, debug("Creating stories file")),
|
|
147
|
-
when(
|
|
148
|
-
answers.withStories,
|
|
149
|
-
template({
|
|
150
|
-
source: reactTemplates.stories.source,
|
|
151
|
-
content: reactTemplates.stories.content,
|
|
152
|
-
dest: path.join(componentDir, `${componentName}.stories.tsx`),
|
|
153
|
-
vars: ctx,
|
|
154
|
-
}),
|
|
155
|
-
),
|
|
156
|
-
|
|
157
|
-
when(answers.withStyles, debug("Creating styles file")),
|
|
158
|
-
when(
|
|
159
|
-
answers.withStyles,
|
|
160
|
-
template({
|
|
161
|
-
source: reactTemplates.styles.source,
|
|
162
|
-
content: reactTemplates.styles.content,
|
|
163
|
-
dest: path.join(componentDir, "styles.css"),
|
|
164
|
-
vars: ctx,
|
|
165
|
-
}),
|
|
166
|
-
),
|
|
167
|
-
|
|
168
|
-
debug("Updating parent index.ts with export"),
|
|
169
|
-
appendExportToParentIndex(parentDir, componentName),
|
|
170
|
-
|
|
171
|
-
info(`Created ${componentName} component at ${componentDir}`),
|
|
172
|
-
]);
|
|
173
|
-
},
|
|
174
|
-
} as const satisfies GeneratorDefinition<ReactComponentAnswers>;
|
|
175
|
-
|
|
176
|
-
export default generator;
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import type { PromptDefinition } from "@canonical/summon-core";
|
|
2
|
-
import type { Framework } from "./types.js";
|
|
3
|
-
import validateComponentPath from "./validateComponentPath.js";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Create component path prompt with framework-specific defaults.
|
|
7
|
-
*
|
|
8
|
-
* This prompt is marked as positional, allowing users to run:
|
|
9
|
-
* summon component react src/components/Button
|
|
10
|
-
* instead of:
|
|
11
|
-
* summon component react --component-path=src/components/Button
|
|
12
|
-
*/
|
|
13
|
-
export default function createComponentPathPrompt(
|
|
14
|
-
framework: Framework,
|
|
15
|
-
): PromptDefinition {
|
|
16
|
-
return {
|
|
17
|
-
name: "componentPath",
|
|
18
|
-
type: "text",
|
|
19
|
-
message: "Component path:",
|
|
20
|
-
default:
|
|
21
|
-
framework === "react"
|
|
22
|
-
? "src/components/MyComponent"
|
|
23
|
-
: "src/lib/components/MyComponent",
|
|
24
|
-
validate: validateComponentPath,
|
|
25
|
-
group: "Component",
|
|
26
|
-
positional: true,
|
|
27
|
-
};
|
|
28
|
-
}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Template context creation for component generators
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { getComponentName, kebabCase } from "./string-helpers/index.js";
|
|
6
|
-
import type {
|
|
7
|
-
BaseComponentAnswers,
|
|
8
|
-
Framework,
|
|
9
|
-
TemplateContext,
|
|
10
|
-
} from "./types.js";
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Create template context from answers
|
|
14
|
-
*/
|
|
15
|
-
export default function createTemplateContext(
|
|
16
|
-
answers: BaseComponentAnswers,
|
|
17
|
-
framework: Framework,
|
|
18
|
-
): TemplateContext {
|
|
19
|
-
const name = getComponentName(answers.componentPath);
|
|
20
|
-
return {
|
|
21
|
-
name,
|
|
22
|
-
kebabName: kebabCase(name),
|
|
23
|
-
generatorName: `@canonical/summon:component-${framework}`,
|
|
24
|
-
version: "0.1.0",
|
|
25
|
-
withStyles: answers.withStyles,
|
|
26
|
-
withStories: answers.withStories,
|
|
27
|
-
withSsrTests: answers.withSsrTests,
|
|
28
|
-
};
|
|
29
|
-
}
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
import { collectUndos, dryRun, readFile } from "@canonical/task";
|
|
2
|
-
import { describe, expect, it } from "vitest";
|
|
3
|
-
import appendExportToParentIndex from "./appendExportToParentIndex.js";
|
|
4
|
-
import dryRunWithFileState from "./dryRunWithFileState.js";
|
|
5
|
-
|
|
6
|
-
describe("appendExportToParentIndex", () => {
|
|
7
|
-
it("appends export when index exists and export not present", () => {
|
|
8
|
-
const result = dryRunWithFileState(
|
|
9
|
-
appendExportToParentIndex("src/components", "Button"),
|
|
10
|
-
{
|
|
11
|
-
"src/components/index.ts": 'export * from "./Other/index.js";\n',
|
|
12
|
-
},
|
|
13
|
-
);
|
|
14
|
-
|
|
15
|
-
const appendEffect = result.effects.find((e) => e._tag === "AppendFile");
|
|
16
|
-
expect(appendEffect).toBeDefined();
|
|
17
|
-
expect((appendEffect as { content: string }).content).toBe(
|
|
18
|
-
'export * from "./Button/index.js";\n',
|
|
19
|
-
);
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it("does nothing when export is already present", () => {
|
|
23
|
-
const result = dryRunWithFileState(
|
|
24
|
-
appendExportToParentIndex("src/components", "Button"),
|
|
25
|
-
{
|
|
26
|
-
"src/components/index.ts": 'export * from "./Button/index.js";\n',
|
|
27
|
-
},
|
|
28
|
-
);
|
|
29
|
-
|
|
30
|
-
const writeEffects = result.effects.filter(
|
|
31
|
-
(e) => e._tag === "AppendFile" || e._tag === "WriteFile",
|
|
32
|
-
);
|
|
33
|
-
expect(writeEffects).toHaveLength(0);
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it("creates new index file when it does not exist", () => {
|
|
37
|
-
const result = dryRunWithFileState(
|
|
38
|
-
appendExportToParentIndex("src/components", "Card"),
|
|
39
|
-
{},
|
|
40
|
-
);
|
|
41
|
-
|
|
42
|
-
const writeEffect = result.effects.find((e) => e._tag === "WriteFile");
|
|
43
|
-
expect(writeEffect).toBeDefined();
|
|
44
|
-
expect((writeEffect as { path: string }).path).toBe(
|
|
45
|
-
"src/components/index.ts",
|
|
46
|
-
);
|
|
47
|
-
expect((writeEffect as { content: string }).content).toBe(
|
|
48
|
-
'export * from "./Card/index.js";\n',
|
|
49
|
-
);
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
it("writeFile for new index has default undo (deleteFile)", () => {
|
|
53
|
-
const task = appendExportToParentIndex("src/components", "Card");
|
|
54
|
-
const undos = collectUndos(task);
|
|
55
|
-
|
|
56
|
-
expect(undos.length).toBeGreaterThan(0);
|
|
57
|
-
const undoEffects = dryRun(undos[0]).effects;
|
|
58
|
-
expect(undoEffects[0]._tag).toBe("DeleteFile");
|
|
59
|
-
expect((undoEffects[0] as { path: string }).path).toBe(
|
|
60
|
-
"src/components/index.ts",
|
|
61
|
-
);
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
it("appendFile carries custom undo (removeLineFromFile)", () => {
|
|
65
|
-
const result = dryRunWithFileState(
|
|
66
|
-
appendExportToParentIndex("src/components", "Button"),
|
|
67
|
-
{ "src/components/index.ts": "" },
|
|
68
|
-
);
|
|
69
|
-
|
|
70
|
-
const appendEffect = result.effects.find((e) => e._tag === "AppendFile");
|
|
71
|
-
expect(appendEffect).toBeDefined();
|
|
72
|
-
expect("undo" in (appendEffect as Record<string, unknown>)).toBe(true);
|
|
73
|
-
expect((appendEffect as Record<string, unknown>).undo).toBeDefined();
|
|
74
|
-
});
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
describe("dryRunWithFileState", () => {
|
|
78
|
-
it("returns empty string for missing files", () => {
|
|
79
|
-
const result = dryRunWithFileState(readFile("/missing/file.ts"), {});
|
|
80
|
-
expect(result.value).toBe("");
|
|
81
|
-
});
|
|
82
|
-
});
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import * as path from "node:path";
|
|
2
|
-
import {
|
|
3
|
-
appendFile,
|
|
4
|
-
exists,
|
|
5
|
-
flatMap,
|
|
6
|
-
ifElseM,
|
|
7
|
-
pure,
|
|
8
|
-
readFile,
|
|
9
|
-
type Task,
|
|
10
|
-
writeFile,
|
|
11
|
-
} from "@canonical/task";
|
|
12
|
-
import removeLineFromFile from "./removeLineFromFile.js";
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Append export to parent index.ts file (or create if not exists).
|
|
16
|
-
* Carries undo metadata: removes the export line (or deletes the index
|
|
17
|
-
* if it was created from scratch).
|
|
18
|
-
*/
|
|
19
|
-
export default function appendExportToParentIndex(
|
|
20
|
-
parentDir: string,
|
|
21
|
-
componentName: string,
|
|
22
|
-
): Task<void> {
|
|
23
|
-
const indexPath = path.join(parentDir, "index.ts");
|
|
24
|
-
const exportLine = `export * from "./${componentName}/index.js";\n`;
|
|
25
|
-
|
|
26
|
-
return ifElseM(
|
|
27
|
-
exists(indexPath),
|
|
28
|
-
// If exists, append (if not already exported)
|
|
29
|
-
flatMap(readFile(indexPath), (content) => {
|
|
30
|
-
if (content.includes(`./${componentName}`)) {
|
|
31
|
-
return pure(undefined); // Already exported
|
|
32
|
-
}
|
|
33
|
-
return appendFile(indexPath, exportLine, true, {
|
|
34
|
-
undo: removeLineFromFile(indexPath, exportLine),
|
|
35
|
-
});
|
|
36
|
-
}),
|
|
37
|
-
// If not exists, create new file (default undo: deleteFile is correct)
|
|
38
|
-
writeFile(indexPath, exportLine),
|
|
39
|
-
);
|
|
40
|
-
}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { dryRunWith, type Effect, type Task } from "@canonical/task";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* dryRunWith that mocks ReadFile and Exists to simulate file state.
|
|
5
|
-
* Test-only helper for file operation tests.
|
|
6
|
-
*/
|
|
7
|
-
export default function dryRunWithFileState<A>(
|
|
8
|
-
task: Task<A>,
|
|
9
|
-
files: Record<string, string>,
|
|
10
|
-
) {
|
|
11
|
-
const mocks = new Map<string, (effect: Effect) => unknown>([
|
|
12
|
-
[
|
|
13
|
-
"ReadFile",
|
|
14
|
-
(effect) => {
|
|
15
|
-
const e = effect as { path: string };
|
|
16
|
-
return files[e.path] ?? "";
|
|
17
|
-
},
|
|
18
|
-
],
|
|
19
|
-
[
|
|
20
|
-
"Exists",
|
|
21
|
-
(effect) => {
|
|
22
|
-
const e = effect as { path: string };
|
|
23
|
-
return e.path in files;
|
|
24
|
-
},
|
|
25
|
-
],
|
|
26
|
-
]);
|
|
27
|
-
return dryRunWith(task, mocks);
|
|
28
|
-
}
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import dryRunWithFileState from "./dryRunWithFileState.js";
|
|
3
|
-
import removeExportFromParentIndex from "./removeExportFromParentIndex.js";
|
|
4
|
-
|
|
5
|
-
describe("removeExportFromParentIndex", () => {
|
|
6
|
-
it("removes export when index exists with the export", () => {
|
|
7
|
-
const result = dryRunWithFileState(
|
|
8
|
-
removeExportFromParentIndex("src/components", "Button"),
|
|
9
|
-
{
|
|
10
|
-
"src/components/index.ts":
|
|
11
|
-
'export * from "./Button/index.js";\nexport * from "./Card/index.js";\n',
|
|
12
|
-
},
|
|
13
|
-
);
|
|
14
|
-
|
|
15
|
-
const writeEffect = result.effects.find((e) => e._tag === "WriteFile");
|
|
16
|
-
expect(writeEffect).toBeDefined();
|
|
17
|
-
expect((writeEffect as { content: string }).content).not.toContain(
|
|
18
|
-
"Button",
|
|
19
|
-
);
|
|
20
|
-
expect((writeEffect as { content: string }).content).toContain("Card");
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
it("does nothing when index does not exist", () => {
|
|
24
|
-
const result = dryRunWithFileState(
|
|
25
|
-
removeExportFromParentIndex("src/components", "Button"),
|
|
26
|
-
{},
|
|
27
|
-
);
|
|
28
|
-
|
|
29
|
-
const writeEffects = result.effects.filter(
|
|
30
|
-
(e) =>
|
|
31
|
-
e._tag === "WriteFile" ||
|
|
32
|
-
e._tag === "DeleteFile" ||
|
|
33
|
-
e._tag === "AppendFile",
|
|
34
|
-
);
|
|
35
|
-
expect(writeEffects).toHaveLength(0);
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
it("deletes index if removing export leaves it empty", () => {
|
|
39
|
-
const result = dryRunWithFileState(
|
|
40
|
-
removeExportFromParentIndex("src/components", "Button"),
|
|
41
|
-
{
|
|
42
|
-
"src/components/index.ts": 'export * from "./Button/index.js";\n',
|
|
43
|
-
},
|
|
44
|
-
);
|
|
45
|
-
|
|
46
|
-
const deleteEffect = result.effects.find((e) => e._tag === "DeleteFile");
|
|
47
|
-
expect(deleteEffect).toBeDefined();
|
|
48
|
-
expect((deleteEffect as { path: string }).path).toBe(
|
|
49
|
-
"src/components/index.ts",
|
|
50
|
-
);
|
|
51
|
-
});
|
|
52
|
-
});
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import * as path from "node:path";
|
|
2
|
-
import { exists, ifElseM, pure, type Task } from "@canonical/task";
|
|
3
|
-
import removeLineFromFile from "./removeLineFromFile.js";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Remove a component export from a parent index.ts.
|
|
7
|
-
* Idempotent — no-op if the export is not present or the file doesn't exist.
|
|
8
|
-
*/
|
|
9
|
-
export default function removeExportFromParentIndex(
|
|
10
|
-
parentDir: string,
|
|
11
|
-
componentName: string,
|
|
12
|
-
): Task<void> {
|
|
13
|
-
const indexPath = path.join(parentDir, "index.ts");
|
|
14
|
-
const exportLine = `export * from "./${componentName}/index.js";\n`;
|
|
15
|
-
|
|
16
|
-
return ifElseM(
|
|
17
|
-
exists(indexPath),
|
|
18
|
-
removeLineFromFile(indexPath, exportLine),
|
|
19
|
-
pure(undefined),
|
|
20
|
-
);
|
|
21
|
-
}
|