UNPKG

nodebasecli

Version:

Cli to create modules for nodebase

233 lines (209 loc) 9.12 kB
#!/usr/bin/env node import fs from 'fs-extra'; import chalk from 'chalk' import { exec } from 'child_process'; // make templates import controllerData from './templates/make/controller.js' import modelData from './templates/make/model.js' import middlewareData from './templates/make/middleware.js' import mailData from './templates/make/mail.js' import mailTemplateData from './templates/make/mailTemplate.js' // install templates import socketController from './templates/install/socketController.js' import socketServer from './templates/install/socketServer.js' // add templates import awsS3Data from './templates/add/AwsS3.js' import awsSqsData from './templates/add/AwsSqs.js' import awsSnsData from './templates/add/AwsSns.js' import awsSesData from './templates/add/AwsSes.js' import awsStsData from './templates/add/AwsSts.js' function camelToSnake(camelCaseString) { return camelCaseString.replace(/([a-z])([A-Z])/g, '$1_$2').toLowerCase(); } const awsPath = `./utils/aws`; function pluralize(string) { // Handle special cases const irregularPlurals = { "person": "people", "man": "men", "child": "children", "foot": "feet", "tooth": "teeth", "mouse": "mice", // Add more irregular plurals as needed }; if (irregularPlurals[string]) { return irregularPlurals[string]; } // Handle common cases if (string.endsWith('y') && !string.endsWith('ay') && !string.endsWith('ey') && !string.endsWith('iy') && !string.endsWith('oy') && !string.endsWith('uy')) { return string.slice(0, -1) + 'ies'; } if (string.endsWith('s') || string.endsWith('x') || string.endsWith('z') || string.endsWith('ch') || string.endsWith('sh')) { return string + 'es'; } return string + 's'; } const installAws = async () => { const data = await fs.readFile('./package.json', 'utf-8') const packageData = JSON.parse(data) const dependencies = Object.entries(packageData.dependencies).map(([key]) => key) if (!dependencies.includes('aws-sdk')) { console.log(''); console.log(chalk.green(`Installing aws sdk...`)); await exec('npm i aws-sdk').on('close', () => { console.log(''); console.log(chalk.green(`Aws sqs installed`)); }); } } const RunCommand = async (command, fileName) => { if (command === 'undefined' || command === undefined) { console.log(chalk.red('Please specify command')) } else if (fileName === 'undefined' || fileName === undefined) { console.log(chalk.red('Please specify file name')) } else { const command_first = command.split(':')[0] const command_second = command.split(':')[1] if (command_first === 'make') { switch (command_second) { case 'controller': const controllerFileLength = fileName.split('/').length let controllerFileArray = []; for (let index = 0; index <= controllerFileLength; index++) { controllerFileArray.push('../') } const pathWithOutFileName = fileName.split('/').slice(0, -1).join('/') const hasFolder = await fs.pathExists(`./app/controllers/${pathWithOutFileName}`) if (!hasFolder) await fs.ensureDir(`./app/controllers/${pathWithOutFileName}`) const controllerFile = controllerData(controllerFileArray.join('')) await fs.writeFile(`./app/controllers/${fileName}.js`, controllerFile); console.log(''); console.log(chalk.green(`Controller created: ${fileName}`)); break case 'model': const fileNameOnly = fileName.split('/').pop() const fileLength = fileName.split('/').length let fileArray = []; for (let index = 0; index <= fileLength; index++) { fileArray.push('../') } const underscore_fileName = pluralize(camelToSnake(fileNameOnly)) const modelFile = modelData(underscore_fileName, fileNameOnly, fileArray.join('')) const modelPathWithOutFileName = fileName.split('/').slice(0, -1).join('/') const hasModelFolder = await fs.pathExists(`./app/models/${modelPathWithOutFileName}`) if (!hasModelFolder) await fs.ensureDir(`./app/models/${modelPathWithOutFileName}`) await fs.writeFile(`./app/models/${fileName}.js`, modelFile); console.log(''); console.log(chalk.green(`Model created: ${fileName}`)); break case 'mail': const fileMailNameOnly = fileName.split('/').pop() const fileMailLength = fileName.split('/').length let fileMailArray = []; for (let index = 0; index <= fileMailLength; index++) { fileMailArray.push('../') } const underscore_mail_fileName = camelToSnake(fileMailNameOnly) const mailFile = mailData(underscore_mail_fileName, fileMailArray.join('')) const mailPathWithOutFileName = fileName.split('/').slice(0, -1).join('/') const hasMailFolder = await fs.pathExists(`./app/mail/${mailPathWithOutFileName}`) if (!hasMailFolder) await fs.ensureDir(`./app/mail/${mailPathWithOutFileName}`) await fs.writeFile(`./app/mail/${fileName}.js`, mailFile); const hasMailTemplateFolder = await fs.pathExists(`./views/email`) if (!hasMailTemplateFolder) await fs.ensureDir(`./views/email`) await fs.writeFile(`./views/email/${underscore_mail_fileName}.hbs`, mailTemplateData); console.log(''); console.log( chalk.green(`Mail created: ${fileName}`) ); break case 'middleware': const middlewarePathWithOutFileName = fileName.split('/').slice(0, -1).join('/') const hasMiddlewareFolder = await fs.pathExists(`./app/middlewares/${middlewarePathWithOutFileName}`) if (!hasMiddlewareFolder) await fs.ensureDir(`./app/middlewares/${middlewarePathWithOutFileName}`) await fs.writeFile(`./app/middlewares/${fileName}.js`, middlewareData); console.log(''); console.log( chalk.green(`Middleware created: ${fileName}`) ); break default: console.log( chalk.red('Command not found') ) } } else if (command_first === 'app') { if (command_second === 'install') { switch (fileName) { case 'socket': console.log(''); console.log(chalk.green(`Installing socket...`)); const installation = exec('npm i socket.io'); installation.on('close', async (code) => { console.log(''); console.log(chalk.green(`Socket installed`)); try { await fs.appendFile('./index.js', socketServer) const hasFolder = await fs.pathExists(`./app/controllers/socket`) if (!hasFolder) await fs.ensureDir(`./app/controllers/socket`) await fs.writeFile(`./app/controllers/socket/socketController.js`, socketController); console.log(''); console.log(chalk.green(`Socket Controller created at: ./app/controllers/socket`)); } catch (e) { console.log(chalk.red(e)) } }) break default: console.log( chalk.red('Command not found') ) } } else if (command_second === 'add') { switch (fileName) { case 'sqs': console.log(''); console.log(chalk.green(`Creating Aws sqs file...`)); if (!await fs.pathExists(awsPath)) await fs.ensureDir(awsPath) await fs.writeFile(`${awsPath}/aws-sqs.js`, awsSqsData); console.log(''); console.log( chalk.green(`Aws sqs created at: ${awsPath}`) ); installAws() break case 'sns': console.log(''); console.log(chalk.green(`Creating Aws sns file...`)); if (!await fs.pathExists(awsPath)) await fs.ensureDir(awsPath) await fs.writeFile( `${awsPath}/aws-sns.js`, awsSnsData ); console.log(''); console.log( chalk.green(`Aws sns created at: ${awsPath}`) ); installAws() break case 's3': console.log(''); console.log(chalk.green(`Creating Aws s3 file...`)); if (!await fs.pathExists(awsPath)) await fs.ensureDir(awsPath) await fs.writeFile( `${awsPath}/aws-s3.js`, awsS3Data ); console.log(''); console.log( chalk.green(`Aws s3 created at: ${awsPath}`) ); installAws() break case 'ses': console.log(''); console.log(chalk.green(`Creating Aws ses file...`)); if (!await fs.pathExists(awsPath)) await fs.ensureDir(awsPath) await fs.writeFile( `${awsPath}/aws-ses.js`, awsSesData ); console.log(''); console.log( chalk.green(`Aws ses created at: ${awsPath}`) ); installAws() break case 'sts': console.log(''); console.log(chalk.green(`Creating Aws sts file...`)); if (!await fs.pathExists(awsPath)) await fs.ensureDir(awsPath) await fs.writeFile( `${awsPath}/aws-sts.js`, awsStsData ); console.log(''); console.log( chalk.green(`Aws sts created at: ${awsPath}`) ); installAws() break default: console.log( chalk.red('Command not found') ) } } else { console.log( chalk.red('Command not found') ) } } else { console.log( chalk.red('Command not found') ) } } } const command = process.argv[2] const fileName = process.argv[3] RunCommand(command, fileName)