fix(cli): make positional arguments required and handle errors in serve and bundle manually (#518)

This commit is contained in:
brushmate 2018-05-31 11:56:35 +02:00 committed by Roman Hotsiy
parent 8e7f27b16d
commit 370d08aa1a

View File

@ -32,7 +32,7 @@ const BUNDLES_DIR = dirname(require.resolve('redoc'));
/* tslint:disable-next-line */ /* tslint:disable-next-line */
YargsParser.command( YargsParser.command(
'serve [spec]', 'serve <spec>',
'start the server', 'start the server',
yargs => { yargs => {
yargs.positional('spec', { yargs.positional('spec', {
@ -60,16 +60,22 @@ YargsParser.command(
return yargs; return yargs;
}, },
async argv => { async argv => {
await serve(argv.port, argv.spec, { const config = {
ssr: argv.ssr, ssr: argv.ssr,
watch: argv.watch, watch: argv.watch,
templateFileName: argv.template, templateFileName: argv.template,
redocOptions: argv.options || {}, redocOptions: argv.options || {},
}); };
try {
await serve(argv.port, argv.spec, config);
} catch (e) {
handleError(e);
}
}, },
) )
.command( .command(
'bundle [spec]', 'bundle <spec>',
'bundle spec into zero-dependency HTML-file', 'bundle spec into zero-dependency HTML-file',
yargs => { yargs => {
yargs.positional('spec', { yargs.positional('spec', {
@ -99,16 +105,22 @@ YargsParser.command(
return yargs; return yargs;
}, },
async argv => { async argv => {
await bundle(argv.spec, { const config = {
ssr: true, ssr: true,
output: argv.o, output: argv.o,
cdn: argv.cdn, cdn: argv.cdn,
title: argv.title, title: argv.title,
templateFileName: argv.template, templateFileName: argv.template,
redocOptions: argv.options || {}, redocOptions: argv.options || {},
}); };
try {
await bundle(argv.spec, config);
} catch (e) {
handleError(e);
}
}, },
) )
.demandCommand() .demandCommand()
.options('t', { .options('t', {
alias: 'template', alias: 'template',
@ -117,10 +129,6 @@ YargsParser.command(
}) })
.options('options', { .options('options', {
describe: 'ReDoc options, use dot notation, e.g. options.nativeScrollbars', describe: 'ReDoc options, use dot notation, e.g. options.nativeScrollbars',
})
.fail((message, error) => {
console.log(error.stack);
process.exit(1);
}).argv; }).argv;
async function serve(port: number, pathToSpec: string, options: Options = {}) { async function serve(port: number, pathToSpec: string, options: Options = {}) {
@ -296,3 +304,8 @@ function isURL(str: string): boolean {
function escapeUnicode(str) { function escapeUnicode(str) {
return str.replace(/\u2028|\u2029/g, m => '\\u202' + (m === '\u2028' ? '8' : '9')); return str.replace(/\u2028|\u2029/g, m => '\\u202' + (m === '\u2028' ? '8' : '9'));
} }
function handleError(error: Error) {
console.error(error.stack);
process.exit(1);
}