feat(app): add workspace startup script to projects
This commit is contained in:
@@ -29,6 +29,11 @@ export namespace Project {
|
||||
color: z.string().optional(),
|
||||
})
|
||||
.optional(),
|
||||
commands: z
|
||||
.object({
|
||||
start: z.string().optional().describe("Startup script to run when creating a new workspace (worktree)"),
|
||||
})
|
||||
.optional(),
|
||||
time: z.object({
|
||||
created: z.number(),
|
||||
updated: z.number(),
|
||||
@@ -287,6 +292,7 @@ export namespace Project {
|
||||
projectID: z.string(),
|
||||
name: z.string().optional(),
|
||||
icon: Info.shape.icon.optional(),
|
||||
commands: Info.shape.commands.optional(),
|
||||
}),
|
||||
async (input) => {
|
||||
const result = await Storage.update<Info>(["project", input.projectID], (draft) => {
|
||||
@@ -299,6 +305,16 @@ export namespace Project {
|
||||
if (input.icon.override !== undefined) draft.icon.override = input.icon.override || undefined
|
||||
if (input.icon.color !== undefined) draft.icon.color = input.icon.color
|
||||
}
|
||||
|
||||
if (input.commands?.start !== undefined) {
|
||||
const start = input.commands.start || undefined
|
||||
draft.commands = {
|
||||
...(draft.commands ?? {}),
|
||||
}
|
||||
draft.commands.start = start
|
||||
if (!draft.commands.start) draft.commands = undefined
|
||||
}
|
||||
|
||||
draft.time.updated = Date.now()
|
||||
})
|
||||
GlobalBus.emit("event", {
|
||||
|
||||
@@ -90,7 +90,7 @@ export const ExperimentalRoutes = lazy(() =>
|
||||
"/worktree",
|
||||
describeRoute({
|
||||
summary: "Create worktree",
|
||||
description: "Create a new git worktree for the current project.",
|
||||
description: "Create a new git worktree for the current project and run any configured startup scripts.",
|
||||
operationId: "worktree.create",
|
||||
responses: {
|
||||
200: {
|
||||
|
||||
@@ -56,7 +56,7 @@ export const ProjectRoutes = lazy(() =>
|
||||
"/:projectID",
|
||||
describeRoute({
|
||||
summary: "Update project",
|
||||
description: "Update project properties such as name, icon and color.",
|
||||
description: "Update project properties such as name, icon, and commands.",
|
||||
operationId: "project.update",
|
||||
responses: {
|
||||
200: {
|
||||
|
||||
@@ -6,6 +6,7 @@ import { NamedError } from "@opencode-ai/util/error"
|
||||
import { Global } from "../global"
|
||||
import { Instance } from "../project/instance"
|
||||
import { Project } from "../project/project"
|
||||
import { Storage } from "../storage/storage"
|
||||
import { fn } from "../util/fn"
|
||||
import { Config } from "@/config/config"
|
||||
|
||||
@@ -25,7 +26,10 @@ export namespace Worktree {
|
||||
export const CreateInput = z
|
||||
.object({
|
||||
name: z.string().optional(),
|
||||
startCommand: z.string().optional(),
|
||||
startCommand: z
|
||||
.string()
|
||||
.optional()
|
||||
.describe("Additional startup script to run after the project's start command"),
|
||||
})
|
||||
.meta({
|
||||
ref: "WorktreeCreateInput",
|
||||
@@ -238,12 +242,23 @@ export namespace Worktree {
|
||||
throw new CreateFailedError({ message: errorText(created) || "Failed to create git worktree" })
|
||||
}
|
||||
|
||||
const cmd = input?.startCommand?.trim()
|
||||
if (!cmd) return info
|
||||
const project = await Storage.read<Project.Info>(["project", Instance.project.id]).catch(() => Instance.project)
|
||||
const startup = project.commands?.start?.trim()
|
||||
if (startup) {
|
||||
const ran = await runStartCommand(info.directory, startup)
|
||||
if (ran.exitCode !== 0) {
|
||||
throw new StartCommandFailedError({
|
||||
message: errorText(ran) || "Project start command failed",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const ran = await runStartCommand(info.directory, cmd)
|
||||
if (ran.exitCode !== 0) {
|
||||
throw new StartCommandFailedError({ message: errorText(ran) || "Worktree start command failed" })
|
||||
const extra = input?.startCommand?.trim()
|
||||
if (extra) {
|
||||
const ran = await runStartCommand(info.directory, extra)
|
||||
if (ran.exitCode !== 0) {
|
||||
throw new StartCommandFailedError({ message: errorText(ran) || "Worktree start command failed" })
|
||||
}
|
||||
}
|
||||
|
||||
return info
|
||||
|
||||
Reference in New Issue
Block a user