backend-scaffold-cli 1.3.0 → 1.4.0
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.
- package/bin/cli.js +91 -4
- package/package.json +1 -1
package/bin/cli.js
CHANGED
|
@@ -37,6 +37,12 @@ program
|
|
|
37
37
|
message: 'Include rate limiting?',
|
|
38
38
|
default: true
|
|
39
39
|
},
|
|
40
|
+
{
|
|
41
|
+
type: 'confirm',
|
|
42
|
+
name: 'useFileUpload',
|
|
43
|
+
message: 'Include file upload support (multer)?',
|
|
44
|
+
default: true
|
|
45
|
+
},
|
|
40
46
|
{
|
|
41
47
|
type: 'confirm',
|
|
42
48
|
name: 'installDeps',
|
|
@@ -61,7 +67,7 @@ program
|
|
|
61
67
|
console.log(chalk.blue(`Creating project in ${projectPath}\n`));
|
|
62
68
|
fs.ensureDirSync(projectPath);
|
|
63
69
|
|
|
64
|
-
createFolderStructure(projectPath);
|
|
70
|
+
createFolderStructure(projectPath, answers);
|
|
65
71
|
createConfigFiles(projectPath, answers);
|
|
66
72
|
createMiddlewareFiles(projectPath, answers);
|
|
67
73
|
createRouteFiles(projectPath, answers);
|
|
@@ -94,7 +100,7 @@ program.parse();
|
|
|
94
100
|
|
|
95
101
|
|
|
96
102
|
|
|
97
|
-
const createFolderStructure = (projectPath) => {
|
|
103
|
+
const createFolderStructure = (projectPath, answers) => {
|
|
98
104
|
const folders = [
|
|
99
105
|
'src/config',
|
|
100
106
|
'src/middleware',
|
|
@@ -105,12 +111,15 @@ const createFolderStructure = (projectPath) => {
|
|
|
105
111
|
'src/validation'
|
|
106
112
|
];
|
|
107
113
|
|
|
114
|
+
if (answers.useFileUpload) {
|
|
115
|
+
folders.push('uploads');
|
|
116
|
+
}
|
|
117
|
+
|
|
108
118
|
folders.forEach(folder => {
|
|
109
119
|
fs.ensureDirSync(path.join(projectPath, folder));
|
|
110
120
|
});
|
|
111
121
|
|
|
112
122
|
console.log(chalk.green(' Folder structure created'));
|
|
113
|
-
|
|
114
123
|
}
|
|
115
124
|
|
|
116
125
|
|
|
@@ -139,6 +148,7 @@ const createConfigFiles = (projectPath, answers) => {
|
|
|
139
148
|
...(answers.useAuth && { jsonwebtoken: '^9.0.0' , bcryptjs: '^2.4.3'}),
|
|
140
149
|
...(answers.useValidation && { joi: '^17.9.0' }),
|
|
141
150
|
...(answers.useRateLimit && { 'express-rate-limit': '^6.7.0' }),
|
|
151
|
+
...(answers.useFileUpload && { multer: '^2.0.0' }),
|
|
142
152
|
|
|
143
153
|
},
|
|
144
154
|
devDependencies: {
|
|
@@ -185,7 +195,6 @@ LOG_LEVEL=debug
|
|
|
185
195
|
|
|
186
196
|
|
|
187
197
|
|
|
188
|
-
|
|
189
198
|
const gitignore = `node_modules/
|
|
190
199
|
.env
|
|
191
200
|
.env.local
|
|
@@ -198,10 +207,14 @@ npm-debug.log*
|
|
|
198
207
|
yarn-debug.log*
|
|
199
208
|
.idea/
|
|
200
209
|
.vscode/
|
|
210
|
+
${answers.useFileUpload ? 'uploads/*\n!uploads/.gitkeep' : ''}
|
|
201
211
|
`;
|
|
202
212
|
|
|
203
213
|
fs.writeFileSync(path.join(projectPath, '.gitignore'), gitignore);
|
|
204
214
|
|
|
215
|
+
if(answers.useFileUpload){
|
|
216
|
+
fs.writeFileSync(path.join(projectPath, 'uploads/.gitkeep'), '');
|
|
217
|
+
}
|
|
205
218
|
console.log(chalk.green(' Config files created'));
|
|
206
219
|
|
|
207
220
|
|
|
@@ -312,6 +325,57 @@ module.exports=limiter;
|
|
|
312
325
|
|
|
313
326
|
|
|
314
327
|
|
|
328
|
+
if(answers.useFileUpload){
|
|
329
|
+
const multerMiddleware= ` const multer = require('multer');
|
|
330
|
+
const path = require('path');
|
|
331
|
+
|
|
332
|
+
// Set storage engine
|
|
333
|
+
const storage = multer.diskStorage({
|
|
334
|
+
destination: function (req,file ,cb){
|
|
335
|
+
cb(null, 'uploads/');
|
|
336
|
+
},
|
|
337
|
+
filename: function (req, file, cb) {
|
|
338
|
+
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random()*1E9);
|
|
339
|
+
cb(null, file.fieldname + '-' + uniqueSuffix + path.extname(file.originalname))
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
//file filter
|
|
346
|
+
const fileFilter = (req, file, cb) =>{
|
|
347
|
+
const allowedTypes = /jpeg|jpg|png|gif/;
|
|
348
|
+
const extname = allowedTypes.test(path.extname(file.originalname).toLowerCase());
|
|
349
|
+
const mimetype = allowedTypes.test(file.mimetype);
|
|
350
|
+
|
|
351
|
+
if(extname && mimetype){
|
|
352
|
+
return cb(null,true);
|
|
353
|
+
}else{
|
|
354
|
+
cb(new Error('Error: Images Only!'));
|
|
355
|
+
}
|
|
356
|
+
};
|
|
357
|
+
|
|
358
|
+
const upload = multer({
|
|
359
|
+
storage: storage,
|
|
360
|
+
limits: { fileSize: 5 * 1024 * 1024 }, // 5MB limit
|
|
361
|
+
fileFilter: fileFilter
|
|
362
|
+
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
module.exports = upload;
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
`;
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
fs.writeFileSync(
|
|
372
|
+
path.join(projectPath, 'src/middleware/upload.js'),
|
|
373
|
+
multerMiddleware
|
|
374
|
+
)
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
|
|
315
379
|
|
|
316
380
|
|
|
317
381
|
|
|
@@ -390,6 +454,7 @@ const createRouteFiles = (projectPath, answers) => {
|
|
|
390
454
|
const routes = `const express = require('express');
|
|
391
455
|
const router = express.Router();
|
|
392
456
|
${answers.useAuth ? "const authenticateToken = require('../middleware/auth');" : ''}
|
|
457
|
+
${answers.useFileUpload ? "const upload = require('../middleware/upload');" : ''}
|
|
393
458
|
|
|
394
459
|
// Public routes
|
|
395
460
|
router.get('/health', (req, res) => {
|
|
@@ -403,6 +468,28 @@ router.get('/protected', authenticateToken, (req, res) => {
|
|
|
403
468
|
});
|
|
404
469
|
` : ''}
|
|
405
470
|
|
|
471
|
+
${answers.useFileUpload ? `
|
|
472
|
+
// File upload route
|
|
473
|
+
router.post('/upload', upload.single('file'), (req,res) =>{
|
|
474
|
+
if(!req.file){
|
|
475
|
+
return res.status(400).json({
|
|
476
|
+
success: false,
|
|
477
|
+
message: 'No file uploaded'
|
|
478
|
+
})
|
|
479
|
+
}
|
|
480
|
+
res.json({
|
|
481
|
+
success: true,
|
|
482
|
+
message: 'File uploaded successfully',
|
|
483
|
+
file: req.file
|
|
484
|
+
});
|
|
485
|
+
|
|
486
|
+
});
|
|
487
|
+
|
|
488
|
+
|
|
489
|
+
`: ''}
|
|
490
|
+
|
|
491
|
+
|
|
492
|
+
|
|
406
493
|
module.exports = router;
|
|
407
494
|
`;
|
|
408
495
|
fs.writeFileSync(
|