Skip to content

Commit 476f120

Browse files
committed
docs: update react component example
1 parent b9a7d77 commit 476f120

1 file changed

Lines changed: 14 additions & 13 deletions

File tree

examples/generate-react-component/generate-react-component.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,15 @@ export default auto({
44
id: "react-component",
55
title: "React Component",
66
params: {
7-
name: {
8-
title: "Component Name",
9-
type: "string",
10-
required: true,
11-
},
127
path: {
138
title: "Component Path",
149
type: "string",
15-
defaultValue: ({ project, params }) => {
10+
defaultValue: ({ project }) => {
1611
if (project.hasDirectory("src/components")) {
17-
return `src/components/${params.name}`;
12+
return `src/components/`;
1813
}
1914
if (project.hasDirectory("components")) {
20-
return `components/${params.name}`;
15+
return `components/`;
2116
}
2217
},
2318
},
@@ -26,15 +21,21 @@ export default auto({
2621
run: async ({ project, params, fileMap, self, t }) => {
2722
console.log("Running:", self.id);
2823

24+
const parts = params.path.split("/");
25+
const name = parts.pop();
26+
if (!name) throw new Error(`Invalid name: ${name}`);
27+
2928
project.createDirectory(params.path);
3029

31-
project.writeFile(t(`${params.path}/index.ts`), t(fileMap["index.ts"]));
32-
project.writeFile(t(`${params.path}/${params.name}.tsx`), t(fileMap["__name__.tsx"]));
33-
if (project.hasDependency("jest")) {
34-
project.writeFile(t(`${params.path}/${params.name}.test.tsx`), t(fileMap["__name__.test.tsx"]));
30+
const template: typeof t = (text: string) => t(text, { ...params, name });
31+
32+
project.writeFile(template(`${params.path}/index.ts`), template(fileMap["index.ts"]));
33+
project.writeFile(template(`${params.path}/${name}.tsx`), template(fileMap["__name__.tsx"]));
34+
if (project.hasDependency("jest") || project.hasDependency("@testing-library/react")) {
35+
project.writeFile(template(`${params.path}/${name}.test.tsx`), template(fileMap["__name__.test.tsx"]));
3536
}
3637
if (project.hasDependency("storybook")) {
37-
project.writeFile(t(`${params.path}/${params.name}.stories.tsx`), t(fileMap["__name__.stories.tsx"]));
38+
project.writeFile(template(`${params.path}/${name}.stories.tsx`), template(fileMap["__name__.stories.tsx"]));
3839
}
3940
},
4041
});

0 commit comments

Comments
 (0)