Added format-less versions of list and request to the command line. This

makes it easier to add direct support for beau on external applications.
This commit is contained in:
David Diaz 2017-10-21 00:54:38 -06:00
parent 10623f6625
commit baa4a4eafc
1 changed files with 67 additions and 19 deletions

View File

@ -1,5 +1,6 @@
#!/usr/bin/env node #!/usr/bin/env node
const program = require('commander'); const program = require('commander');
const process = require('process');
const Beau = require('../beau'); const Beau = require('../beau');
const yaml = require('js-yaml'); const yaml = require('js-yaml');
const fs = require('fs'); const fs = require('fs');
@ -12,11 +13,29 @@ const package = require('../package.json');
program program
.version(package.version) .version(package.version)
.usage(`[options] -r <Request Alias>`) .usage(`[options] -r <Request Alias>`)
.option('-r, --request [request]', `The alias for the request you'd like to trigger.`) .option(
.option('-v, --verbose', `Show all the information related to the current request and it's response.`) '-r, --request [request]',
.option('-c, --config [file]', 'Specify your request config file. Defaults to beau.yml in the current directory.', 'beau.yml') `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.',
'beau.yml'
)
.option('-l, --list', `List all requests in the config file.`) .option('-l, --list', `List all requests in the config file.`)
.option('-t, --truncate [length]', `Truncate the content to the given length`) .option('-L, --clean-list', `List all requests with no formatting.`)
.option(
'-t, --truncate [length]',
`Truncate the content to the given length`
)
.parse(process.argv); .parse(process.argv);
if (!fs.existsSync(program.config)) { if (!fs.existsSync(program.config)) {
@ -27,20 +46,32 @@ if (!fs.existsSync(program.config)) {
const config = yaml.safeLoad(fs.readFileSync(program.config, 'utf-8')); const config = yaml.safeLoad(fs.readFileSync(program.config, 'utf-8'));
const beau = new Beau(config); const beau = new Beau(config);
if (typeof program.list === 'undefined' && if (
typeof program.request === 'undefined') { typeof program.list === 'undefined' &&
typeof program.request === 'undefined' &&
typeof program.cleanList === 'undefined' &&
typeof program.cleanRequest === 'undefined'
) {
program.help(); program.help();
} }
if (program.cleanList) {
beau.requests.list.forEach(({ VERB, ALIAS, ENDPOINT }) => {
console.log(`${VERB}\t${ALIAS}\t${ENDPOINT}`);
});
}
if (program.list) { if (program.list) {
new Line().padding(2) new Line()
.padding(2)
.column('HTTP Verb', 20, [clc.cyan]) .column('HTTP Verb', 20, [clc.cyan])
.column('Alias', 30, [clc.cyan]) .column('Alias', 30, [clc.cyan])
.column('Endpoint', 20, [clc.cyan]) .column('Endpoint', 20, [clc.cyan])
.output(); .output();
beau.requests.list.forEach(({VERB, ALIAS, ENDPOINT}) => beau.requests.list.forEach(({ VERB, ALIAS, ENDPOINT }) =>
new Line().padding(2) new Line()
.padding(2)
.column(VERB, 20, [clc.yellow]) .column(VERB, 20, [clc.yellow])
.column(ALIAS, 30, [clc.yellow]) .column(ALIAS, 30, [clc.yellow])
.column(ENDPOINT) .column(ENDPOINT)
@ -50,6 +81,18 @@ if (program.list) {
new Line().output(); new Line().output();
} }
if (program.cleanRequest) {
beau.requests.execByAlias(program.cleanRequest).then(res => {
let {status, headers, body} = res.response;
let {endpoint} = res.request;
process.stdout.write(status + '\n');
process.stdout.write(endpoint + '\n');
process.stdout.write(JSON.stringify(headers) + '\n');
process.stdout.write(JSON.stringify(body) + '\n');
});
}
if (program.request) { if (program.request) {
const spinner = new Spinner(clc.yellow(`Requesting: ${program.request}`)); const spinner = new Spinner(clc.yellow(`Requesting: ${program.request}`));
@ -63,14 +106,18 @@ if (program.request) {
let { status, body } = res.response; let { status, body } = res.response;
let { endpoint } = res.request; let { endpoint } = res.request;
status = status.toString().startsWith(2) ? clc.green(status) : clc.red(status); status = status.toString().startsWith(2)
? clc.green(status)
: clc.red(status);
new Line().padding(2) new Line()
.padding(2)
.column('Status', 20, [clc.cyan]) .column('Status', 20, [clc.cyan])
.column('Endpoint', 20, [clc.cyan]) .column('Endpoint', 20, [clc.cyan])
.output(); .output();
new Line().padding(2) new Line()
.padding(2)
.column(status, 20) .column(status, 20)
.column(endpoint) .column(endpoint)
.output(); .output();
@ -87,7 +134,8 @@ if (program.request) {
} }
process.exit(0); process.exit(0);
}).catch(function(err) { })
.catch(function(err) {
new Line().output(); new Line().output();
console.error(err); console.error(err);
process.exit(1); process.exit(1);