diff --git a/.gitignore b/.gitignore index 84e2c76f..d3e74c33 100644 --- a/.gitignore +++ b/.gitignore @@ -28,6 +28,7 @@ bundles/ typings/* !typings/styled-patch.d.ts cli/index.js +cli/__test__/*/**/redoc-static.html /benchmark/revisions diff --git a/cli/__test__/build/configRedoc/.redocly.yaml b/cli/__test__/build/configRedoc/.redocly.yaml new file mode 100644 index 00000000..d0c49e68 --- /dev/null +++ b/cli/__test__/build/configRedoc/.redocly.yaml @@ -0,0 +1,2 @@ +features.openapi: + disableSearch: true diff --git a/cli/__test__/build/configRedoc/index.test.ts b/cli/__test__/build/configRedoc/index.test.ts new file mode 100644 index 00000000..1b0a40b8 --- /dev/null +++ b/cli/__test__/build/configRedoc/index.test.ts @@ -0,0 +1,34 @@ +import { spawnSync } from 'child_process'; + +describe('build', () => { + it('should use .redocly.yaml', () => { + const r = spawnSync('node', ['../../../index.js', 'build', ' ../../../../demo/openapi.yaml'], { + cwd: __dirname, + shell: true, + }); + + const out = r.stdout.toString('utf-8'); + expect(out).toContain('Found .redocly.yaml and use option from features.openapi'); + expect(out).toContain('bundled successfully'); + }); + + it('should use inline options and ignore .redocly.yaml', () => { + const r = spawnSync( + 'node', + [ + '../../../index.js', + 'build', + ' ../../../../demo/openapi.yaml', + '--options.disableSearch=true', + ], + { + cwd: __dirname, + shell: true, + }, + ); + + const out = r.stdout.toString('utf-8'); + expect(out).not.toContain('Found .redocly.yaml and use option from features.openapi'); + expect(out).toContain('bundled successfully'); + }); +}); diff --git a/cli/index.ts b/cli/index.ts index 9f6d764d..4550ddd8 100644 --- a/cli/index.ts +++ b/cli/index.ts @@ -25,6 +25,12 @@ import { import * as mkdirp from 'mkdirp'; import * as YargsParser from 'yargs'; +// eslint-disable-next-line import/no-extraneous-dependencies +import { findConfig } from '@redocly/openapi-core'; +// eslint-disable-next-line import/no-extraneous-dependencies +import { parseYaml } from '@redocly/openapi-core'; +// eslint-disable-next-line import/no-extraneous-dependencies +import { Config } from '@redocly/openapi-core'; interface Options { ssr?: boolean; @@ -447,6 +453,17 @@ function getObjectOrJSON(options) { handleError(e); } default: + const configFile = findConfig(); + if (configFile) { + console.log(`Found ${configFile} and use option from features.openapi`); + try { + const config = parseYaml(readFileSync(configFile, 'utf-8')) as Config; + + return config['features.openapi']; + } catch (e) { + console.warn(`Found ${configFile} but failed to parse: ${e.message}`); + } + } return {}; } } diff --git a/package.json b/package.json index d4c4a0a5..1c9b1b2a 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,7 @@ "bundle:standalone": "webpack --env production --env standalone --mode=production", "bundle:lib": "webpack --mode=production && npm run declarations", "bundle:browser": "webpack --env production --env browser --mode=production", - "bundle": "npm run bundle:clean && npm run bundle:lib && npm run bundle:browser && npm run bundle:standalone", + "bundle": "npm run bundle:clean && npm run bundle:lib && npm run bundle:browser && npm run bundle:standalone && npm run compile:cli", "declarations": "tsc --emitDeclarationOnly -p tsconfig.lib.json && cp -R src/types typings/", "stats": "webpack --env production --env standalone --json --profile --mode=production > stats.json", "prettier": "prettier --write \"cli/index.ts\" \"src/**/*.{ts,tsx}\"",