diff --git a/cli/index.ts b/cli/index.ts index 73e3a20948..7bb27bb2a1 100644 --- a/cli/index.ts +++ b/cli/index.ts @@ -69,9 +69,11 @@ YargsParser.command( watch: argv.watch as boolean, templateFileName: argv.template as string, templateOptions: argv.templateOptions || {}, - redocOptions: argv.options || {}, + redocOptions: getObjectOrJSON(argv.options), }; + console.log(config); + try { await serve(argv.port as number, argv.spec as string, config); } catch (e) { @@ -124,7 +126,7 @@ YargsParser.command( disableGoogleFont: argv.disableGoogleFont as boolean, templateFileName: argv.template as string, templateOptions: argv.templateOptions || {}, - redocOptions: argv.options || {}, + redocOptions: getObjectOrJSON(argv.options), }; try { @@ -353,3 +355,15 @@ function handleError(error: Error) { console.error(error.stack); process.exit(1); } + +function getObjectOrJSON(options) { + try { + return options && typeof options === 'string' + ? JSON.parse(options) : options + ? options + : {}; + } catch (e) { + console.log(`Encountered error:\n${options}\nis not a valid JSON.`); + handleError(e); + } +}