@canonical/summon-component 0.29.0 → 0.31.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -26
- package/{src/index.ts → dist/esm/index.js} +5 -8
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/lit/generator.js +152 -0
- package/dist/esm/lit/generator.js.map +1 -0
- package/dist/esm/lit/index.js +5 -0
- package/dist/esm/lit/index.js.map +1 -0
- package/dist/esm/lit/types.js +5 -0
- package/dist/esm/lit/types.js.map +1 -0
- package/dist/esm/react/generator.js +129 -0
- package/dist/esm/react/generator.js.map +1 -0
- package/dist/esm/react/index.js +5 -0
- package/dist/esm/react/index.js.map +1 -0
- package/dist/esm/react/types.js +5 -0
- package/dist/esm/react/types.js.map +1 -0
- package/dist/esm/shared/createComponentPathPrompt.js +23 -0
- package/dist/esm/shared/createComponentPathPrompt.js.map +1 -0
- package/dist/esm/shared/createTemplateContext.js +20 -0
- package/dist/esm/shared/createTemplateContext.js.map +1 -0
- package/dist/esm/shared/file-operations/appendExportToParentIndex.js +25 -0
- package/dist/esm/shared/file-operations/appendExportToParentIndex.js.map +1 -0
- package/dist/esm/shared/file-operations/dryRunWithFileState.js +25 -0
- package/dist/esm/shared/file-operations/dryRunWithFileState.js.map +1 -0
- package/{src/shared/file-operations/index.ts → dist/esm/shared/file-operations/index.js} +1 -0
- package/dist/esm/shared/file-operations/index.js.map +1 -0
- package/dist/esm/shared/file-operations/removeExportFromParentIndex.js +13 -0
- package/dist/esm/shared/file-operations/removeExportFromParentIndex.js.map +1 -0
- package/dist/esm/shared/file-operations/removeLineFromFile.js +19 -0
- package/dist/esm/shared/file-operations/removeLineFromFile.js.map +1 -0
- package/dist/esm/shared/index.js +11 -0
- package/dist/esm/shared/index.js.map +1 -0
- package/dist/esm/shared/loadTemplate.js +56 -0
- package/dist/esm/shared/loadTemplate.js.map +1 -0
- package/dist/esm/shared/packageName.js +11 -0
- package/dist/esm/shared/packageName.js.map +1 -0
- package/dist/esm/shared/sharedPrompts.js +28 -0
- package/dist/esm/shared/sharedPrompts.js.map +1 -0
- package/dist/esm/shared/string-helpers/getComponentName.js +8 -0
- package/dist/esm/shared/string-helpers/getComponentName.js.map +1 -0
- package/dist/esm/shared/string-helpers/getParentDir.js +8 -0
- package/dist/esm/shared/string-helpers/getParentDir.js.map +1 -0
- package/{src/shared/string-helpers/index.ts → dist/esm/shared/string-helpers/index.js} +1 -0
- package/dist/esm/shared/string-helpers/index.js.map +1 -0
- package/dist/esm/shared/types.js +5 -0
- package/dist/esm/shared/types.js.map +1 -0
- package/dist/esm/shared/validateComponentPath.js +27 -0
- package/dist/esm/shared/validateComponentPath.js.map +1 -0
- package/dist/esm/svelte/generator.js +165 -0
- package/dist/esm/svelte/generator.js.map +1 -0
- package/dist/esm/svelte/index.js +5 -0
- package/dist/esm/svelte/index.js.map +1 -0
- package/dist/esm/svelte/types.js +5 -0
- package/dist/esm/svelte/types.js.map +1 -0
- package/dist/types/index.d.ts +87 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/lit/generator.d.ts +52 -0
- package/dist/types/lit/generator.d.ts.map +1 -0
- package/{src/lit/index.ts → dist/types/lit/index.d.ts} +1 -1
- package/dist/types/lit/index.d.ts.map +1 -0
- package/{src/lit/types.ts → dist/types/lit/types.d.ts} +1 -2
- package/dist/types/lit/types.d.ts.map +1 -0
- package/dist/types/react/generator.d.ts +28 -0
- package/dist/types/react/generator.d.ts.map +1 -0
- package/{src/react/index.ts → dist/types/react/index.d.ts} +1 -1
- package/dist/types/react/index.d.ts.map +1 -0
- package/{src/react/types.ts → dist/types/react/types.d.ts} +1 -2
- package/dist/types/react/types.d.ts.map +1 -0
- package/dist/types/shared/createComponentPathPrompt.d.ts +12 -0
- package/dist/types/shared/createComponentPathPrompt.d.ts.map +1 -0
- package/dist/types/shared/createTemplateContext.d.ts +9 -0
- package/dist/types/shared/createTemplateContext.d.ts.map +1 -0
- package/dist/types/shared/file-operations/appendExportToParentIndex.d.ts +8 -0
- package/dist/types/shared/file-operations/appendExportToParentIndex.d.ts.map +1 -0
- package/dist/types/shared/file-operations/dryRunWithFileState.d.ts +7 -0
- package/dist/types/shared/file-operations/dryRunWithFileState.d.ts.map +1 -0
- package/dist/types/shared/file-operations/index.d.ts +4 -0
- package/dist/types/shared/file-operations/index.d.ts.map +1 -0
- package/dist/types/shared/file-operations/removeExportFromParentIndex.d.ts +7 -0
- package/dist/types/shared/file-operations/removeExportFromParentIndex.d.ts.map +1 -0
- package/dist/types/shared/file-operations/removeLineFromFile.d.ts +7 -0
- package/dist/types/shared/file-operations/removeLineFromFile.d.ts.map +1 -0
- package/{src/shared/index.ts → dist/types/shared/index.d.ts} +2 -1
- package/dist/types/shared/index.d.ts.map +1 -0
- package/dist/types/shared/loadTemplate.d.ts +20 -0
- package/dist/types/shared/loadTemplate.d.ts.map +1 -0
- package/dist/types/shared/packageName.d.ts +11 -0
- package/dist/types/shared/packageName.d.ts.map +1 -0
- package/dist/types/shared/sharedPrompts.d.ts +7 -0
- package/dist/types/shared/sharedPrompts.d.ts.map +1 -0
- package/{src/shared/string-helpers/getComponentName.ts → dist/types/shared/string-helpers/getComponentName.d.ts} +2 -5
- package/dist/types/shared/string-helpers/getComponentName.d.ts.map +1 -0
- package/{src/shared/string-helpers/getParentDir.ts → dist/types/shared/string-helpers/getParentDir.d.ts} +2 -5
- package/dist/types/shared/string-helpers/getParentDir.d.ts.map +1 -0
- package/dist/types/shared/string-helpers/index.d.ts +4 -0
- package/dist/types/shared/string-helpers/index.d.ts.map +1 -0
- package/dist/types/shared/types.d.ts +34 -0
- package/dist/types/shared/types.d.ts.map +1 -0
- package/dist/types/shared/validateComponentPath.d.ts +9 -0
- package/dist/types/shared/validateComponentPath.d.ts.map +1 -0
- package/dist/types/svelte/generator.d.ts +52 -0
- package/dist/types/svelte/generator.d.ts.map +1 -0
- package/{src/svelte/index.ts → dist/types/svelte/index.d.ts} +1 -1
- package/dist/types/svelte/index.d.ts.map +1 -0
- package/{src/svelte/types.ts → dist/types/svelte/types.d.ts} +3 -4
- package/dist/types/svelte/types.d.ts.map +1 -0
- package/package.json +21 -10
- package/CHANGELOG.md +0 -245
- package/CONVENTIONS.md +0 -184
- package/biome.json +0 -6
- package/src/index.test.ts +0 -514
- package/src/lit/generator.ts +0 -190
- package/src/react/generator.ts +0 -176
- package/src/shared/createComponentPathPrompt.ts +0 -28
- package/src/shared/createTemplateContext.ts +0 -29
- package/src/shared/file-operations/appendExportToParentIndex.test.ts +0 -82
- package/src/shared/file-operations/appendExportToParentIndex.ts +0 -40
- package/src/shared/file-operations/dryRunWithFileState.ts +0 -28
- package/src/shared/file-operations/removeExportFromParentIndex.test.ts +0 -52
- package/src/shared/file-operations/removeExportFromParentIndex.ts +0 -21
- package/src/shared/file-operations/removeLineFromFile.test.ts +0 -66
- package/src/shared/file-operations/removeLineFromFile.ts +0 -29
- package/src/shared/loadTemplate.test.ts +0 -100
- package/src/shared/loadTemplate.ts +0 -70
- package/src/shared/sharedPrompts.ts +0 -30
- package/src/shared/string-helpers/getComponentName.test.ts +0 -18
- package/src/shared/string-helpers/getParentDir.test.ts +0 -16
- package/src/shared/types.ts +0 -36
- package/src/shared/validateComponentPath.test.ts +0 -40
- package/src/shared/validateComponentPath.ts +0 -16
- package/src/svelte/generator.ts +0 -224
- package/src/testing/integration/generators-undo.test.ts +0 -200
- package/tsconfig.json +0 -9
- package/vitest.config.ts +0 -18
- /package/{src → dist/esm}/templates/lit/component.ts.ejs +0 -0
- /package/{src → dist/esm}/templates/lit/index.ts.ejs +0 -0
- /package/{src → dist/esm}/templates/lit/stories.ts.ejs +0 -0
- /package/{src → dist/esm}/templates/lit/styles.css.ejs +0 -0
- /package/{src → dist/esm}/templates/lit/tests.ts.ejs +0 -0
- /package/{src → dist/esm}/templates/lit/types.ts.ejs +0 -0
- /package/{src → dist/esm}/templates/react/component.tsx.ejs +0 -0
- /package/{src → dist/esm}/templates/react/index.ts.ejs +0 -0
- /package/{src → dist/esm}/templates/react/ssr.test.tsx.ejs +0 -0
- /package/{src → dist/esm}/templates/react/stories.tsx.ejs +0 -0
- /package/{src → dist/esm}/templates/react/test.tsx.ejs +0 -0
- /package/{src → dist/esm}/templates/react/types.ts.ejs +0 -0
- /package/{src → dist/esm}/templates/shared/styles.css.ejs +0 -0
- /package/{src → dist/esm}/templates/svelte/component.svelte.ejs +0 -0
- /package/{src → dist/esm}/templates/svelte/index.ts.ejs +0 -0
- /package/{src → dist/esm}/templates/svelte/ssr.test.ts.ejs +0 -0
- /package/{src → dist/esm}/templates/svelte/stories.svelte.ejs +0 -0
- /package/{src → dist/esm}/templates/svelte/stories.ts.ejs +0 -0
- /package/{src → dist/esm}/templates/svelte/test.ts.ejs +0 -0
- /package/{src → dist/esm}/templates/svelte/types.ts.ejs +0 -0
package/src/react/generator.ts
DELETED
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* React Component Generator
|
|
3
|
-
*
|
|
4
|
-
* Generates a React component with a consistent file structure:
|
|
5
|
-
* - `ComponentName.tsx` - Main component file
|
|
6
|
-
* - `types.ts` - TypeScript types and props interface
|
|
7
|
-
* - `index.ts` - Barrel export
|
|
8
|
-
* - `ComponentName.test.tsx` - Unit tests with @testing-library/react
|
|
9
|
-
* - `ComponentName.ssr.test.tsx` - SSR tests (optional)
|
|
10
|
-
* - `ComponentName.stories.tsx` - Storybook stories (optional)
|
|
11
|
-
* - `styles.css` - CSS styles (optional)
|
|
12
|
-
*
|
|
13
|
-
* @module
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
import * as path from "node:path";
|
|
17
|
-
import { fileURLToPath } from "node:url";
|
|
18
|
-
import { type GeneratorDefinition, template } from "@canonical/summon-core";
|
|
19
|
-
import { debug, info, mkdir, sequence_, when } from "@canonical/task";
|
|
20
|
-
import pkg from "../../package.json" with { type: "json" };
|
|
21
|
-
|
|
22
|
-
import {
|
|
23
|
-
appendExportToParentIndex,
|
|
24
|
-
createComponentPathPrompt,
|
|
25
|
-
createTemplateContext,
|
|
26
|
-
getComponentName,
|
|
27
|
-
getParentDir,
|
|
28
|
-
sharedPrompts,
|
|
29
|
-
} from "../shared/index.js";
|
|
30
|
-
import loadTemplate from "../shared/loadTemplate.js";
|
|
31
|
-
import type { ReactComponentAnswers } from "./types.js";
|
|
32
|
-
|
|
33
|
-
// =============================================================================
|
|
34
|
-
// Template Paths & Content (loaded eagerly via top-level await)
|
|
35
|
-
// =============================================================================
|
|
36
|
-
|
|
37
|
-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
38
|
-
const templatesDir = path.join(__dirname, "..", "templates");
|
|
39
|
-
|
|
40
|
-
const reactTemplates = {
|
|
41
|
-
component: await loadTemplate(
|
|
42
|
-
path.join(templatesDir, "react", "component.tsx.ejs"),
|
|
43
|
-
),
|
|
44
|
-
types: await loadTemplate(path.join(templatesDir, "react", "types.ts.ejs")),
|
|
45
|
-
index: await loadTemplate(path.join(templatesDir, "react", "index.ts.ejs")),
|
|
46
|
-
test: await loadTemplate(path.join(templatesDir, "react", "test.tsx.ejs")),
|
|
47
|
-
ssrTest: await loadTemplate(
|
|
48
|
-
path.join(templatesDir, "react", "ssr.test.tsx.ejs"),
|
|
49
|
-
),
|
|
50
|
-
stories: await loadTemplate(
|
|
51
|
-
path.join(templatesDir, "react", "stories.tsx.ejs"),
|
|
52
|
-
),
|
|
53
|
-
styles: await loadTemplate(
|
|
54
|
-
path.join(templatesDir, "shared", "styles.css.ejs"),
|
|
55
|
-
),
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
// =============================================================================
|
|
59
|
-
// Generator Definition
|
|
60
|
-
// =============================================================================
|
|
61
|
-
|
|
62
|
-
const generator = {
|
|
63
|
-
meta: {
|
|
64
|
-
name: "component/react",
|
|
65
|
-
displayName: `${pkg.name}:react`,
|
|
66
|
-
description:
|
|
67
|
-
"Generate a React component with TypeScript, tests, stories, and styles",
|
|
68
|
-
version: "0.1.0",
|
|
69
|
-
help: `Generate a React component with TypeScript, tests, stories, and styles.
|
|
70
|
-
|
|
71
|
-
FEATURES:
|
|
72
|
-
- TypeScript types and props interface
|
|
73
|
-
- Unit tests with @testing-library/react
|
|
74
|
-
- Storybook integration (optional)
|
|
75
|
-
- CSS styles (optional)
|
|
76
|
-
- SSR tests (optional)
|
|
77
|
-
- Auto-export from parent index.ts
|
|
78
|
-
|
|
79
|
-
The component name is extracted from the path and must be PascalCase.
|
|
80
|
-
For example, 'src/components/Button' creates a 'Button' component.`,
|
|
81
|
-
examples: [
|
|
82
|
-
"summon component react --component-path=src/components/Button",
|
|
83
|
-
"summon component react --component-path=src/components/Card --with-styles --with-stories",
|
|
84
|
-
"summon component react --component-path=src/components/Modal --no-with-ssr-tests",
|
|
85
|
-
"summon component react --component-path=src/components/Button --dry-run",
|
|
86
|
-
],
|
|
87
|
-
},
|
|
88
|
-
|
|
89
|
-
prompts: [createComponentPathPrompt("react"), ...sharedPrompts],
|
|
90
|
-
|
|
91
|
-
generate: (answers) => {
|
|
92
|
-
const componentName = getComponentName(answers.componentPath);
|
|
93
|
-
const componentDir = answers.componentPath;
|
|
94
|
-
const parentDir = getParentDir(answers.componentPath);
|
|
95
|
-
const ctx = createTemplateContext(answers, "react");
|
|
96
|
-
|
|
97
|
-
return sequence_([
|
|
98
|
-
info(`Generating React component: ${componentName}`),
|
|
99
|
-
|
|
100
|
-
debug("Creating component directory"),
|
|
101
|
-
mkdir(componentDir),
|
|
102
|
-
|
|
103
|
-
debug("Creating main component file"),
|
|
104
|
-
template({
|
|
105
|
-
source: reactTemplates.component.source,
|
|
106
|
-
content: reactTemplates.component.content,
|
|
107
|
-
dest: path.join(componentDir, `${componentName}.tsx`),
|
|
108
|
-
vars: ctx,
|
|
109
|
-
}),
|
|
110
|
-
|
|
111
|
-
debug("Creating types file"),
|
|
112
|
-
template({
|
|
113
|
-
source: reactTemplates.types.source,
|
|
114
|
-
content: reactTemplates.types.content,
|
|
115
|
-
dest: path.join(componentDir, "types.ts"),
|
|
116
|
-
vars: ctx,
|
|
117
|
-
}),
|
|
118
|
-
|
|
119
|
-
debug("Creating index barrel file"),
|
|
120
|
-
template({
|
|
121
|
-
source: reactTemplates.index.source,
|
|
122
|
-
content: reactTemplates.index.content,
|
|
123
|
-
dest: path.join(componentDir, "index.ts"),
|
|
124
|
-
vars: ctx,
|
|
125
|
-
}),
|
|
126
|
-
|
|
127
|
-
debug("Creating test file"),
|
|
128
|
-
template({
|
|
129
|
-
source: reactTemplates.test.source,
|
|
130
|
-
content: reactTemplates.test.content,
|
|
131
|
-
dest: path.join(componentDir, `${componentName}.tests.tsx`),
|
|
132
|
-
vars: ctx,
|
|
133
|
-
}),
|
|
134
|
-
|
|
135
|
-
when(answers.withSsrTests, debug("Creating SSR test file")),
|
|
136
|
-
when(
|
|
137
|
-
answers.withSsrTests,
|
|
138
|
-
template({
|
|
139
|
-
source: reactTemplates.ssrTest.source,
|
|
140
|
-
content: reactTemplates.ssrTest.content,
|
|
141
|
-
dest: path.join(componentDir, `${componentName}.ssr.tests.tsx`),
|
|
142
|
-
vars: ctx,
|
|
143
|
-
}),
|
|
144
|
-
),
|
|
145
|
-
|
|
146
|
-
when(answers.withStories, debug("Creating stories file")),
|
|
147
|
-
when(
|
|
148
|
-
answers.withStories,
|
|
149
|
-
template({
|
|
150
|
-
source: reactTemplates.stories.source,
|
|
151
|
-
content: reactTemplates.stories.content,
|
|
152
|
-
dest: path.join(componentDir, `${componentName}.stories.tsx`),
|
|
153
|
-
vars: ctx,
|
|
154
|
-
}),
|
|
155
|
-
),
|
|
156
|
-
|
|
157
|
-
when(answers.withStyles, debug("Creating styles file")),
|
|
158
|
-
when(
|
|
159
|
-
answers.withStyles,
|
|
160
|
-
template({
|
|
161
|
-
source: reactTemplates.styles.source,
|
|
162
|
-
content: reactTemplates.styles.content,
|
|
163
|
-
dest: path.join(componentDir, "styles.css"),
|
|
164
|
-
vars: ctx,
|
|
165
|
-
}),
|
|
166
|
-
),
|
|
167
|
-
|
|
168
|
-
debug("Updating parent index.ts with export"),
|
|
169
|
-
appendExportToParentIndex(parentDir, componentName),
|
|
170
|
-
|
|
171
|
-
info(`Created ${componentName} component at ${componentDir}`),
|
|
172
|
-
]);
|
|
173
|
-
},
|
|
174
|
-
} as const satisfies GeneratorDefinition<ReactComponentAnswers>;
|
|
175
|
-
|
|
176
|
-
export default generator;
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import type { PromptDefinition } from "@canonical/summon-core";
|
|
2
|
-
import type { Framework } from "./types.js";
|
|
3
|
-
import validateComponentPath from "./validateComponentPath.js";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Create component path prompt with framework-specific defaults.
|
|
7
|
-
*
|
|
8
|
-
* This prompt is marked as positional, allowing users to run:
|
|
9
|
-
* summon component react src/components/Button
|
|
10
|
-
* instead of:
|
|
11
|
-
* summon component react --component-path=src/components/Button
|
|
12
|
-
*/
|
|
13
|
-
export default function createComponentPathPrompt(
|
|
14
|
-
framework: Framework,
|
|
15
|
-
): PromptDefinition {
|
|
16
|
-
return {
|
|
17
|
-
name: "componentPath",
|
|
18
|
-
type: "text",
|
|
19
|
-
message: "Component path:",
|
|
20
|
-
default:
|
|
21
|
-
framework === "react"
|
|
22
|
-
? "src/components/MyComponent"
|
|
23
|
-
: "src/lib/components/MyComponent",
|
|
24
|
-
validate: validateComponentPath,
|
|
25
|
-
group: "Component",
|
|
26
|
-
positional: true,
|
|
27
|
-
};
|
|
28
|
-
}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Template context creation for component generators
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { getComponentName, kebabCase } from "./string-helpers/index.js";
|
|
6
|
-
import type {
|
|
7
|
-
BaseComponentAnswers,
|
|
8
|
-
Framework,
|
|
9
|
-
TemplateContext,
|
|
10
|
-
} from "./types.js";
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Create template context from answers
|
|
14
|
-
*/
|
|
15
|
-
export default function createTemplateContext(
|
|
16
|
-
answers: BaseComponentAnswers,
|
|
17
|
-
framework: Framework,
|
|
18
|
-
): TemplateContext {
|
|
19
|
-
const name = getComponentName(answers.componentPath);
|
|
20
|
-
return {
|
|
21
|
-
name,
|
|
22
|
-
kebabName: kebabCase(name),
|
|
23
|
-
generatorName: `@canonical/summon:component-${framework}`,
|
|
24
|
-
version: "0.1.0",
|
|
25
|
-
withStyles: answers.withStyles,
|
|
26
|
-
withStories: answers.withStories,
|
|
27
|
-
withSsrTests: answers.withSsrTests,
|
|
28
|
-
};
|
|
29
|
-
}
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
import { collectUndos, dryRun, readFile } from "@canonical/task";
|
|
2
|
-
import { describe, expect, it } from "vitest";
|
|
3
|
-
import appendExportToParentIndex from "./appendExportToParentIndex.js";
|
|
4
|
-
import dryRunWithFileState from "./dryRunWithFileState.js";
|
|
5
|
-
|
|
6
|
-
describe("appendExportToParentIndex", () => {
|
|
7
|
-
it("appends export when index exists and export not present", () => {
|
|
8
|
-
const result = dryRunWithFileState(
|
|
9
|
-
appendExportToParentIndex("src/components", "Button"),
|
|
10
|
-
{
|
|
11
|
-
"src/components/index.ts": 'export * from "./Other/index.js";\n',
|
|
12
|
-
},
|
|
13
|
-
);
|
|
14
|
-
|
|
15
|
-
const appendEffect = result.effects.find((e) => e._tag === "AppendFile");
|
|
16
|
-
expect(appendEffect).toBeDefined();
|
|
17
|
-
expect((appendEffect as { content: string }).content).toBe(
|
|
18
|
-
'export * from "./Button/index.js";\n',
|
|
19
|
-
);
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it("does nothing when export is already present", () => {
|
|
23
|
-
const result = dryRunWithFileState(
|
|
24
|
-
appendExportToParentIndex("src/components", "Button"),
|
|
25
|
-
{
|
|
26
|
-
"src/components/index.ts": 'export * from "./Button/index.js";\n',
|
|
27
|
-
},
|
|
28
|
-
);
|
|
29
|
-
|
|
30
|
-
const writeEffects = result.effects.filter(
|
|
31
|
-
(e) => e._tag === "AppendFile" || e._tag === "WriteFile",
|
|
32
|
-
);
|
|
33
|
-
expect(writeEffects).toHaveLength(0);
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it("creates new index file when it does not exist", () => {
|
|
37
|
-
const result = dryRunWithFileState(
|
|
38
|
-
appendExportToParentIndex("src/components", "Card"),
|
|
39
|
-
{},
|
|
40
|
-
);
|
|
41
|
-
|
|
42
|
-
const writeEffect = result.effects.find((e) => e._tag === "WriteFile");
|
|
43
|
-
expect(writeEffect).toBeDefined();
|
|
44
|
-
expect((writeEffect as { path: string }).path).toBe(
|
|
45
|
-
"src/components/index.ts",
|
|
46
|
-
);
|
|
47
|
-
expect((writeEffect as { content: string }).content).toBe(
|
|
48
|
-
'export * from "./Card/index.js";\n',
|
|
49
|
-
);
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
it("writeFile for new index has default undo (deleteFile)", () => {
|
|
53
|
-
const task = appendExportToParentIndex("src/components", "Card");
|
|
54
|
-
const undos = collectUndos(task);
|
|
55
|
-
|
|
56
|
-
expect(undos.length).toBeGreaterThan(0);
|
|
57
|
-
const undoEffects = dryRun(undos[0]).effects;
|
|
58
|
-
expect(undoEffects[0]._tag).toBe("DeleteFile");
|
|
59
|
-
expect((undoEffects[0] as { path: string }).path).toBe(
|
|
60
|
-
"src/components/index.ts",
|
|
61
|
-
);
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
it("appendFile carries custom undo (removeLineFromFile)", () => {
|
|
65
|
-
const result = dryRunWithFileState(
|
|
66
|
-
appendExportToParentIndex("src/components", "Button"),
|
|
67
|
-
{ "src/components/index.ts": "" },
|
|
68
|
-
);
|
|
69
|
-
|
|
70
|
-
const appendEffect = result.effects.find((e) => e._tag === "AppendFile");
|
|
71
|
-
expect(appendEffect).toBeDefined();
|
|
72
|
-
expect("undo" in (appendEffect as Record<string, unknown>)).toBe(true);
|
|
73
|
-
expect((appendEffect as Record<string, unknown>).undo).toBeDefined();
|
|
74
|
-
});
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
describe("dryRunWithFileState", () => {
|
|
78
|
-
it("returns empty string for missing files", () => {
|
|
79
|
-
const result = dryRunWithFileState(readFile("/missing/file.ts"), {});
|
|
80
|
-
expect(result.value).toBe("");
|
|
81
|
-
});
|
|
82
|
-
});
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import * as path from "node:path";
|
|
2
|
-
import {
|
|
3
|
-
appendFile,
|
|
4
|
-
exists,
|
|
5
|
-
flatMap,
|
|
6
|
-
ifElseM,
|
|
7
|
-
pure,
|
|
8
|
-
readFile,
|
|
9
|
-
type Task,
|
|
10
|
-
writeFile,
|
|
11
|
-
} from "@canonical/task";
|
|
12
|
-
import removeLineFromFile from "./removeLineFromFile.js";
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Append export to parent index.ts file (or create if not exists).
|
|
16
|
-
* Carries undo metadata: removes the export line (or deletes the index
|
|
17
|
-
* if it was created from scratch).
|
|
18
|
-
*/
|
|
19
|
-
export default function appendExportToParentIndex(
|
|
20
|
-
parentDir: string,
|
|
21
|
-
componentName: string,
|
|
22
|
-
): Task<void> {
|
|
23
|
-
const indexPath = path.join(parentDir, "index.ts");
|
|
24
|
-
const exportLine = `export * from "./${componentName}/index.js";\n`;
|
|
25
|
-
|
|
26
|
-
return ifElseM(
|
|
27
|
-
exists(indexPath),
|
|
28
|
-
// If exists, append (if not already exported)
|
|
29
|
-
flatMap(readFile(indexPath), (content) => {
|
|
30
|
-
if (content.includes(`./${componentName}`)) {
|
|
31
|
-
return pure(undefined); // Already exported
|
|
32
|
-
}
|
|
33
|
-
return appendFile(indexPath, exportLine, true, {
|
|
34
|
-
undo: removeLineFromFile(indexPath, exportLine),
|
|
35
|
-
});
|
|
36
|
-
}),
|
|
37
|
-
// If not exists, create new file (default undo: deleteFile is correct)
|
|
38
|
-
writeFile(indexPath, exportLine),
|
|
39
|
-
);
|
|
40
|
-
}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { dryRunWith, type Effect, type Task } from "@canonical/task";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* dryRunWith that mocks ReadFile and Exists to simulate file state.
|
|
5
|
-
* Test-only helper for file operation tests.
|
|
6
|
-
*/
|
|
7
|
-
export default function dryRunWithFileState<A>(
|
|
8
|
-
task: Task<A>,
|
|
9
|
-
files: Record<string, string>,
|
|
10
|
-
) {
|
|
11
|
-
const mocks = new Map<string, (effect: Effect) => unknown>([
|
|
12
|
-
[
|
|
13
|
-
"ReadFile",
|
|
14
|
-
(effect) => {
|
|
15
|
-
const e = effect as { path: string };
|
|
16
|
-
return files[e.path] ?? "";
|
|
17
|
-
},
|
|
18
|
-
],
|
|
19
|
-
[
|
|
20
|
-
"Exists",
|
|
21
|
-
(effect) => {
|
|
22
|
-
const e = effect as { path: string };
|
|
23
|
-
return e.path in files;
|
|
24
|
-
},
|
|
25
|
-
],
|
|
26
|
-
]);
|
|
27
|
-
return dryRunWith(task, mocks);
|
|
28
|
-
}
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import dryRunWithFileState from "./dryRunWithFileState.js";
|
|
3
|
-
import removeExportFromParentIndex from "./removeExportFromParentIndex.js";
|
|
4
|
-
|
|
5
|
-
describe("removeExportFromParentIndex", () => {
|
|
6
|
-
it("removes export when index exists with the export", () => {
|
|
7
|
-
const result = dryRunWithFileState(
|
|
8
|
-
removeExportFromParentIndex("src/components", "Button"),
|
|
9
|
-
{
|
|
10
|
-
"src/components/index.ts":
|
|
11
|
-
'export * from "./Button/index.js";\nexport * from "./Card/index.js";\n',
|
|
12
|
-
},
|
|
13
|
-
);
|
|
14
|
-
|
|
15
|
-
const writeEffect = result.effects.find((e) => e._tag === "WriteFile");
|
|
16
|
-
expect(writeEffect).toBeDefined();
|
|
17
|
-
expect((writeEffect as { content: string }).content).not.toContain(
|
|
18
|
-
"Button",
|
|
19
|
-
);
|
|
20
|
-
expect((writeEffect as { content: string }).content).toContain("Card");
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
it("does nothing when index does not exist", () => {
|
|
24
|
-
const result = dryRunWithFileState(
|
|
25
|
-
removeExportFromParentIndex("src/components", "Button"),
|
|
26
|
-
{},
|
|
27
|
-
);
|
|
28
|
-
|
|
29
|
-
const writeEffects = result.effects.filter(
|
|
30
|
-
(e) =>
|
|
31
|
-
e._tag === "WriteFile" ||
|
|
32
|
-
e._tag === "DeleteFile" ||
|
|
33
|
-
e._tag === "AppendFile",
|
|
34
|
-
);
|
|
35
|
-
expect(writeEffects).toHaveLength(0);
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
it("deletes index if removing export leaves it empty", () => {
|
|
39
|
-
const result = dryRunWithFileState(
|
|
40
|
-
removeExportFromParentIndex("src/components", "Button"),
|
|
41
|
-
{
|
|
42
|
-
"src/components/index.ts": 'export * from "./Button/index.js";\n',
|
|
43
|
-
},
|
|
44
|
-
);
|
|
45
|
-
|
|
46
|
-
const deleteEffect = result.effects.find((e) => e._tag === "DeleteFile");
|
|
47
|
-
expect(deleteEffect).toBeDefined();
|
|
48
|
-
expect((deleteEffect as { path: string }).path).toBe(
|
|
49
|
-
"src/components/index.ts",
|
|
50
|
-
);
|
|
51
|
-
});
|
|
52
|
-
});
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import * as path from "node:path";
|
|
2
|
-
import { exists, ifElseM, pure, type Task } from "@canonical/task";
|
|
3
|
-
import removeLineFromFile from "./removeLineFromFile.js";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Remove a component export from a parent index.ts.
|
|
7
|
-
* Idempotent — no-op if the export is not present or the file doesn't exist.
|
|
8
|
-
*/
|
|
9
|
-
export default function removeExportFromParentIndex(
|
|
10
|
-
parentDir: string,
|
|
11
|
-
componentName: string,
|
|
12
|
-
): Task<void> {
|
|
13
|
-
const indexPath = path.join(parentDir, "index.ts");
|
|
14
|
-
const exportLine = `export * from "./${componentName}/index.js";\n`;
|
|
15
|
-
|
|
16
|
-
return ifElseM(
|
|
17
|
-
exists(indexPath),
|
|
18
|
-
removeLineFromFile(indexPath, exportLine),
|
|
19
|
-
pure(undefined),
|
|
20
|
-
);
|
|
21
|
-
}
|
|
@@ -1,66 +0,0 @@
|
|
|
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
|
-
});
|
|
@@ -1,29 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
2
|
-
import { tmpdir } from "node:os";
|
|
3
|
-
import { join } from "node:path";
|
|
4
|
-
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
5
|
-
import loadTemplate from "./loadTemplate.js";
|
|
6
|
-
|
|
7
|
-
describe("loadTemplate", () => {
|
|
8
|
-
let dir: string;
|
|
9
|
-
|
|
10
|
-
beforeEach(() => {
|
|
11
|
-
dir = mkdtempSync(join(tmpdir(), "pragma-load-template-"));
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
afterEach(() => {
|
|
15
|
-
rmSync(dir, { recursive: true, force: true });
|
|
16
|
-
// Ensure no embedded-files stub leaks between tests.
|
|
17
|
-
delete (globalThis as { Bun?: unknown }).Bun;
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
it("reads template content from the filesystem", async () => {
|
|
21
|
-
const path = join(dir, "component.tsx.ejs");
|
|
22
|
-
writeFileSync(path, "export const <%= name %> = () => null;");
|
|
23
|
-
|
|
24
|
-
const result = await loadTemplate(path);
|
|
25
|
-
|
|
26
|
-
expect(result.source).toBe(path);
|
|
27
|
-
expect(result.content).toBe("export const <%= name %> = () => null;");
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
it("falls back to Bun.embeddedFiles when the file is not on disk", async () => {
|
|
31
|
-
// No file written to disk → fs read fails → embedded lookup runs.
|
|
32
|
-
// Blob names are content-hashed (e.g. "component-a1b2c3.tsx.ejs"); the
|
|
33
|
-
// loader dehashes by stripping the "-<hash>." segment.
|
|
34
|
-
const missingPath = join(dir, "component.tsx.ejs");
|
|
35
|
-
(globalThis as { Bun?: unknown }).Bun = {
|
|
36
|
-
embeddedFiles: [
|
|
37
|
-
Object.assign(new Blob(["EMBEDDED CONTENT"]), {
|
|
38
|
-
name: "component-a1b2c3d4.tsx.ejs",
|
|
39
|
-
}),
|
|
40
|
-
],
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
const result = await loadTemplate(missingPath);
|
|
44
|
-
|
|
45
|
-
expect(result.content).toBe("EMBEDDED CONTENT");
|
|
46
|
-
expect(result.source).toBe(missingPath);
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
it("returns the disk content even when an embedded blob also exists", async () => {
|
|
50
|
-
const path = join(dir, "types.ts.ejs");
|
|
51
|
-
writeFileSync(path, "DISK WINS");
|
|
52
|
-
(globalThis as { Bun?: unknown }).Bun = {
|
|
53
|
-
embeddedFiles: [
|
|
54
|
-
Object.assign(new Blob(["EMBEDDED"]), {
|
|
55
|
-
name: "types-deadbeef.ts.ejs",
|
|
56
|
-
}),
|
|
57
|
-
],
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
const result = await loadTemplate(path);
|
|
61
|
-
|
|
62
|
-
expect(result.content).toBe("DISK WINS");
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
// A total miss (no disk file, no matching embedded blob) must fail loud:
|
|
66
|
-
// callers await this for required templates and do not guard against empty
|
|
67
|
-
// content, so a silent "" would generate blank files (notably in a compiled
|
|
68
|
-
// binary, where the disk read always fails).
|
|
69
|
-
it("throws on a total miss (no disk, no embedded match)", async () => {
|
|
70
|
-
const missingPath = join(dir, "nonexistent.ejs");
|
|
71
|
-
|
|
72
|
-
await expect(loadTemplate(missingPath)).rejects.toThrow(
|
|
73
|
-
/Template not found/,
|
|
74
|
-
);
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it("throws when an embedded blob exists but none matches the basename", async () => {
|
|
78
|
-
const missingPath = join(dir, "wanted.ejs");
|
|
79
|
-
(globalThis as { Bun?: unknown }).Bun = {
|
|
80
|
-
embeddedFiles: [
|
|
81
|
-
Object.assign(new Blob(["x"]), { name: "other-cafef00d.ejs" }),
|
|
82
|
-
],
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
await expect(loadTemplate(missingPath)).rejects.toThrow(
|
|
86
|
-
/Template not found/,
|
|
87
|
-
);
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
it("throws when Bun is present but exposes no embeddedFiles", async () => {
|
|
91
|
-
// Running under the Bun runtime (not a compiled binary): `Bun` exists but
|
|
92
|
-
// `embeddedFiles` is undefined, so the embedded lookup is skipped.
|
|
93
|
-
const missingPath = join(dir, "wanted.ejs");
|
|
94
|
-
(globalThis as { Bun?: unknown }).Bun = {};
|
|
95
|
-
|
|
96
|
-
await expect(loadTemplate(missingPath)).rejects.toThrow(
|
|
97
|
-
/Template not found/,
|
|
98
|
-
);
|
|
99
|
-
});
|
|
100
|
-
});
|