Skip to content

Commit

Permalink
feat(cli): added support for JSON string value for --options CLI argu…
Browse files Browse the repository at this point in the history
…ment (#1047)

closes #797
  • Loading branch information
knidarkness authored and RomanHotsiy committed Sep 30, 2019
1 parent 8632b19 commit 2a28130
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 2a28130

Please sign in to comment.