@@ -65,14 +65,23 @@ const main = async () => {
65
65
resolvedPath ,
66
66
cliResults
67
67
) ;
68
+
68
69
logger . success ( `✅ Setup environment variables ${ addedEnvVars . join ( ", " ) } ` ) ;
69
70
70
- const nextJsDir = await detectPagesOrAppDir ( resolvedPath ) ;
71
+ const usesSrcDir = await detectUseOfSrcDir ( resolvedPath ) ;
72
+
73
+ if ( usesSrcDir ) {
74
+ logger . info ( "📁 Detected use of src directory" ) ;
75
+ }
76
+
77
+ const nextJsDir = await detectPagesOrAppDir ( resolvedPath , usesSrcDir ) ;
78
+
79
+ const routeDir = pathModule . join ( resolvedPath , usesSrcDir ? "src" : "" ) ;
71
80
72
81
if ( nextJsDir === "pages" ) {
73
- await createTriggerPageRoute ( resolvedPath , cliResults ) ;
82
+ await createTriggerPageRoute ( routeDir , cliResults , usesSrcDir ) ;
74
83
} else {
75
- await createTriggerAppRoute ( resolvedPath , cliResults ) ;
84
+ await createTriggerAppRoute ( routeDir , cliResults , usesSrcDir ) ;
76
85
}
77
86
78
87
const api = new TriggerApi ( apiKey , cliResults . flags . triggerUrl ) ;
@@ -144,20 +153,55 @@ async function detectTypescriptProject(path: string): Promise<boolean> {
144
153
}
145
154
}
146
155
156
+ async function detectUseOfSrcDir ( path : string ) : Promise < boolean > {
157
+ // Detects if the project is using a src directory
158
+ try {
159
+ await fs . access ( pathModule . join ( path , "src" ) ) ;
160
+ return true ;
161
+ } catch ( error ) {
162
+ return false ;
163
+ }
164
+ }
165
+
147
166
// Detect the use of pages or app dir in the Next.js project
148
167
// Import the next.config.js file and check for experimental: { appDir: true }
149
- async function detectPagesOrAppDir ( path : string ) : Promise < "pages" | "app" > {
168
+ async function detectPagesOrAppDir (
169
+ path : string ,
170
+ usesSrcDir = false
171
+ ) : Promise < "pages" | "app" > {
150
172
const nextConfigPath = pathModule . join ( path , "next.config.js" ) ;
151
173
const importedConfig = await import ( nextConfigPath ) ;
152
174
153
175
if ( importedConfig ?. default ?. experimental ?. appDir ) {
154
176
return "app" ;
155
177
} else {
178
+ // We need to check if src/app/page.tsx exists
179
+ // Or app/page.tsx exists
180
+ // If so then we return app
181
+ // If not return pages
182
+
183
+ const appPagePath = pathModule . join (
184
+ path ,
185
+ usesSrcDir ? "src" : "" ,
186
+ "app" ,
187
+ "page.tsx"
188
+ ) ;
189
+
190
+ const appPageExists = await pathExists ( appPagePath ) ;
191
+
192
+ if ( appPageExists ) {
193
+ return "app" ;
194
+ }
195
+
156
196
return "pages" ;
157
197
}
158
198
}
159
199
160
- async function createTriggerPageRoute ( path : string , cliResults : CliResults ) {
200
+ async function createTriggerPageRoute (
201
+ path : string ,
202
+ cliResults : CliResults ,
203
+ usesSrcDir = false
204
+ ) {
161
205
const routeContent = `
162
206
import { Job, TriggerClient, eventTrigger } from "@trigger.dev/sdk";
163
207
import { createPagesRoute } from "@trigger.dev/nextjs";
@@ -203,10 +247,16 @@ export default handler;
203
247
}
204
248
205
249
await fs . writeFile ( pathModule . join ( directories , "trigger.ts" ) , routeContent ) ;
206
- logger . success ( "✅ Create pages route at /pages/api/trigger.ts" ) ;
250
+ logger . success (
251
+ `✅ Created pages route at ${ usesSrcDir ? "src/" : "" } pages/api/trigger.ts`
252
+ ) ;
207
253
}
208
254
209
- async function createTriggerAppRoute ( path : string , cliResults : CliResults ) {
255
+ async function createTriggerAppRoute (
256
+ path : string ,
257
+ cliResults : CliResults ,
258
+ usesSrcDir = false
259
+ ) {
210
260
const routeContent = `
211
261
import { Job, TriggerClient, eventTrigger } from "@trigger.dev/sdk";
212
262
import { createAppRoute } from "@trigger.dev/nextjs";
@@ -250,7 +300,9 @@ export const { POST, dynamic } = createAppRoute(client, {
250
300
}
251
301
252
302
await fs . writeFile ( pathModule . join ( directories , "route.ts" ) , routeContent ) ;
253
- logger . success ( "✅ Create app route at /app/api/trigger/route.ts" ) ;
303
+ logger . success (
304
+ `✅ Created app route at ${ usesSrcDir ? "src/" : "" } app/api/trigger.ts`
305
+ ) ;
254
306
}
255
307
256
308
type EnvironmentVariable = "TRIGGER_API_KEY" | "TRIGGER_API_URL" | "VERCEL_URL" ;
0 commit comments