From 4e8ee0305d0830ebdbd7442613b834c9ca343ec6 Mon Sep 17 00:00:00 2001 From: Siarhei Bautrukevich Date: Thu, 31 Jan 2019 04:51:44 +0300 Subject: [PATCH] feat(cli): Add templateOptions param to pass additional data to custom template (#792) * Add templateOptions param to pass additional data to custom template * Update README for ReDoc cli --- cli/README.md | 1 + cli/index.ts | 15 +++++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/cli/README.md b/cli/README.md index 7b836f4711..9c4d8dd663 100644 --- a/cli/README.md +++ b/cli/README.md @@ -17,5 +17,6 @@ Some examples: - Bundle with main color changed to `orange`:
`$ redoc-cli bundle [spec] --options.theme.colors.primary.main=orange` - Serve with `nativeScrollbars` option set to true:
`$ redoc-cli serve [spec] --options.nativeScrollbars` - Bundle using custom template (check [default template](https://github.com/Rebilly/ReDoc/blob/master/cli/template.hbs) for reference):
`$ redoc-cli bundle [spec] -t custom.hbs` +- Bundle using custom template and add custom `templateOptions`:
`$ redoc-cli bundle [spec] -t custom.hbs --templateOptions.metaDescription "Page meta description"` For more details run `redoc-cli --help`. diff --git a/cli/index.ts b/cli/index.ts index d360949914..3abe4c6ff9 100644 --- a/cli/index.ts +++ b/cli/index.ts @@ -26,6 +26,7 @@ interface Options { output?: string; title?: string; templateFileName?: string; + templateOptions?: any; redocOptions?: any; } @@ -65,6 +66,7 @@ YargsParser.command( ssr: argv.ssr, watch: argv.watch, templateFileName: argv.template, + templateOptions: argv.templateOptions || {}, redocOptions: argv.options || {}, }; @@ -112,6 +114,7 @@ YargsParser.command( cdn: argv.cdn, title: argv.title, templateFileName: argv.template, + templateOptions: argv.templateOptions || {}, redocOptions: argv.options || {}, }; @@ -128,6 +131,9 @@ YargsParser.command( describe: 'Path to handlebars page template, see https://git.io/vh8fP for the example ', type: 'string', }) + .options('templateOptions', { + describe: 'Additional options that you want pass to template. Use dot notation, e.g. templateOptions.metaDescription', + }) .options('options', { describe: 'ReDoc options, use dot notation, e.g. options.nativeScrollbars', }).argv; @@ -207,7 +213,7 @@ async function bundle(pathToSpec, options: Options = {}) { async function getPageHTML( spec: any, pathToSpec: string, - { ssr, cdn, title, templateFileName, redocOptions = {} }: Options, + { ssr, cdn, title, templateFileName, templateOptions, redocOptions = {} }: Options, ) { let html; let css; @@ -241,15 +247,16 @@ async function getPageHTML( ssr ? 'hydrate(__redoc_state, container);' : `init("spec.json", ${JSON.stringify(redocOptions)}, container)` - }; + }; `, redocHead: ssr ? (cdn - ? '' - : ``) + css + ? '' + : ``) + css : '', title, + templateOptions, }); }