@canonical/summon-component 0.17.1 → 0.21.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 +42 -0
- package/CONVENTIONS.md +184 -0
- package/README.md +5 -5
- package/package.json +14 -10
- package/src/{__tests__/generators.test.ts → index.test.ts} +179 -7
- package/src/index.ts +4 -4
- package/src/{webcomponents → lit}/generator.ts +33 -36
- package/src/lit/index.ts +6 -0
- package/src/lit/types.ts +8 -0
- package/src/react/generator.ts +9 -12
- 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.ts → file-operations/appendExportToParentIndex.ts} +12 -11
- 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/types.ts +1 -1
- package/src/shared/validateComponentPath.test.ts +40 -0
- package/src/shared/{validation.ts → validateComponentPath.ts} +3 -13
- package/src/svelte/generator.ts +7 -10
- package/src/svelte/index.ts +1 -1
- package/src/templates/{webcomponents → lit}/component.ts.ejs +8 -10
- package/src/templates/{webcomponents → lit}/index.ts.ejs +0 -2
- package/src/templates/{webcomponents → lit}/stories.ts.ejs +3 -5
- package/src/templates/lit/styles.css.ejs +49 -0
- package/src/templates/lit/tests.ts.ejs +27 -0
- package/src/templates/lit/types.ts.ejs +6 -0
- package/src/templates/react/component.tsx.ejs +0 -2
- package/src/templates/react/index.ts.ejs +0 -2
- package/src/templates/react/ssr.test.tsx.ejs +0 -2
- package/src/templates/react/stories.tsx.ejs +1 -3
- package/src/templates/react/test.tsx.ejs +0 -2
- package/src/templates/react/types.ts.ejs +0 -2
- package/src/templates/shared/styles.css.ejs +0 -2
- package/src/templates/svelte/component.svelte.ejs +0 -2
- package/src/templates/svelte/index.ts.ejs +0 -2
- package/src/templates/svelte/ssr.test.ts.ejs +0 -2
- package/src/templates/svelte/stories.svelte.ejs +0 -1
- package/src/templates/svelte/stories.ts.ejs +0 -2
- package/src/templates/svelte/test.ts.ejs +0 -2
- package/src/templates/svelte/types.ts.ejs +0 -2
- package/src/testing/integration/generators-undo.test.ts +200 -0
- package/vitest.config.ts +12 -0
- package/src/shared/prompts.ts +0 -57
- package/src/shared/string-helpers.ts +0 -29
- package/src/templates/webcomponents/styles.css.ejs +0 -16
- package/src/templates/webcomponents/tests.ts.ejs +0 -29
- package/src/templates/webcomponents/types.ts.ejs +0 -8
- package/src/webcomponents/index.ts +0 -6
- package/src/webcomponents/types.ts +0 -8
|
@@ -10,8 +10,8 @@
|
|
|
10
10
|
*
|
|
11
11
|
* @example
|
|
12
12
|
* ```bash
|
|
13
|
-
* summon component
|
|
14
|
-
* summon component
|
|
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
15
|
* ```
|
|
16
16
|
*
|
|
17
17
|
* @module
|
|
@@ -19,15 +19,9 @@
|
|
|
19
19
|
|
|
20
20
|
import * as path from "node:path";
|
|
21
21
|
import { fileURLToPath } from "node:url";
|
|
22
|
-
import {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
info,
|
|
26
|
-
mkdir,
|
|
27
|
-
sequence_,
|
|
28
|
-
template,
|
|
29
|
-
when,
|
|
30
|
-
} from "@canonical/summon";
|
|
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" };
|
|
31
25
|
|
|
32
26
|
import {
|
|
33
27
|
appendExportToParentIndex,
|
|
@@ -36,7 +30,7 @@ import {
|
|
|
36
30
|
getComponentName,
|
|
37
31
|
getParentDir,
|
|
38
32
|
} from "../shared/index.js";
|
|
39
|
-
import type {
|
|
33
|
+
import type { LitAnswers } from "./types.js";
|
|
40
34
|
|
|
41
35
|
// =============================================================================
|
|
42
36
|
// Template Paths
|
|
@@ -45,20 +39,20 @@ import type { WebComponentAnswers } from "./types.js";
|
|
|
45
39
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
46
40
|
const templatesDir = path.join(__dirname, "..", "templates");
|
|
47
41
|
|
|
48
|
-
const
|
|
49
|
-
component: path.join(templatesDir, "
|
|
50
|
-
index: path.join(templatesDir, "
|
|
51
|
-
types: path.join(templatesDir, "
|
|
52
|
-
tests: path.join(templatesDir, "
|
|
53
|
-
stories: path.join(templatesDir, "
|
|
54
|
-
styles: path.join(templatesDir, "
|
|
42
|
+
const litTemplates = {
|
|
43
|
+
component: path.join(templatesDir, "lit", "component.ts.ejs"),
|
|
44
|
+
index: path.join(templatesDir, "lit", "index.ts.ejs"),
|
|
45
|
+
types: path.join(templatesDir, "lit", "types.ts.ejs"),
|
|
46
|
+
tests: path.join(templatesDir, "lit", "tests.ts.ejs"),
|
|
47
|
+
stories: path.join(templatesDir, "lit", "stories.ts.ejs"),
|
|
48
|
+
styles: path.join(templatesDir, "lit", "styles.css.ejs"),
|
|
55
49
|
};
|
|
56
50
|
|
|
57
51
|
// =============================================================================
|
|
58
52
|
// Custom Prompts (without SSR tests)
|
|
59
53
|
// =============================================================================
|
|
60
54
|
|
|
61
|
-
const
|
|
55
|
+
const litPrompts = [
|
|
62
56
|
{
|
|
63
57
|
name: "withStyles",
|
|
64
58
|
type: "confirm",
|
|
@@ -85,11 +79,12 @@ const webComponentPrompts = [
|
|
|
85
79
|
* This generator creates a complete Lit web component with TypeScript support,
|
|
86
80
|
* testing infrastructure, and optional Storybook stories and CSS styles.
|
|
87
81
|
*
|
|
88
|
-
* @see {@link https://summon.dev/generators/component/
|
|
82
|
+
* @see {@link https://summon.dev/generators/component/lit} for documentation
|
|
89
83
|
*/
|
|
90
|
-
|
|
84
|
+
const generator = {
|
|
91
85
|
meta: {
|
|
92
|
-
name: "component/
|
|
86
|
+
name: "component/lit",
|
|
87
|
+
displayName: `${pkg.name}:lit`,
|
|
93
88
|
description:
|
|
94
89
|
"Generate a Lit web component with TypeScript, tests, stories, and styles",
|
|
95
90
|
version: "0.1.0",
|
|
@@ -106,20 +101,20 @@ The component name is extracted from the path and must be PascalCase.
|
|
|
106
101
|
For example, 'src/lib/components/Button' creates a 'Button' component
|
|
107
102
|
with the custom element tag 'button'.`,
|
|
108
103
|
examples: [
|
|
109
|
-
"summon component
|
|
110
|
-
"summon component
|
|
111
|
-
"summon component
|
|
112
|
-
"summon component
|
|
104
|
+
"summon component lit --component-path=src/lib/components/Button",
|
|
105
|
+
"summon component lit --component-path=src/lib/components/Card --with-styles --with-stories",
|
|
106
|
+
"summon component lit --component-path=src/lib/components/Modal --no-with-styles",
|
|
107
|
+
"summon component lit --component-path=src/lib/components/Button --dry-run",
|
|
113
108
|
],
|
|
114
109
|
},
|
|
115
110
|
|
|
116
|
-
prompts: [createComponentPathPrompt("
|
|
111
|
+
prompts: [createComponentPathPrompt("lit"), ...litPrompts],
|
|
117
112
|
|
|
118
113
|
generate: (answers) => {
|
|
119
114
|
const componentName = getComponentName(answers.componentPath);
|
|
120
115
|
const componentDir = answers.componentPath;
|
|
121
116
|
const parentDir = getParentDir(answers.componentPath);
|
|
122
|
-
const ctx = createTemplateContext(answers, "
|
|
117
|
+
const ctx = createTemplateContext(answers, "lit");
|
|
123
118
|
|
|
124
119
|
return sequence_([
|
|
125
120
|
info(`Generating Lit web component: ${componentName}`),
|
|
@@ -129,28 +124,28 @@ with the custom element tag 'button'.`,
|
|
|
129
124
|
|
|
130
125
|
debug("Creating main component file"),
|
|
131
126
|
template({
|
|
132
|
-
source:
|
|
127
|
+
source: litTemplates.component,
|
|
133
128
|
dest: path.join(componentDir, `${componentName}.ts`),
|
|
134
129
|
vars: ctx,
|
|
135
130
|
}),
|
|
136
131
|
|
|
137
132
|
debug("Creating index barrel file"),
|
|
138
133
|
template({
|
|
139
|
-
source:
|
|
134
|
+
source: litTemplates.index,
|
|
140
135
|
dest: path.join(componentDir, "index.ts"),
|
|
141
136
|
vars: ctx,
|
|
142
137
|
}),
|
|
143
138
|
|
|
144
139
|
debug("Creating types file"),
|
|
145
140
|
template({
|
|
146
|
-
source:
|
|
141
|
+
source: litTemplates.types,
|
|
147
142
|
dest: path.join(componentDir, "types.ts"),
|
|
148
143
|
vars: ctx,
|
|
149
144
|
}),
|
|
150
145
|
|
|
151
146
|
debug("Creating test file"),
|
|
152
147
|
template({
|
|
153
|
-
source:
|
|
148
|
+
source: litTemplates.tests,
|
|
154
149
|
dest: path.join(componentDir, `${componentName}.tests.ts`),
|
|
155
150
|
vars: ctx,
|
|
156
151
|
}),
|
|
@@ -159,7 +154,7 @@ with the custom element tag 'button'.`,
|
|
|
159
154
|
when(
|
|
160
155
|
answers.withStories,
|
|
161
156
|
template({
|
|
162
|
-
source:
|
|
157
|
+
source: litTemplates.stories,
|
|
163
158
|
dest: path.join(componentDir, `${componentName}.stories.ts`),
|
|
164
159
|
vars: ctx,
|
|
165
160
|
}),
|
|
@@ -169,7 +164,7 @@ with the custom element tag 'button'.`,
|
|
|
169
164
|
when(
|
|
170
165
|
answers.withStyles,
|
|
171
166
|
template({
|
|
172
|
-
source:
|
|
167
|
+
source: litTemplates.styles,
|
|
173
168
|
dest: path.join(componentDir, "styles.css"),
|
|
174
169
|
vars: ctx,
|
|
175
170
|
}),
|
|
@@ -181,4 +176,6 @@ with the custom element tag 'button'.`,
|
|
|
181
176
|
info(`Created ${componentName} component at ${componentDir}`),
|
|
182
177
|
]);
|
|
183
178
|
},
|
|
184
|
-
} as const satisfies GeneratorDefinition<
|
|
179
|
+
} as const satisfies GeneratorDefinition<LitAnswers>;
|
|
180
|
+
|
|
181
|
+
export default generator;
|
package/src/lit/index.ts
ADDED
package/src/lit/types.ts
ADDED
package/src/react/generator.ts
CHANGED
|
@@ -21,15 +21,9 @@
|
|
|
21
21
|
|
|
22
22
|
import * as path from "node:path";
|
|
23
23
|
import { fileURLToPath } from "node:url";
|
|
24
|
-
import {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
info,
|
|
28
|
-
mkdir,
|
|
29
|
-
sequence_,
|
|
30
|
-
template,
|
|
31
|
-
when,
|
|
32
|
-
} from "@canonical/summon";
|
|
24
|
+
import { type GeneratorDefinition, template } from "@canonical/summon-core";
|
|
25
|
+
import { debug, info, mkdir, sequence_, when } from "@canonical/task";
|
|
26
|
+
import pkg from "../../package.json" with { type: "json" };
|
|
33
27
|
|
|
34
28
|
import {
|
|
35
29
|
appendExportToParentIndex,
|
|
@@ -70,9 +64,10 @@ const reactTemplates = {
|
|
|
70
64
|
*
|
|
71
65
|
* @see {@link https://summon.dev/generators/component/react} for documentation
|
|
72
66
|
*/
|
|
73
|
-
|
|
67
|
+
const generator = {
|
|
74
68
|
meta: {
|
|
75
69
|
name: "component/react",
|
|
70
|
+
displayName: `${pkg.name}:react`,
|
|
76
71
|
description:
|
|
77
72
|
"Generate a React component with TypeScript, tests, stories, and styles",
|
|
78
73
|
version: "0.1.0",
|
|
@@ -134,7 +129,7 @@ For example, 'src/components/Button' creates a 'Button' component.`,
|
|
|
134
129
|
debug("Creating test file"),
|
|
135
130
|
template({
|
|
136
131
|
source: reactTemplates.test,
|
|
137
|
-
dest: path.join(componentDir, `${componentName}.
|
|
132
|
+
dest: path.join(componentDir, `${componentName}.tests.tsx`),
|
|
138
133
|
vars: ctx,
|
|
139
134
|
}),
|
|
140
135
|
|
|
@@ -143,7 +138,7 @@ For example, 'src/components/Button' creates a 'Button' component.`,
|
|
|
143
138
|
answers.withSsrTests,
|
|
144
139
|
template({
|
|
145
140
|
source: reactTemplates.ssrTest,
|
|
146
|
-
dest: path.join(componentDir, `${componentName}.ssr.
|
|
141
|
+
dest: path.join(componentDir, `${componentName}.ssr.tests.tsx`),
|
|
147
142
|
vars: ctx,
|
|
148
143
|
}),
|
|
149
144
|
),
|
|
@@ -175,3 +170,5 @@ For example, 'src/components/Button' creates a 'Button' component.`,
|
|
|
175
170
|
]);
|
|
176
171
|
},
|
|
177
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
|
+
});
|
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* File operation utilities for component generators
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
1
|
import * as path from "node:path";
|
|
6
2
|
import {
|
|
7
3
|
appendFile,
|
|
@@ -12,15 +8,18 @@ import {
|
|
|
12
8
|
readFile,
|
|
13
9
|
type Task,
|
|
14
10
|
writeFile,
|
|
15
|
-
} from "@canonical/
|
|
11
|
+
} from "@canonical/task";
|
|
12
|
+
import removeLineFromFile from "./removeLineFromFile.js";
|
|
16
13
|
|
|
17
14
|
/**
|
|
18
|
-
* Append export to parent index.ts file (or create if not exists)
|
|
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).
|
|
19
18
|
*/
|
|
20
|
-
export
|
|
19
|
+
export default function appendExportToParentIndex(
|
|
21
20
|
parentDir: string,
|
|
22
21
|
componentName: string,
|
|
23
|
-
): Task<void>
|
|
22
|
+
): Task<void> {
|
|
24
23
|
const indexPath = path.join(parentDir, "index.ts");
|
|
25
24
|
const exportLine = `export * from "./${componentName}/index.js";\n`;
|
|
26
25
|
|
|
@@ -31,9 +30,11 @@ export const appendExportToParentIndex = (
|
|
|
31
30
|
if (content.includes(`./${componentName}`)) {
|
|
32
31
|
return pure(undefined); // Already exported
|
|
33
32
|
}
|
|
34
|
-
return appendFile(indexPath, exportLine
|
|
33
|
+
return appendFile(indexPath, exportLine, true, {
|
|
34
|
+
undo: removeLineFromFile(indexPath, exportLine),
|
|
35
|
+
});
|
|
35
36
|
}),
|
|
36
|
-
// If not exists, create new file
|
|
37
|
+
// If not exists, create new file (default undo: deleteFile is correct)
|
|
37
38
|
writeFile(indexPath, exportLine),
|
|
38
39
|
);
|
|
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";
|