@@ -141,62 +141,70 @@ export const initializeProjectTool = {
141141 handler : toolHandler ( InitializeProjectInput . shape , async ( input , { ctx } ) => {
142142 ctx . logger ?. log ( "calling initialize_project" , { input } ) ;
143143
144- const cwd = input . cwd ?? ( await ctx . getCwd ( ) ) ;
144+ let projectRef : string | undefined = input . projectRef ;
145145
146- if ( ! cwd ) {
147- return respondWithError (
148- "No current working directory found. Please provide a projectRef or a cwd."
149- ) ;
150- }
146+ if ( ! projectRef ) {
147+ const cwd = input . cwd ?? ( await ctx . getCwd ( ) ) ;
151148
152- // Try to load the config file
153- const [ _ , config ] = await tryCatch ( loadConfig ( { cwd } ) ) ;
154-
155- if ( config ?. configFile ) {
156- if ( typeof config . project === "string" && config . project . startsWith ( "proj_" ) ) {
157- ctx . logger ?. log ( "initialize_project existing project" , {
158- config,
159- projectRef : config . project ,
160- } ) ;
161-
162- return {
163- content : [
164- {
165- type : "text" ,
166- text : `We found an existing trigger.config.ts file in the current working directory. Skipping initialization.` ,
167- } ,
168- ] ,
169- } ;
170- } else {
149+ if ( ! cwd ) {
171150 return respondWithError (
172- "Could not find the project ref in the config file . Please provide a projectRef."
151+ "No current working directory found . Please provide a projectRef or a cwd ."
173152 ) ;
174153 }
175- }
176154
177- const cliApiClient = await ctx . getCliApiClient ( ) ;
155+ // Try to load the config file
156+ const [ _ , config ] = await tryCatch ( loadConfig ( { cwd } ) ) ;
157+
158+ if ( config ?. configFile ) {
159+ if ( typeof config . project === "string" && config . project . startsWith ( "proj_" ) ) {
160+ ctx . logger ?. log ( "initialize_project existing project" , {
161+ config,
162+ projectRef : config . project ,
163+ } ) ;
164+
165+ return {
166+ content : [
167+ {
168+ type : "text" ,
169+ text : `We found an existing trigger.config.ts file in the current working directory. Skipping initialization.` ,
170+ } ,
171+ ] ,
172+ } ;
173+ } else {
174+ return respondWithError (
175+ "Could not find the project ref in the config file. Please provide a projectRef."
176+ ) ;
177+ }
178+ }
178179
179- const project = await cliApiClient . createProject ( input . orgParam , {
180- name : input . projectName ,
181- } ) ;
180+ const cliApiClient = await ctx . getCliApiClient ( ) ;
182181
183- if ( ! project . success ) {
184- return respondWithError (
185- `Failed to create project ${ input . projectName } in organization ${ input . orgParam } : ${ project . error } `
186- ) ;
182+ const project = await cliApiClient . createProject ( input . orgParam , {
183+ name : input . projectName ,
184+ } ) ;
185+
186+ if ( ! project . success ) {
187+ return respondWithError (
188+ `Failed to create project ${ input . projectName } in organization ${ input . orgParam } : ${ project . error } `
189+ ) ;
190+ }
191+
192+ ctx . logger ?. log ( "initialize_project new project" , {
193+ project : project . data ,
194+ } ) ;
195+
196+ projectRef = project . data . externalRef ;
187197 }
188198
189- ctx . logger ?. log ( "initialize_project new project" , {
190- project : project . data ,
191- } ) ;
199+ const cliApiClient = await ctx . getCliApiClient ( ) ;
192200
193201 const projectEnv = await cliApiClient . getProjectEnv ( {
194- projectRef : project . data . externalRef ,
202+ projectRef : projectRef ,
195203 env : "dev" ,
196204 } ) ;
197205
198206 const manualSetupGuide = await getManualSetupGuide (
199- project . data . externalRef ,
207+ projectRef ,
200208 projectEnv . success ? projectEnv . data . apiKey : undefined ,
201209 projectEnv . success ? projectEnv . data . apiUrl : undefined
202210 ) ;
0 commit comments