-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
69 lines (61 loc) · 1.77 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
const fs = require("node:fs");
const path = require("node:path");
const scaffoldData = require("./scaffoldData.json");
const CURRENT_PATH = process.cwd();
const ARGS = process.argv.slice(2);
String.prototype.capitalize = function () {
return this.charAt(0).toUpperCase() + this.slice(1);
};
const createFolders = (_folders) => {
_folders.folders.forEach((folder) => {
const curPath = `${CURRENT_PATH}/${_folders.parent}/${folder}`;
const parentPath = `${CURRENT_PATH}/${_folders.parent}`;
if (fs.existsSync(curPath)) return;
if (!fs.existsSync(parentPath)) fs.mkdirSync(parentPath);
fs.mkdir(curPath, (err) => {
if (err) {
console.warn(curPath);
}
});
createFiles(curPath, _folders.type);
});
};
const createFiles = (folderPath, folderType) => {
const filePath = `${folderPath}/${path.basename(folderPath)}.jsx`;
const fileName = path.basename(folderPath);
if (fs.existsSync(filePath)) return;
switch (folderType) {
case "pages":
{
fs.writeFile(filePath, fileTemplate(fileName), (err) => {
if (err) {
console.warn(filePath);
}
});
}
break;
case "components":
{
fs.writeFile(filePath, fileTemplate(fileName), (err) => {
if (err) {
console.warn(filePath);
}
});
}
break;
default:
break;
}
};
const fileTemplate = (fileName = "", fileType) => {
return ` import React from 'react'
const ${fileName.capitalize()} = () => {
return (
<div>${fileName.capitalize()}</div>
)
}
export default ${fileName.capitalize()}`;
};
createFolders(scaffoldData.folders.primaryFolders);
createFolders(scaffoldData.folders.mainComponents);
createFolders(scaffoldData.folders.mainPages);