nodejs-backpack
Advanced tools
Comparing version 2.0.6 to 2.0.7
#!/usr/bin/env node | ||
const fs=require("fs-extra"),path=require("path");var clc=require("cli-color");const{loadSchemaFromFile,modelsParams,capitalize,searchableFields}=require("./helper"),command=process.argv[2],fileName=process.argv[3];if("sample:files"===command){const a=process.cwd(),b=path.join(a,"sample-app.js");let e=fs.readFileSync(path.join(__dirname,"sample-app.js"),"utf8");fs.writeFileSync(b,e);const d=path.join(a,".sample-env");let s=fs.readFileSync(path.join(__dirname,".sample-env"),"utf8");fs.writeFileSync(d,s);const f=path.join(a,"sample-package.json");let i=fs.readFileSync(path.join(__dirname,"sample-package.json"),"utf8");fs.writeFileSync(f,i),console.log(clc.green('"sample-app.js", "sample-package.json" & ".sample-env" file created in the root directory.'))}else if("make:schema"===command){const h=process.cwd(),i=path.join(h,"models"),j=(fs.existsSync(i)||fs.mkdirSync(i),path.join(i,fileName+".js"));let e=fs.readFileSync(path.join(__dirname,"sample-schema.js"),"utf8");e=e.replace(/\${fileContent}/g,fileName),newSchemaFileCreate(j,e,fileName)}else if("make:apis"===command){const l=process.argv.find(e=>e.startsWith("--url=")),m=process.argv.find(e=>e.startsWith("--schema=")),n=(-1===l&&-1===m||void 0===l&&void 0===m?(console.error(clc.bgRed("Missing parameters. Please provide --url=<api_url> and --schema=<schema_name>.")),process.exit(1)):-1===l||void 0===l?(console.error(clc.bgRed("Missing parameters. Please provide --url=<api_url>.")),process.exit(1)):-1!==m&&void 0!==m||(console.error(clc.bgRed("Missing parameters. Please provide --schema=<schema_name>.")),process.exit(1)),l.split("=")[1]),o=m.split("=")[1],p=process.cwd(),q=path.join(p,"models",o+".js"),r=(fs.existsSync(q)||(console.error(clc.bgRed(`Schema file "${o}.js" does not exist in '/models/${o}.js'.`)),process.exit(1)),path.join(p,"routes")),s=path.join(p,"helpers"),t=path.join(r,"validationRequest"),u=path.join(t,""+fileName),v=path.join(p,"controllers");fs.existsSync(r)||fs.mkdirSync(r),fs.existsSync(t)||fs.mkdirSync(t),fs.existsSync(u)||fs.mkdirSync(u),fs.existsSync(v)||fs.mkdirSync(v),fs.existsSync(s)||fs.mkdirSync(s),controllerFileCreate(v,fileName,o),indexRouteFileCreate(r,fileName),validationFileCreate(q,o,u),newRouteFileCreate(r,fileName,n,o),helperFileCreate(s)}else if("make:auth"===command){const y=process.cwd(),z=path.join(y,"middleware"),A=(fs.existsSync(z)||fs.mkdirSync(z),path.join(y,"routes")),B=(fs.existsSync(A)||fs.mkdirSync(A),path.join(y,"controllers")),C=(fs.existsSync(B)||fs.mkdirSync(B),path.join(y,"config")),D=(fs.existsSync(C)||fs.mkdirSync(C),path.join(y,"models"));fs.existsSync(D)||fs.mkdirSync(D),authMiddlewareFileCreate(z),authRouteFileCreate(A),authControllersFileCreate(B),authConfigFileCreate(C),authSchemaFileCreate(D)}else console.error(clc.bgRed("Unknown command: "+command));async function newSchemaFileCreate(e,s,i){await fileExists(e)?console.error(clc.bgRed(` | ||
'models/${i}.js' file already exist!, Kindly delete existing and try again.`)):(fs.writeFileSync(e,s),console.log(clc.green(` | ||
'models/${i}.js' file created!`)))}async function indexRouteFileCreate(e,s){e=path.join(e,"IndexRoute.js");try{await fileExists(e)||(i=fs.readFileSync(path.join(__dirname,"sample-index-route.js"),"utf8"),fs.writeFileSync(e,i),console.log(clc.green("\nIndexRoute.js file created successfully!"))),data=await readFile(e,"utf8");var i,a=`router.use("/", ${s}Route);`,r=(updateIndexFile(e,a,data.indexOf("module.exports = router;")),`const ${s}Route = require("./${s}Route");`);updateIndexFile(e,r,data.indexOf("const router = express.Router();"))}catch(e){console.error(e)}}function fileExists(i){return new Promise((s,e)=>{fs.access(i,fs.constants.F_OK,e=>{s(!e)})})}function readFile(e,s){return new Promise((i,a)=>{fs.readFile(e,s,(e,s)=>{e?a(e):i(s)})})}function writeFile(e,a,r){return new Promise((s,i)=>{fs.writeFile(e,a,r,e=>{e?i(e):s()})})}async function updateIndexFile(e,s,i){try{var a=await readFile(e,"utf8");-1===i||a.includes(s)?console.log(clc.yellow(s+" already imported in IndexRoute.js!")):(await writeFile(e,a.slice(0,i)+s+"\n"+a.slice(i),"utf8"),console.log(clc.green(s+" imported into IndexRoute.js successfully!")))}catch(e){console.log(clc.red("An error occurred:",e))}}async function controllerFileCreate(a,r,t){a=path.join(a,r+"Controller.js");if(await fileExists(a))console.log(clc.bgRed(` | ||
'models/${i}.js' file created!`)))}async function indexRouteFileCreate(e,s){e=path.join(e,"IndexRoute.js");try{await fileExists(e)||(i=fs.readFileSync(path.join(__dirname,"sample-index-route.js"),"utf8"),fs.writeFileSync(e,i),console.log(clc.green("\nIndexRoute.js file created successfully!"))),data=await readFile(e,"utf8");var i,a=`router.use("/", ${s}Route);`,r=(updateIndexFile(e,a,data.indexOf("module.exports = router;")),data=await readFile(e,"utf8"),`const ${s}Route = require("./${s}Route");`);updateIndexFile(e,r,data.indexOf("const router = express.Router();"))}catch(e){console.error(e)}}function fileExists(i){return new Promise((s,e)=>{fs.access(i,fs.constants.F_OK,e=>{s(!e)})})}function readFile(e,s){return new Promise((i,a)=>{fs.readFile(e,s,(e,s)=>{e?a(e):i(s)})})}function writeFile(e,a,r){return new Promise((s,i)=>{fs.writeFile(e,a,r,e=>{e?i(e):s()})})}async function updateIndexFile(e,s,i){try{var a=await readFile(e,"utf8");-1===i||a.includes(s)?console.log(clc.yellow(s+" already imported in IndexRoute.js!")):(await writeFile(e,a.slice(0,i)+s+"\n"+a.slice(i),"utf8"),console.log(clc.green(s+" imported into IndexRoute.js successfully!")))}catch(e){console.log(clc.red("An error occurred:",e))}}async function controllerFileCreate(a,r,t){a=path.join(a,r+"Controller.js");if(await fileExists(a))console.log(clc.bgRed(` | ||
${r}Controller.js file already exist!`));else{let e=fs.readFileSync(path.join(__dirname,"sample-controller.js"),"utf8"),s=(e=(e=e.replace(/\${fileName}/g,r)).replace(/\${schemaName}/g,t),"["),i="{";var l,o=process.cwd(),o=path.join(o,"models",t+".js"),t=loadSchemaFromFile(o),o=searchableFields(t);for(l of o)s+=` | ||
@@ -6,0 +6,0 @@ { ${l}: { $regex: searchRgx, $options: "i" } },`,i+=` |
{ | ||
"name": "nodejs-backpack", | ||
"version": "2.0.6", | ||
"version": "2.0.7", | ||
"description": "NodeJs Backpack is a powerful tool designed to simplify the development of Node.js REST APIs with ease and precision. It provides a streamlined workflow by automatically generating Mongoose schemas and handling the necessary configurations in your project. With just a few simple commands, NodeJs Backpack can create REST API components such as controllers and routes, while also ensuring route validation is implemented flawlessly. Additionally, NodeJs Backpack offers a command to generate sample files, providing a solid starting point for your project. With NodeJs Backpack, you can quickly set up your project and focus on building your APIs, letting the tool handle the repetitive tasks and allowing you to follow a smooth development process.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1982393
983