|
| 1 | +import fs from "node:fs"; |
| 2 | +import { join } from "node:path"; |
| 3 | +import { getSablierIndexerEnvio, type Indexer } from "@sablier/indexers"; |
| 4 | +import { Command } from "commander"; |
| 5 | +import { $ } from "execa"; |
| 6 | +import * as yaml from "js-yaml"; |
| 7 | +import _ from "lodash"; |
| 8 | +import type { CliOptions } from "../../types"; |
| 9 | + |
| 10 | +const CHAIN_ID_SEPOLIA = 11155111; |
| 11 | + |
| 12 | +type Category = { |
| 13 | + /** @see https://docusaurus.io/feature-requests/p/hiding-parts-of-docs-in-autogenerated-sidebar */ |
| 14 | + className?: "hidden"; |
| 15 | + collapsible: boolean; |
| 16 | + collapsed: boolean; |
| 17 | + label: string; |
| 18 | + position: number; |
| 19 | +}; |
| 20 | + |
| 21 | +type GraphQLOptions = CliOptions & { |
| 22 | + protocol: string; |
| 23 | + vendor: string; |
| 24 | +}; |
| 25 | + |
| 26 | +/* -------------------------------------------------------------------------- */ |
| 27 | +/* COMMAND */ |
| 28 | +/* -------------------------------------------------------------------------- */ |
| 29 | + |
| 30 | +export function createGraphQLCommand(): Command { |
| 31 | + return new Command("graphql") |
| 32 | + .description("Generate GraphQL schema documentation") |
| 33 | + .requiredOption("-p, --protocol <protocol>", "generate for specific protocol") |
| 34 | + .requiredOption("-v, --vendor <vendor>", "generate for specific vendor") |
| 35 | + .action(async (options, command: Command) => { |
| 36 | + const globalOptions = command.parent?.opts() || {}; |
| 37 | + const mergedOptions: GraphQLOptions = { ...globalOptions, ...options }; |
| 38 | + await generateGraphQL(mergedOptions); |
| 39 | + }); |
| 40 | +} |
| 41 | + |
| 42 | +export async function generateGraphQL(options: GraphQLOptions): Promise<void> { |
| 43 | + const protocols: Indexer.Protocol[] = ["airdrops", "flow", "lockup"]; |
| 44 | + const vendors = ["graph", "envio"]; |
| 45 | + |
| 46 | + if (options.protocol !== "all" && !protocols.includes(options.protocol as Indexer.Protocol)) { |
| 47 | + throw new Error(`Invalid protocol: ${options.protocol}. Valid options: ${protocols.join(", ")}, all`); |
| 48 | + } |
| 49 | + if (options.vendor !== "all" && !vendors.includes(options.vendor as Indexer.Vendor)) { |
| 50 | + throw new Error(`Invalid vendor: ${options.vendor}. Valid options: ${vendors.join(", ")}, all`); |
| 51 | + } |
| 52 | + |
| 53 | + let targetProtocols = protocols; |
| 54 | + if (options.protocol !== "all") { |
| 55 | + targetProtocols = _.filter(protocols, (p) => p === options.protocol); |
| 56 | + } |
| 57 | + |
| 58 | + const basePaths: string[] = []; |
| 59 | + for (const p of targetProtocols) { |
| 60 | + if (options.vendor === "all" || options.vendor === "graph") { |
| 61 | + basePaths.push(await generateGraph(p)); |
| 62 | + } |
| 63 | + if (options.vendor === "all" || options.vendor === "envio") { |
| 64 | + basePaths.push(await generateEnvio(p)); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + cleanupDocs(basePaths); |
| 69 | +} |
| 70 | + |
| 71 | +/* -------------------------------------------------------------------------- */ |
| 72 | +/* INTERNAL LOGIC */ |
| 73 | +/* -------------------------------------------------------------------------- */ |
| 74 | + |
| 75 | +const COLLAPSED = { |
| 76 | + collapsed: true, |
| 77 | + collapsible: true, |
| 78 | +}; |
| 79 | + |
| 80 | +const THE_GRAPH_CATEGORY: Category = { ...COLLAPSED, label: "The Graph", position: 2 }; |
| 81 | +const ENVIO_CATEGORY: Category = { ...COLLAPSED, label: "Envio", position: 3 }; |
| 82 | +const QUERIES_CATEGORY: Category = { ...COLLAPSED, label: "Queries", position: 2 }; // first is the overview |
| 83 | +const OBJECTS_CATEGORY: Category = { ...COLLAPSED, label: "Objects", position: 3 }; |
| 84 | +const ENUMS_CATEGORY: Category = { ...COLLAPSED, className: "hidden", label: "Enums", position: 4 }; |
| 85 | +const INPUTS_CATEGORY: Category = { ...COLLAPSED, className: "hidden", label: "Inputs", position: 5 }; |
| 86 | +const SCALARS_CATEGORY: Category = { ...COLLAPSED, className: "hidden", label: "Scalars", position: 6 }; |
| 87 | + |
| 88 | +function cleanupDocs(basePaths: string[]): void { |
| 89 | + for (const basePath of basePaths) { |
| 90 | + fs.rmSync(join(basePath, "directives"), { force: true, recursive: true }); |
| 91 | + fs.rmSync(join(basePath, "subscriptions"), { force: true, recursive: true }); |
| 92 | + |
| 93 | + // Delete all _category_.yml files |
| 94 | + fs.unlinkSync(join(basePath, "queries", "_category_.yml")); |
| 95 | + fs.unlinkSync(join(basePath, "enums", "_category_.yml")); |
| 96 | + fs.unlinkSync(join(basePath, "inputs", "_category_.yml")); |
| 97 | + fs.unlinkSync(join(basePath, "objects", "_category_.yml")); |
| 98 | + fs.unlinkSync(join(basePath, "scalars", "_category_.yml")); |
| 99 | + |
| 100 | + // Rewrite _category_.yml files |
| 101 | + fs.writeFileSync(join(basePath, "queries", "_category_.yml"), yaml.dump(QUERIES_CATEGORY)); |
| 102 | + fs.writeFileSync(join(basePath, "objects", "_category_.yml"), yaml.dump(OBJECTS_CATEGORY)); |
| 103 | + fs.writeFileSync(join(basePath, "enums", "_category_.yml"), yaml.dump(ENUMS_CATEGORY)); |
| 104 | + fs.writeFileSync(join(basePath, "inputs", "_category_.yml"), yaml.dump(INPUTS_CATEGORY)); |
| 105 | + fs.writeFileSync(join(basePath, "scalars", "_category_.yml"), yaml.dump(SCALARS_CATEGORY)); |
| 106 | + |
| 107 | + // Write vendor category file at base path |
| 108 | + if (basePath.includes("envio")) { |
| 109 | + fs.writeFileSync(join(basePath, "_category_.yml"), yaml.dump(ENVIO_CATEGORY)); |
| 110 | + } else if (basePath.includes("the-graph")) { |
| 111 | + fs.writeFileSync(join(basePath, "_category_.yml"), yaml.dump(THE_GRAPH_CATEGORY)); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +async function generateEnvio(protocol: Indexer.Protocol): Promise<string> { |
| 117 | + const base = `./docs/api/${protocol}/graphql/envio`; |
| 118 | + const schemaURL = getSablierIndexerEnvio({ chainId: CHAIN_ID_SEPOLIA, protocol }).endpoint.url; |
| 119 | + |
| 120 | + await runCommand(base, schemaURL); |
| 121 | + console.log(`✔️ Generated GraphQL docs for Envio vendor and ${_.capitalize(protocol)} protocol\n`); |
| 122 | + return base; |
| 123 | +} |
| 124 | + |
| 125 | +async function generateGraph(protocol: Indexer.Protocol): Promise<string> { |
| 126 | + const base = `./docs/api/${protocol}/graphql/the-graph`; |
| 127 | + const schemaURL = `https://api.studio.thegraph.com/query/112500/sablier-${protocol}-experimental/version/latest`; |
| 128 | + |
| 129 | + await runCommand(base, schemaURL); |
| 130 | + console.log(`✔️ Generated GraphQL docs for The Graph vendor and ${_.capitalize(protocol)} protocol\n`); |
| 131 | + return base; |
| 132 | +} |
| 133 | + |
| 134 | +/** |
| 135 | + * @see https://graphql-markdown.dev/docs/settings#baseurl |
| 136 | + * @see {@link file://./../../../config/plugins.ts} |
| 137 | + */ |
| 138 | +async function runCommand(base: string, schema: string): Promise<void> { |
| 139 | + await $({ stdio: "inherit" })`bun docusaurus graphql-to-doc --base ${base} --schema ${schema}`; |
| 140 | +} |
0 commit comments