@@ -244,6 +244,46 @@ export namespace Worktree {
244244 return $ `bash -lc ${ cmd } ` . nothrow ( ) . cwd ( directory )
245245 }
246246
247+ type StartKind = "project" | "worktree"
248+
249+ async function runStartScript ( directory : string , cmd : string , kind : StartKind ) {
250+ const text = cmd . trim ( )
251+ if ( ! text ) return true
252+
253+ const ran = await runStartCommand ( directory , text )
254+ if ( ran . exitCode === 0 ) return true
255+
256+ log . error ( "worktree start command failed" , {
257+ kind,
258+ directory,
259+ message : errorText ( ran ) ,
260+ } )
261+ return false
262+ }
263+
264+ async function runStartScripts ( directory : string , input : { projectID : string ; extra ?: string } ) {
265+ const project = await Storage . read < Project . Info > ( [ "project" , input . projectID ] ) . catch ( ( ) => undefined )
266+ const startup = project ?. commands ?. start ?. trim ( ) ?? ""
267+ const ok = await runStartScript ( directory , startup , "project" )
268+ if ( ! ok ) return false
269+
270+ const extra = input . extra ?? ""
271+ await runStartScript ( directory , extra , "worktree" )
272+ return true
273+ }
274+
275+ function queueStartScripts ( directory : string , input : { projectID : string ; extra ?: string } ) {
276+ setTimeout ( ( ) => {
277+ const start = async ( ) => {
278+ await runStartScripts ( directory , input )
279+ }
280+
281+ void start ( ) . catch ( ( error ) => {
282+ log . error ( "worktree start task failed" , { directory, error } )
283+ } )
284+ } , 0 )
285+ }
286+
247287 export const create = fn ( CreateInput . optional ( ) , async ( input ) => {
248288 if ( Instance . project . vcs !== "git" ) {
249289 throw new NotGitError ( { message : "Worktrees are only supported for git projects" } )
@@ -318,27 +358,7 @@ export namespace Worktree {
318358 } ,
319359 } )
320360
321- const project = await Storage . read < Project . Info > ( [ "project" , projectID ] ) . catch ( ( ) => undefined )
322- const startup = project ?. commands ?. start ?. trim ( ) ?? ""
323-
324- const run = async ( cmd : string , kind : "project" | "worktree" ) => {
325- const ran = await runStartCommand ( info . directory , cmd )
326- if ( ran . exitCode === 0 ) return true
327- log . error ( "worktree start command failed" , {
328- kind,
329- directory : info . directory ,
330- message : errorText ( ran ) ,
331- } )
332- return false
333- }
334-
335- if ( startup ) {
336- const ok = await run ( startup , "project" )
337- if ( ! ok ) return
338- }
339- if ( extra ) {
340- await run ( extra , "worktree" )
341- }
361+ await runStartScripts ( info . directory , { projectID, extra } )
342362 }
343363
344364 void start ( ) . catch ( ( error ) => {
@@ -522,26 +542,7 @@ export namespace Worktree {
522542 }
523543
524544 const projectID = Instance . project . id
525- setTimeout ( ( ) => {
526- const start = async ( ) => {
527- const project = await Storage . read < Project . Info > ( [ "project" , projectID ] ) . catch ( ( ) => undefined )
528- const startup = project ?. commands ?. start ?. trim ( ) ?? ""
529- if ( ! startup ) return
530-
531- const ran = await runStartCommand ( worktreePath , startup )
532- if ( ran . exitCode === 0 ) return
533-
534- log . error ( "worktree start command failed" , {
535- kind : "project" ,
536- directory : worktreePath ,
537- message : errorText ( ran ) ,
538- } )
539- }
540-
541- void start ( ) . catch ( ( error ) => {
542- log . error ( "worktree start task failed" , { directory : worktreePath , error } )
543- } )
544- } , 0 )
545+ queueStartScripts ( worktreePath , { projectID } )
545546
546547 return true
547548 } )
0 commit comments