Skip to content

Commit 370d08a

Browse files
brushmateRomanHotsiy
authored andcommitted
fix(cli): make positional arguments required and handle errors in serve and bundle manually (#518)
1 parent 8e7f27b commit 370d08a

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

cli/index.ts

+27-14
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const BUNDLES_DIR = dirname(require.resolve('redoc'));
3232

3333
/* tslint:disable-next-line */
3434
YargsParser.command(
35-
'serve [spec]',
35+
'serve <spec>',
3636
'start the server',
3737
yargs => {
3838
yargs.positional('spec', {
@@ -60,16 +60,22 @@ YargsParser.command(
6060
return yargs;
6161
},
6262
async argv => {
63-
await serve(argv.port, argv.spec, {
63+
const config = {
6464
ssr: argv.ssr,
6565
watch: argv.watch,
6666
templateFileName: argv.template,
6767
redocOptions: argv.options || {},
68-
});
68+
};
69+
70+
try {
71+
await serve(argv.port, argv.spec, config);
72+
} catch (e) {
73+
handleError(e);
74+
}
6975
},
7076
)
7177
.command(
72-
'bundle [spec]',
78+
'bundle <spec>',
7379
'bundle spec into zero-dependency HTML-file',
7480
yargs => {
7581
yargs.positional('spec', {
@@ -99,16 +105,22 @@ YargsParser.command(
99105
return yargs;
100106
},
101107
async argv => {
102-
await bundle(argv.spec, {
108+
const config = {
103109
ssr: true,
104110
output: argv.o,
105111
cdn: argv.cdn,
106112
title: argv.title,
107113
templateFileName: argv.template,
108114
redocOptions: argv.options || {},
109-
});
115+
};
116+
117+
try {
118+
await bundle(argv.spec, config);
119+
} catch (e) {
120+
handleError(e);
121+
}
110122
},
111-
)
123+
)
112124
.demandCommand()
113125
.options('t', {
114126
alias: 'template',
@@ -117,10 +129,6 @@ YargsParser.command(
117129
})
118130
.options('options', {
119131
describe: 'ReDoc options, use dot notation, e.g. options.nativeScrollbars',
120-
})
121-
.fail((message, error) => {
122-
console.log(error.stack);
123-
process.exit(1);
124132
}).argv;
125133

126134
async function serve(port: number, pathToSpec: string, options: Options = {}) {
@@ -229,13 +237,13 @@ async function getPageHTML(
229237
ssr
230238
? 'hydrate(__redoc_state, container);'
231239
: `init("spec.json", ${JSON.stringify(redocOptions)}, container)`
232-
};
240+
};
233241
234242
</script>`,
235243
redocHead: ssr
236244
? (cdn
237-
? '<script src="https://unpkg.com/redoc@next/bundles/redoc.standalone.js"></script>'
238-
: `<script>${redocStandaloneSrc}</script>`) + css
245+
? '<script src="https://unpkg.com/redoc@next/bundles/redoc.standalone.js"></script>'
246+
: `<script>${redocStandaloneSrc}</script>`) + css
239247
: '<script src="redoc.standalone.js"></script>',
240248
title,
241249
});
@@ -296,3 +304,8 @@ function isURL(str: string): boolean {
296304
function escapeUnicode(str) {
297305
return str.replace(/\u2028|\u2029/g, m => '\\u202' + (m === '\u2028' ? '8' : '9'));
298306
}
307+
308+
function handleError(error: Error) {
309+
console.error(error.stack);
310+
process.exit(1);
311+
}

0 commit comments

Comments
 (0)