From 1f49934f594168fd860697c8ab4b124ce9eefd34 Mon Sep 17 00:00:00 2001 From: AmirSa12 Date: Sat, 13 Sep 2025 15:18:50 +0330 Subject: [PATCH 1/2] add bun handler --- bin/handlers/bun-handler.ts | 197 +++++++++++++++++++++++++++++++++++- 1 file changed, 196 insertions(+), 1 deletion(-) diff --git a/bin/handlers/bun-handler.ts b/bin/handlers/bun-handler.ts index 225b049..673cf92 100644 --- a/bin/handlers/bun-handler.ts +++ b/bin/handlers/bun-handler.ts @@ -1,5 +1,200 @@ import type { PackageManagerCompletion } from '../package-manager-completion.js'; +import { stripAnsiEscapes, type ParsedOption } from '../utils/text-utils.js'; +import { + LazyCommand, + OptionHandlers, + commonOptionHandlers, + setupLazyOptionLoading, + setupCommandArguments, + safeExec, + safeExecSync, +} from '../utils/shared.js'; + +const COMMANDS_SECTION_RE = /^Commands:\s*$/i; +const FLAGS_SECTION_RE = /^Flags:\s*$/i; +const SECTION_END_RE = /^(Examples|Full documentation|Learn more)/i; +const COMMAND_VALIDATION_RE = /^[a-z][a-z0-9-]*$/; +const BUN_OPTION_RE = + /^\s*(?:-([a-zA-Z]),?\s*)?--([a-z][a-z0-9-]*)(?:=<[^>]+>)?\s+(.+)$/; +const MAIN_COMMAND_RE = /^ ([a-z][a-z0-9-]*)\s+(.+)$/; +const CONTINUATION_COMMAND_RE = /^\s{12,}([a-z][a-z0-9-]*)\s+(.+)$/; +const EMPTY_LINE_FOLLOWED_BY_NON_COMMAND_RE = /^\s+[a-z]/; +const DESCRIPTION_SPLIT_RE = /\s{2,}/; +const CAPITAL_LETTER_START_RE = /^[A-Z]/; +const LINE_SPLIT_RE = /\r?\n/; + +function toLines(text: string): string[] { + return stripAnsiEscapes(text).split(LINE_SPLIT_RE); +} + +function findSectionStart(lines: string[], header: RegExp): number { + for (let i = 0; i < lines.length; i++) { + if (header.test(lines[i].trim())) return i + 1; + } + return -1; +} + +const bunOptionHandlers: OptionHandlers = { + ...commonOptionHandlers, + + silent(complete) { + complete('true', 'Enable silent mode'); + complete('false', 'Disable silent mode'); + }, + + backend(complete) { + complete('clonefile', 'Clone files (default, fastest)'); + complete('hardlink', 'Use hard links'); + complete('symlink', 'Use symbolic links'); + complete('copyfile', 'Copy files'); + }, + + linker(complete) { + complete('isolated', 'Isolated linker strategy'); + complete('hoisted', 'Hoisted linker strategy'); + }, + + omit(complete) { + complete('dev', 'Omit devDependencies'); + complete('optional', 'Omit optionalDependencies'); + complete('peer', 'Omit peerDependencies'); + }, + + shell(complete) { + complete('bun', 'Use Bun shell'); + complete('system', 'Use system shell'); + }, + + 'unhandled-rejections'(complete) { + complete('strict', 'Strict unhandled rejection handling'); + complete('throw', 'Throw on unhandled rejections'); + complete('warn', 'Warn on unhandled rejections'); + complete('none', 'Ignore unhandled rejections'); + complete('warn-with-error-code', 'Warn with error code'); + }, +}; + +/** ---------- Commands ---------- */ +export function parseBunHelp(helpText: string): Record { + const lines = toLines(helpText); + + const startIndex = findSectionStart(lines, COMMANDS_SECTION_RE); + if (startIndex === -1) return {}; + + const commands: Record = {}; + + for (let i = startIndex; i < lines.length; i++) { + const line = lines[i]; + + // stop when we hit Flags section or empty line followed by non-command content + if ( + FLAGS_SECTION_RE.test(line.trim()) || + (line.trim() === '' && + i + 1 < lines.length && + !lines[i + 1].match(EMPTY_LINE_FOLLOWED_BY_NON_COMMAND_RE)) + ) { + break; + } + + if (!line.trim()) continue; + + // main command row + const main = line.match(MAIN_COMMAND_RE); + if (main) { + const [, command, rest] = main; + if (COMMAND_VALIDATION_RE.test(command)) { + const parts = rest.split(DESCRIPTION_SPLIT_RE); + let desc = parts[parts.length - 1]; + + if (desc && CAPITAL_LETTER_START_RE.test(desc)) { + commands[command] = desc.trim(); + } else if (parts.length > 1) { + for (const p of parts) { + if (CAPITAL_LETTER_START_RE.test(p)) { + commands[command] = p.trim(); + break; + } + } + } + } + } + + const cont = line.match(CONTINUATION_COMMAND_RE); + if (cont) { + const [, command, description] = cont; + if (COMMAND_VALIDATION_RE.test(command)) { + commands[command] = description.trim(); + } + } + } + + return commands; +} + +export async function getBunCommandsFromMainHelp(): Promise< + Record +> { + const output = await safeExec('bun --help'); + return output ? parseBunHelp(output) : {}; +} + +export function parseBunOptions( + helpText: string, + { flagsOnly = true }: { flagsOnly?: boolean } = {} +): ParsedOption[] { + const lines = toLines(helpText); + const out: ParsedOption[] = []; + + const start = findSectionStart(lines, FLAGS_SECTION_RE); + if (start === -1) return out; + + for (let i = start; i < lines.length; i++) { + const line = lines[i]; + if (SECTION_END_RE.test(line.trim())) break; + + const m = line.match(BUN_OPTION_RE); + if (!m) continue; + + const [, short, long, desc] = m; + const takesValue = line.includes('=<'); // bun shows value as --opt= + if (flagsOnly && takesValue) continue; + + out.push({ + short: short || undefined, + long, + desc: desc.trim(), + }); + } + + return out; +} + +function loadBunOptionsSync(cmd: LazyCommand, command: string): void { + const output = safeExecSync(`bun ${command} --help`); + if (!output) return; + + const options = parseBunOptions(output, { flagsOnly: false }); + + for (const { long, short, desc } of options) { + const exists = cmd.optionsRaw?.get?.(long); + if (exists) continue; + + const handler = bunOptionHandlers[long]; + if (handler) cmd.option(long, desc, handler, short); + else cmd.option(long, desc, short); + } +} export async function setupBunCompletions( completion: PackageManagerCompletion -): Promise {} +): Promise { + try { + const commands = await getBunCommandsFromMainHelp(); + + for (const [command, description] of Object.entries(commands)) { + const c = completion.command(command, description); + setupCommandArguments(c, command, 'bun'); + setupLazyOptionLoading(c, command, 'bun', loadBunOptionsSync); + } + } catch {} +} From 64e6f5a976788d83a525668521c03be6392b18fe Mon Sep 17 00:00:00 2001 From: AmirSa12 Date: Sun, 14 Sep 2025 11:45:18 +0330 Subject: [PATCH 2/2] update --- bin/handlers/bun-handler.ts | 38 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/bin/handlers/bun-handler.ts b/bin/handlers/bun-handler.ts index 673cf92..24753ac 100644 --- a/bin/handlers/bun-handler.ts +++ b/bin/handlers/bun-handler.ts @@ -37,44 +37,38 @@ function findSectionStart(lines: string[], header: RegExp): number { const bunOptionHandlers: OptionHandlers = { ...commonOptionHandlers, - silent(complete) { - complete('true', 'Enable silent mode'); - complete('false', 'Disable silent mode'); - }, - backend(complete) { - complete('clonefile', 'Clone files (default, fastest)'); - complete('hardlink', 'Use hard links'); - complete('symlink', 'Use symbolic links'); - complete('copyfile', 'Copy files'); + complete('clonefile', ' '); + complete('hardlink', ' '); + complete('symlink', ' '); + complete('copyfile', ' '); }, linker(complete) { - complete('isolated', 'Isolated linker strategy'); - complete('hoisted', 'Hoisted linker strategy'); + complete('isolated', ' '); + complete('hoisted', ' '); }, omit(complete) { - complete('dev', 'Omit devDependencies'); - complete('optional', 'Omit optionalDependencies'); - complete('peer', 'Omit peerDependencies'); + complete('dev', ' '); + complete('optional', ' '); + complete('peer', ' '); }, shell(complete) { - complete('bun', 'Use Bun shell'); - complete('system', 'Use system shell'); + complete('bun', ' '); + complete('system', ' '); }, 'unhandled-rejections'(complete) { - complete('strict', 'Strict unhandled rejection handling'); - complete('throw', 'Throw on unhandled rejections'); - complete('warn', 'Warn on unhandled rejections'); - complete('none', 'Ignore unhandled rejections'); - complete('warn-with-error-code', 'Warn with error code'); + complete('strict', ' '); + complete('throw', ' '); + complete('warn', ' '); + complete('none', ' '); + complete('warn-with-error-code', ' '); }, }; -/** ---------- Commands ---------- */ export function parseBunHelp(helpText: string): Record { const lines = toLines(helpText);