Skip to content

Commit

Permalink
Version 2.3.0 📦
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafashykh committed Jun 8, 2021
1 parent bebab74 commit 973ba8a
Show file tree
Hide file tree
Showing 11 changed files with 249 additions and 12 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ps-cli/express",
"version": "2.2.0",
"version": "2.3.0",
"description": "Express Boilerplate Boilerplate based upto express-generator",
"main": "src/index.js",
"scripts": {
Expand All @@ -22,6 +22,7 @@
"enquirer": "^2.3.6",
"figlet": "^1.5.0",
"ora": "^5.4.0",
"replace-in-file": "^6.2.0",
"shelljs": "^0.8.4",
"update-notifier": "^5.1.0"
},
Expand Down
50 changes: 42 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,24 @@ const store = require('./utils/store');
(async () => {
program.name('@ps-cli/express');

// Help
program.helpOption('-h, --help', 'Get the help info');
program.addHelpText('before', `\n`);
program.addHelpText('after', data.helpInfo);

// Global Options
program
.option('-v, --version', `${packJson.version}`)
.description('Gives the verison of CLI')
.action(() => {
console.log(`${chalk.yellow(`version`)}: ${packJson.version}`);
process.exit(0);
.action((options) => {
if (options.version) {
console.log(`${chalk.yellow(`version`)}: ${packJson.version}`);
process.exit(0);
}
})


// Commads
// New Project Sequence
program
.command(`new <name>`)
.option('-i, --install', 'intsall all dependantcies')
Expand All @@ -38,12 +45,39 @@ const store = require('./utils/store');
await jsWorkflows.new();
})

// Help
program.helpOption('-h, --help', 'Get the help info');
program.addHelpText('before', `\n`);
program.addHelpText('after', data.helpInfo);
// Component Generatiion
const generate = program
.command('generate')
.alias('g')

// Route
generate
.command('route <name>')
.alias('r')
.option('-c, --crud', 'Generates CRUDS as well')
.helpOption('-h, --help', 'Help in options')
.description('Generate a route')
.action(async (name, options) => {
store.set({ vars: { name }, ...options })
await jsWorkflows.route();
})

generate
.command('service <name>')
.alias('s')
.helpOption('-h, --help', 'Help in options')
.description('Generate a service')
.action(async (name, options) => {
console.log('[SERVICE]');
})

program.parse();


if (program.args.length === 0) {
program.outputHelp();
}

})();


Expand Down
3 changes: 3 additions & 0 deletions src/template/js/new/src/@services/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
// Add services here
};
108 changes: 108 additions & 0 deletions src/template/js/route-crud/controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
const { handleError } = require('../../@shared/utils');


/**
* @description To get all {{name}}
* @param {*} req
* @param {*} res
* @param {*} next
* @returns {Array<{{name}}>} {{name}}
*/
exports.getAll = (req, res, next) => {
try {
return res.status(200).send({
endpoint: '/v1/{{name}}',
method: req.method,
description: 'Dummy endpoint to get all {{name}}',
{{name}}: []
});
} catch (err) {
return handleError(res, err);
}
}


/**
* @description To get {{name}} by id
* @param {*} req
* @param {*} res
* @param {*} next
* @returns {*} {{name}}
*/
exports.getById = (req, res, next) => {
try {
const { id } = req.params;
return res.status(200).send({
endpoint: '/v1/{{name}}/:id',
method: req.method,
description: 'Dummy endpoint to get {{name}} with id',
{{name}}: {id}
});
} catch (err) {
return handleError(res, err);
}
}


/**
* @description Adds a {{name}}
* @param {*} req
* @param {*} res
* @param {*} next
* @returns {*} Added {{name}}
*/
exports.add = (req, res, next) => {
try {
return res.status(201).send({
endpoint: '/v1/{{name}}',
method: req.method,
description: 'Dummy endpoint to add {{name}}',
{{name}}: req.body
});
} catch (err) {
return handleError(res, err);
}
}

/**
* @description Updates a {{name}}
* @param {*} req
* @param {*} res
* @param {*} next
* @returns {*} update {{name}}
*/
exports.patch = (req, res, next) => {
try {
const { id } = req.params;
return res.status(200).send({
endpoint: '/v1/{{name}}/:id',
method: req.method,
description: 'Dummy endpoint to update {{name}}',
{{name}}: { id, ...req.body }
});
} catch (err) {
return handleError(res, err);
}
}


/**
* @description To delete a record
* @param {*} req
* @param {*} res
* @param {*} next
* @returns {*} http response
*/
exports.delete = (req, res, next) => {
try {
const { id } = req.params;
return res.status(200).send({
endpoint: '/v1/{{name}}/:id',
method: req.method,
description: 'Dummy endpoint to update {{name}}',
{{name}}: {id}
});
} catch (err) {
return handleError(res, err);
}
}
13 changes: 13 additions & 0 deletions src/template/js/route-crud/routes-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const express = require('express');
const router = express.Router();
const controller = require('./controller');

// Add all routes here
router.get('', controller.getAll);
router.get('/:id', controller.getById);
router.post('', controller.add);
router.patch('/:id', controller.patch);
router.delete('/:id', controller.delete);


module.exports = router;
12 changes: 12 additions & 0 deletions src/template/js/route/controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const { handleError } = require('../../@shared/utils');

exports.default = (req, res, next) => {
try {
return res.status(200).send({
endpoint: '/v1/{{name}}',
description: 'auto-generate with @ps-cli@express'
});
} catch (err) {
return handleError(res, err);
}
}
9 changes: 9 additions & 0 deletions src/template/js/route/routes-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const express = require('express');
const router = express.Router();
const controller = require('./controller');

// Add all routes here
router.get('', controller.default);


module.exports = router;
3 changes: 3 additions & 0 deletions src/template/js/service/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = () => {

}
4 changes: 3 additions & 1 deletion src/utils/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ const shell = require('shelljs');
const ask = require('./ask');
const chalk = require('chalk');
const store = require('./store');
const path = require('path');

module.exports = async () => {
let git = !!store.get('git');
let name = store.get('appName');
const vars = store.get('vars');
const { name } = vars

// Confirming
if (!git) {
Expand Down
2 changes: 1 addition & 1 deletion src/workflows/js/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
new: require('./new'),
model: require('./route'),
route: require('./route'),
model: require('./model'),
service: require('./service'),
}
54 changes: 53 additions & 1 deletion src/workflows/js/route.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,55 @@
module.exports = () => {
const shell = require('shelljs');
const path = require('path');
const store = require('../../utils/store');
const copy = require('copy-template-dir');
const replace = require('replace-in-file');
const chalk = require('chalk');
const ask = require('../../utils/ask');


/**
* @description New Route workflow
*/
module.exports = async () => {
const vars = store.get('vars');
let crud = store.get('crud')


if (!crud) {
console.log('\n');
const _crud = await ask({name: 'CRUD', message: 'Do want to generate CRUDs ?', hint: '(yes|no)', initial: 'no', choices: ['yes', 'no'] });
if (_crud === 'yes') crud = true;
}

let inDir = path.join(__dirname, `../../template/js/route`);
let outDir = path.join(process.cwd(), './src/@routes/', `${vars.name}`);

if (crud) {
inDir = path.join(__dirname, `../../template/js/route-crud`);
outDir = path.join(process.cwd(), './src/@routes/', `${vars.name}`);
}

if (!shell.test('-e', path.join(process.cwd(), './package.json'))) {
shell.echo('The command can only be run in @root folder having package.json');
process.exit(0);
}

if (shell.test('-e', outDir)){
shell.echo('\nThe route already exsists\n');
process.exit(0);
}


copy(inDir, outDir, vars, async (err, createdFiles) => {
if (err) throw err;
createdFiles.forEach((filePath) => console.log(`${chalk.green("[Created]:")} ${filePath.replace(process.cwd(), '')}`));
});


replace({
files: path.join(`${outDir}`, '../index.js'),
from: `module.exports = (app) => {`,
to: `module.exports = (app) => {
app.use('/v1/${vars.name}', require('./${vars.name}/routes-config'));`
})
}

0 comments on commit 973ba8a

Please sign in to comment.