11import { BuildManifest , BuildTarget } from "@trigger.dev/core/v3" ;
22import { binaryForRuntime , BuildContext , BuildExtension } from "@trigger.dev/core/v3/build" ;
33import assert from "node:assert" ;
4+ import { glob } from 'fast-glob' ;
45import { existsSync } from "node:fs" ;
56import { cp , readdir } from "node:fs/promises" ;
67import { dirname , join , resolve } from "node:path" ;
@@ -112,11 +113,18 @@ export class PrismaExtension implements BuildExtension {
112113
113114 context . logger . debug ( `PrismaExtension is generating the Prisma client for version ${ version } ` ) ;
114115
115- const usingSchemaFolder = dirname ( this . _resolvedSchemaPath ) . endsWith ( "schema" ) ;
116+ // Multi-file schemas can be used by specifying a directory instead of a file. https://www.prisma.io/docs/orm/prisma-schema/overview/location#multi-file-prisma-schema
117+ const usingSchemaFolder = ! this . _resolvedSchemaPath . endsWith ( ".prisma" ) ;
118+
119+ context . logger . debug ( `Using schema folder: ${ usingSchemaFolder } ` ) ;
116120
117121 const commands : string [ ] = [ ] ;
118122
119- let prismaDir : string | undefined ;
123+ const prismaSourceDir = usingSchemaFolder
124+ ? this . _resolvedSchemaPath
125+ : dirname ( this . _resolvedSchemaPath ) ;
126+
127+ const prismaDestinationDir = join ( manifest . outputPath , "prisma" ) ;
120128
121129 const generatorFlags : string [ ] = [ ] ;
122130
@@ -127,26 +135,22 @@ export class PrismaExtension implements BuildExtension {
127135 if ( this . options . typedSql ) {
128136 generatorFlags . push ( `--sql` ) ;
129137
130- const prismaDir = usingSchemaFolder
131- ? dirname ( dirname ( this . _resolvedSchemaPath ) )
132- : dirname ( this . _resolvedSchemaPath ) ;
133-
134138 context . logger . debug ( `Using typedSql` ) ;
135139
136140 // Find all the files prisma/sql/*.sql
137- const sqlFiles = await readdir ( join ( prismaDir , "sql" ) ) . then ( ( files ) =>
141+ const sqlFiles = await readdir ( join ( prismaSourceDir , "sql" ) ) . then ( ( files ) =>
138142 files . filter ( ( file ) => file . endsWith ( ".sql" ) )
139143 ) ;
140144
141145 context . logger . debug ( `Found sql files` , {
142146 sqlFiles,
143147 } ) ;
144148
145- const sqlDestinationPath = join ( manifest . outputPath , "prisma" , "sql" ) ;
149+ const sqlDestinationPath = join ( prismaDestinationDir , "sql" ) ;
146150
147151 for ( const file of sqlFiles ) {
148152 const destination = join ( sqlDestinationPath , file ) ;
149- const source = join ( prismaDir , "sql" , file ) ;
153+ const source = join ( prismaSourceDir , "sql" , file ) ;
150154
151155 context . logger . debug ( `Copying the sql from ${ source } to ${ destination } ` ) ;
152156
@@ -155,28 +159,20 @@ export class PrismaExtension implements BuildExtension {
155159 }
156160
157161 if ( usingSchemaFolder ) {
158- const schemaDir = dirname ( this . _resolvedSchemaPath ) ;
159-
160- prismaDir = dirname ( schemaDir ) ;
161-
162- context . logger . debug ( `Using the schema folder: ${ schemaDir } ` ) ;
162+ context . logger . debug ( `Using the schema folder: ${ this . _resolvedSchemaPath } ` ) ;
163163
164164 // Find all the files in schemaDir that end with .prisma (excluding the schema.prisma file)
165- const prismaFiles = await readdir ( schemaDir ) . then ( ( files ) =>
166- files . filter ( ( file ) => file . endsWith ( ".prisma" ) )
167- ) ;
165+ const prismaFiles = await glob ( "**/*.prisma" , {
166+ cwd : this . _resolvedSchemaPath
167+ } )
168168
169169 context . logger . debug ( `Found prisma files in the schema folder` , {
170170 prismaFiles,
171171 } ) ;
172172
173- const schemaDestinationPath = join ( manifest . outputPath , "prisma" , "schema" ) ;
174-
175- const allPrismaFiles = [ ...prismaFiles ] ;
176-
177- for ( const file of allPrismaFiles ) {
178- const destination = join ( schemaDestinationPath , file ) ;
179- const source = join ( schemaDir , file ) ;
173+ for ( const file of prismaFiles ) {
174+ const destination = join ( prismaDestinationDir , file ) ;
175+ const source = join ( this . _resolvedSchemaPath , file ) ;
180176
181177 context . logger . debug ( `Copying the prisma schema from ${ source } to ${ destination } ` ) ;
182178
@@ -186,15 +182,14 @@ export class PrismaExtension implements BuildExtension {
186182 commands . push (
187183 `${ binaryForRuntime (
188184 manifest . runtime
189- ) } node_modules/prisma/build/index.js generate ${ generatorFlags . join ( " " ) } ` // Don't add the --schema flag or this will fail
185+ ) } node_modules/prisma/build/index.js generate --schema ./prisma ${ generatorFlags . join ( " " ) } ` // Don't add the --schema flag or this will fail
190186 ) ;
191187 } else {
192- prismaDir = dirname ( this . _resolvedSchemaPath ) ;
193188 // Now we need to add a layer that:
194189 // Copies the prisma schema to the build outputPath
195190 // Adds the `prisma` CLI dependency to the dependencies
196191 // Adds the `prisma generate` command, which generates the Prisma client
197- const schemaDestinationPath = join ( manifest . outputPath , "prisma" , "schema.prisma" ) ;
192+ const schemaDestinationPath = join ( prismaDestinationDir , "schema.prisma" ) ;
198193 // Copy the prisma schema to the build output path
199194 context . logger . debug (
200195 `Copying the prisma schema from ${ this . _resolvedSchemaPath } to ${ schemaDestinationPath } `
@@ -215,8 +210,8 @@ export class PrismaExtension implements BuildExtension {
215210
216211 if ( this . options . migrate ) {
217212 // Copy the migrations directory to the build output path
218- const migrationsDir = join ( prismaDir , "migrations" ) ;
219- const migrationsDestinationPath = join ( manifest . outputPath , "prisma" , "migrations" ) ;
213+ const migrationsDir = join ( prismaSourceDir , "migrations" ) ;
214+ const migrationsDestinationPath = join ( prismaDestinationDir , "migrations" ) ;
220215
221216 context . logger . debug (
222217 `Copying the prisma migrations from ${ migrationsDir } to ${ migrationsDestinationPath } `
0 commit comments