| 1 | import { Command } from "@oclif/core"; |
| 2 | import * as fs from "node:fs"; |
| 3 | import * as prettier from "prettier"; |
| 4 | |
| 5 | function getFigArgs(args: Command.Arg.Cached[]): Fig.Arg[] { |
| 6 | const figArgs: Fig.Arg[] = []; |
| 7 | for (const arg of args) { |
| 8 | if (arg.hidden) continue; |
| 9 | figArgs.push({ |
| 10 | name: arg.name, |
| 11 | ...(arg.description && { description: arg.description }), |
| 12 | ...(arg.default && { default: `${arg.default}` }), |
| 13 | ...(arg.options?.length && { suggestions: arg.options }), |
| 14 | ...(arg.required === false && { isOptional: true }), |
| 15 | }); |
| 16 | } |
| 17 | return figArgs; |
| 18 | } |
| 19 | |
| 20 | function getFigOptions(options: Command.Flag.Cached[]): Fig.Option[] { |
| 21 | const figOptions: Fig.Option[] = []; |
| 22 | for (const flag of options) { |
| 23 | figOptions.push({ |
| 24 | name: flag.char ? [`-${flag.char}`, `--${flag.name}`] : `--${flag.name}`, |
| 25 | ...(flag.description && { description: flag.description }), |
| 26 | ...(flag.type === "option" && { |
| 27 | args: { |
| 28 | description: flag.helpValue, |
| 29 | ...(flag.options?.length && { suggestions: flag.options }), |
| 30 | ...(flag.default && { default: `${flag.default}` }), |
| 31 | }, |
| 32 | }), |
| 33 | ...(flag.required === true && { isRequired: true }), |
| 34 | ...(flag.type === "boolean" && flag.allowNo && { exclusiveOn: [`--no-${flag.name}`] }), |
| 35 | hidden: flag.hidden, |
| 36 | }); |
| 37 | if (flag.type === "boolean" && flag.allowNo) { |
| 38 | figOptions.push({ |
| 39 | name: `--no-${flag.name}`, |
| 40 | }); |
| 41 | } |
| 42 | } |
| 43 | return figOptions; |
| 44 | } |
| 45 | |
| 46 | function getFigSubcommands(commands: Command.Loadable[]): Fig.Subcommand[] { |
| 47 | const subcommands: Fig.Subcommand[] = []; |
| 48 | for (const command of commands) { |
| 49 | // skip this command |
| 50 | if (command.id === "generate-fig-spec") continue; |
| 51 | |
| 52 | const options: Fig.Option[] = getFigOptions(Object.values(command.flags)); |
| 53 | const args: Fig.Arg[] = getFigArgs(Object.values(command.args)); |
| 54 | |
| 55 | subcommands.push({ |
| 56 | name: command.id, |
| 57 | ...(command.description && { description: command.description }), |
| 58 | ...(options.length && { options }), |
| 59 | ...(args.length && { args }), |
| 60 | hidden: command.hidden, |
| 61 | }); |
| 62 | } |
| 63 | return subcommands; |
| 64 | } |
| 65 | |
| 66 | export class GenerateFigSpecCommand extends Command { |
| 67 | static description = "Generate a Fig completion spec"; |
| 68 | |
| 69 | async run() { |
| 70 | const { flags: parsedFlags } = await this.parse(GenerateFigSpecCommand); |
| 71 | |
| 72 | const spec: Fig.Spec = { |
| 73 | name: this.config.name, |
| 74 | subcommands: getFigSubcommands(this.config.commands), |
| 75 | }; |
| 76 | |
| 77 | const template = await prettier.format( |
| 78 | `// Autogenerated by @fig/complete-oclif |
| 79 | |
| 80 | const completionSpec: Fig.Spec = ${JSON.stringify(spec)}; |
| 81 | |
| 82 | export default completionSpec; |
| 83 | `, |
| 84 | { parser: "typescript" } |
| 85 | ); |
| 86 | |
| 87 | if (parsedFlags.output) { |
| 88 | fs.writeFileSync(parsedFlags.output, template); |
| 89 | } else { |
| 90 | this.log(template); |
| 91 | } |
| 92 | } |
| 93 | } |