Browse Source

chore: cleanup

adamelmore 2 months ago
parent
commit
407f34fed5
1 changed files with 42 additions and 41 deletions
  1. 42 41
      packages/opencode/src/worktree/index.ts

+ 42 - 41
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.Info>(["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.Info>(["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.Info>(["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
   })