-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathnew_component.mjs
78 lines (65 loc) · 1.83 KB
/
new_component.mjs
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
71
72
73
74
75
76
77
78
import fs from 'node:fs';
import path from 'path';
import chalk from 'chalk';
import { exec } from 'child_process';
console.log(chalk.blue('new-component 1.0\n'));
if (process.argv.length < 3) {
console.log(chalk.red('Add a name for the component'));
process.exit(0);
}
const component_name = process.argv[2];
if (
component_name.substring(0, 1) ===
component_name.substring(0, 1).toLowerCase()
) {
console.log(chalk.red(
`Component name needs to start with capital letter: ${component_name}`,
));
process.exit(0);
}
console.log(chalk.blue(`Create Component '${component_name}'`));
const base_folder = "./src/components/";
const comp_folder = path.join(base_folder, component_name);
function createFile(folder, name, content) {
// Check if the directory exists
if (!fs.existsSync(folder))
// If the directory does not exist, create it
fs.mkdirSync(folder, { recursive: true });
console.log(path.join(folder, name));
exec('git add '+path.join(folder, name));
fs.writeFileSync(path.join(folder, name), content);
}
createFile(
comp_folder,
"index.js",
`export * from "./${component_name}";
export { default } from "./${component_name}";
`,
);
createFile(
comp_folder,
component_name + ".jsx",
`import React from 'react';
import styles from './${component_name}.module.css';
function ${component_name}() {
return <div></div>;
}
export default ${component_name};
`,
);
createFile(comp_folder, component_name + ".module.css", ``);
createFile(comp_folder, component_name + ".stories.jsx", `import ${component_name} from "./${component_name}";
const meta = {
component: ${component_name},
argTypes: {
},
};
export default meta;
export const Normal = {
render: (args) => (
<${component_name} {...args}>
</${component_name}>
),
};
`);
console.log(chalk.green(`\nSuccessfully created '${component_name}'!`));