-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·101 lines (90 loc) · 2.88 KB
/
index.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env node
const commander = require("commander");
const fs = require("fs");
const path = require("path");
const validateComponentName = require("./ValidateComponentName");
const program = new commander.Command();
program
.name("create-new-react-component")
.usage("<componentName> [options]")
.version("1.1.0")
.description(
"Create a new React component with an optional CSS file. " +
"The component will be created in a new directory with the same name as the component."
)
.arguments("[componentName]", "Name of the component to create")
.option(
"--style <suffix>",
"Create a CSS file with the given suffix, if not given, set as 'css'",
".css"
)
.option(
"-l, --lang <lang>",
"Choose the file style (js or ts), if not given, set as 'js'",
"js"
)
.option("--withProps", "Create a component with props")
.option(
"--withImportReact",
"Create a component with `import React from react`"
)
.action((componentName, options) => {
createComponent(componentName, options);
});
function createComponent(componentName, options) {
if (!componentName) {
console.error("Error: <componentName> is required.");
process.exit(1); // Exit the process with an error code
}
if (!validateComponentName(componentName)) {
console.error(
"Error: Component name must be in PascalCase (e.g., MyComponent)"
);
process.exit(1);
}
const componentDir = path.join(process.cwd(), componentName);
const indexFilePath = path.join(componentDir, `index.${options.lang}`);
const componentFilePath = path.join(
componentDir,
`${componentName}.${options.lang === "ts" ? "tsx" : "jsx"}`
);
const stylesFilePath = path.join(
componentDir,
`${componentName}.module.${options.style}`
);
const indexFileContent = `export { default } from './${componentName}';`;
const ComponentFileContent = require("./ComponentFileContent");
const componentFileContent = new ComponentFileContent(
componentName,
options.style,
options.lang,
options.withProps,
options.withImportReact
);
const componentFileContentContent =
componentFileContent.generateComponentContent();
const stylesFileContent = `/* Add your component styles here */
.${componentName} {
}
`;
if (fs.existsSync(componentDir)) {
console.error(`Component ${componentName} already exists`);
return;
}
try {
fs.mkdirSync(componentDir);
fs.writeFileSync(indexFilePath, indexFileContent);
fs.writeFileSync(componentFilePath, componentFileContentContent);
if (options.style) {
fs.writeFileSync(stylesFilePath, stylesFileContent);
}
console.log(
`Component ${componentName} created successfully${
options.style ? " with language" : ""
} (${options.lang})`
);
} catch (err) {
console.error(`Error creating component ${componentName}:`, err);
}
}
program.parse(process.argv);