-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·290 lines (270 loc) · 9.09 KB
/
app.js
File metadata and controls
executable file
·290 lines (270 loc) · 9.09 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env node
"use strict";
const fs = require("fs");
const childProc = require("child_process");
const path = require("path");
const readline = require("readline");
const makeParentFolder = (parentFolderName) => {
return new Promise((resolve, reject) => {
fs.mkdir(path.resolve(process.cwd(), `./${parentFolderName}`), (err) => {
if (err) {
console.log("Process exited -> \n", err);
reject();
} else {
console.log("Created parent folder.");
resolve();
}
});
});
};
const makeFolders = (folders) => {
if (folders.length === 0) {
console.log("No folders were made in root directory.");
return;
}
const makingFolders = childProc.spawn("mkdir", [...folders]);
makingFolders.on("error", (err) => {
console.log("Error occured while creating root folders");
});
makingFolders.on("close", (code) => {
if (code !== 0) {
console.log("Error occured while creating root folders");
} else {
console.log("Created all root folders.");
}
});
};
const npm_work = async (
npmPackagesForServer,
npmPackagesForClient,
clientFolderName
) => {
const npm_init = childProc.spawn("npm", ["init", "--y"]);
npm_init.on("error", (err) => {
console.log("Error occured in npm init");
});
npm_init.on("close", (code) => {
if (code !== 0) {
console.log("Error occured in npm init");
} else {
console.log("npm init successfull.");
}
const npm_client = childProc.spawn("npx", [
"create-react-app",
`./${clientFolderName}`,
]);
npm_client.on("error", (err) => {
console.log("Error occured in npx create-react-app");
});
npm_client.on("close", (code) => {
if (code !== 0) {
console.log("Error occured in npx create-react-app");
return;
}
console.log("React app setup done.");
if (npmPackagesForClient.length === 0) {
console.log("No npm packages were added for react app.");
return;
}
const npm_packages_client = childProc.spawn(
"npm",
["i", ...npmPackagesForClient],
{
cwd: `./${clientFolderName}`,
}
);
npm_packages_client.on("error", (err) => {
console.log("npm i for client failed.");
});
npm_packages_client.on("close", (code) => {
if (code !== 0) {
console.log("npm i for react app failed due to invalid arguments.");
} else {
console.log("Installed npm packages for react app.");
}
});
});
const npm_packages_server = childProc.spawn("npm", [
"i",
"express",
"mongoose",
"dotenv",
...npmPackagesForServer,
]);
npm_packages_server.on("error", (err) => {
console.log("npm i for server failed.");
});
npm_packages_server.on("close", (code) => {
if (code !== 0) {
console.log("npm i for server failed due to invalid arguments.");
} else {
console.log("Installed npm packages for server.");
}
});
});
};
const git_work = async (link) => {
const git_init = childProc.spawn("git", ["init"]);
git_init.on("close", (code) => {
const git_remote = childProc.spawn("git", [
"remote",
"add",
"origin",
`${link}`,
]);
git_remote.on("error", (err) => {
console.log("git remote add origin failed.");
});
git_remote.on("close", (code) => {
if (code !== 0) {
console.log("git remote add origin failed.");
} else {
console.log("Git repository setup complete.");
}
});
});
};
const setup_project = async (
parentFolderName,
rootFolders,
clientFolderName,
npmPackagesForServer,
npmPackagesForClient,
ifGit,
gitlink
) => {
await makeParentFolder(parentFolderName);
process.chdir(`./${parentFolderName}`);
console.log(`Switched working directory to ${process.cwd()}`);
makeFolders(rootFolders);
fs.copyFile(
path.resolve(__dirname, "./default_files/server.js"),
path.resolve(process.cwd(), "./server.js"),
() => console.log("Created server.js in root directory.")
);
fs.copyFile(
path.resolve(__dirname, "./default_files/.env"),
path.resolve(process.cwd(), "./.env"),
() => console.log("Created .env file.")
);
npm_work(npmPackagesForServer, npmPackagesForClient, clientFolderName);
if (ifGit) {
git_work(gitlink);
fs.copyFile(
path.resolve(__dirname, "./default_files/.gitignore"),
path.resolve(process.cwd(), "./.gitignore"),
() => console.log("Created .gitignore in root directory.")
);
}
};
const cli = () => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question(
"Name of your project folder\nDefaults to 'Project_Folder'\n",
(ans) => {
// ! Enter Project name
const projectName =
(ans.match(/[a-zA-Z-_0-9]+/gm) && ans.match(/[a-zA-Z-_0-9]+/gm)[0]) ||
"Project_Folder";
console.log("\x1b[36m%s\x1b[0m", `Project Name set as\n${projectName}`);
rl.question(
"npm packages for server.Other than express, mongoose, and dotenv.\nDefaults to []\n",
(ans) => {
// ! NPM Packages for server other than mongoose, express, and dotenv
const server_packages = ans.match(/[a-zA-Z0-9-_@/]+/gm) || [];
console.log(
"\x1b[36m%s\x1b[0m",
`npm packages for server(other than express, mongoose, and dotenv).\n${server_packages}`
);
rl.question(
"Folders to make in root directory\nDefaults to []\n",
(ans) => {
// ! Folders to make in root directory
const folders = ans.match(/[a-zA-Z-_0-9]+/gm) || [];
console.log(
"\x1b[36m%s\x1b[0m",
`Folders to make in root directory are\n${folders}`
);
rl.question(
"Folder for client side?\n(It would be made if it does not already exist in root directory)\nDefault to 'client'\n",
(ans) => {
// ! Enter name for client folder
const clientFolder =
(ans.match(/[a-zA-Z-_0-9]+/gm) &&
ans.match(/[a-zA-Z-_0-9]+/gm)[0]) ||
"client";
console.log(
"\x1b[36m%s\x1b[0m",
`Client folder is set as\n${clientFolder}`
);
rl.question(
"npm packages for react app.\nDefaults to []\n",
(ans) => {
// ! NPM Packages for React app
const client_packages =
ans.match(/[a-zA-Z0-9-_@/]+/gm) || [];
console.log(
"\x1b[36m%s\x1b[0m",
`npm packages for react app\n${client_packages}`
);
rl.question(
"Do you want to add a github repo?\n(Enter 'yes' to init and add a github repo and anything else for otherwise)\n",
(ans) => {
// ! Enter if you want to setup a github repo
if (/yes/i.test(ans)) {
rl.question("Link to github repo.\n", (ans) => {
// ! Enter the link to github repo
const github_link = (ans && ans.trim()) || "";
console.log(
"\x1b[36m%s\x1b[0m",
`Link to github repo.\n${github_link}`
);
rl.close();
console.log(
"\x1b[32m%s\x1b[0m",
"\n\nSetting up the project for you...\n\n"
);
//* SETUP PROJECT
setup_project(
projectName,
folders,
clientFolder,
server_packages,
client_packages,
true,
github_link
);
});
} else {
rl.close();
console.log(
"\x1b[32m%s\x1b[0m",
"\n\nSetting up the project for you...\n\n"
);
setup_project(
projectName,
folders,
clientFolder,
server_packages,
client_packages,
false,
null
);
}
}
);
}
);
}
);
}
);
}
);
}
);
};
cli();