-
Notifications
You must be signed in to change notification settings - Fork 1
/
scaffold-component.js
70 lines (57 loc) · 1.82 KB
/
scaffold-component.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// We're using ES module imports but you could do the same with Node's `require`.
import { writeFileSync, mkdirSync, existsSync } from "fs";
console.log(process.argv);
// Find the flag starting with our flag name
const nameFlag = process.argv.find((arg) => arg.startsWith("--name="));
// Handle a missing flag.
if (!nameFlag) {
throw new Error("Please pass in a component name using the `--name` flag");
}
// Split the string at the question mark
const flagPieces = nameFlag.split("=");
// Use the piece after the question mark as our name.
const componentName = flagPieces[1];
// You can tweak the path to better match your project's structure.
// All directories in this path must already exist.
const componentsPath = "./src/components/";
const directoryPath = `${componentsPath}${componentName}`;
// Throw an error if a directory with this name already exists.
if (existsSync(directoryPath)) {
throw new Error(
`A component directory named ${componentName} already exists`
);
}
// Make the directory
mkdirSync(directoryPath);
writeFileSync(
`${directoryPath}/${componentName}.tsx`,
`
import './${componentName}.scss';
export const ${componentName} = () => (
<div>I'm a ${componentName} component!</div>
);
`.trim()
);
writeFileSync(`${directoryPath}/${componentName}.scss`, "");
writeFileSync(
`${directoryPath}/${componentName}.test.ts`,
`
test('This is an example test that should be populated or removed before merging.', () => {
expect(sum(1, 2)).toBe(3);
});
`.trim()
);
writeFileSync(
`${directoryPath}/${componentName}.stories.mdx`,
`
import { Canvas, Meta, Story } from '@storybook/addon-docs';
import { ${componentName} } from './${componentName}.tsx';
<Meta title="Components/${componentName}" />
# ${componentName}
<Canvas>
<Story name="Basic Usage">
<${componentName} />
</Story>
</Canvas>
`.trim()
);