From 2a28130c82969229a8b72a486d2997fddd9426f4 Mon Sep 17 00:00:00 2001 From: Sergey Dubovyk Date: Mon, 30 Sep 2019 12:56:03 +0300 Subject: [PATCH] feat(cli): added support for JSON string value for --options CLI argument (#1047) closes #797 --- cli/index.ts | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) 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); + } +}