1
- import { ReadonlyDeep } from '@prisma/generator-helper' ;
2
1
import { DELEGATE_AUX_RELATION_PREFIX } from '@zenstackhq/runtime' ;
3
2
import {
4
3
PluginError ,
@@ -114,21 +113,18 @@ export class EnhancerGenerator {
114
113
115
114
if ( this . needsLogicalClient ) {
116
115
prismaTypesFixed = true ;
117
- resultPrismaTypeImport = ` ${ LOGICAL_CLIENT_GENERATION_PATH } /index-fixed` ;
116
+ resultPrismaTypeImport = LOGICAL_CLIENT_GENERATION_PATH ;
118
117
const result = await this . generateLogicalPrisma ( ) ;
119
118
dmmf = result . dmmf ;
120
119
}
121
120
122
121
// reexport PrismaClient types (original or fixed)
123
- const modelsDts = this . project . createSourceFile (
124
- path . join ( this . outDir , 'models.d. ts' ) ,
122
+ const modelsTs = this . project . createSourceFile (
123
+ path . join ( this . outDir , 'models.ts' ) ,
125
124
`export * from '${ resultPrismaTypeImport } ';` ,
126
125
{ overwrite : true }
127
126
) ;
128
- await modelsDts . save ( ) ;
129
-
130
- // reexport values from the original PrismaClient (enums, etc.)
131
- fs . writeFileSync ( path . join ( this . outDir , 'models.js' ) , `module.exports = require('${ prismaImport } ');` ) ;
127
+ this . saveSourceFile ( modelsTs ) ;
132
128
133
129
const authDecl = getAuthDecl ( getDataModelAndTypeDefs ( this . model ) ) ;
134
130
const authTypes = authDecl ? generateAuthType ( this . model , authDecl ) : '' ;
177
173
return {
178
174
dmmf,
179
175
newPrismaClientDtsPath : prismaTypesFixed
180
- ? path . resolve ( this . outDir , LOGICAL_CLIENT_GENERATION_PATH , 'index-fixed .d.ts' )
176
+ ? path . resolve ( this . outDir , LOGICAL_CLIENT_GENERATION_PATH , 'index.d.ts' )
181
177
: undefined ,
182
178
} ;
183
179
}
@@ -422,7 +418,7 @@ export type Enhanced<Client> =
422
418
return dmmf ;
423
419
}
424
420
425
- private shouldBeOptional ( field : ReadonlyDeep < DMMF . SchemaArg > , dataModel : DataModel ) {
421
+ private shouldBeOptional ( field : DMMF . SchemaArg , dataModel : DataModel ) {
426
422
const dmField = dataModel . fields . find ( ( f ) => f . name === field . name ) ;
427
423
if ( ! dmField ) {
428
424
return false ;
@@ -457,7 +453,7 @@ export type Enhanced<Client> =
457
453
}
458
454
459
455
private async processClientTypes ( prismaClientDir : string ) {
460
- // make necessary updates to the generated `index.d.ts` file and save it as `index-fixed.d.ts`
456
+ // make necessary updates to the generated `index.d.ts` file and overwrite it
461
457
const project = new Project ( ) ;
462
458
const sf = project . addSourceFileAtPath ( path . join ( prismaClientDir , 'index.d.ts' ) ) ;
463
459
@@ -472,8 +468,7 @@ export type Enhanced<Client> =
472
468
}
473
469
} ) ;
474
470
475
- // transform index.d.ts and save it into a new file (better perf than in-line editing)
476
-
471
+ // transform index.d.ts and write it into a new file (better perf than in-line editing)
477
472
const sfNew = project . createSourceFile ( path . join ( prismaClientDir , 'index-fixed.d.ts' ) , undefined , {
478
473
overwrite : true ,
479
474
} ) ;
@@ -483,6 +478,9 @@ export type Enhanced<Client> =
483
478
this . generateExtraTypes ( sfNew ) ;
484
479
485
480
sfNew . formatText ( ) ;
481
+
482
+ // Save the transformed file over the original
483
+ await sfNew . move ( sf . getFilePath ( ) , { overwrite : true } ) ;
486
484
await sfNew . save ( ) ;
487
485
}
488
486
0 commit comments