-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·59 lines (47 loc) · 1.31 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
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const styles = require("./styles.js");
if (process.argv.length < 3) {
console.error("Component name should be provided");
process.exit(1);
}
const componentName = process.argv[process.argv.length - 1];
const dir = path.join(process.cwd(), componentName);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
function writeToFile(name, getData = () => "") {
const pathToFile = path.join(dir, name);
const componentName = path.basename(dir);
fs.writeFile(
path.join(dir, name),
getData(componentName),
{ encoding: "utf8" },
err => {
if (err) {
console.error("Error writing file!", err);
process.exit(1);
} else {
console.log("Creating", pathToFile);
}
}
);
}
const toLower = str => `${str.charAt(0).toLowerCase()}${str.slice(1)}`;
writeToFile("operations.graphql");
writeToFile(
"index.js",
componentName => `import React from 'react';
import cs from './styles';
const ${componentName} = () => (
<div className={cs.${toLower(componentName)}}>👋</div>
);
export default ${componentName};`
);
const getStylesContent = componentName =>
styles[componentName]
? styles[componentName]
: `.${toLower(componentName)} {
}`;
writeToFile("styles.module.css", getStylesContent);