Skip to content

Commit

Permalink
Added an --all-versions flag to gen-api-docs and clean-api-docs (#1018)
Browse files Browse the repository at this point in the history
* Added --all-versions flag to gen-api-docs and clean-api-docs to avoid having to know which apis have versions

* Cleaned up in prep for PR

* Updated docs
  • Loading branch information
dv-stewarts authored Nov 18, 2024
1 parent 8c67733 commit 6ad497b
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,4 @@ demo/**/sidebar.ts
demo/**/versions.json

.idea
.tool-versions
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,16 @@ yarn docusaurus gen-api-docs petstore

> The example above will only generate API docs relative to `petstore`.
If you have multiple versions of the same API, `gen-api-docs` only generates the latest. To generate all versions, use the `--all-versions` flag.

Example:

```bash
yarn docusaurus gen-api-docs all --all-versions
```

> This will generate API docs for all versions of all the OpenAPI specification (OAS) files referenced in your `docusaurus-plugin-openapi-docs` config.
### Cleaning API Docs

To clean/remove all API Docs, run the following command from the root directory of your project:
Expand All @@ -291,6 +301,16 @@ yarn docusaurus clean-api-docs petstore

> The example above will remove all API docs relative to `burgers`.
If you have multiple versions of the same API, `clean-api-docs` only cleans the latest. To clean all versions, use the `--all-versions` flag.

Example:

```bash
yarn docusaurus clean-api-docs all --all-versions
```

> This will clean API docs for all versions of all the OpenAPI specification (OAS) files referenced in your `docusaurus-plugin-openapi-docs` config.
### Versioning OpenAPI docs

To generate _all_ versioned OpenAPI docs, run the following command from the root directory of your project:
Expand Down
6 changes: 3 additions & 3 deletions demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
"clean-api-docs": "docusaurus clean-api-docs",
"gen-api-docs:version": "docusaurus gen-api-docs:version",
"clean-api-docs:version": "docusaurus clean-api-docs:version",
"gen-all": "docusaurus gen-api-docs all && docusaurus gen-api-docs:version petstore_versioned:all",
"clean-all": "docusaurus clean-api-docs all && docusaurus clean-api-docs:version petstore_versioned:all",
"re-gen": "yarn clean-all && yarn gen-all"
"gen-all": "docusaurus gen-api-docs all --all-versions",
"clean-all": "docusaurus clean-api-docs all --all-versions",
"re-gen": "yarn clean-all-versions && yarn gen-all-versions"
},
"dependencies": {
"@docusaurus/core": "3.6.1",
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
"canaryBeta:publish": "yarn lerna publish from-package --dist-tag canary --yes --no-verify-access",
"serve": "yarn workspace demo serve",
"start": "yarn workspace demo start",
"gen-api": "yarn workspace demo gen-api-docs all",
"clean-api": "yarn workspace demo clean-api-docs all",
"gen-api": "yarn workspace demo gen-api-docs all --all-versions",
"clean-api": "yarn workspace demo clean-api-docs all --all-versions",
"cy:run": "cypress run",
"cy:open": "cypress open",
"format": "prettier . --check --ignore-unknown --ignore-path .prettierignore",
Expand Down
20 changes: 20 additions & 0 deletions packages/docusaurus-plugin-openapi-docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,16 @@ yarn docusaurus gen-api-docs petstore

> The example above will only generate API docs relative to `petstore`.
If you have multiple versions of the same API, `gen-api-docs` only generates the latest. To generate all versions, use the `--all-versions` flag.

Example:

```bash
yarn docusaurus gen-api-docs all --all-versions
```

> This will generate API docs for all versions of all the OpenAPI specification (OAS) files referenced in your `docusaurus-plugin-openapi-docs` config.
### Cleaning API Docs

To clean/remove all API Docs, run the following command from the root directory of your project:
Expand All @@ -290,6 +300,16 @@ yarn docusaurus clean-api-docs petstore

> The example above will remove all API docs relative to `burgers`.
If you have multiple versions of the same API, `clean-api-docs` only cleans the latest. To clean all versions, use the `--all-versions` flag.

Example:

```bash
yarn docusaurus clean-api-docs all --all-versions
```

> This will clean API docs for all versions of all the OpenAPI specification (OAS) files referenced in your `docusaurus-plugin-openapi-docs` config.
### Versioning OpenAPI docs

To generate _all_ versioned OpenAPI docs, run the following command from the root directory of your project:
Expand Down
89 changes: 84 additions & 5 deletions packages/docusaurus-plugin-openapi-docs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ import {
createSchemaPageMD,
createTagPageMD,
} from "./markdown";
import { readOpenapiFiles, processOpenapiFiles } from "./openapi";
import { processOpenapiFiles, readOpenapiFiles } from "./openapi";
import { OptionsSchema } from "./options";
import generateSidebarSlice from "./sidebars";
import type {
PluginOptions,
LoadedContent,
APIOptions,
ApiMetadata,
APIOptions,
ApiPageMetadata,
InfoPageMetadata,
TagPageMetadata,
LoadedContent,
PluginOptions,
SchemaPageMetadata,
TagPageMetadata,
} from "./types";

export function isURL(str: string): boolean {
Expand Down Expand Up @@ -586,6 +586,66 @@ custom_edit_url: null
}
}

async function generateAllVersions(options: APIOptions, pluginId: any) {
const parentOptions = Object.assign({}, options);
const { versions } = parentOptions as any;

if (versions != null && Object.keys(versions).length > 0) {
const version = parentOptions.version as string;
const label = parentOptions.label as string;

const baseUrl = parentOptions.baseUrl as string;
let parentVersion = {} as any;

parentVersion[version] = { label: label, baseUrl: baseUrl };
const mergedVersions = Object.assign(parentVersion, versions);

// Prepare for merge
delete parentOptions.versions;
delete parentOptions.version;
delete parentOptions.label;
delete parentOptions.baseUrl;
delete parentOptions.downloadUrl;

await generateVersions(mergedVersions, parentOptions.outputDir);
Object.keys(versions).forEach(async (key) => {
if (key === "all") {
console.error(
chalk.red(
"Can't use id 'all' for OpenAPI docs versions configuration key."
)
);
}
const versionOptions = versions[key];
const mergedOptions = {
...parentOptions,
...versionOptions,
};
await generateApiDocs(mergedOptions, pluginId);
});
}
}

async function cleanAllVersions(options: APIOptions) {
const parentOptions = Object.assign({}, options);

const { versions } = parentOptions as any;

delete parentOptions.versions;

if (versions != null && Object.keys(versions).length > 0) {
await cleanVersions(parentOptions.outputDir);
Object.keys(versions).forEach(async (key) => {
const versionOptions = versions[key];
const mergedOptions = {
...parentOptions,
...versionOptions,
};
await cleanApiDocs(mergedOptions);
});
}
}

return {
name: `docusaurus-plugin-openapi-docs`,

Expand All @@ -598,9 +658,11 @@ custom_edit_url: null
.usage("<id>")
.arguments("<id>")
.option("-p, --plugin-id <plugin>", "OpenAPI docs plugin ID.")
.option("--all-versions", "Generate all versions.")
.action(async (id, instance) => {
const options = instance.opts();
const pluginId = options.pluginId;
const allVersions = options.allVersions;
const pluginInstances = getPluginInstances(plugins);
let targetConfig: any;
let targetDocsPluginId: any;
Expand Down Expand Up @@ -637,6 +699,12 @@ custom_edit_url: null
} else {
Object.keys(targetConfig).forEach(async function (key) {
await generateApiDocs(targetConfig[key], targetDocsPluginId);
if (allVersions) {
await generateAllVersions(
targetConfig[key],
targetDocsPluginId
);
}
});
}
} else if (!targetConfig[id]) {
Expand All @@ -645,6 +713,9 @@ custom_edit_url: null
);
} else {
await generateApiDocs(targetConfig[id], targetDocsPluginId);
if (allVersions) {
await generateAllVersions(targetConfig[id], targetDocsPluginId);
}
}
});

Expand Down Expand Up @@ -748,9 +819,11 @@ custom_edit_url: null
.usage("<id>")
.arguments("<id>")
.option("-p, --plugin-id <plugin>", "OpenAPI docs plugin ID.")
.option("--all-versions", "Clean all versions.")
.action(async (id, instance) => {
const options = instance.opts();
const pluginId = options.pluginId;
const allVersions = options.allVersions;
const pluginInstances = getPluginInstances(plugins);
let targetConfig: any;
if (pluginId) {
Expand Down Expand Up @@ -784,10 +857,16 @@ custom_edit_url: null
} else {
Object.keys(targetConfig).forEach(async function (key) {
await cleanApiDocs(targetConfig[key]);
if (allVersions) {
await cleanAllVersions(targetConfig[key]);
}
});
}
} else {
await cleanApiDocs(targetConfig[id]);
if (allVersions) {
await cleanAllVersions(targetConfig[id]);
}
}
});

Expand Down

0 comments on commit 6ad497b

Please sign in to comment.