Skip to content

Commit

Permalink
feat(cli): add options to specify redoc options
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanHotsiy committed Mar 20, 2018
1 parent 7ee1ab2 commit 2732c89
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Options = {
output?: string;
title?: string;
templateFileName?: string;
redocOptions?: object;
};

const BUNDLES_DIR = dirname(require.resolve('redoc'));
Expand Down Expand Up @@ -60,6 +61,7 @@ yargs
ssr: argv.ssr,
watch: argv.watch,
templateFileName: argv.template,
redocOptions: argv.options || {},
});
} catch (e) {
console.log(e.stack);
Expand Down Expand Up @@ -104,6 +106,7 @@ yargs
cdn: argv.cdn,
title: argv.title,
templateFileName: argv.template,
redocOptions: argv.options || {},
});
} catch (e) {
console.log(e.message);
Expand All @@ -115,6 +118,9 @@ yargs
describe: 'Path to handlebars page template, see https://git.io/vxZ3V for the example ',
type: 'string',
})
.options('options', {
describe: 'ReDoc options, use dot notation, e.g. options.nativeScrollbars',
})
.demandCommand().argv;

async function serve(port: number, pathToSpec: string, options: Options = {}) {
Expand Down Expand Up @@ -188,13 +194,13 @@ async function bundle(pathToSpec, options: Options = {}) {
async function getPageHTML(
spec: any,
pathToSpec: string,
{ ssr, cdn, title, templateFileName }: Options,
{ ssr, cdn, title, templateFileName, redocOptions = {} }: Options,
) {
let html, css, state;
let redocStandaloneSrc;
if (ssr) {
console.log('Prerendering docs');
const store = await createStore(spec, pathToSpec);
const store = await createStore(spec, pathToSpec, redocOptions);
const sheet = new ServerStyleSheet();
html = renderToString(sheet.collectStyles(React.createElement(Redoc, { store })));
css = sheet.getStyleTags();
Expand All @@ -207,15 +213,17 @@ async function getPageHTML(

templateFileName = templateFileName ? templateFileName : join(__dirname, './template.hbs');
const template = compile(readFileSync(templateFileName).toString());
console.log(readFileSync(templateFileName).toString());
debugger;
return template({
redocHTML: `
<script>
${(ssr && `const __redoc_state = ${JSON.stringify(state)};`) || ''}
document.addEventListener('DOMContentLoaded', function() {
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>
<div id="redoc">${(ssr && html) || ''}</div>`,
Expand Down

0 comments on commit 2732c89

Please sign in to comment.