-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
78 lines (64 loc) · 2.05 KB
/
index.js
File metadata and controls
78 lines (64 loc) · 2.05 KB
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
#!/usr/bin/env node
import chalk from "chalk";
import path from "node:path";
import { cwd } from "node:process";
import { mkdirSync, writeFileSync } from "node:fs";
import gradient from "gradient-string";
import figlet from "figlet";
// getting component names from cli arguments.
const cliArguments = process.argv;
const componentNames = cliArguments
.slice(2)
.filter(componentName => componentName != "-ts");
const isTs = cliArguments.includes("-ts");
// exiting if no componentNames are specified in the cli.
if (componentNames.length === 0) {
console.log(chalk.red("Please specify the component name(s)."));
console.log(chalk.blueBright("eg., srfc Box"));
process.exit(1);
}
// creating Components for all the component names specified in the cli.
const currentTerminalDirectory = cwd();
componentNames.forEach(componentName => {
try {
const componentDirectory = path.resolve(
currentTerminalDirectory,
componentName
);
const componentJsPath = path.resolve(
componentDirectory,
`${componentName}.jsx`
);
const componentTsPath = path.resolve(
componentDirectory,
`${componentName}.tsx`
);
const componentCssModulePath = path.resolve(
componentDirectory,
`${componentName}.module.css`
);
mkdirSync(componentDirectory);
if (isTs) {
writeFileSync(componentTsPath, getComponentJs(componentName));
} else {
writeFileSync(componentJsPath, getComponentJs(componentName));
}
writeFileSync(componentCssModulePath, getComponentModuleCss(componentName));
} catch (error) {
console.log(chalk.red(error.message));
process.exit(1);
}
});
//displaying a cool done message.
figlet("Done :)", (error, data) => {
console.log(gradient.mind(data));
});
function getComponentJs(componentName) {
return `import styles from "./${componentName}.module.css";
export default function ${componentName}() {
return <div className={styles.${componentName}}>${componentName}</div>;
}`;
}
function getComponentModuleCss(componentName) {
return `.${componentName} {}`;
}