Skip to content

Commit 6f19a3e

Browse files
committed
Fix for the init CLI for next.js projects that use the src directory
1 parent e8442ee commit 6f19a3e

File tree

3 files changed

+70
-13
lines changed

3 files changed

+70
-13
lines changed

.changeset/clever-bats-learn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@trigger.dev/init": patch
3+
---
4+
5+
Fix for Next.js projects using the src dir

packages/init-trigger/src/index.ts

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,23 @@ const main = async () => {
6565
resolvedPath,
6666
cliResults
6767
);
68+
6869
logger.success(`✅ Setup environment variables ${addedEnvVars.join(", ")}`);
6970

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" : "");
7180

7281
if (nextJsDir === "pages") {
73-
await createTriggerPageRoute(resolvedPath, cliResults);
82+
await createTriggerPageRoute(routeDir, cliResults, usesSrcDir);
7483
} else {
75-
await createTriggerAppRoute(resolvedPath, cliResults);
84+
await createTriggerAppRoute(routeDir, cliResults, usesSrcDir);
7685
}
7786

7887
const api = new TriggerApi(apiKey, cliResults.flags.triggerUrl);
@@ -144,20 +153,55 @@ async function detectTypescriptProject(path: string): Promise<boolean> {
144153
}
145154
}
146155

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+
147166
// Detect the use of pages or app dir in the Next.js project
148167
// 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"> {
150172
const nextConfigPath = pathModule.join(path, "next.config.js");
151173
const importedConfig = await import(nextConfigPath);
152174

153175
if (importedConfig?.default?.experimental?.appDir) {
154176
return "app";
155177
} 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+
156196
return "pages";
157197
}
158198
}
159199

160-
async function createTriggerPageRoute(path: string, cliResults: CliResults) {
200+
async function createTriggerPageRoute(
201+
path: string,
202+
cliResults: CliResults,
203+
usesSrcDir = false
204+
) {
161205
const routeContent = `
162206
import { Job, TriggerClient, eventTrigger } from "@trigger.dev/sdk";
163207
import { createPagesRoute } from "@trigger.dev/nextjs";
@@ -203,10 +247,16 @@ export default handler;
203247
}
204248

205249
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+
);
207253
}
208254

209-
async function createTriggerAppRoute(path: string, cliResults: CliResults) {
255+
async function createTriggerAppRoute(
256+
path: string,
257+
cliResults: CliResults,
258+
usesSrcDir = false
259+
) {
210260
const routeContent = `
211261
import { Job, TriggerClient, eventTrigger } from "@trigger.dev/sdk";
212262
import { createAppRoute } from "@trigger.dev/nextjs";
@@ -250,7 +300,9 @@ export const { POST, dynamic } = createAppRoute(client, {
250300
}
251301

252302
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+
);
254306
}
255307

256308
type EnvironmentVariable = "TRIGGER_API_KEY" | "TRIGGER_API_URL" | "VERCEL_URL";

pnpm-lock.yaml

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)