From 407f34fed5140c4eb3b378c606a422de7e313d9a Mon Sep 17 00:00:00 2001 From: adamelmore <2363879+adamdottv@users.noreply.github.com> Date: Sun, 25 Jan 2026 13:19:23 -0600 Subject: [PATCH] chore: cleanup --- packages/opencode/src/worktree/index.ts | 83 +++++++++++++------------ 1 file changed, 42 insertions(+), 41 deletions(-) diff --git a/packages/opencode/src/worktree/index.ts b/packages/opencode/src/worktree/index.ts index dd165d56f..0f2e2f4a0 100644 --- a/packages/opencode/src/worktree/index.ts +++ b/packages/opencode/src/worktree/index.ts @@ -244,6 +244,46 @@ export namespace Worktree { return $`bash -lc ${cmd}`.nothrow().cwd(directory) } + type StartKind = "project" | "worktree" + + async function runStartScript(directory: string, cmd: string, kind: StartKind) { + const text = cmd.trim() + if (!text) return true + + const ran = await runStartCommand(directory, text) + if (ran.exitCode === 0) return true + + log.error("worktree start command failed", { + kind, + directory, + message: errorText(ran), + }) + return false + } + + async function runStartScripts(directory: string, input: { projectID: string; extra?: string }) { + const project = await Storage.read(["project", input.projectID]).catch(() => undefined) + const startup = project?.commands?.start?.trim() ?? "" + const ok = await runStartScript(directory, startup, "project") + if (!ok) return false + + const extra = input.extra ?? "" + await runStartScript(directory, extra, "worktree") + return true + } + + function queueStartScripts(directory: string, input: { projectID: string; extra?: string }) { + setTimeout(() => { + const start = async () => { + await runStartScripts(directory, input) + } + + void start().catch((error) => { + log.error("worktree start task failed", { directory, error }) + }) + }, 0) + } + export const create = fn(CreateInput.optional(), async (input) => { if (Instance.project.vcs !== "git") { throw new NotGitError({ message: "Worktrees are only supported for git projects" }) @@ -318,27 +358,7 @@ export namespace Worktree { }, }) - const project = await Storage.read(["project", projectID]).catch(() => undefined) - const startup = project?.commands?.start?.trim() ?? "" - - const run = async (cmd: string, kind: "project" | "worktree") => { - const ran = await runStartCommand(info.directory, cmd) - if (ran.exitCode === 0) return true - log.error("worktree start command failed", { - kind, - directory: info.directory, - message: errorText(ran), - }) - return false - } - - if (startup) { - const ok = await run(startup, "project") - if (!ok) return - } - if (extra) { - await run(extra, "worktree") - } + await runStartScripts(info.directory, { projectID, extra }) } void start().catch((error) => { @@ -522,26 +542,7 @@ export namespace Worktree { } const projectID = Instance.project.id - setTimeout(() => { - const start = async () => { - const project = await Storage.read(["project", projectID]).catch(() => undefined) - const startup = project?.commands?.start?.trim() ?? "" - if (!startup) return - - const ran = await runStartCommand(worktreePath, startup) - if (ran.exitCode === 0) return - - log.error("worktree start command failed", { - kind: "project", - directory: worktreePath, - message: errorText(ran), - }) - } - - void start().catch((error) => { - log.error("worktree start task failed", { directory: worktreePath, error }) - }) - }, 0) + queueStartScripts(worktreePath, { projectID }) return true })