| 15 | import { detectMiddlewareUsage } from "./middleware"; |
| 16 | |
| 17 | export class NextJs implements Framework { |
| 18 | id = "nextjs"; |
| 19 | name = "Next.js"; |
| 20 | |
| 21 | async isMatch(path: string, packageManager: PackageManager): Promise<boolean> { |
| 22 | const hasNextConfigFile = await detectNextConfigFile(path); |
| 23 | if (hasNextConfigFile) { |
| 24 | return true; |
| 25 | } |
| 26 | |
| 27 | return await detectNextDependency(path); |
| 28 | } |
| 29 | |
| 30 | async dependencies(): Promise<InstallPackage[]> { |
| 31 | return [ |
| 32 | { name: "@trigger.dev/sdk", tag: "latest" }, |
| 33 | { name: "@trigger.dev/nextjs", tag: "latest" }, |
| 34 | { name: "@trigger.dev/react", tag: "latest" }, |
| 35 | ]; |
| 36 | } |
| 37 | |
| 38 | possibleEnvFilenames(): string[] { |
| 39 | return [".env.local", ".env"]; |
| 40 | } |
| 41 | |
| 42 | publicKeyEnvName = "NEXT_PUBLIC_TRIGGER_PUBLIC_API_KEY"; |
| 43 | |
| 44 | async install( |
| 45 | path: string, |
| 46 | options: { typescript: boolean; packageManager: PackageManager; endpointSlug: string } |
| 47 | ): Promise<void> { |
| 48 | const usesSrcDir = await detectUseOfSrcDir(path); |
| 49 | if (usesSrcDir) { |
| 50 | logger.info("📁 Detected use of src directory"); |
| 51 | } |
| 52 | |
| 53 | const nextJsDir = await detectPagesOrAppDir(path); |
| 54 | const nextJsVersion = await detectNextVersion(path); |
| 55 | const routeDir = pathModule.join(path, usesSrcDir ? "src" : ""); |
| 56 | const pathAlias = await getPathAlias({ |
| 57 | projectPath: path, |
| 58 | isTypescriptProject: options.typescript, |
| 59 | extraDirectories: usesSrcDir ? ["src"] : undefined, |
| 60 | }); |
| 61 | const fileExtension = options.typescript ? ".ts" : ".js"; |
| 62 | |
| 63 | if (nextJsDir === "pages") { |
| 64 | const apiRoutePath = pathModule.join(routeDir, "pages", "api", `trigger${fileExtension}`); |
| 65 | if (nextJsVersion && nextJsVersion !== 'latest' && nextJsVersion < "13.5") { |
| 66 | await createTriggerRoute({ |
| 67 | path: routeDir, |
| 68 | apiRoutePath, |
| 69 | template: "pagesApiRoute.js", |
| 70 | fileExtension, |
| 71 | endpointSlug: options.endpointSlug, |
| 72 | pathAlias |
| 73 | }); |
| 74 | } else { |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…