feat: added support for file paths as --options cli argument (#1049)

This commit is contained in:
Sergey Dubovyk 2019-10-12 12:30:41 +03:00 committed by Roman Hotsiy
parent 4d4cfd65aa
commit 4adb927463

View File

@ -14,7 +14,7 @@ import * as zlib from 'zlib';
import { createStore, loadAndBundleSpec, Redoc } from 'redoc';
import { watch } from 'chokidar';
import { createReadStream, existsSync, readFileSync, ReadStream, writeFileSync } from 'fs';
import { createReadStream, existsSync, readFileSync, ReadStream, writeFileSync, lstatSync } from 'fs';
import * as mkdirp from 'mkdirp';
import * as YargsParser from 'yargs';
@ -357,13 +357,23 @@ function handleError(error: Error) {
}
function getObjectOrJSON(options) {
switch (typeof options) {
case 'object':
return options;
case 'string':
try {
return options && typeof options === 'string'
? JSON.parse(options) : options
? options
: {};
if (existsSync(options) && lstatSync(options).isFile()) {
return JSON.parse(readFileSync(options, 'utf-8'));
} else {
return JSON.parse(options);
}
} catch (e) {
console.log(`Encountered error:\n${options}\nis not a valid JSON.`);
console.log(
`Encountered error:\n\n${options}\n\nis neither a file with a valid JSON object neither a stringified JSON object.`
);
handleError(e);
}
default:
return {};
}
}