-
Notifications
You must be signed in to change notification settings - Fork 328
Expand file tree
/
Copy pathcreateAxios.command.js
More file actions
106 lines (93 loc) · 2.95 KB
/
createAxios.command.js
File metadata and controls
106 lines (93 loc) · 2.95 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const shell = require("shelljs");
const path = require("path");
const fs = require("fs");
const wrapTagAround = require("configure-react/utils/wrapTagAround");
const importAtTop = require("configure-react/utils/importAtTop");
const detail = require("configure-react/commands/details.json");
const {
editReadme,
createDotEnv,
createComponent,
checkIfRoot,
makeCodePritter,
endingScreen,
} = require("configure-react/utils");
const createAxios = (args) => {
if (!checkIfRoot(args)) {
return console.log(
"You are not in the root of a react app. Please run this command in the root of a react app."
);
}
const currentPath = process.cwd();
startCreatingAxios(currentPath);
endingScreen();
};
const startCreatingAxios = (currentPath) => {
shell.exec("npm i axios");
const readmePath = path.join(currentPath, "./README.md");
editReadme(readmePath, "axios");
shell.mkdir("-p", path.join(currentPath, "./src/api")); // -p flag creates parent directories if they don't exist
// create file if not exist
const env = path.join(currentPath, "./.env");
const componentPath = path.join(currentPath, "./src/components");
// if (!fs.existsSync(env)) {
createDotEnv(env);
createComponent(componentPath);
// }
shell.touch(path.join(currentPath, "./src/api/index.js"));
shell.touch(path.join(currentPath, "./src/api/login.js"));
shell.touch(path.join(currentPath, "./src/api/register.js"));
shell.touch(path.join(currentPath, "./src/api/getData.js"));
shell.touch(path.join(currentPath, "./src/api/postData.js"));
const indexFilePath = path.join(currentPath, "./src/api/index.js");
const loginFilePath = path.join(currentPath, "./src/api/login.js");
const registerFilePath = path.join(currentPath, "./src/api/register.js");
const getDataFilePath = path.join(currentPath, "./src/api/getData.js");
const postDataFilePath = path.join(currentPath, "./src/api/postData.js");
// edit index.js
const apiIndex = detail.apiIndexData.join("\n");
fs.writeFileSync(indexFilePath, makeCodePritter(apiIndex), "utf8", (err) => {
if (err) throw err;
});
// edit login.js
const loginApiData = detail.loginApiData.join("\n");
fs.writeFileSync(
loginFilePath,
makeCodePritter(loginApiData),
"utf8",
(err) => {
if (err) throw err;
}
);
// edit register.js
const registerApiData = detail.registerApiData.join("\n");
fs.writeFileSync(
registerFilePath,
makeCodePritter(registerApiData),
"utf8",
(err) => {
if (err) throw err;
}
);
// edit getData.js
const getApiData = detail.getApiData.join("\n");
fs.writeFileSync(
getDataFilePath,
makeCodePritter(getApiData),
"utf8",
(err) => {
if (err) throw err;
}
);
// edit postData.js
const postApiData = detail.postApiData.join("\n");
fs.writeFileSync(
postDataFilePath,
makeCodePritter(postApiData),
"utf8",
(err) => {
if (err) throw err;
}
);
};
module.exports = createAxios;