@mahjongg/mern-mvc
Version:
A CLI that will build a MERN stack application using create-react-app
48 lines (41 loc) • 1.58 kB
JavaScript
const fs = require('fs');
const path = require('path');
const buildRoute = (name, options) => {
//options:
// notFunctionBased: -f, --no-function
// boolean that decides if the route should be function based or a standard export
// noPassport: -p --no-passport
// boolean that decides if passport should be included in the route
// routes: -r --routes
// list of routes to include in the file (default to GET?)
let routeString = "";
if (!options.notFunctionBased) {
routeString += `module.exports = function(${!options.noPassport ? 'passport' : '' }) {\n`;
}
routeString += `${!options.notFunctionBased ? "\t" : ''}const router = require('express').Router();\n\n`;
if(options.routes) {
options.routes.forEach((item)=>{
routeString += `${!options.notFunctionBased ? "\t" : ''}router.get("/${item}",function(req,res){
${!options.notFunctionBased ? "\t" : ''}});\n\n`;
});
}
if (!options.notFunctionBased) {
routeString += ` return router;\n};`;
} else {
routeString += `modules.exports = router;`
}
routeString += "\n";
fs.mkdir(path.join(process.cwd(), name), err => {
if (err && err.code === "EEXISTS") {
console.log("A folder with that name already exists!");
return false;
} else {
fs.writeFile(path.join(process.cwd(), name, "index.js"), routeString, function(){
console.log("done");
});
}
});
};
module.exports = {
buildRoute
};