Skip to content

Commit

Permalink
fix(cli): make positional arguments required and handle errors in ser…
Browse files Browse the repository at this point in the history
…ve and bundle manually (#518)
  • Loading branch information
brushmate authored and RomanHotsiy committed May 31, 2018
1 parent 8e7f27b commit 370d08a
Showing 1 changed file with 27 additions and 14 deletions.
41 changes: 27 additions & 14 deletions cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const BUNDLES_DIR = dirname(require.resolve('redoc'));

/* tslint:disable-next-line */
YargsParser.command(
'serve [spec]',
'serve <spec>',
'start the server',
yargs => {
yargs.positional('spec', {
Expand Down Expand Up @@ -60,16 +60,22 @@ YargsParser.command(
return yargs;
},
async argv => {
await serve(argv.port, argv.spec, {
const config = {
ssr: argv.ssr,
watch: argv.watch,
templateFileName: argv.template,
redocOptions: argv.options || {},
});
};

try {
await serve(argv.port, argv.spec, config);
} catch (e) {
handleError(e);
}
},
)
.command(
'bundle [spec]',
'bundle <spec>',
'bundle spec into zero-dependency HTML-file',
yargs => {
yargs.positional('spec', {
Expand Down Expand Up @@ -99,16 +105,22 @@ YargsParser.command(
return yargs;
},
async argv => {
await bundle(argv.spec, {
const config = {
ssr: true,
output: argv.o,
cdn: argv.cdn,
title: argv.title,
templateFileName: argv.template,
redocOptions: argv.options || {},
});
};

try {
await bundle(argv.spec, config);
} catch (e) {
handleError(e);
}
},
)
)
.demandCommand()
.options('t', {
alias: 'template',
Expand All @@ -117,10 +129,6 @@ YargsParser.command(
})
.options('options', {
describe: 'ReDoc options, use dot notation, e.g. options.nativeScrollbars',
})
.fail((message, error) => {
console.log(error.stack);
process.exit(1);
}).argv;

async function serve(port: number, pathToSpec: string, options: Options = {}) {
Expand Down Expand Up @@ -229,13 +237,13 @@ async function getPageHTML(
ssr
? 'hydrate(__redoc_state, container);'
: `init("spec.json", ${JSON.stringify(redocOptions)}, container)`
};
};
</script>`,
redocHead: ssr
? (cdn
? '<script src="https://unpkg.com/redoc@next/bundles/redoc.standalone.js"></script>'
: `<script>${redocStandaloneSrc}</script>`) + css
? '<script src="https://unpkg.com/redoc@next/bundles/redoc.standalone.js"></script>'
: `<script>${redocStandaloneSrc}</script>`) + css
: '<script src="redoc.standalone.js"></script>',
title,
});
Expand Down Expand Up @@ -296,3 +304,8 @@ function isURL(str: string): boolean {
function escapeUnicode(str) {
return str.replace(/\u2028|\u2029/g, m => '\\u202' + (m === '\u2028' ? '8' : '9'));
}

function handleError(error: Error) {
console.error(error.stack);
process.exit(1);
}

0 comments on commit 370d08a

Please sign in to comment.