| 1 | /* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | import { Argument, Command, Option } from "commander"; |
| 3 | import prettier from "prettier"; |
| 4 | |
| 5 | export interface Options { |
| 6 | figSpecCommandName?: string; |
| 7 | } |
| 8 | |
| 9 | const DEFAULT_FIG_SUBCOMMAND_NAME = "generate-fig-spec"; |
| 10 | |
| 11 | function convertDefaultValue(v: unknown) { |
| 12 | if (typeof v === "string") return v; |
| 13 | if (Array.isArray(v)) return v.join(","); |
| 14 | if (typeof v === "object") return undefined; |
| 15 | return String(v); |
| 16 | } |
| 17 | |
| 18 | async function getTemplate(spec: Fig.Spec): Promise<string> { |
| 19 | return prettier.format( |
| 20 | ` |
| 21 | // Autogenerated by @fig/complete-commander |
| 22 | |
| 23 | const completionSpec: Fig.Spec = ${JSON.stringify(spec)} |
| 24 | |
| 25 | export default completionSpec; |
| 26 | `, |
| 27 | { parser: "typescript" } |
| 28 | ); |
| 29 | } |
| 30 | |
| 31 | function generateArg(_arg: Argument & Record<string, any>): Fig.Arg { |
| 32 | const { _name: name, description, required, variadic, defaultValue } = _arg; |
| 33 | |
| 34 | const arg: Fig.Arg = { name }; |
| 35 | |
| 36 | if (description) arg.description = description; |
| 37 | if (!required) arg.isOptional = true; |
| 38 | if (variadic) arg.isVariadic = true; |
| 39 | if (defaultValue) arg.default = convertDefaultValue(defaultValue); |
| 40 | return arg; |
| 41 | } |
| 42 | |
| 43 | function generateOption(_option: Option & Record<string, any>): Fig.Option { |
| 44 | const { |
| 45 | short, |
| 46 | long, |
| 47 | flags, |
| 48 | description, |
| 49 | mandatory, |
| 50 | required, |
| 51 | optional, |
| 52 | variadic, |
| 53 | argChoices, |
| 54 | defaultValue, |
| 55 | } = _option; |
| 56 | |
| 57 | const name = new Set<string>(); |
| 58 | if (short) name.add(short); |
| 59 | if (long) name.add(long); |
| 60 | const option: Fig.Option = { name: Array.from(name) }; |
| 61 | |
| 62 | if (description) option.description = description; |
| 63 | if (mandatory) option.isRequired = true; |
| 64 | // Option argument e.g. "-f, --flag <string>" |
| 65 | // If required and optional are both false it does not have an argument |
| 66 | if (required || optional) { |
| 67 | // eslint-disable-next-line no-useless-escape |
| 68 | const matches = flags.match(/.*[\[<](.*)[\]>]/); // !!! This is all but an useless escape. It is required to make the regex working |
| 69 | |
| 70 | const arg: Fig.Arg = { |
| 71 | name: matches ? matches[1].replace(/\./g, "") : "", |
| 72 | }; |
| 73 | if (optional) arg.isOptional = true; |
| 74 | if (variadic) arg.isVariadic = true; |
| 75 | if (argChoices) arg.suggestions = argChoices; |
| 76 | if (defaultValue) arg.default = convertDefaultValue(defaultValue); |
| 77 | option.args = arg; |
| 78 | } |
| 79 | return option; |
| 80 | } |
| 81 | |
| 82 | interface ExtendedCommand extends Command { |
| 83 | _name: string; |
| 84 | _description: string; |
| 85 | _aliases: string[]; |
| 86 | _args: Argument[]; |
| 87 | options: Option[]; |
| 88 | _helpDescription: string; |
| 89 | _helpShortFlag: string; |
| 90 | _helpLongFlag: string; |
| 91 | _addImplicitHelpCommandL?: boolean; // Deliberately undefined, not decided whether true or false |
| 92 | _helpCommandName: string; |
| 93 | _helpCommandnameAndArgs: string; |
| 94 | _helpCommandDescription: string; |
| 95 | _hasHelpOption: boolean; |
| 96 | _hidden: boolean; |
| 97 | } |
| 98 | |
| 99 | function helpSubcommand({ |
| 100 | _helpCommandName, // 'help' |
| 101 | _helpCommandDescription, |
| 102 | _helpCommandnameAndArgs, // 'help [cmd]' |
| 103 | }: ExtendedCommand): Fig.Subcommand { |
| 104 | const [, arg] = _helpCommandnameAndArgs.split(" "); |
| 105 | return { |
| 106 | name: _helpCommandName, |
| 107 | description: _helpCommandDescription, |
| 108 | priority: 49, |
| 109 | ...(arg && { |
| 110 | args: { |
| 111 | name: arg.slice(1, -1), |
| 112 | isOptional: true, |
| 113 | template: "help", |
| 114 | }, |
| 115 | }), |
| 116 | }; |
| 117 | } |
| 118 | |
| 119 | function helpOption({ |
| 120 | _helpDescription, |
| 121 | _helpShortFlag, |
| 122 | _helpLongFlag, |
| 123 | }: ExtendedCommand): Fig.Option { |
| 124 | return { |
| 125 | name: [_helpShortFlag, _helpLongFlag], |
| 126 | description: _helpDescription, |
| 127 | priority: 49, |
| 128 | }; |
| 129 | } |
| 130 | |
| 131 | function generateCommand( |
| 132 | _command: Command & Record<string, any>, |
| 133 | figSpecCommandName: string |
| 134 | ): Fig.Subcommand | undefined { |
| 135 | const { |
| 136 | _name, |
| 137 | _description, |
| 138 | _aliases, |
| 139 | commands, |
| 140 | _args, |
| 141 | options, |
| 142 | _addImplicitHelpCommandL, |
| 143 | _hasHelpOption, |
| 144 | _hidden, |
| 145 | } = _command as ExtendedCommand; |
| 146 | |
| 147 | if (_name === figSpecCommandName) return undefined; |
| 148 | const name = _aliases.length > 0 ? Array.from(new Set([_name, ..._aliases])) : _name; |
| 149 | const command: Fig.Subcommand = { name }; |
| 150 | |
| 151 | if (_description) command.description = _description; |
| 152 | if (_hidden) command.hidden = true; |
| 153 | // Subcommands |
| 154 | if (commands.length) { |
| 155 | command.subcommands = []; |
| 156 | for (const cmd of commands) { |
| 157 | const subcommand = generateCommand(cmd, figSpecCommandName); |
| 158 | if (subcommand) command.subcommands.push(subcommand); |
| 159 | } |
| 160 | if (_addImplicitHelpCommandL !== false) { |
| 161 | command.subcommands.push(helpSubcommand(_command as ExtendedCommand)); |
| 162 | } |
| 163 | } |
| 164 | // Options |
| 165 | command.options = []; |
| 166 | if (options.length) { |
| 167 | command.options = options.map(generateOption); |
| 168 | } |
| 169 | if (_hasHelpOption) { |
| 170 | command.options.push(helpOption(_command as ExtendedCommand)); |
| 171 | } |
| 172 | // Args |
| 173 | if (_args.length) { |
| 174 | command.args = _args.map(generateArg); |
| 175 | } |
| 176 | return command; |
| 177 | } |
| 178 | |
| 179 | export async function generateCompletionSpec( |
| 180 | command: Command, |
| 181 | options?: Options |
| 182 | ): Promise<string | undefined> { |
| 183 | const figSpecCommandName = options?.figSpecCommandName || DEFAULT_FIG_SUBCOMMAND_NAME; |
| 184 | // The first subcommand will never have the name of the `figSpecCommandName` |
| 185 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 186 | const spec = getTemplate(generateCommand(command, figSpecCommandName)!); |
| 187 | return spec; |
| 188 | } |
| 189 | |
| 190 | export function addCompletionSpecCommand(command: Command) { |
| 191 | command |
| 192 | .command(DEFAULT_FIG_SUBCOMMAND_NAME) |
| 193 | .description("Print the Fig completion spec") |
| 194 | .action(async () => { |
| 195 | const spec = await generateCompletionSpec(command); |
| 196 | console.log(spec); |
| 197 | }); |
| 198 | } |