-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponentConstractor.js
executable file
·54 lines (51 loc) · 1.57 KB
/
componentConstractor.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
#!/usr/bin/env node
const fs = require("node:fs").promises;
const path = require("path");
const { getConfig } = require("./finder.js");
const componentName = process.argv[2];
let outputDir;
if (!componentName) {
console.log("Usage:component <component_name>");
return;
}
if (!componentName.includes("-")) {
console.log(
"\x1b[36m%s\x1b[0m",
"Component name must be kebab-case : <nav-bar>"
);
return;
}
const componentTag = componentName.toLowerCase();
const componentClassName =
componentName.charAt(0).toUpperCase() + componentName.slice(1);
const jsTemplatePath = path.join(__dirname, "component-template.js");
(async () => {
try {
outputDir = await getConfig().paths.project;
outputDir = path.join(outputDir, "frontend", "src");
fs.access(outputDir, fs.constants.F_OK, (err) => {
if (err) {
console.error(
"\x1b[31m%s\x1b[0m",
"The frontend/src directory does not exist"
);
return;
}
});
await fs.writeFile(
path.join(outputDir, "assets/style", `${componentName}.css`),
`/****** ${componentName}.css ******/`
);
const jsTemplate = await fs.readFile(jsTemplatePath, "utf8");
const processedJsTemplate = jsTemplate
.replace(/__COMPONENT_NAME__/g, componentClassName.replace(/-/g, ""))
.replace(/__COMPONENT_TAG__/g, componentTag);
await fs.writeFile(
path.join(outputDir, "components", `${componentName}.js`),
processedJsTemplate
);
console.log(`Component ${componentName} created successfully!`);
} catch (err) {
console.err(err);
}
})();