feat(cli): add options to specify redoc options

This commit is contained in:
Roman Hotsiy 2018-03-20 11:31:36 +02:00
parent 7ee1ab23a8
commit 2732c89316
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0

View File

@ -21,6 +21,7 @@ type Options = {
output?: string; output?: string;
title?: string; title?: string;
templateFileName?: string; templateFileName?: string;
redocOptions?: object;
}; };
const BUNDLES_DIR = dirname(require.resolve('redoc')); const BUNDLES_DIR = dirname(require.resolve('redoc'));
@ -60,6 +61,7 @@ yargs
ssr: argv.ssr, ssr: argv.ssr,
watch: argv.watch, watch: argv.watch,
templateFileName: argv.template, templateFileName: argv.template,
redocOptions: argv.options || {},
}); });
} catch (e) { } catch (e) {
console.log(e.stack); console.log(e.stack);
@ -104,6 +106,7 @@ yargs
cdn: argv.cdn, cdn: argv.cdn,
title: argv.title, title: argv.title,
templateFileName: argv.template, templateFileName: argv.template,
redocOptions: argv.options || {},
}); });
} catch (e) { } catch (e) {
console.log(e.message); console.log(e.message);
@ -115,6 +118,9 @@ yargs
describe: 'Path to handlebars page template, see https://git.io/vxZ3V for the example ', describe: 'Path to handlebars page template, see https://git.io/vxZ3V for the example ',
type: 'string', type: 'string',
}) })
.options('options', {
describe: 'ReDoc options, use dot notation, e.g. options.nativeScrollbars',
})
.demandCommand().argv; .demandCommand().argv;
async function serve(port: number, pathToSpec: string, options: Options = {}) { async function serve(port: number, pathToSpec: string, options: Options = {}) {
@ -188,13 +194,13 @@ async function bundle(pathToSpec, options: Options = {}) {
async function getPageHTML( async function getPageHTML(
spec: any, spec: any,
pathToSpec: string, pathToSpec: string,
{ ssr, cdn, title, templateFileName }: Options, { ssr, cdn, title, templateFileName, redocOptions = {} }: Options,
) { ) {
let html, css, state; let html, css, state;
let redocStandaloneSrc; let redocStandaloneSrc;
if (ssr) { if (ssr) {
console.log('Prerendering docs'); console.log('Prerendering docs');
const store = await createStore(spec, pathToSpec); const store = await createStore(spec, pathToSpec, redocOptions);
const sheet = new ServerStyleSheet(); const sheet = new ServerStyleSheet();
html = renderToString(sheet.collectStyles(React.createElement(Redoc, { store }))); html = renderToString(sheet.collectStyles(React.createElement(Redoc, { store })));
css = sheet.getStyleTags(); css = sheet.getStyleTags();
@ -207,15 +213,17 @@ async function getPageHTML(
templateFileName = templateFileName ? templateFileName : join(__dirname, './template.hbs'); templateFileName = templateFileName ? templateFileName : join(__dirname, './template.hbs');
const template = compile(readFileSync(templateFileName).toString()); const template = compile(readFileSync(templateFileName).toString());
console.log(readFileSync(templateFileName).toString());
debugger;
return template({ return template({
redocHTML: ` redocHTML: `
<script> <script>
${(ssr && `const __redoc_state = ${JSON.stringify(state)};`) || ''} ${(ssr && `const __redoc_state = ${JSON.stringify(state)};`) || ''}
document.addEventListener('DOMContentLoaded', function() { document.addEventListener('DOMContentLoaded', function() {
var container = document.getElementById('redoc'); var container = document.getElementById('redoc');
Redoc.${ssr ? 'hydrate(__redoc_state, container);' : 'init("spec.json", {}, container)'}; Redoc.${
ssr
? 'hydrate(__redoc_state, container);'
: `init("spec.json", ${JSON.stringify(redocOptions)}, container)`
};
}); });
</script> </script>
<div id="redoc">${(ssr && html) || ''}</div>`, <div id="redoc">${(ssr && html) || ''}</div>`,