@canonical/summon-component 0.20.0 → 0.22.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/CHANGELOG.md +16 -0
- package/package.json +11 -8
- package/src/{__tests__/generators.test.ts → index.test.ts} +168 -1
- package/src/lit/generator.ts +3 -1
- package/src/lit/index.ts +1 -1
- package/src/react/generator.ts +3 -1
- package/src/react/index.ts +1 -1
- package/src/shared/createComponentPathPrompt.ts +28 -0
- package/src/shared/{template-context.ts → createTemplateContext.ts} +4 -4
- package/src/shared/file-operations/appendExportToParentIndex.test.ts +75 -0
- package/src/shared/file-operations/appendExportToParentIndex.ts +40 -0
- package/src/shared/file-operations/dryRunWithFileState.ts +28 -0
- package/src/shared/file-operations/index.ts +3 -0
- package/src/shared/file-operations/removeExportFromParentIndex.test.ts +52 -0
- package/src/shared/file-operations/removeExportFromParentIndex.ts +21 -0
- package/src/shared/file-operations/removeLineFromFile.test.ts +66 -0
- package/src/shared/file-operations/removeLineFromFile.ts +29 -0
- package/src/shared/index.ts +6 -5
- package/src/shared/sharedPrompts.ts +30 -0
- package/src/shared/string-helpers/getComponentName.test.ts +18 -0
- package/src/shared/string-helpers/getComponentName.ts +8 -0
- package/src/shared/string-helpers/getParentDir.test.ts +16 -0
- package/src/shared/string-helpers/getParentDir.ts +8 -0
- package/src/shared/string-helpers/index.ts +3 -0
- package/src/shared/validateComponentPath.test.ts +40 -0
- package/src/shared/{validation.ts → validateComponentPath.ts} +3 -13
- package/src/svelte/generator.ts +3 -1
- package/src/svelte/index.ts +1 -1
- package/src/testing/integration/generators-undo.test.ts +200 -0
- package/vitest.config.ts +12 -0
- package/src/shared/file-operations.ts +0 -83
- package/src/shared/prompts.ts +0 -57
- package/src/shared/string-helpers.ts +0 -29
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,22 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [0.22.0](https://github.com/canonical/pragma/compare/v0.22.0-experimental.0...v0.22.0) (2026-04-03)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @canonical/summon-component
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
# [0.21.0](https://github.com/canonical/pragma/compare/v0.20.1...v0.21.0) (2026-04-01)
|
|
15
|
+
|
|
16
|
+
**Note:** Version bump only for package @canonical/summon-component
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
6
22
|
# [0.20.0](https://github.com/canonical/pragma/compare/v0.19.0...v0.20.0) (2026-03-26)
|
|
7
23
|
|
|
8
24
|
**Note:** Version bump only for package @canonical/summon-component
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@canonical/summon-component",
|
|
3
3
|
"description": "Component generators for Summon - React, Svelte and Lit component scaffolding",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.22.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.ts",
|
|
7
7
|
"author": {
|
|
@@ -29,18 +29,21 @@
|
|
|
29
29
|
"test": "vitest run"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
|
-
"@biomejs/biome": "2.4.
|
|
33
|
-
"@canonical/biome-config": "^0.
|
|
34
|
-
"@canonical/typescript-config": "^0.
|
|
35
|
-
"@canonical/webarchitect": "^0.
|
|
32
|
+
"@biomejs/biome": "2.4.9",
|
|
33
|
+
"@canonical/biome-config": "^0.22.0",
|
|
34
|
+
"@canonical/typescript-config": "^0.22.0",
|
|
35
|
+
"@canonical/webarchitect": "^0.22.0",
|
|
36
36
|
"@types/bun": "^1.3.10",
|
|
37
37
|
"@types/node": "^24.12.0",
|
|
38
38
|
"typescript": "^5.9.3",
|
|
39
39
|
"vitest": "^4.0.18"
|
|
40
40
|
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@canonical/utils": "^0.22.0"
|
|
43
|
+
},
|
|
41
44
|
"peerDependencies": {
|
|
42
|
-
"@canonical/summon-core": "^0.18.0",
|
|
43
|
-
"@canonical/task": "^0.18.0"
|
|
45
|
+
"@canonical/summon-core": "^0.18.0 || ^0.20.0",
|
|
46
|
+
"@canonical/task": "^0.18.0 || ^0.20.0"
|
|
44
47
|
},
|
|
45
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "99e187cf46d8bd7f3933705322fa4ab2d4a711e7"
|
|
46
49
|
}
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
import * as fs from "node:fs";
|
|
9
9
|
import { dryRun, dryRunWith, type Effect, type Task } from "@canonical/task";
|
|
10
10
|
import { describe, expect, it } from "vitest";
|
|
11
|
-
import { generators } from "
|
|
11
|
+
import { generators } from "./index.js";
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* Helper: dry-run with actual file reading for templates.
|
|
@@ -335,3 +335,170 @@ describe("component/svelte generator", () => {
|
|
|
335
335
|
});
|
|
336
336
|
});
|
|
337
337
|
});
|
|
338
|
+
|
|
339
|
+
// =============================================================================
|
|
340
|
+
// Lit Generator
|
|
341
|
+
// =============================================================================
|
|
342
|
+
|
|
343
|
+
describe("component/lit generator", () => {
|
|
344
|
+
const generator = generators["component/lit"];
|
|
345
|
+
|
|
346
|
+
describe("meta", () => {
|
|
347
|
+
it("has correct name", () => {
|
|
348
|
+
expect(generator.meta.name).toBe("component/lit");
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
it("has description containing Lit", () => {
|
|
352
|
+
expect(generator.meta.description).toContain("Lit");
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
it("has version", () => {
|
|
356
|
+
expect(generator.meta.version).toBe("0.1.0");
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
it("has help text mentioning decorators", () => {
|
|
360
|
+
expect(generator.meta.help).toBeDefined();
|
|
361
|
+
expect(generator.meta.help).toContain("decorators");
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
it("has examples", () => {
|
|
365
|
+
expect(generator.meta.examples).toBeDefined();
|
|
366
|
+
expect(generator.meta.examples?.length).toBeGreaterThan(0);
|
|
367
|
+
});
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
describe("prompts", () => {
|
|
371
|
+
it("has componentPath prompt with lib default path", () => {
|
|
372
|
+
const prompt = generator.prompts.find((p) => p.name === "componentPath");
|
|
373
|
+
expect(prompt).toBeDefined();
|
|
374
|
+
expect(prompt?.default).toBe("src/lib/components/MyComponent");
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
it("has withStyles prompt", () => {
|
|
378
|
+
const prompt = generator.prompts.find((p) => p.name === "withStyles");
|
|
379
|
+
expect(prompt).toBeDefined();
|
|
380
|
+
expect(prompt?.type).toBe("confirm");
|
|
381
|
+
expect(prompt?.default).toBe(true);
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
it("has withStories prompt", () => {
|
|
385
|
+
const prompt = generator.prompts.find((p) => p.name === "withStories");
|
|
386
|
+
expect(prompt).toBeDefined();
|
|
387
|
+
expect(prompt?.type).toBe("confirm");
|
|
388
|
+
expect(prompt?.default).toBe(true);
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
it("does not have withSsrTests prompt", () => {
|
|
392
|
+
const prompt = generator.prompts.find((p) => p.name === "withSsrTests");
|
|
393
|
+
expect(prompt).toBeUndefined();
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
it("does not have useTsStories prompt", () => {
|
|
397
|
+
const prompt = generator.prompts.find((p) => p.name === "useTsStories");
|
|
398
|
+
expect(prompt).toBeUndefined();
|
|
399
|
+
});
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
describe("generate", () => {
|
|
403
|
+
it("creates lit component files with all options enabled", () => {
|
|
404
|
+
const task = generator.generate({
|
|
405
|
+
componentPath: "src/lib/components/Button",
|
|
406
|
+
withStyles: true,
|
|
407
|
+
withStories: true,
|
|
408
|
+
withSsrTests: false,
|
|
409
|
+
});
|
|
410
|
+
|
|
411
|
+
const result = dryRun(task);
|
|
412
|
+
const paths = result.effects
|
|
413
|
+
.filter((e) => e._tag === "WriteFile")
|
|
414
|
+
.map((e) => (e as { path: string }).path);
|
|
415
|
+
|
|
416
|
+
expect(paths).toContain("src/lib/components/Button/Button.ts");
|
|
417
|
+
expect(paths).toContain("src/lib/components/Button/index.ts");
|
|
418
|
+
expect(paths).toContain("src/lib/components/Button/types.ts");
|
|
419
|
+
expect(paths).toContain("src/lib/components/Button/Button.tests.ts");
|
|
420
|
+
expect(paths).toContain("src/lib/components/Button/Button.stories.ts");
|
|
421
|
+
expect(paths).toContain("src/lib/components/Button/styles.css");
|
|
422
|
+
});
|
|
423
|
+
|
|
424
|
+
it("creates minimal component without optional files", () => {
|
|
425
|
+
const task = generator.generate({
|
|
426
|
+
componentPath: "src/lib/components/Icon",
|
|
427
|
+
withStyles: false,
|
|
428
|
+
withStories: false,
|
|
429
|
+
withSsrTests: false,
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
const result = dryRun(task);
|
|
433
|
+
const paths = result.effects
|
|
434
|
+
.filter((e) => e._tag === "WriteFile")
|
|
435
|
+
.map((e) => (e as { path: string }).path);
|
|
436
|
+
|
|
437
|
+
expect(paths).toContain("src/lib/components/Icon/Icon.ts");
|
|
438
|
+
expect(paths).toContain("src/lib/components/Icon/index.ts");
|
|
439
|
+
expect(paths).toContain("src/lib/components/Icon/types.ts");
|
|
440
|
+
expect(paths).toContain("src/lib/components/Icon/Icon.tests.ts");
|
|
441
|
+
expect(paths).not.toContain("src/lib/components/Icon/Icon.stories.ts");
|
|
442
|
+
expect(paths).not.toContain("src/lib/components/Icon/styles.css");
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
it("extracts component name from path", () => {
|
|
446
|
+
const task = generator.generate({
|
|
447
|
+
componentPath: "src/lib/components/MyButton",
|
|
448
|
+
withStyles: false,
|
|
449
|
+
withStories: false,
|
|
450
|
+
withSsrTests: false,
|
|
451
|
+
});
|
|
452
|
+
|
|
453
|
+
const result = dryRunWithTemplates(task);
|
|
454
|
+
const mainFile = result.effects.find(
|
|
455
|
+
(e) =>
|
|
456
|
+
e._tag === "WriteFile" &&
|
|
457
|
+
(e as { path: string }).path.endsWith("MyButton.ts"),
|
|
458
|
+
);
|
|
459
|
+
|
|
460
|
+
expect(mainFile).toBeDefined();
|
|
461
|
+
const content = (mainFile as { content: string }).content;
|
|
462
|
+
expect(content).toContain("MyButton");
|
|
463
|
+
});
|
|
464
|
+
|
|
465
|
+
it("generates kebab-case tag name", () => {
|
|
466
|
+
const task = generator.generate({
|
|
467
|
+
componentPath: "src/lib/components/MyButton",
|
|
468
|
+
withStyles: false,
|
|
469
|
+
withStories: false,
|
|
470
|
+
withSsrTests: false,
|
|
471
|
+
});
|
|
472
|
+
|
|
473
|
+
const result = dryRunWithTemplates(task);
|
|
474
|
+
const mainFile = result.effects.find(
|
|
475
|
+
(e) =>
|
|
476
|
+
e._tag === "WriteFile" &&
|
|
477
|
+
(e as { path: string }).path.endsWith("MyButton.ts"),
|
|
478
|
+
);
|
|
479
|
+
|
|
480
|
+
const content = (mainFile as { content: string }).content;
|
|
481
|
+
expect(content).toContain("my-button");
|
|
482
|
+
});
|
|
483
|
+
|
|
484
|
+
it("appends export to parent index", () => {
|
|
485
|
+
const task = generator.generate({
|
|
486
|
+
componentPath: "src/lib/components/Card",
|
|
487
|
+
withStyles: false,
|
|
488
|
+
withStories: false,
|
|
489
|
+
withSsrTests: false,
|
|
490
|
+
});
|
|
491
|
+
|
|
492
|
+
const result = dryRun(task);
|
|
493
|
+
const parentIndex = result.effects.find(
|
|
494
|
+
(e) =>
|
|
495
|
+
e._tag === "WriteFile" &&
|
|
496
|
+
(e as { path: string }).path === "src/lib/components/index.ts",
|
|
497
|
+
);
|
|
498
|
+
|
|
499
|
+
expect(parentIndex).toBeDefined();
|
|
500
|
+
const content = (parentIndex as { content: string }).content;
|
|
501
|
+
expect(content).toContain('export * from "./Card/index.js"');
|
|
502
|
+
});
|
|
503
|
+
});
|
|
504
|
+
});
|
package/src/lit/generator.ts
CHANGED
|
@@ -81,7 +81,7 @@ const litPrompts = [
|
|
|
81
81
|
*
|
|
82
82
|
* @see {@link https://summon.dev/generators/component/lit} for documentation
|
|
83
83
|
*/
|
|
84
|
-
|
|
84
|
+
const generator = {
|
|
85
85
|
meta: {
|
|
86
86
|
name: "component/lit",
|
|
87
87
|
displayName: `${pkg.name}:lit`,
|
|
@@ -177,3 +177,5 @@ with the custom element tag 'button'.`,
|
|
|
177
177
|
]);
|
|
178
178
|
},
|
|
179
179
|
} as const satisfies GeneratorDefinition<LitAnswers>;
|
|
180
|
+
|
|
181
|
+
export default generator;
|
package/src/lit/index.ts
CHANGED
package/src/react/generator.ts
CHANGED
|
@@ -64,7 +64,7 @@ const reactTemplates = {
|
|
|
64
64
|
*
|
|
65
65
|
* @see {@link https://summon.dev/generators/component/react} for documentation
|
|
66
66
|
*/
|
|
67
|
-
|
|
67
|
+
const generator = {
|
|
68
68
|
meta: {
|
|
69
69
|
name: "component/react",
|
|
70
70
|
displayName: `${pkg.name}:react`,
|
|
@@ -170,3 +170,5 @@ For example, 'src/components/Button' creates a 'Button' component.`,
|
|
|
170
170
|
]);
|
|
171
171
|
},
|
|
172
172
|
} as const satisfies GeneratorDefinition<ReactComponentAnswers>;
|
|
173
|
+
|
|
174
|
+
export default generator;
|
package/src/react/index.ts
CHANGED
|
@@ -0,0 +1,28 @@
|
|
|
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
|
+
}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Template context creation for component generators
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import { getComponentName, kebabCase } from "./string-helpers.js";
|
|
5
|
+
import { getComponentName, kebabCase } from "./string-helpers/index.js";
|
|
6
6
|
import type {
|
|
7
7
|
BaseComponentAnswers,
|
|
8
8
|
Framework,
|
|
@@ -12,10 +12,10 @@ import type {
|
|
|
12
12
|
/**
|
|
13
13
|
* Create template context from answers
|
|
14
14
|
*/
|
|
15
|
-
export
|
|
15
|
+
export default function createTemplateContext(
|
|
16
16
|
answers: BaseComponentAnswers,
|
|
17
17
|
framework: Framework,
|
|
18
|
-
): TemplateContext
|
|
18
|
+
): TemplateContext {
|
|
19
19
|
const name = getComponentName(answers.componentPath);
|
|
20
20
|
return {
|
|
21
21
|
name,
|
|
@@ -26,4 +26,4 @@ export const createTemplateContext = (
|
|
|
26
26
|
withStories: answers.withStories,
|
|
27
27
|
withSsrTests: answers.withSsrTests,
|
|
28
28
|
};
|
|
29
|
-
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { collectUndos, dryRun } 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
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
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
|
+
}
|
package/src/shared/index.ts
CHANGED
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
* Shared utilities and types for component generators
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
export
|
|
6
|
-
export
|
|
7
|
-
export * from "./
|
|
8
|
-
export
|
|
5
|
+
export { default as createComponentPathPrompt } from "./createComponentPathPrompt.js";
|
|
6
|
+
export { default as createTemplateContext } from "./createTemplateContext.js";
|
|
7
|
+
export * from "./file-operations/index.js";
|
|
8
|
+
export { default as sharedPrompts } from "./sharedPrompts.js";
|
|
9
|
+
export * from "./string-helpers/index.js";
|
|
9
10
|
export type * from "./types.js";
|
|
10
|
-
export
|
|
11
|
+
export { default as validateComponentPath } from "./validateComponentPath.js";
|
|
@@ -0,0 +1,30 @@
|
|
|
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;
|
|
@@ -0,0 +1,18 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
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,20 +1,10 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Validation utilities for component generators
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
1
|
import * as path from "node:path";
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Check if a string is in PascalCase
|
|
9
|
-
*/
|
|
10
|
-
export const isPascalCase = (str: string): boolean => {
|
|
11
|
-
return /^[A-Z][a-zA-Z0-9]*$/.test(str);
|
|
12
|
-
};
|
|
2
|
+
import { isPascalCase } from "./string-helpers/index.js";
|
|
13
3
|
|
|
14
4
|
/**
|
|
15
5
|
* Validate component path
|
|
16
6
|
*/
|
|
17
|
-
export
|
|
7
|
+
export default function validateComponentPath(value: unknown): true | string {
|
|
18
8
|
if (!value || typeof value !== "string") {
|
|
19
9
|
return "Component path is required";
|
|
20
10
|
}
|
|
@@ -23,4 +13,4 @@ export const validateComponentPath = (value: unknown): true | string => {
|
|
|
23
13
|
return "Component name must be in PascalCase (e.g., MyComponent)";
|
|
24
14
|
}
|
|
25
15
|
return true;
|
|
26
|
-
}
|
|
16
|
+
}
|
package/src/svelte/generator.ts
CHANGED
|
@@ -67,7 +67,7 @@ const svelteTemplates = {
|
|
|
67
67
|
*
|
|
68
68
|
* @see {@link https://summon.dev/generators/component/svelte} for documentation
|
|
69
69
|
*/
|
|
70
|
-
|
|
70
|
+
const generator = {
|
|
71
71
|
meta: {
|
|
72
72
|
name: "component/svelte",
|
|
73
73
|
displayName: `${pkg.name}:svelte`,
|
|
@@ -201,3 +201,5 @@ For example, 'src/lib/components/Button' creates a 'Button' component.`,
|
|
|
201
201
|
]);
|
|
202
202
|
},
|
|
203
203
|
} as const satisfies GeneratorDefinition<SvelteComponentAnswers>;
|
|
204
|
+
|
|
205
|
+
export default generator;
|
package/src/svelte/index.ts
CHANGED
|
@@ -0,0 +1,200 @@
|
|
|
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/vitest.config.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
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"],
|
|
10
|
+
},
|
|
11
|
+
},
|
|
12
|
+
});
|
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* File operation utilities for component generators
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import * as path from "node:path";
|
|
6
|
-
import {
|
|
7
|
-
appendFile,
|
|
8
|
-
deleteFile,
|
|
9
|
-
exists,
|
|
10
|
-
flatMap,
|
|
11
|
-
ifElseM,
|
|
12
|
-
pure,
|
|
13
|
-
readFile,
|
|
14
|
-
type Task,
|
|
15
|
-
writeFile,
|
|
16
|
-
} from "@canonical/task";
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Remove a specific line from a file. If the file becomes empty,
|
|
20
|
-
* delete it entirely. Idempotent — no-op if the line is not present.
|
|
21
|
-
*/
|
|
22
|
-
export const removeLineFromFile = (
|
|
23
|
-
filePath: string,
|
|
24
|
-
line: string,
|
|
25
|
-
): Task<void> =>
|
|
26
|
-
flatMap(readFile(filePath), (content) => {
|
|
27
|
-
const trimmedLine = line.trim();
|
|
28
|
-
const filtered = content
|
|
29
|
-
.split("\n")
|
|
30
|
-
.filter((l) => l.trim() !== trimmedLine)
|
|
31
|
-
.join("\n");
|
|
32
|
-
|
|
33
|
-
if (filtered.trim() === "") {
|
|
34
|
-
return deleteFile(filePath, { undo: null });
|
|
35
|
-
}
|
|
36
|
-
return writeFile(filePath, filtered, { undo: null });
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Append export to parent index.ts file (or create if not exists).
|
|
41
|
-
* Carries undo metadata: removes the export line (or deletes the index
|
|
42
|
-
* if it was created from scratch).
|
|
43
|
-
*/
|
|
44
|
-
export const appendExportToParentIndex = (
|
|
45
|
-
parentDir: string,
|
|
46
|
-
componentName: string,
|
|
47
|
-
): Task<void> => {
|
|
48
|
-
const indexPath = path.join(parentDir, "index.ts");
|
|
49
|
-
const exportLine = `export * from "./${componentName}/index.js";\n`;
|
|
50
|
-
|
|
51
|
-
return ifElseM(
|
|
52
|
-
exists(indexPath),
|
|
53
|
-
// If exists, append (if not already exported)
|
|
54
|
-
flatMap(readFile(indexPath), (content) => {
|
|
55
|
-
if (content.includes(`./${componentName}`)) {
|
|
56
|
-
return pure(undefined); // Already exported
|
|
57
|
-
}
|
|
58
|
-
return appendFile(indexPath, exportLine, true, {
|
|
59
|
-
undo: removeLineFromFile(indexPath, exportLine),
|
|
60
|
-
});
|
|
61
|
-
}),
|
|
62
|
-
// If not exists, create new file (default undo: deleteFile is correct)
|
|
63
|
-
writeFile(indexPath, exportLine),
|
|
64
|
-
);
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Remove a component export from a parent index.ts.
|
|
69
|
-
* Idempotent — no-op if the export is not present or the file doesn't exist.
|
|
70
|
-
*/
|
|
71
|
-
export const removeExportFromParentIndex = (
|
|
72
|
-
parentDir: string,
|
|
73
|
-
componentName: string,
|
|
74
|
-
): Task<void> => {
|
|
75
|
-
const indexPath = path.join(parentDir, "index.ts");
|
|
76
|
-
const exportLine = `export * from "./${componentName}/index.js";\n`;
|
|
77
|
-
|
|
78
|
-
return ifElseM(
|
|
79
|
-
exists(indexPath),
|
|
80
|
-
removeLineFromFile(indexPath, exportLine),
|
|
81
|
-
pure(undefined),
|
|
82
|
-
);
|
|
83
|
-
};
|
package/src/shared/prompts.ts
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Shared prompt definitions for component generators
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import type { PromptDefinition } from "@canonical/summon-core";
|
|
6
|
-
import type { Framework } from "./types.js";
|
|
7
|
-
import { validateComponentPath } from "./validation.js";
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Create component path prompt with framework-specific defaults.
|
|
11
|
-
*
|
|
12
|
-
* This prompt is marked as positional, allowing users to run:
|
|
13
|
-
* summon component react src/components/Button
|
|
14
|
-
* instead of:
|
|
15
|
-
* summon component react --component-path=src/components/Button
|
|
16
|
-
*/
|
|
17
|
-
export const createComponentPathPrompt = (
|
|
18
|
-
framework: Framework,
|
|
19
|
-
): PromptDefinition => ({
|
|
20
|
-
name: "componentPath",
|
|
21
|
-
type: "text",
|
|
22
|
-
message: "Component path:",
|
|
23
|
-
default:
|
|
24
|
-
framework === "react"
|
|
25
|
-
? "src/components/MyComponent"
|
|
26
|
-
: "src/lib/components/MyComponent",
|
|
27
|
-
validate: validateComponentPath,
|
|
28
|
-
group: "Component",
|
|
29
|
-
positional: true,
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Shared prompts for both frameworks
|
|
34
|
-
*/
|
|
35
|
-
export const sharedPrompts: PromptDefinition[] = [
|
|
36
|
-
{
|
|
37
|
-
name: "withStyles",
|
|
38
|
-
type: "confirm",
|
|
39
|
-
message: "Include styles?",
|
|
40
|
-
default: true,
|
|
41
|
-
group: "Options",
|
|
42
|
-
},
|
|
43
|
-
{
|
|
44
|
-
name: "withStories",
|
|
45
|
-
type: "confirm",
|
|
46
|
-
message: "Include Storybook stories?",
|
|
47
|
-
default: true,
|
|
48
|
-
group: "Options",
|
|
49
|
-
},
|
|
50
|
-
{
|
|
51
|
-
name: "withSsrTests",
|
|
52
|
-
type: "confirm",
|
|
53
|
-
message: "Include SSR tests?",
|
|
54
|
-
default: true,
|
|
55
|
-
group: "Options",
|
|
56
|
-
},
|
|
57
|
-
];
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* String manipulation utilities for component generators
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import * as path from "node:path";
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Convert PascalCase to kebab-case
|
|
9
|
-
*/
|
|
10
|
-
export const kebabCase = (str: string): string => {
|
|
11
|
-
return str
|
|
12
|
-
.replace(/([a-z])([A-Z])/g, "$1-$2")
|
|
13
|
-
.replace(/([A-Z])([A-Z][a-z])/g, "$1-$2")
|
|
14
|
-
.toLowerCase();
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Extract component name from path
|
|
19
|
-
*/
|
|
20
|
-
export const getComponentName = (componentPath: string): string => {
|
|
21
|
-
return path.basename(componentPath);
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Get parent directory of component
|
|
26
|
-
*/
|
|
27
|
-
export const getParentDir = (componentPath: string): string => {
|
|
28
|
-
return path.dirname(componentPath);
|
|
29
|
-
};
|