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.
This commit is contained in:
David Diaz 2017-12-06 12:42:16 -06:00
parent 6a3e0e136b
commit 35983addf7
3 changed files with 97 additions and 127 deletions

175
bin/beau
View File

@ -10,116 +10,38 @@ const jsome = require('jsome');
const package = require('../package.json'); const package = require('../package.json');
program.version(package.version);
program program
.version(package.version) .command('request <alias>')
.usage(`[options] -r <Request Alias>`)
.option( .option(
'-r, --request [request]', '-c --config <config>',
`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]',
'Specify your request config file. Defaults to beau.yml in the current directory.', 'Specify your request config file. Defaults to beau.yml in the current directory.',
'beau.yml' 'beau.yml'
) )
.option('-l, --list', `List all requests in the config file.`)
.option('-L, --clean-list', `List all requests with no formatting.`)
.option( .option(
'-t, --truncate [length]', '-v --verbose',
`Truncate the content to the given length` '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)) { if (format) {
console.error(`The config file, ${program.config} was not found.`); spinner = new Spinner(clc.yellow(`Requesting: ${alias}`));
process.exit(1); spinner.start();
} }
const config = yaml.safeLoad(fs.readFileSync(program.config, 'utf-8')); try {
const beau = new Beau(config); let res = await beau.requests.execByAlias(alias);
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 (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 => {
let { status, headers, body } = res.response; let { status, headers, body } = res.response;
let { endpoint } = res.request; let { endpoint } = res.request;
console.log(status); if (format) {
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 (program.request) {
const spinner = new Spinner(clc.yellow(`Requesting: ${program.request}`));
spinner.start();
beau.requests
.execByAlias(program.request)
.then(res => {
spinner.stop(); spinner.stop();
let { status, body } = res.response;
let { endpoint } = res.request;
status = status.toString().startsWith(2) status = status.toString().startsWith(2)
? clc.green(status) ? clc.green(status)
: clc.red(status); : clc.red(status);
@ -138,19 +60,72 @@ if (program.request) {
new Line().output(); new Line().output();
let maxLength = +(program.truncate || res.length); if (verbose) {
if (program.verbose) {
jsome(res); jsome(res);
} else { } else {
jsome(body); jsome(body);
} }
} else {
console.log(status);
console.log(endpoint);
console.log(JSON.stringify(headers));
console.log(JSON.stringify(body));
}
process.exit(0); process.exit(0);
}) } catch (err) {
.catch(err => {
new Line().output(); new Line().output();
console.error(err); console.error(err);
process.exit(1); process.exit(1);
}
});
program
.command('list')
.option(
'-c --config <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);
}

11
package-lock.json generated
View File

@ -433,9 +433,9 @@
"integrity": "sha1-k4NwpXtKUd6ix3wV1cX9+JUWQAk=" "integrity": "sha1-k4NwpXtKUd6ix3wV1cX9+JUWQAk="
}, },
"commander": { "commander": {
"version": "2.11.0", "version": "2.12.2",
"resolved": "https://registry.npmjs.org/commander/-/commander-2.11.0.tgz", "resolved": "https://registry.npmjs.org/commander/-/commander-2.12.2.tgz",
"integrity": "sha512-b0553uYA5YAEGgyYIGYROzKQ7X5RAqedkfjiZxwi0kL1g3bOaBNNZfYkzt/CL0umgD5wc9Jec2FbB98CjkMRvQ==" "integrity": "sha512-BFnaq5ZOGcDN7FlrtBT4xxkgIToalIIxwjxLWVJ8bGTpe1LroqMiqQXdA7ygc7CRvaYS+9zfPGFnJqFSayx+AA=="
}, },
"concat-map": { "concat-map": {
"version": "0.0.1", "version": "0.0.1",
@ -1649,11 +1649,6 @@
"integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==",
"dev": true "dev": true
}, },
"minimist": {
"version": "0.0.10",
"resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.10.tgz",
"integrity": "sha1-3j+YVD2/lggr5IrRoMfNqDYwHc8="
},
"mkdirp": { "mkdirp": {
"version": "0.5.1", "version": "0.5.1",
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz",

View File

@ -12,7 +12,7 @@
"dependencies": { "dependencies": {
"cli-color": "^1.1.0", "cli-color": "^1.1.0",
"clui": "^0.3.1", "clui": "^0.3.1",
"commander": "^2.9.0", "commander": "^2.12.2",
"js-yaml": "^3.7.0", "js-yaml": "^3.7.0",
"jsome": "^2.3.26", "jsome": "^2.3.26",
"request": "^2.83.0", "request": "^2.83.0",