-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileHandler.js
94 lines (85 loc) · 2.31 KB
/
fileHandler.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
const fs = require("fs");
//add all the imports on top of the file
const addImports = (_hooks, dependencies) => {
const importHooks = (hooks) => {
if (!hooks) {
return `import React from 'react';`;
} else {
return `import React,{${hooks.join(",")}} from 'react';`;
}
};
const importDependencies = (_dependencies) => {
if (!_dependencies) {
return "";
}
return _dependencies
.map((dependency) => {
return `import ${dependency.file} from '${dependency.path}';`;
})
.join("\n");
};
const reactImport = importHooks(_hooks);
const dependenciesImport = importDependencies(dependencies);
return `${reactImport}\n${dependenciesImport}`;
};
//create the main jsx function
const createMain = (fileName = "") => {
return `const ${fileName
.charAt(0)
.toUpperCase()
.concat(fileName.slice(1))} = () => {
`;
};
//Add the important hooks here
const addHooks = (hookData) => {
const useStateHook = (values) => {
return values
.map((value) => {
return `const [${value}, set${value}] = useState('');`;
})
.join("\n");
};
const useEffectHook = (values) => {
return values
.map((value) => {
return `useEffect(() => {
// ${value}
}, [${value}]);`;
})
.join("\n");
};
const useState = useStateHook(hookData.useState.values);
const useEffect = useEffectHook(hookData.useEffect.values);
return `${useState}\n${useEffect}`;
};
//Add important functions here
const addFunctions = (params) => {};
//Add the return JSX statment here
const addReturn = (params) => {};
//add the whole JSX here
const addJSX = (params) => {};
//add the export statement here
const addExport = (params) => {};
const template = (file) => {
console.log(`${addImports(file.hooks, file.dependencies)} \n\n${createMain(file.fileName)}\n
${addHooks(file.hookData)}
`);
};
const testComponent = {
fileName: "testComponent",
hooks: ["useState", "useEffect"],
dependencies: [
{ file: "styled", path: "styledComponents" },
{ file: "axios", path: "axios" },
],
hookData: {
useState: {
values: ["test", "test2"],
},
useEffect: {
values: ["test", "test2"],
},
},
};
// console.log(addImports(testComponent.hooks, testComponent.dependencies));
template(testComponent);