import type { RuntimeWorktreeListResult, RuntimeWorktreePsResult, RuntimeWorktreeRecord } from '../../shared/runtime-types' import type { CommandHandler } from '../dispatch' import { formatWorktreeList, formatWorktreePs, formatWorktreeShow, printResult } from '../format' import { getOptionalNullableNumberFlag, getOptionalNumberFlag, getOptionalPositiveIntegerFlag, getOptionalStringFlag, getRequiredStringFlag } from '../flags' import { getRequiredWorktreeSelector, resolveCurrentWorktreeSelector } from '../selectors' type HookWarningResult = { warning?: string } function printHookWarning(result: HookWarningResult, json: boolean): void { if (!json && result.warning) { console.error(`warning: ${result.warning}`) } } export const WORKTREE_HANDLERS: Record = { 'worktree ps': async ({ flags, client, json }) => { const result = await client.call('worktree.ps', { limit: getOptionalPositiveIntegerFlag(flags, 'limit') }) printResult(result, json, formatWorktreePs) }, 'worktree list': async ({ flags, client, json }) => { const result = await client.call('worktree.list', { repo: getOptionalStringFlag(flags, 'repo'), limit: getOptionalPositiveIntegerFlag(flags, 'limit') }) printResult(result, json, formatWorktreeList) }, 'worktree show': async ({ flags, client, cwd, json }) => { const result = await client.call<{ worktree: RuntimeWorktreeRecord }>('worktree.show', { worktree: await getRequiredWorktreeSelector(flags, 'worktree', cwd, client) }) printResult(result, json, formatWorktreeShow) }, 'worktree current': async ({ client, cwd, json }) => { const result = await client.call<{ worktree: RuntimeWorktreeRecord }>('worktree.show', { worktree: await resolveCurrentWorktreeSelector(cwd, client) }) printResult(result, json, formatWorktreeShow) }, 'worktree create': async ({ flags, client, json }) => { const result = await client.call<{ worktree: RuntimeWorktreeRecord; warning?: string }>( 'worktree.create', { repo: getRequiredStringFlag(flags, 'repo'), name: getRequiredStringFlag(flags, 'name'), baseBranch: getOptionalStringFlag(flags, 'base-branch'), linkedIssue: getOptionalNumberFlag(flags, 'issue'), comment: getOptionalStringFlag(flags, 'comment'), runHooks: flags.get('run-hooks') === true, activate: flags.get('activate') === true || flags.get('run-hooks') === true } ) printHookWarning(result.result, json) printResult(result, json, formatWorktreeShow) }, 'worktree set': async ({ flags, client, cwd, json }) => { const result = await client.call<{ worktree: RuntimeWorktreeRecord }>('worktree.set', { worktree: await getRequiredWorktreeSelector(flags, 'worktree', cwd, client), displayName: getOptionalStringFlag(flags, 'display-name'), linkedIssue: getOptionalNullableNumberFlag(flags, 'issue'), comment: getOptionalStringFlag(flags, 'comment') }) printResult(result, json, formatWorktreeShow) }, 'worktree rm': async ({ flags, client, cwd, json }) => { const result = await client.call<{ removed: boolean; warning?: string }>('worktree.rm', { worktree: await getRequiredWorktreeSelector(flags, 'worktree', cwd, client), force: flags.get('force') === true, runHooks: flags.get('run-hooks') === true }) printHookWarning(result.result, json) printResult(result, json, (value) => `removed: ${value.removed}`) } }