From 385e7f5c1e596400d61f371dd144475649c3e79a Mon Sep 17 00:00:00 2001 From: Kacper Date: Sun, 11 Jan 2026 00:01:23 +0100 Subject: [PATCH] fix: address pr comments --- apps/server/src/routes/worktree/index.ts | 2 +- .../src/routes/worktree/routes/create.ts | 28 +++++++------------ .../src/routes/worktree/routes/init-script.ts | 21 +++++--------- .../src/services/init-script-service.ts | 6 ++-- .../worktrees/worktrees-section.tsx | 8 +++--- apps/ui/src/lib/http-api-client.ts | 3 +- 6 files changed, 28 insertions(+), 40 deletions(-) diff --git a/apps/server/src/routes/worktree/index.ts b/apps/server/src/routes/worktree/index.ts index a98377fb..54f7ba9e 100644 --- a/apps/server/src/routes/worktree/index.ts +++ b/apps/server/src/routes/worktree/index.ts @@ -95,7 +95,7 @@ export function createWorktreeRoutes(events: EventEmitter): Router { router.post('/list-dev-servers', createListDevServersHandler()); // Init script routes - router.post('/init-script', validatePathParams('projectPath'), createGetInitScriptHandler()); + router.get('/init-script', createGetInitScriptHandler()); router.put('/init-script', validatePathParams('projectPath'), createPutInitScriptHandler()); router.delete('/init-script', validatePathParams('projectPath'), createDeleteInitScriptHandler()); router.post( diff --git a/apps/server/src/routes/worktree/routes/create.ts b/apps/server/src/routes/worktree/routes/create.ts index ecc3b9b0..87ad7844 100644 --- a/apps/server/src/routes/worktree/routes/create.ts +++ b/apps/server/src/routes/worktree/routes/create.ts @@ -22,8 +22,7 @@ import { } from '../common.js'; import { trackBranch } from './branch-tracking.js'; import { createLogger } from '@automaker/utils'; -import { runInitScript, getInitScriptPath, hasInitScriptRun } from '../../../services/init-script-service.js'; -import fs from 'fs'; +import { runInitScript } from '../../../services/init-script-service.js'; const logger = createLogger('Worktree'); @@ -181,11 +180,6 @@ export function createCreateHandler(events: EventEmitter) { // normalizePath converts to forward slashes for API consistency const absoluteWorktreePath = path.resolve(worktreePath); - // Check if init script exists and should be run (only for new worktrees) - const initScriptPath = getInitScriptPath(projectPath); - const hasInitScript = fs.existsSync(initScriptPath); - const alreadyRan = await hasInitScriptRun(projectPath, branchName); - // Respond immediately (non-blocking) res.json({ success: true, @@ -197,17 +191,15 @@ export function createCreateHandler(events: EventEmitter) { }); // Trigger init script asynchronously after response - if (hasInitScript && !alreadyRan) { - logger.info(`Triggering init script for worktree: ${branchName}`); - runInitScript({ - projectPath, - worktreePath: absoluteWorktreePath, - branch: branchName, - emitter: events, - }).catch((err) => { - logger.error(`Init script failed for ${branchName}:`, err); - }); - } + // runInitScript internally checks if script exists and hasn't already run + runInitScript({ + projectPath, + worktreePath: absoluteWorktreePath, + branch: branchName, + emitter: events, + }).catch((err) => { + logger.error(`Init script failed for ${branchName}:`, err); + }); } catch (error) { logError(error, 'Create worktree failed'); res.status(500).json({ success: false, error: getErrorMessage(error) }); diff --git a/apps/server/src/routes/worktree/routes/init-script.ts b/apps/server/src/routes/worktree/routes/init-script.ts index 0389bce3..e97bddf3 100644 --- a/apps/server/src/routes/worktree/routes/init-script.ts +++ b/apps/server/src/routes/worktree/routes/init-script.ts @@ -33,12 +33,12 @@ function getInitScriptPath(projectPath: string): string { export function createGetInitScriptHandler() { return async (req: Request, res: Response): Promise => { try { - const { projectPath } = req.body as { projectPath: string }; + const projectPath = req.query.projectPath as string; if (!projectPath) { res.status(400).json({ success: false, - error: 'projectPath is required', + error: 'projectPath query parameter is required', }); return; } @@ -142,18 +142,11 @@ export function createDeleteInitScriptHandler() { const scriptPath = getInitScriptPath(projectPath); - try { - await secureFs.rm(scriptPath, { force: true }); - logger.info(`Deleted init script at ${scriptPath}`); - res.json({ - success: true, - }); - } catch { - // File doesn't exist - still success - res.json({ - success: true, - }); - } + await secureFs.rm(scriptPath, { force: true }); + logger.info(`Deleted init script at ${scriptPath}`); + res.json({ + success: true, + }); } catch (error) { logError(error, 'Delete init script failed'); res.status(500).json({ diff --git a/apps/server/src/services/init-script-service.ts b/apps/server/src/services/init-script-service.ts index ac354bff..78328a1d 100644 --- a/apps/server/src/services/init-script-service.ts +++ b/apps/server/src/services/init-script-service.ts @@ -150,10 +150,12 @@ export class InitScriptService { : 'No shell found (/bin/bash or /bin/sh)'; logger.error(error); - // Update metadata with error + // Update metadata with error, preserving existing metadata + const existingMetadata = await readWorktreeMetadata(projectPath, branch); await writeWorktreeMetadata(projectPath, branch, { branch, - createdAt: new Date().toISOString(), + createdAt: existingMetadata?.createdAt || new Date().toISOString(), + pr: existingMetadata?.pr, initScriptRan: true, initScriptStatus: 'failed', initScriptError: error, diff --git a/apps/ui/src/components/views/settings-view/worktrees/worktrees-section.tsx b/apps/ui/src/components/views/settings-view/worktrees/worktrees-section.tsx index fd43f3d0..20eb8680 100644 --- a/apps/ui/src/components/views/settings-view/worktrees/worktrees-section.tsx +++ b/apps/ui/src/components/views/settings-view/worktrees/worktrees-section.tsx @@ -14,7 +14,7 @@ import { PanelBottomClose, } from 'lucide-react'; import { cn } from '@/lib/utils'; -import { apiPost, apiPut, apiDelete } from '@/lib/api-fetch'; +import { apiGet, apiPut, apiDelete } from '@/lib/api-fetch'; import { toast } from 'sonner'; import { useAppStore } from '@/store/app-store'; @@ -77,9 +77,9 @@ export function WorktreesSection({ useWorktrees, onUseWorktreesChange }: Worktre const loadInitScript = async () => { setIsLoading(true); try { - const response = await apiPost('/api/worktree/init-script', { - projectPath: currentProject.path, - }); + const response = await apiGet( + `/api/worktree/init-script?projectPath=${encodeURIComponent(currentProject.path)}` + ); if (response.success) { const content = response.content || ''; setScriptContent(content); diff --git a/apps/ui/src/lib/http-api-client.ts b/apps/ui/src/lib/http-api-client.ts index f6f6e266..968e2d30 100644 --- a/apps/ui/src/lib/http-api-client.ts +++ b/apps/ui/src/lib/http-api-client.ts @@ -1613,7 +1613,8 @@ export class HttpApiClient implements ElectronAPI { getPRInfo: (worktreePath: string, branchName: string) => this.post('/api/worktree/pr-info', { worktreePath, branchName }), // Init script methods - getInitScript: (projectPath: string) => this.post('/api/worktree/init-script', { projectPath }), + getInitScript: (projectPath: string) => + this.get(`/api/worktree/init-script?projectPath=${encodeURIComponent(projectPath)}`), setInitScript: (projectPath: string, content: string) => this.put('/api/worktree/init-script', { projectPath, content }), deleteInitScript: (projectPath: string) =>