@graphql-markdown/cli
Version:
NodeJS CLI for generating Markdown documentation from a GraphQL schema.
111 lines (110 loc) • 4.86 kB
JavaScript
;
/**
* This module provides the CLI functionality for generating documentation from GraphQL schemas.
* It exports utilities to run the documentation generator both programmatically and via CLI.
*
* @module cli
* @see {@link https://graphql-markdown.dev | GraphQL Markdown Documentation}
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getGraphQLMarkdownCli = exports.runGraphQLMarkdown = void 0;
const commander_1 = __importDefault(require("commander"));
const core_1 = require("@graphql-markdown/core");
const logger_1 = __importDefault(require("@graphql-markdown/logger"));
const COMMAND = "graphql-to-doc";
const DESCRIPTION = "Generate GraphQL Schema Documentation";
const DEFAULT_ID = "default";
/**
* Runs the GraphQL Markdown CLI to generate documentation from a GraphQL schema.
*
* @param options - Options for configuring the GraphQL Markdown CLI.
* @param cliOptions - Command-line options passed to the CLI.
* @param loggerModule - Optional logger module to use.
*
* @example
* ```typescript
* await runGraphQLMarkdown(
* { id: "custom" },
* { schema: "./schema.graphql", root: "./docs" },
* "custom-logger"
* );
* ```
*/
const runGraphQLMarkdown = async (options, cliOptions, loggerModule) => {
const config = await (0, core_1.buildConfig)(options, cliOptions, options.id);
if (cliOptions.config) {
console.dir(config, { depth: null });
return;
}
await (0, core_1.generateDocFromSchema)({
...config,
loggerModule,
});
};
exports.runGraphQLMarkdown = runGraphQLMarkdown;
/**
* Configures and returns the GraphQL Markdown CLI.
*
* @param options - Options for configuring the GraphQL Markdown CLI.
* @param loggerModule - Optional logger module to use.
* @param customMdxParser - Optional MDX parser configuration.
*
* @returns The configured CLI instance.
*
* @example
* ```typescript
* const cli = getGraphQLMarkdownCli(
* { id: "custom" },
* "custom-logger",
* true
* );
* await cli.parseAsync(process.argv);
* ```
*/
const getGraphQLMarkdownCli = (options, loggerModule, customMdxParser) => {
void (0, logger_1.default)(loggerModule);
const isDefaultId = typeof options === "undefined" ||
!("id" in options) ||
("id" in options && options.id === DEFAULT_ID);
const cmd = isDefaultId ? COMMAND : `${COMMAND}:${options.id}`;
const description = isDefaultId
? DESCRIPTION
: `${DESCRIPTION} for configuration with id ${options.id}`;
const cli = commander_1.default;
const command = cli
.command(cmd)
.description(description)
.option("-s, --schema <schema>", "Schema location")
.option("-r, --root <rootPath>", "Root folder for doc generation")
.option("-b, --base <baseURL>", "Base URL to be used by static generator")
.option("-l, --link <linkRoot>", "Root for links in documentation")
.option("-h, --homepage <homepage>", "File location for doc landing page")
.option("--hierarchy <hierarchy>", "Schema entity hierarchy: `api`, `entity`, `flat`")
.option("--noCode", "Disable code section for types")
.option("--noExample", "Disable example section for types")
.option("--noParentType", "Disable parent type name as field prefix")
.option("--noRelatedType", "Disable related types sections")
.option("--noTypeBadges", "Disable badges for types")
.option("--index", "Enable generated index for categories")
.option("-f, --force", "Force document generation")
.option("-d, --diff <diffMethod>", "Set diff method")
.option("-t, --tmp <tmpDir>", "Set temp dir for schema diff")
.option("--groupByDirective <@directive(field|=fallback)>", "Group documentation by directive")
.option("--only <@directive...>", "Only print types with matching directive")
.option("--skip <@directive...>", "Skip types with matching directive")
.option("--deprecated <option>", "Option for printing deprecated entities: `default`, `group` or `skip`")
.option("--pretty", "Prettify generated files")
.option("--config", "Print configuration (for debugging)");
// allows passing the mdx package to the CLI
if (customMdxParser === true || typeof customMdxParser === "string") {
command.option("--mdxParser <mdxParser>", "Set MDX package processor", typeof customMdxParser === "string" ? customMdxParser : undefined);
}
command.action(async (cliOptions) => {
await (0, exports.runGraphQLMarkdown)(options, cliOptions, loggerModule);
});
return command;
};
exports.getGraphQLMarkdownCli = getGraphQLMarkdownCli;