-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin.js
executable file
·197 lines (177 loc) · 5.98 KB
/
bin.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
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
#!/usr/bin/env node
const process = require("process");
const fs = require("fs");
const clc = require("cli-color");
const inquirer = require("inquirer");
const child_process = require("child_process");
const arg = require("arg");
const args = arg({
"--port": Number,
"--page": [String],
"--backend": Boolean,
"--opts": Boolean,
});
const buildComponent = (page) => {
fs.mkdir(`${process.cwd()}/src/pages/${page}`, { recursive: true }, (err) => {
if (err) {
console.log(clc.redBright("Something went wrong...😓"));
}
fs.appendFileSync(
`${process.cwd()}/src/pages/${page}/${page}.jsx`,
`
import React from 'react'
import './${page}.css'
const ${page} = () => {
return (
<div>${page}</div>
)
}
export default ${page}
`
);
fs.appendFileSync(`${process.cwd()}/src/pages/${page}/${page}.css`, "");
});
console.log(
clc.blueBright("Page is created and files are setup... \n Happy Coding ✨")
);
};
const buildBackend = (port = 8080) => {
console.log(
clc.blueBright("Building folders and initializing your server...⛳️")
);
console.log(
clc.blueBright(
"This might take a few seconds...⏳ (Genie: I am getting stuff ready for you)"
)
);
fs.mkdirSync(`${process.cwd()}/model`);
fs.mkdirSync(`${process.cwd()}/routes`);
fs.appendFileSync(`${process.cwd()}/.env`, `\nPORT=${port}\n`);
// * install module / packages
child_process.execSync("npm init -y", { stdio: [] });
child_process.execSync("npm install express", { stdio: [] });
child_process.execSync("npm install cors", { stdio: [] });
child_process.execSync("npm i jsonwebtoken", { stdio: [] });
child_process.execSync("npm i mongoose", { stdio: [] });
child_process.execSync("npm i dotenv", { stdio: [] });
child_process.execSync("npm i bcryptjs", { stdio: [] });
child_process.execSync("npm i -D nodemon", { stdio: [] });
// * making files and appending the biolerplate code into server.js file.
fs.appendFileSync(
`${process.cwd()}/server.js`,
`
const express = require('express');
const app = express();
require('dotenv').config();
const cors = require('cors');
// middlewares
app.use(cors());
app.use(express.json());
// defining port
const PORT = process.env.PORT || 3001;
// setting up an empty GET Route
app.get('/', (req, res)=>{res.json({message: "You've come to the right place... it's a GET request!!"})});
// Starting Server on PORT
app.listen(PORT, () => console.log('Server started on PORT Number: ' + PORT))
`
);
console.log(
clc.blueBright(
"Folders are created and server.js is initialized with boilerplate code...\n Happy Coding ✨"
)
);
};
const giveOpts = () => {
console.log(
clc.blueBright.bold(
`\nHeyy, I am Genie 🧞♂️. I can build: \n1. Basic backend folder structures with all the required packages and boiler plate code\n2. Clean your react project by removing all the unnecessary files and adding all the necessary folders.\n`
)
);
inquirer
.prompt([
{
type: "list",
name: "choice",
choices: ["Backend Builder", "React Cleaner"],
},
])
.then((answers) => {
if (answers.choice == "Backend Builder") {
inquirer
.prompt([
{
type: "list",
name: "MongoDB URI",
choices: ["I have mongoDB URI", "I do not have a mongoDB URI"],
},
])
.then((ans) => {
if (ans["MongoDB URI"] == "I have mongoDB URI") {
inquirer
.prompt({
type: "input",
name: "URI",
message: "Enter your mongo URI here",
})
.then((ans) => {
fs.appendFileSync(
`${process.cwd()}/.env`,
`JWT_SECRET=secret\nmongo_URI=${ans.URI}`
);
fs.mkdirSync(`${process.cwd()}/db`);
fs.appendFileSync(
`${process.cwd()}/db/connection.js`,
`
const mongoose = require('mongoose')
require('dotenv').config()
const mongo_URI = process.env.mongo_URI
const connectToDB = async () => {
mongoose.connect(mongo_URI, (err) => {
if(err) console.log('Can not connect to the DB 🔴')
console.log('Connected to the DB 🟢')
})
}
// mongodb+srv://sangam:sangam@cluster0.5iqyskn.mongodb.net/?retryWrites=true&w=majority
module.exports = connectToDB;
`
);
buildBackend();
});
} else {
fs.appendFileSync(`${process.cwd()}/.env`, `JWT_SECRET=secret\n`);
fs.mkdirSync(`${process.cwd()}/db`);
buildBackend();
}
});
} else if (answers.choice == "React Cleaner") {
fs.unlinkSync(`${process.cwd()}/src/reportWebVitals.js`);
fs.unlinkSync(`${process.cwd()}/src/setupTests.js`);
fs.unlinkSync(`${process.cwd()}/src/App.test.js`);
fs.mkdirSync(`${process.cwd()}/src/components`);
fs.mkdirSync(`${process.cwd()}/src/assets`);
fs.mkdirSync(`${process.cwd()}/src/pages`);
console.log(
clc.blueBright(
"Folders are created and files are deleted... \n Happy Coding ✨"
)
);
}
})
.catch((error) => {
if (error.isTtyError) {
console.log(clc.redBright("Some error occured with the prompt"));
} else {
console.log(clc.redBright("Something went wrong...😓"));
}
});
};
const isCreateBackend = args["--backend"];
const isOpts = args["--opts"];
const pageName = args["--page"];
if (isCreateBackend) {
buildBackend(args["--port"]);
} else if (isOpts) {
giveOpts();
} else if (pageName.length > 0) {
buildComponent(pageName[0]);
}