From b895e9cecc97cebea22e084410a92eca0aa3ec15 Mon Sep 17 00:00:00 2001 From: mmeigs Date: Mon, 13 Mar 2023 12:21:04 -0400 Subject: [PATCH] DOP-3525: Options json support (#16) --- README.md | 26 ++++++++++---------- cli/__test__/build/configRedoc/index.test.ts | 25 ------------------- cli/__test__/data/version-data.json | 8 ------ cli/index.ts | 16 ------------ options.json | 13 ++++++++++ src/components/Redoc/Redoc.tsx | 4 +-- src/services/RedocNormalizedOptions.ts | 4 +-- version-data.json | 8 ------ 8 files changed, 29 insertions(+), 75 deletions(-) delete mode 100644 cli/__test__/data/version-data.json create mode 100644 options.json delete mode 100644 version-data.json diff --git a/README.md b/README.md index 3b6072c6..1209e4da 100644 --- a/README.md +++ b/README.md @@ -37,12 +37,20 @@ Make sure to run `npm run bundle` on the root folder of this repo before running With `node` installed, run by doing the following: ``` -node build --options= --output= +node build --options --output= ``` -#### Versioned Options in the CLI +#### Options Available to include -If wanting to add versioning support in your build, you must create a JSON file with the following data structure (replace example values with any mock values): +Custom DOP options are available to include within a local JSON file. + +Within your `options.json` in the root directory, specify the `siteTitle`, `backNavigationPath`, `versionData`, and/or `ignoreIncompatibleTypes` flag. + +The `siteTitle` and `backNavigationPath` properties are used to personalize the `SideMenuBackButton` UI and navigation. + +The `ignoreIncompatibleTypes` flag will silence only "Incompatible Types" errors in the build process. + +The `versionData` property enables and populates the `VersionSelector` dropdown for versioned OpenAPI specs. The structure is as follows: ``` { @@ -55,18 +63,10 @@ If wanting to add versioning support in your build, you must create a JSON file } ``` -After doing so, run the build much the same as above but with the following option added: -Note: the path to the version data option JSON file should be referenced in relation to the `redoc/cli` directory +After specifying all options, run the build: ``` ---options.versionData= -``` - -Such as: -Note: this is referencing a JSON file in the root directory - -``` -node build --options.versionData=../version-data.json +node build --options options.json ``` ### Releasing diff --git a/cli/__test__/build/configRedoc/index.test.ts b/cli/__test__/build/configRedoc/index.test.ts index dda8ad03..854b4cf2 100644 --- a/cli/__test__/build/configRedoc/index.test.ts +++ b/cli/__test__/build/configRedoc/index.test.ts @@ -27,29 +27,4 @@ describe('build', () => { expect(result).toContain('Found .redocly.yaml and using features.openapi options'); expect(result).toContain('bundled successfully'); }); - - it('should ingest version data from json file', () => { - const pathToTestData = '__test__/data/version-data.json'; - const r = spawnSync( - 'ts-node', - [ - '../../../index.ts', - 'build', - ' ../../../../demo/openapi.yaml', - '--output=redoc-test.html', - `--options.versionData=${pathToTestData}`, - ], - { - cwd: __dirname, - shell: true, - }, - ); - - const out = r.stdout.toString('utf-8'); - const err = r.stderr.toString('utf-8'); - const result = `${out}\n${err}`; - - expect(result).toContain(`Found ${pathToTestData} and using version data.`); - expect(result).toContain('bundled successfully'); - }); }); diff --git a/cli/__test__/data/version-data.json b/cli/__test__/data/version-data.json deleted file mode 100644 index 76273100..00000000 --- a/cli/__test__/data/version-data.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "active": { - "apiVersion": "2.0", - "resourceVersion": "2023-02-14" - }, - "rootUrl": "https://mongodb.com/docs/atlas/reference/api-resources-spec/v2", - "resourceVersions": ["2022-09-09", "2022-10-18", "2028-02-14"] -} diff --git a/cli/index.ts b/cli/index.ts index 513c386f..bbfcbcf8 100644 --- a/cli/index.ts +++ b/cli/index.ts @@ -439,22 +439,6 @@ function handleError(error: Error) { function getObjectOrJSON(options) { switch (typeof options) { case 'object': - if (options?.versionData) { - const { versionData: versionPath } = options; - const versionFilePath = resolve(__dirname, versionPath); - try { - if (existsSync(versionFilePath) && lstatSync(versionFilePath).isFile()) { - const versionData = JSON.parse(readFileSync(versionFilePath, 'utf-8')); - options.versionData = versionData; - console.log(`Found ${versionPath} and using version data.`); - } - } catch (e) { - console.log( - `Encountered error:\n\n${versionPath}\n\nis not a file with a valid JSON object.`, - ); - handleError(e); - } - } return options; case 'string': try { diff --git a/options.json b/options.json new file mode 100644 index 00000000..5304bb74 --- /dev/null +++ b/options.json @@ -0,0 +1,13 @@ +{ + "versionData": { + "active": { + "apiVersion": "2.0", + "resourceVersion": "2023-02-14" + }, + "rootUrl": "https://mongodb.com/docs/atlas/reference/api-resources-spec/v2", + "resourceVersions": ["2022-09-09", "2022-10-18", "2023-02-14"] + }, + "backNavigationPath": "https://www.mongodb.com/docs/atlas/", + "siteTitle": "MongoDB Atlas", + "ignoreIncompatibleTypes": true +} diff --git a/src/components/Redoc/Redoc.tsx b/src/components/Redoc/Redoc.tsx index 914ff530..af041283 100644 --- a/src/components/Redoc/Redoc.tsx +++ b/src/components/Redoc/Redoc.tsx @@ -70,9 +70,7 @@ export class Redoc extends React.Component { /> )) || null} - {options.versionData && typeof options.versionData == 'object' && ( - - )} + {options.versionData && } diff --git a/src/services/RedocNormalizedOptions.ts b/src/services/RedocNormalizedOptions.ts index ce263fe1..64114556 100644 --- a/src/services/RedocNormalizedOptions.ts +++ b/src/services/RedocNormalizedOptions.ts @@ -61,7 +61,7 @@ export interface RedocRawOptions { backNavigationPath?: string; ignoreIncompatibleTypes?: boolean | string; siteTitle?: string; - versionData?: string; + versionData?: VersionData; } export interface VersionData { @@ -279,7 +279,7 @@ export class RedocNormalizedOptions { backNavigationPath?: string; ignoreIncompatibleTypes: boolean; siteTitle?: string; - versionData?: string | VersionData; + versionData?: VersionData; constructor(raw: RedocRawOptions, defaults: RedocRawOptions = {}) { raw = { ...defaults, ...raw }; diff --git a/version-data.json b/version-data.json deleted file mode 100644 index c298d012..00000000 --- a/version-data.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "active": { - "apiVersion": "2.0", - "resourceVersion": "2023-02-14" - }, - "rootUrl": "https://mongodb.com/docs/atlas/reference/api-resources-spec/v2", - "resourceVersions": ["2022-09-09", "2022-10-18", "2023-02-14"] -}