From 35983addf7d6ded27b27b2dc87a3f7da63330a02 Mon Sep 17 00:00:00 2001 From: David Diaz Date: Wed, 6 Dec 2017 12:42:16 -0600 Subject: [PATCH] Refactored the cli. Started making use of commander's commands and actions. This changes how beau is invoked but I feel like it's a lot easier to work with now. --- bin/beau | 211 ++++++++++++++++++++-------------------------- package-lock.json | 11 +-- package.json | 2 +- 3 files changed, 97 insertions(+), 127 deletions(-) diff --git a/bin/beau b/bin/beau index c184a47..bb49676 100755 --- a/bin/beau +++ b/bin/beau @@ -10,147 +10,122 @@ const jsome = require('jsome'); const package = require('../package.json'); +program.version(package.version); + program - .version(package.version) - .usage(`[options] -r `) + .command('request ') .option( - '-r, --request [request]', - `The alias for the request you'd like to trigger.` - ) - .option( - '-R, --clean-request [request]', - `The alias for the request you'd like to trigger. This one doesn't format the output.` - ) - .option( - '-v, --verbose', - `Show all the information related to the current request and it's response.` - ) - .option( - '-c, --config [file]', + '-c --config ', 'Specify your request config file. Defaults to beau.yml in the current directory.', 'beau.yml' ) - .option('-l, --list', `List all requests in the config file.`) - .option('-L, --clean-list', `List all requests with no formatting.`) .option( - '-t, --truncate [length]', - `Truncate the content to the given length` + '-v --verbose', + 'Show all the information available on the current request.', + false ) - .parse(process.argv); + .option('--no-format', 'Return the text without any special formatting.') + .action(async (alias, { config, format, verbose }) => { + const beau = loadConfig(config); + let spinner; -if (!fs.existsSync(program.config)) { - console.error(`The config file, ${program.config} was not found.`); - process.exit(1); -} - -const config = yaml.safeLoad(fs.readFileSync(program.config, 'utf-8')); -const beau = new Beau(config); - -if ( - typeof program.list === 'undefined' && - typeof program.request === 'undefined' && - typeof program.cleanList === 'undefined' && - typeof program.cleanRequest === 'undefined' -) { - program.help(); -} - -if (program.cleanList) { - beau.requests.list.forEach(({ VERB, ALIAS, ENDPOINT, DOCUMENTATION }) => { - if (typeof DOCUMENTATION !== 'undefined') { - let { title, description } = DOCUMENTATION; - console.log( - `${VERB}\t${ALIAS}\t${ENDPOINT}\t${title}\t${description}` - ); - } else { - console.log(`${VERB}\t${ALIAS}\t${ENDPOINT}`); + if (format) { + spinner = new Spinner(clc.yellow(`Requesting: ${alias}`)); + spinner.start(); } - }); -} -if (program.list) { - new Line() - .padding(2) - .column('HTTP Verb', 20, [clc.cyan]) - .column('Alias', 30, [clc.cyan]) - .column('Endpoint', 20, [clc.cyan]) - .output(); - - beau.requests.list.forEach(({ VERB, ALIAS, ENDPOINT }) => - new Line() - .padding(2) - .column(VERB, 20, [clc.yellow]) - .column(ALIAS, 30, [clc.yellow]) - .column(ENDPOINT) - .output() - ); - - new Line().output(); -} - -if (program.cleanRequest) { - beau.requests - .execByAlias(program.cleanRequest) - .then(res => { + try { + let res = await beau.requests.execByAlias(alias); let { status, headers, body } = res.response; let { endpoint } = res.request; - console.log(status); - console.log(endpoint); - console.log(JSON.stringify(headers)); - console.log(JSON.stringify(body)); - }) - .catch(err => { - new Line().output(); - console.error(err); - process.exit(1); - }); -} + if (format) { + spinner.stop(); -if (program.request) { - const spinner = new Spinner(clc.yellow(`Requesting: ${program.request}`)); + status = status.toString().startsWith(2) + ? clc.green(status) + : clc.red(status); - spinner.start(); + new Line() + .padding(2) + .column('Status', 20, [clc.cyan]) + .column('Endpoint', 20, [clc.cyan]) + .output(); - beau.requests - .execByAlias(program.request) - .then(res => { - spinner.stop(); + new Line() + .padding(2) + .column(status, 20) + .column(endpoint) + .output(); - let { status, body } = res.response; - let { endpoint } = res.request; + new Line().output(); - status = status.toString().startsWith(2) - ? clc.green(status) - : clc.red(status); - - new Line() - .padding(2) - .column('Status', 20, [clc.cyan]) - .column('Endpoint', 20, [clc.cyan]) - .output(); - - new Line() - .padding(2) - .column(status, 20) - .column(endpoint) - .output(); - - new Line().output(); - - let maxLength = +(program.truncate || res.length); - - if (program.verbose) { - jsome(res); + if (verbose) { + jsome(res); + } else { + jsome(body); + } } else { - jsome(body); + console.log(status); + console.log(endpoint); + console.log(JSON.stringify(headers)); + console.log(JSON.stringify(body)); } process.exit(0); - }) - .catch(err => { + } catch (err) { new Line().output(); console.error(err); process.exit(1); - }); + } + }); + +program + .command('list') + .option( + '-c --config ', + 'Specify your request config file. Defaults to beau.yml in the current directory.', + 'beau.yml' + ) + .option('--no-format', 'Return the text without any special formatting.') + .action(({ config, format }) => { + const beau = loadConfig(config); + + if (format) { + new Line() + .padding(2) + .column('HTTP Verb', 20, [clc.cyan]) + .column('Alias', 30, [clc.cyan]) + .column('Endpoint', 20, [clc.cyan]) + .output(); + + beau.requests.list.forEach(({ VERB, ALIAS, ENDPOINT }) => + new Line() + .padding(2) + .column(VERB, 20, [clc.yellow]) + .column(ALIAS, 30, [clc.yellow]) + .column(ENDPOINT) + .output() + ); + + new Line().output(); + } else { + beau.requests.list.forEach(({ VERB, ALIAS, ENDPOINT }) => { + console.log(`${VERB}\t${ALIAS}\t${ENDPOINT}`); + }); + } + }); + +program.parse(process.argv); + +if (!program.args.length) program.help(); + +function loadConfig(configFile) { + if (!fs.existsSync(configFile)) { + console.error(`The config file, ${configFile} was not found.`); + process.exit(1); + } + + const config = yaml.safeLoad(fs.readFileSync(configFile, 'utf-8')); + return new Beau(config); } diff --git a/package-lock.json b/package-lock.json index 26374ee..8032478 100644 --- a/package-lock.json +++ b/package-lock.json @@ -433,9 +433,9 @@ "integrity": "sha1-k4NwpXtKUd6ix3wV1cX9+JUWQAk=" }, "commander": { - "version": "2.11.0", - "resolved": "https://registry.npmjs.org/commander/-/commander-2.11.0.tgz", - "integrity": "sha512-b0553uYA5YAEGgyYIGYROzKQ7X5RAqedkfjiZxwi0kL1g3bOaBNNZfYkzt/CL0umgD5wc9Jec2FbB98CjkMRvQ==" + "version": "2.12.2", + "resolved": "https://registry.npmjs.org/commander/-/commander-2.12.2.tgz", + "integrity": "sha512-BFnaq5ZOGcDN7FlrtBT4xxkgIToalIIxwjxLWVJ8bGTpe1LroqMiqQXdA7ygc7CRvaYS+9zfPGFnJqFSayx+AA==" }, "concat-map": { "version": "0.0.1", @@ -1649,11 +1649,6 @@ "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", "dev": true }, - "minimist": { - "version": "0.0.10", - "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.10.tgz", - "integrity": "sha1-3j+YVD2/lggr5IrRoMfNqDYwHc8=" - }, "mkdirp": { "version": "0.5.1", "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", diff --git a/package.json b/package.json index 19208ff..147d248 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "dependencies": { "cli-color": "^1.1.0", "clui": "^0.3.1", - "commander": "^2.9.0", + "commander": "^2.12.2", "js-yaml": "^3.7.0", "jsome": "^2.3.26", "request": "^2.83.0",