@canonical/summon-component 0.1.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 +453 -0
- package/biome.json +6 -0
- package/package.json +42 -0
- package/src/__tests__/generators.test.ts +310 -0
- package/src/index.ts +36 -0
- package/src/react/generator.ts +177 -0
- package/src/react/index.ts +6 -0
- package/src/react/types.ts +8 -0
- package/src/shared/file-operations.ts +39 -0
- package/src/shared/index.ts +10 -0
- package/src/shared/prompts.ts +57 -0
- package/src/shared/string-helpers.ts +29 -0
- package/src/shared/template-context.ts +29 -0
- package/src/shared/types.ts +36 -0
- package/src/shared/validation.ts +26 -0
- package/src/svelte/generator.ts +196 -0
- package/src/svelte/index.ts +6 -0
- package/src/svelte/types.ts +11 -0
- package/src/templates/react/component.tsx.ejs +24 -0
- package/src/templates/react/index.ts.ejs +4 -0
- package/src/templates/react/ssr.test.tsx.ejs +13 -0
- package/src/templates/react/stories.tsx.ejs +26 -0
- package/src/templates/react/test.tsx.ejs +24 -0
- package/src/templates/react/types.ts.ejs +8 -0
- package/src/templates/shared/styles.css.ejs +5 -0
- package/src/templates/svelte/component.svelte.ejs +25 -0
- package/src/templates/svelte/index.ts.ejs +4 -0
- package/src/templates/svelte/ssr.test.ts.ejs +12 -0
- package/src/templates/svelte/stories.svelte.ejs +20 -0
- package/src/templates/svelte/stories.ts.ejs +21 -0
- package/src/templates/svelte/test.ts.ejs +18 -0
- package/src/templates/svelte/types.ts.ejs +9 -0
- package/tsconfig.json +9 -0
package/README.md
ADDED
|
@@ -0,0 +1,453 @@
|
|
|
1
|
+
# @canonical/summon-component
|
|
2
|
+
|
|
3
|
+
Component scaffolding for React and Svelte projects. Generates production-ready component structures with TypeScript, tests, stories, and styles — all following consistent conventions.
|
|
4
|
+
|
|
5
|
+
## Why Use This?
|
|
6
|
+
|
|
7
|
+
Creating a new component by hand means creating 5-7 files with boilerplate that's almost identical every time. This generator handles that in one command, ensuring:
|
|
8
|
+
|
|
9
|
+
- Consistent file structure across your codebase
|
|
10
|
+
- Proper TypeScript types from the start
|
|
11
|
+
- Test files that actually test something
|
|
12
|
+
- Storybook stories ready to customize
|
|
13
|
+
- Auto-registration in your component barrel
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
bun add @canonical/summon-component
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Requires `@canonical/summon` as a peer dependency:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
bun add @canonical/summon
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Or link globally for use across projects:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
cd /path/to/summon-component
|
|
31
|
+
bun link # for bun
|
|
32
|
+
npm link # for npm
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
## Quick Start
|
|
38
|
+
|
|
39
|
+
### React Component
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
# Interactive — prompts guide you
|
|
43
|
+
summon component react
|
|
44
|
+
|
|
45
|
+
# Direct — specify path
|
|
46
|
+
summon component react --component-path=src/components/Button
|
|
47
|
+
|
|
48
|
+
# Preview — see what would be created
|
|
49
|
+
summon component react --component-path=src/components/Button --dry-run
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Svelte Component
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
# Interactive
|
|
56
|
+
summon component svelte
|
|
57
|
+
|
|
58
|
+
# Direct
|
|
59
|
+
summon component svelte --component-path=src/lib/components/Card
|
|
60
|
+
|
|
61
|
+
# With TypeScript stories instead of Svelte CSF
|
|
62
|
+
summon component svelte --component-path=src/lib/components/Card --use-ts-stories
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
---
|
|
66
|
+
|
|
67
|
+
## What Gets Generated
|
|
68
|
+
|
|
69
|
+
### React
|
|
70
|
+
|
|
71
|
+
For `summon component react --component-path=src/components/Button`:
|
|
72
|
+
|
|
73
|
+
```
|
|
74
|
+
src/components/Button/
|
|
75
|
+
├── Button.tsx # Component implementation
|
|
76
|
+
├── types.ts # Props interface
|
|
77
|
+
├── index.ts # Barrel export
|
|
78
|
+
├── Button.test.tsx # Unit tests (Testing Library)
|
|
79
|
+
├── Button.ssr.test.tsx # SSR tests (optional)
|
|
80
|
+
├── Button.stories.tsx # Storybook stories (optional)
|
|
81
|
+
└── styles.css # Component styles (optional)
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
And appends to `src/components/index.ts`:
|
|
85
|
+
|
|
86
|
+
```typescript
|
|
87
|
+
export * from "./Button";
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
#### Generated Component
|
|
91
|
+
|
|
92
|
+
```tsx
|
|
93
|
+
// Button.tsx
|
|
94
|
+
import type { ButtonProps } from "./types";
|
|
95
|
+
import "./styles.css";
|
|
96
|
+
|
|
97
|
+
export const Button = ({
|
|
98
|
+
className,
|
|
99
|
+
children,
|
|
100
|
+
...props
|
|
101
|
+
}: ButtonProps): JSX.Element => {
|
|
102
|
+
return (
|
|
103
|
+
<div className={`button${className ? ` ${className}` : ""}`} {...props}>
|
|
104
|
+
{children}
|
|
105
|
+
</div>
|
|
106
|
+
);
|
|
107
|
+
};
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
#### Generated Types
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
// types.ts
|
|
114
|
+
import type { HTMLAttributes, PropsWithChildren } from "react";
|
|
115
|
+
|
|
116
|
+
export interface ButtonProps
|
|
117
|
+
extends PropsWithChildren<HTMLAttributes<HTMLDivElement>> {}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
#### Generated Test
|
|
121
|
+
|
|
122
|
+
```tsx
|
|
123
|
+
// Button.test.tsx
|
|
124
|
+
import { render, screen } from "@testing-library/react";
|
|
125
|
+
import { describe, expect, it } from "vitest";
|
|
126
|
+
import { Button } from "./Button";
|
|
127
|
+
|
|
128
|
+
describe("Button", () => {
|
|
129
|
+
it("renders children", () => {
|
|
130
|
+
render(<Button>Hello</Button>);
|
|
131
|
+
expect(screen.getByText("Hello")).toBeInTheDocument();
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("applies custom className", () => {
|
|
135
|
+
render(<Button className="custom">Content</Button>);
|
|
136
|
+
expect(screen.getByText("Content")).toHaveClass("custom");
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### Svelte
|
|
142
|
+
|
|
143
|
+
For `summon component svelte --component-path=src/lib/components/Card`:
|
|
144
|
+
|
|
145
|
+
```
|
|
146
|
+
src/lib/components/Card/
|
|
147
|
+
├── Card.svelte # Svelte 5 component with runes
|
|
148
|
+
├── types.ts # Props interface
|
|
149
|
+
├── index.ts # Barrel export
|
|
150
|
+
├── Card.svelte.test.ts # Unit tests
|
|
151
|
+
├── Card.ssr.test.ts # SSR tests (optional)
|
|
152
|
+
├── Card.stories.svelte # Storybook CSF (optional)
|
|
153
|
+
└── styles.css # External styles (optional, or inline <style>)
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
#### Generated Component
|
|
157
|
+
|
|
158
|
+
```svelte
|
|
159
|
+
<!-- Card.svelte -->
|
|
160
|
+
<script lang="ts">
|
|
161
|
+
import type { CardProps } from "./types";
|
|
162
|
+
|
|
163
|
+
let {
|
|
164
|
+
class: className = "",
|
|
165
|
+
children,
|
|
166
|
+
...props
|
|
167
|
+
}: CardProps = $props();
|
|
168
|
+
</script>
|
|
169
|
+
|
|
170
|
+
<div class="card{className ? ` ${className}` : ''}" {...props}>
|
|
171
|
+
{@render children?.()}
|
|
172
|
+
</div>
|
|
173
|
+
|
|
174
|
+
<style>
|
|
175
|
+
.card {
|
|
176
|
+
/* Component styles */
|
|
177
|
+
}
|
|
178
|
+
</style>
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
Uses Svelte 5 runes (`$props()`) and render tags (`@render`).
|
|
182
|
+
|
|
183
|
+
---
|
|
184
|
+
|
|
185
|
+
## Options Reference
|
|
186
|
+
|
|
187
|
+
### React Options
|
|
188
|
+
|
|
189
|
+
| Flag | Description | Default |
|
|
190
|
+
|------|-------------|---------|
|
|
191
|
+
| `--component-path` | Full path for the component (e.g., `src/components/Button`) | Interactive prompt |
|
|
192
|
+
| `--with-styles` | Include `styles.css` file | `true` |
|
|
193
|
+
| `--no-with-styles` | Skip styles file | — |
|
|
194
|
+
| `--with-stories` | Include Storybook stories | `true` |
|
|
195
|
+
| `--no-with-stories` | Skip stories file | — |
|
|
196
|
+
| `--with-ssr-tests` | Include SSR test file | `true` |
|
|
197
|
+
| `--no-with-ssr-tests` | Skip SSR tests | — |
|
|
198
|
+
|
|
199
|
+
### Svelte Options
|
|
200
|
+
|
|
201
|
+
| Flag | Description | Default |
|
|
202
|
+
|------|-------------|---------|
|
|
203
|
+
| `--component-path` | Full path for the component (e.g., `src/lib/components/Card`) | Interactive prompt |
|
|
204
|
+
| `--with-styles` | Include inline `<style>` block | `true` |
|
|
205
|
+
| `--no-with-styles` | Skip styles | — |
|
|
206
|
+
| `--with-stories` | Include Storybook stories | `true` |
|
|
207
|
+
| `--no-with-stories` | Skip stories | — |
|
|
208
|
+
| `--use-ts-stories` | Use `.stories.ts` instead of `.stories.svelte` | `false` |
|
|
209
|
+
| `--with-ssr-tests` | Include SSR test file | `true` |
|
|
210
|
+
| `--no-with-ssr-tests` | Skip SSR tests | — |
|
|
211
|
+
|
|
212
|
+
### Global Options
|
|
213
|
+
|
|
214
|
+
| Flag | Description |
|
|
215
|
+
|------|-------------|
|
|
216
|
+
| `--dry-run`, `-d` | Preview without writing files |
|
|
217
|
+
| `--yes`, `-y` | Skip confirmation prompts |
|
|
218
|
+
| `--no-preview` | Skip the file preview step |
|
|
219
|
+
| `--help` | Show all options |
|
|
220
|
+
|
|
221
|
+
---
|
|
222
|
+
|
|
223
|
+
## Component Path Convention
|
|
224
|
+
|
|
225
|
+
The `--component-path` determines:
|
|
226
|
+
1. **Directory location** — where files are created
|
|
227
|
+
2. **Component name** — derived from the last path segment
|
|
228
|
+
|
|
229
|
+
```bash
|
|
230
|
+
--component-path=src/components/UserProfile
|
|
231
|
+
# └──────────────┘└─────────┘
|
|
232
|
+
# directory name: UserProfile
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
The component name is used as-is for PascalCase (class names) and converted for other cases:
|
|
236
|
+
- `UserProfile.tsx` (filename)
|
|
237
|
+
- `UserProfileProps` (types)
|
|
238
|
+
- `user-profile` (CSS class in styles)
|
|
239
|
+
|
|
240
|
+
---
|
|
241
|
+
|
|
242
|
+
## Barrel Export Auto-Update
|
|
243
|
+
|
|
244
|
+
The generator automatically appends an export to the parent directory's `index.ts`:
|
|
245
|
+
|
|
246
|
+
```bash
|
|
247
|
+
summon component react --component-path=src/components/Button
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
Appends to `src/components/index.ts`:
|
|
251
|
+
|
|
252
|
+
```typescript
|
|
253
|
+
export * from "./Button";
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
If the barrel file doesn't exist, it's created.
|
|
257
|
+
|
|
258
|
+
---
|
|
259
|
+
|
|
260
|
+
## Customization
|
|
261
|
+
|
|
262
|
+
### Override with Local Generators
|
|
263
|
+
|
|
264
|
+
Create a local generator to override the installed one. Local generators take precedence:
|
|
265
|
+
|
|
266
|
+
```
|
|
267
|
+
your-project/
|
|
268
|
+
└── generators/
|
|
269
|
+
└── component/
|
|
270
|
+
└── react/
|
|
271
|
+
└── index.ts # Your custom React component generator
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
Now `summon component react` uses your version.
|
|
275
|
+
|
|
276
|
+
### Extend the Base Generators
|
|
277
|
+
|
|
278
|
+
Import and compose with the existing generators:
|
|
279
|
+
|
|
280
|
+
```typescript
|
|
281
|
+
// generators/component/react/index.ts
|
|
282
|
+
import { generators } from "@canonical/summon-component";
|
|
283
|
+
import { sequence_, writeFile } from "@canonical/summon";
|
|
284
|
+
|
|
285
|
+
const baseGenerator = generators["component/react"];
|
|
286
|
+
|
|
287
|
+
export const generator = {
|
|
288
|
+
...baseGenerator,
|
|
289
|
+
|
|
290
|
+
// Add custom prompts
|
|
291
|
+
prompts: [
|
|
292
|
+
...baseGenerator.prompts,
|
|
293
|
+
{
|
|
294
|
+
name: "withI18n",
|
|
295
|
+
type: "confirm",
|
|
296
|
+
message: "Include i18n hooks?",
|
|
297
|
+
default: false,
|
|
298
|
+
},
|
|
299
|
+
],
|
|
300
|
+
|
|
301
|
+
// Extend generation
|
|
302
|
+
generate: (answers) => sequence_([
|
|
303
|
+
// Run base generator
|
|
304
|
+
baseGenerator.generate(answers),
|
|
305
|
+
|
|
306
|
+
// Add your files
|
|
307
|
+
answers.withI18n && writeFile(
|
|
308
|
+
`${answers.componentPath}/useTranslations.ts`,
|
|
309
|
+
`export const useTranslations = () => ({ t: (k: string) => k });\n`
|
|
310
|
+
),
|
|
311
|
+
].filter(Boolean)),
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
export const generators = {
|
|
315
|
+
"component/react": generator,
|
|
316
|
+
};
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
### Modify Templates
|
|
320
|
+
|
|
321
|
+
Fork the package and edit templates in `src/react/templates/` or `src/svelte/templates/`. Templates use [EJS syntax](https://ejs.co/).
|
|
322
|
+
|
|
323
|
+
---
|
|
324
|
+
|
|
325
|
+
## Testing Your Components
|
|
326
|
+
|
|
327
|
+
The generated test files use Vitest and Testing Library. Run them with:
|
|
328
|
+
|
|
329
|
+
```bash
|
|
330
|
+
# If you have vitest configured
|
|
331
|
+
bun test
|
|
332
|
+
|
|
333
|
+
# Or specifically
|
|
334
|
+
bun vitest src/components/Button/Button.test.tsx
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
### SSR Tests
|
|
338
|
+
|
|
339
|
+
SSR tests verify the component renders without errors on the server:
|
|
340
|
+
|
|
341
|
+
```typescript
|
|
342
|
+
// Button.ssr.test.tsx
|
|
343
|
+
import { renderToString } from "react-dom/server";
|
|
344
|
+
import { describe, expect, it } from "vitest";
|
|
345
|
+
import { Button } from "./Button";
|
|
346
|
+
|
|
347
|
+
describe("Button SSR", () => {
|
|
348
|
+
it("renders without hydration errors", () => {
|
|
349
|
+
const html = renderToString(<Button>Click me</Button>);
|
|
350
|
+
expect(html).toContain("Click me");
|
|
351
|
+
});
|
|
352
|
+
});
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
---
|
|
356
|
+
|
|
357
|
+
## Storybook Integration
|
|
358
|
+
|
|
359
|
+
Generated stories work with Storybook 7+:
|
|
360
|
+
|
|
361
|
+
```tsx
|
|
362
|
+
// Button.stories.tsx
|
|
363
|
+
import type { Meta, StoryObj } from "@storybook/react";
|
|
364
|
+
import { Button } from "./Button";
|
|
365
|
+
|
|
366
|
+
const meta: Meta<typeof Button> = {
|
|
367
|
+
component: Button,
|
|
368
|
+
title: "Components/Button",
|
|
369
|
+
};
|
|
370
|
+
|
|
371
|
+
export default meta;
|
|
372
|
+
type Story = StoryObj<typeof Button>;
|
|
373
|
+
|
|
374
|
+
export const Default: Story = {
|
|
375
|
+
args: {
|
|
376
|
+
children: "Click me",
|
|
377
|
+
},
|
|
378
|
+
};
|
|
379
|
+
```
|
|
380
|
+
|
|
381
|
+
### Svelte CSF vs TypeScript Stories
|
|
382
|
+
|
|
383
|
+
By default, Svelte components get `.stories.svelte` files using [Svelte CSF](https://github.com/storybookjs/addon-svelte-csf). Use `--use-ts-stories` for traditional TypeScript stories if you prefer.
|
|
384
|
+
|
|
385
|
+
---
|
|
386
|
+
|
|
387
|
+
## Programmatic Usage
|
|
388
|
+
|
|
389
|
+
Use the generators in your own code:
|
|
390
|
+
|
|
391
|
+
```typescript
|
|
392
|
+
import { generators } from "@canonical/summon-component";
|
|
393
|
+
import { dryRun, getAffectedFiles } from "@canonical/summon";
|
|
394
|
+
|
|
395
|
+
const reactGenerator = generators["component/react"];
|
|
396
|
+
|
|
397
|
+
// Preview what would be created
|
|
398
|
+
const task = reactGenerator.generate({
|
|
399
|
+
componentPath: "src/components/Button",
|
|
400
|
+
withStyles: true,
|
|
401
|
+
withStories: true,
|
|
402
|
+
withSsrTests: false,
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
const { effects } = dryRun(task);
|
|
406
|
+
console.log("Would create:", getAffectedFiles(effects));
|
|
407
|
+
```
|
|
408
|
+
|
|
409
|
+
---
|
|
410
|
+
|
|
411
|
+
## Troubleshooting
|
|
412
|
+
|
|
413
|
+
### "Generator not found"
|
|
414
|
+
|
|
415
|
+
Ensure the package is installed and discoverable:
|
|
416
|
+
|
|
417
|
+
```bash
|
|
418
|
+
# Check if summon sees it
|
|
419
|
+
summon
|
|
420
|
+
|
|
421
|
+
# Should show:
|
|
422
|
+
# component [pkg] (has subtopics)
|
|
423
|
+
# └─ react, svelte
|
|
424
|
+
```
|
|
425
|
+
|
|
426
|
+
If not, verify installation:
|
|
427
|
+
|
|
428
|
+
```bash
|
|
429
|
+
bun add @canonical/summon-component
|
|
430
|
+
```
|
|
431
|
+
|
|
432
|
+
### "Cannot find module @canonical/summon"
|
|
433
|
+
|
|
434
|
+
Install the peer dependency:
|
|
435
|
+
|
|
436
|
+
```bash
|
|
437
|
+
bun add @canonical/summon
|
|
438
|
+
```
|
|
439
|
+
|
|
440
|
+
### Barrel file not updated
|
|
441
|
+
|
|
442
|
+
The generator appends to `{parentDir}/index.ts`. If your project uses a different convention (e.g., `index.tsx` or no barrel), manually add the export.
|
|
443
|
+
|
|
444
|
+
---
|
|
445
|
+
|
|
446
|
+
## Related
|
|
447
|
+
|
|
448
|
+
- **[@canonical/summon](../summon/)** — The generator framework (required peer dependency)
|
|
449
|
+
- **[@canonical/summon-package](../summon-package/)** — Package scaffolding for the monorepo
|
|
450
|
+
|
|
451
|
+
## License
|
|
452
|
+
|
|
453
|
+
GPL-3.0
|
package/biome.json
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@canonical/summon-component",
|
|
3
|
+
"description": "Component generators for Summon - React and Svelte component scaffolding",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "src/index.ts",
|
|
7
|
+
"author": {
|
|
8
|
+
"email": "webteam@canonical.com",
|
|
9
|
+
"name": "Canonical Webteam"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "https://github.com/canonical/pragma"
|
|
14
|
+
},
|
|
15
|
+
"license": "GPL-3.0",
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/canonical/pragma/issues"
|
|
18
|
+
},
|
|
19
|
+
"homepage": "https://github.com/canonical/pragma#readme",
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "echo 'No build needed - runs directly from TypeScript'",
|
|
22
|
+
"build:all": "bun run build",
|
|
23
|
+
"check": "bun run check:biome && bun run check:ts && bun run check:webarchitect",
|
|
24
|
+
"check:fix": "bun run check:biome:fix && bun run check:ts",
|
|
25
|
+
"check:biome": "biome check",
|
|
26
|
+
"check:biome:fix": "biome check --write",
|
|
27
|
+
"check:ts": "tsc --noEmit",
|
|
28
|
+
"check:webarchitect": "webarchitect base",
|
|
29
|
+
"test": "vitest run"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@biomejs/biome": "2.3.11",
|
|
33
|
+
"@canonical/biome-config": "^0.11.0",
|
|
34
|
+
"@canonical/typescript-config-base": "^0.11.0",
|
|
35
|
+
"bun-types": "^1.0.0",
|
|
36
|
+
"typescript": "^5.9.3",
|
|
37
|
+
"vitest": "^3.2.4"
|
|
38
|
+
},
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"@canonical/summon": "^0.1.0"
|
|
41
|
+
}
|
|
42
|
+
}
|