mirror of
https://github.com/Redocly/redoc.git
synced 2025-02-16 18:00:33 +03:00
feat: added support for file paths as --options cli argument (#1049)
This commit is contained in:
parent
4d4cfd65aa
commit
4adb927463
34
cli/index.ts
34
cli/index.ts
|
@ -14,7 +14,7 @@ import * as zlib from 'zlib';
|
||||||
import { createStore, loadAndBundleSpec, Redoc } from 'redoc';
|
import { createStore, loadAndBundleSpec, Redoc } from 'redoc';
|
||||||
|
|
||||||
import { watch } from 'chokidar';
|
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 mkdirp from 'mkdirp';
|
||||||
|
|
||||||
import * as YargsParser from 'yargs';
|
import * as YargsParser from 'yargs';
|
||||||
|
@ -283,13 +283,13 @@ async function getPageHTML(
|
||||||
ssr
|
ssr
|
||||||
? 'hydrate(__redoc_state, container);'
|
? 'hydrate(__redoc_state, container);'
|
||||||
: `init("spec.json", ${JSON.stringify(redocOptions)}, container)`
|
: `init("spec.json", ${JSON.stringify(redocOptions)}, container)`
|
||||||
};
|
};
|
||||||
|
|
||||||
</script>`,
|
</script>`,
|
||||||
redocHead: ssr
|
redocHead: ssr
|
||||||
? (cdn
|
? (cdn
|
||||||
? '<script src="https://unpkg.com/redoc@next/bundles/redoc.standalone.js"></script>'
|
? '<script src="https://unpkg.com/redoc@next/bundles/redoc.standalone.js"></script>'
|
||||||
: `<script>${redocStandaloneSrc}</script>`) + css
|
: `<script>${redocStandaloneSrc}</script>`) + css
|
||||||
: '<script src="redoc.standalone.js"></script>',
|
: '<script src="redoc.standalone.js"></script>',
|
||||||
title,
|
title,
|
||||||
disableGoogleFont,
|
disableGoogleFont,
|
||||||
|
@ -357,13 +357,23 @@ function handleError(error: Error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function getObjectOrJSON(options) {
|
function getObjectOrJSON(options) {
|
||||||
try {
|
switch (typeof options) {
|
||||||
return options && typeof options === 'string'
|
case 'object':
|
||||||
? JSON.parse(options) : options
|
return options;
|
||||||
? options
|
case 'string':
|
||||||
: {};
|
try {
|
||||||
} catch (e) {
|
if (existsSync(options) && lstatSync(options).isFile()) {
|
||||||
console.log(`Encountered error:\n${options}\nis not a valid JSON.`);
|
return JSON.parse(readFileSync(options, 'utf-8'));
|
||||||
handleError(e);
|
} else {
|
||||||
|
return JSON.parse(options);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
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 {};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user