|
| 1 | +import { Command, Config, Flags } from "@oclif/core"; |
| 2 | +import fs from "node:fs"; |
| 3 | +import path from "node:path"; |
| 4 | + |
| 5 | +import BaseCommand from "../extensions/base-command.js"; |
| 6 | + |
| 7 | +export default class DocsGenerator extends BaseCommand { |
| 8 | + static description = "Generate command and overview markdown files for the CLI."; |
| 9 | + |
| 10 | + static flags = { |
| 11 | + "output-dir": Flags.string({ |
| 12 | + char: "o", |
| 13 | + description: "The output directory for the documentation files.", |
| 14 | + |
| 15 | + }), |
| 16 | + } |
| 17 | + |
| 18 | + static hidden = true; |
| 19 | + |
| 20 | + generateCommandMarkdown(command: Command.Loadable, flags: [string, Command.Flag.Cached][]): string { |
| 21 | + const options = flags.length > 0 |
| 22 | + ? |
| 23 | + flags.filter(([_, flag]) => !flag.hidden && flag.helpGroup !== "GLOBAL") |
| 24 | + .sort(([_, flag]) => (flag.required ? -1 : 1)) |
| 25 | + .map(([name, flag]) => { |
| 26 | + const required = flag.required ? "**Required:** Yes" : "**Required:** No"; |
| 27 | + const typeValue = flag.type === "option" ? flag.helpValue ?? "" : "<flag>"; |
| 28 | + const type = `**Type:** \`${typeValue}\``; |
| 29 | + const description = flag.description ?? ""; |
| 30 | + const flagName = flag.char ? `-${flag.char}, --${name}` : `--${name}`; |
| 31 | + return `- **\`${flagName}\`** \n ${description} \n ${type} ${required}`; |
| 32 | + }) |
| 33 | + .join("\n\n") |
| 34 | + : ""; |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | + let examplesText = ""; |
| 39 | + |
| 40 | + if (command.examples){ |
| 41 | + for (const example of command.examples) { |
| 42 | + if (typeof example === "string") { |
| 43 | + examplesText += `\n${example}`; |
| 44 | + } |
| 45 | + |
| 46 | + if(typeof example === "object") { |
| 47 | + examplesText += `\n# ${example.description}\n${example.command}\n`; |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + const commandName = command.id.split(":").join(" "); |
| 53 | + |
| 54 | + examplesText = examplesText.replaceAll("<%= config.bin %>", "itp").replaceAll("<%= command.id %>", commandName).trimEnd(); |
| 55 | + |
| 56 | + const apiReference = command.apiReference as string; |
| 57 | + const apiReferenceName = command.apiReferenceName as string; |
| 58 | + |
| 59 | + return `# itp ${commandName}\n\n${command.description || ""}\n\n## Options\n\n${options}\n\n## Examples\n\n\`\`\`bash${examplesText}\n\`\`\`\n\n## API Reference\n\n[${apiReferenceName}](${apiReference})`; |
| 60 | + } |
| 61 | + |
| 62 | + async generateDocs(config: Config, basePath: string) { |
| 63 | + const filteredCommands = config.commands.filter(c => !c.id.includes("help") && !c.id.includes("plugins") && !c.hidden); |
| 64 | + if(!filteredCommands) { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + for (const command of filteredCommands) { |
| 69 | + const markdown: string = this.generateCommandMarkdown(command, Object.entries(command.flags)); |
| 70 | + |
| 71 | + const commandDepth = command.id.split(":"); |
| 72 | + let filePath = basePath; |
| 73 | + for (const depth of commandDepth) { |
| 74 | + filePath = `${filePath}/${depth}`; |
| 75 | + } |
| 76 | + |
| 77 | + this.writeToFile(filePath, markdown); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + async run() { |
| 82 | + const {flags} = await this.parse(DocsGenerator); |
| 83 | + |
| 84 | + const config = await Config.load( |
| 85 | + { |
| 86 | + devPlugins: false, |
| 87 | + root: process.cwd(), |
| 88 | + userPlugins: false, |
| 89 | + } |
| 90 | + ); |
| 91 | + |
| 92 | + this.generateDocs(config, flags["output-dir"] ?? `${config.root}/docs`); |
| 93 | + } |
| 94 | + |
| 95 | + writeToFile(filePath: string, markdown: string) { |
| 96 | + const finalPath = `${filePath}.md`; |
| 97 | + console.log(`Writing to directory: ${finalPath}`); |
| 98 | + console.log(markdown); |
| 99 | + |
| 100 | + const dirName = path.dirname(finalPath); |
| 101 | + if (!fs.existsSync(dirName)) { |
| 102 | + fs.mkdirSync(dirName, { |
| 103 | + recursive: true, |
| 104 | + }); |
| 105 | + } |
| 106 | + |
| 107 | + fs.writeFileSync(finalPath, markdown); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | + |
| 112 | + |
0 commit comments