|
| 1 | +import * as zsh from './zsh'; |
| 2 | +import * as bash from './bash'; |
| 3 | +import * as fish from './fish'; |
| 4 | +import * as powershell from './powershell'; |
| 5 | +import type { Command as CommanderCommand } from 'commander'; |
| 6 | +import { Completion } from './'; |
| 7 | + |
| 8 | +const execPath = process.execPath; |
| 9 | +const processArgs = process.argv.slice(1); |
| 10 | +const quotedExecPath = quoteIfNeeded(execPath); |
| 11 | +const quotedProcessArgs = processArgs.map(quoteIfNeeded); |
| 12 | +const quotedProcessExecArgs = process.execArgv.map(quoteIfNeeded); |
| 13 | + |
| 14 | +const x = `${quotedExecPath} ${quotedProcessExecArgs.join(' ')} ${quotedProcessArgs[0]}`; |
| 15 | + |
| 16 | +function quoteIfNeeded(path: string): string { |
| 17 | + return path.includes(' ') ? `'${path}'` : path; |
| 18 | +} |
| 19 | + |
| 20 | +export default function tab(instance: CommanderCommand): Completion { |
| 21 | + const completion = new Completion(); |
| 22 | + const programName = instance.name(); |
| 23 | + |
| 24 | + // Process the root command |
| 25 | + processRootCommand(completion, instance, programName); |
| 26 | + |
| 27 | + // Process all subcommands |
| 28 | + processSubcommands(completion, instance, programName); |
| 29 | + |
| 30 | + // Add the complete command |
| 31 | + instance |
| 32 | + .command('complete [shell]') |
| 33 | + .allowUnknownOption(true) |
| 34 | + .description('Generate shell completion scripts') |
| 35 | + .action(async (shell, options) => { |
| 36 | + // Check if there are arguments after -- |
| 37 | + const dashDashIndex = process.argv.indexOf('--'); |
| 38 | + let extra: string[] = []; |
| 39 | + |
| 40 | + if (dashDashIndex !== -1) { |
| 41 | + extra = process.argv.slice(dashDashIndex + 1); |
| 42 | + // If shell is actually part of the extra args, adjust accordingly |
| 43 | + if (shell && extra.length > 0 && shell === '--') { |
| 44 | + shell = undefined; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + switch (shell) { |
| 49 | + case 'zsh': { |
| 50 | + const script = zsh.generate(programName, x); |
| 51 | + console.log(script); |
| 52 | + break; |
| 53 | + } |
| 54 | + case 'bash': { |
| 55 | + const script = bash.generate(programName, x); |
| 56 | + console.log(script); |
| 57 | + break; |
| 58 | + } |
| 59 | + case 'fish': { |
| 60 | + const script = fish.generate(programName, x); |
| 61 | + console.log(script); |
| 62 | + break; |
| 63 | + } |
| 64 | + case 'powershell': { |
| 65 | + const script = powershell.generate(programName, x); |
| 66 | + console.log(script); |
| 67 | + break; |
| 68 | + } |
| 69 | + case 'debug': { |
| 70 | + // Debug mode to print all collected commands |
| 71 | + const commandMap = new Map<string, CommanderCommand>(); |
| 72 | + collectCommands(instance, '', commandMap); |
| 73 | + console.log('Collected commands:'); |
| 74 | + for (const [path, cmd] of commandMap.entries()) { |
| 75 | + console.log( |
| 76 | + `- ${path || '<root>'}: ${cmd.description() || 'No description'}` |
| 77 | + ); |
| 78 | + } |
| 79 | + break; |
| 80 | + } |
| 81 | + default: { |
| 82 | + // Parse current command context for autocompletion |
| 83 | + return completion.parse(extra); |
| 84 | + } |
| 85 | + } |
| 86 | + }); |
| 87 | + |
| 88 | + return completion; |
| 89 | +} |
| 90 | + |
| 91 | +function processRootCommand( |
| 92 | + completion: Completion, |
| 93 | + command: CommanderCommand, |
| 94 | + programName: string |
| 95 | +): void { |
| 96 | + // Add the root command |
| 97 | + completion.addCommand('', command.description() || '', [], async () => []); |
| 98 | + |
| 99 | + // Add root command options |
| 100 | + for (const option of command.options) { |
| 101 | + // Extract short flag from the name if it exists (e.g., "-c, --config" -> "c") |
| 102 | + const flags = option.flags; |
| 103 | + const shortFlag = flags.match(/^-([a-zA-Z]), --/)?.[1]; |
| 104 | + const longFlag = flags.match(/--([a-zA-Z0-9-]+)/)?.[1]; |
| 105 | + |
| 106 | + if (longFlag) { |
| 107 | + completion.addOption( |
| 108 | + '', |
| 109 | + `--${longFlag}`, |
| 110 | + option.description || '', |
| 111 | + async () => [], |
| 112 | + shortFlag |
| 113 | + ); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +function processSubcommands( |
| 119 | + completion: Completion, |
| 120 | + rootCommand: CommanderCommand, |
| 121 | + programName: string |
| 122 | +): void { |
| 123 | + // Build a map of command paths |
| 124 | + const commandMap = new Map<string, CommanderCommand>(); |
| 125 | + |
| 126 | + // Collect all commands with their full paths |
| 127 | + collectCommands(rootCommand, '', commandMap); |
| 128 | + |
| 129 | + // Process each command |
| 130 | + for (const [path, cmd] of commandMap.entries()) { |
| 131 | + if (path === '') continue; // Skip root command, already processed |
| 132 | + |
| 133 | + // Extract positional arguments from usage |
| 134 | + const usage = cmd.usage(); |
| 135 | + const args = (usage?.match(/\[.*?\]|<.*?>/g) || []).map((arg) => |
| 136 | + arg.startsWith('[') |
| 137 | + ); // true if optional (wrapped in []) |
| 138 | + |
| 139 | + // Add command to completion |
| 140 | + completion.addCommand(path, cmd.description() || '', args, async () => []); |
| 141 | + |
| 142 | + // Add command options |
| 143 | + for (const option of cmd.options) { |
| 144 | + // Extract short flag from the name if it exists (e.g., "-c, --config" -> "c") |
| 145 | + const flags = option.flags; |
| 146 | + const shortFlag = flags.match(/^-([a-zA-Z]), --/)?.[1]; |
| 147 | + const longFlag = flags.match(/--([a-zA-Z0-9-]+)/)?.[1]; |
| 148 | + |
| 149 | + if (longFlag) { |
| 150 | + completion.addOption( |
| 151 | + path, |
| 152 | + `--${longFlag}`, |
| 153 | + option.description || '', |
| 154 | + async () => [], |
| 155 | + shortFlag |
| 156 | + ); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + // For commands with subcommands, add a special handler |
| 161 | + if (cmd.commands.length > 0) { |
| 162 | + const subcommandNames = cmd.commands |
| 163 | + .filter((subcmd) => subcmd.name() !== 'complete') |
| 164 | + .map((subcmd) => ({ |
| 165 | + value: subcmd.name(), |
| 166 | + description: subcmd.description() || '', |
| 167 | + })); |
| 168 | + |
| 169 | + if (subcommandNames.length > 0) { |
| 170 | + const cmdObj = completion.commands.get(path); |
| 171 | + if (cmdObj) { |
| 172 | + cmdObj.handler = async () => subcommandNames; |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +function collectCommands( |
| 180 | + command: CommanderCommand, |
| 181 | + parentPath: string, |
| 182 | + commandMap: Map<string, CommanderCommand> |
| 183 | +): void { |
| 184 | + // Add this command to the map |
| 185 | + commandMap.set(parentPath, command); |
| 186 | + |
| 187 | + // Process subcommands |
| 188 | + for (const subcommand of command.commands) { |
| 189 | + // Skip the completion command |
| 190 | + if (subcommand.name() === 'complete') continue; |
| 191 | + |
| 192 | + // Build the full path for this subcommand |
| 193 | + const subcommandPath = parentPath |
| 194 | + ? `${parentPath} ${subcommand.name()}` |
| 195 | + : subcommand.name(); |
| 196 | + |
| 197 | + // Recursively collect subcommands |
| 198 | + collectCommands(subcommand, subcommandPath, commandMap); |
| 199 | + } |
| 200 | +} |
0 commit comments