docusaurus-openapi-docs icon indicating copy to clipboard operation
docusaurus-openapi-docs copied to clipboard

Is there a way to support multiple instances of the plugin?

Open philip-ellis-sp opened this issue 1 year ago • 3 comments

I would like to separate out my documentation into two separate folders in docusaurus. For example:

plugins: [
      [
        "@docusaurus/plugin-content-docs",
        {
          id: "id1",
          path: "folder1",
          routeBasePath: "id1",
          sidebarPath: require.resolve("./folder1/sidebar.js"),
        },
      ],
      [
        "@docusaurus/plugin-content-docs",
        {
          id: "id2",
          path: "folder2",
          routeBasePath: "id2",
          sidebarPath: require.resolve("./folder2/sidebar.js"),
        },
      ],

but I cannot find a way to tell the plugin cli to use the second instance of the plugin, or to define two docsPluginId variables in the same plugin instance. This is what I tried setting up:

[
        "docusaurus-plugin-openapi-docs",
        {
          id: "id1",
          docsPluginId: "id1",
          config: {
            id1: {
              specPath: "static/id1.yaml",
              outputDir: "id1/api",
              sidebarOptions: {
                groupPathsBy: "tag",
                categoryLinkSource: "tag"
              }
            }
          }
        }
      ],
[
        "docusaurus-plugin-openapi-docs",
        {
          id: "id2",
          docsPluginId: "id2",
          config: {
            id2: {
              specPath: "static/id2.yaml",
              outputDir: "id2/api",
              sidebarOptions: {
                groupPathsBy: "tag",
                categoryLinkSource: "tag"
              }
            }
          }
        }
      ],

This all works for docusaurus, but when running npm run docusaurus gen-api-docs all it only generates the docs for the first instance. Running for the id1 instance just throws ID 'id1' does not exist in OpenAPI docs config.

Is there support for more than one instance of the plugin?

philip-ellis-sp avatar Aug 29 '22 15:08 philip-ellis-sp

:tada: Thanks for opening your first issue here! Welcome to the community!

I found that docusaurus does not run the cli action for multiple instances of plugin, but by adding the .parse() command to the end of the command it then runs an action for each instance. For example:

        extendCli(cli) {
            cli
                .command(`gen-api-docs`)
                .description(`Generates OpenAPI docs in MDX file format and sidebar.js (if enabled).`)
                .usage("<id>")
                .arguments("<id>")
                .action(async (id, instance, args) => {
                if (!args) {
                    return
                }
                if (args[0] === "all") {
                    if (config[args[0]]) {
                        console.error(chalk_1.default.red("Can't use id 'all' for OpenAPI docs configuration key."));
                    }
                    else {
                        Object.keys(config).forEach(async function (key) {
                            await generateApiDocs(config[key]);
                        });
                    }
                }
                else if (!config[args[0]]) {
                    console.error(chalk_1.default.red(`ID '${args[0]}' does not exist in OpenAPI docs config.`));
                }
                else {
                    await generateApiDocs(config[args[0]]);
                }
            }).parse();

allows for the definition of multiple instances and the creating of the api specs for all of them. The problem with this is that it causes the command to be run no matter what, so it's not a fix but perhaps pointing to what could be the solution

philip-ellis-sp avatar Aug 29 '22 19:08 philip-ellis-sp

Thanks for the research into this @philip-ellis-sp! TBH multi-plugin instances hasn't been thoroughly tested/explored yet, but I agree that it should be supported.

@csestito Have we looked into parse() before?

sserrata avatar Aug 30 '22 19:08 sserrata