Beau/bin/beau

189 lines
3.9 KiB
JavaScript
Executable File

#!/usr/bin/env node
const program = require('commander');
const process = require('process');
const Beau = require('../src/beau');
const yaml = require('js-yaml');
const fs = require('fs');
const { Line, Spinner } = require('clui');
const clc = require('cli-color');
const jsome = require('jsome');
const dotenv = require('dotenv');
const updateNotifier = require('update-notifier');
const package = require('../package.json');
updateNotifier({ pkg: package }).notify();
program.version(package.version);
program
.command('request <alias>')
.option(
'-c --config <config>',
'Specify your request config file. Defaults to beau.yml in the current directory.',
'beau.yml'
)
.option(
'--verbose',
'Show all the information available on the current request.',
false
)
.option('--no-format', 'Return the text without any special formatting.')
.action(async (alias, { config, format, verbose }) => {
const beau = loadConfig(config);
let spinner;
if (format) {
spinner = new Spinner(clc.yellow(`Requesting: ${alias}`));
spinner.start();
}
try {
let res = await beau.requests.execByAlias(alias);
let { status, headers, body } = res.response;
let { endpoint } = res.request;
if (format) {
spinner.stop();
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();
if (verbose) {
jsome(res);
} else {
jsome(body);
}
} else {
console.log(status);
console.log(endpoint);
console.log(JSON.stringify(headers));
console.log(JSON.stringify(body));
}
process.exit(0);
} catch (err) {
new Line().output();
console.error(err.message);
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
.command('init')
.option(
'-e --endpoint <endpoint>',
'Allows you to set the default endpoint',
null
)
.action(({ endpoint }) => {
const newFile = `# Beau.yml
version: 1${
endpoint === null
? `
# endpoint: http://example.com
`
: `
endpoint: ${endpoint}
`
}
# defaults:
# params:
# userId: 25
# GET /profile: profile
# GET /posts:
# alias: posts
# params:
# order: ASC
# POST /profile:
# alias: save-profile
# headers:
# authentication: Bearer token
# payload:
# name: David
# lastname: Diaz
`;
if (!fs.existsSync('beau.yml')) {
fs.writeFileSync('beau.yml', newFile);
console.info('beau.yml created!');
} else {
console.error('beau.yml already exists.');
}
});
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'));
const env = dotenv.config().parsed || {};
return new Beau(config, env);
}