@@ -26,7 +26,7 @@ import {
26
26
type Model ,
27
27
} from '@zenstackhq/sdk/ast' ;
28
28
import { getDMMF , getPrismaClientImportSpec , getPrismaVersion , type DMMF } from '@zenstackhq/sdk/prisma' ;
29
- import { upperCaseFirst } from '@zenstackhq/runtime/local-helpers' ;
29
+ import { invariant , upperCaseFirst } from '@zenstackhq/runtime/local-helpers' ;
30
30
import fs from 'fs' ;
31
31
import path from 'path' ;
32
32
import semver from 'semver' ;
@@ -40,6 +40,7 @@ import {
40
40
SyntaxKind ,
41
41
TypeAliasDeclaration ,
42
42
VariableStatement ,
43
+ type StatementStructures ,
43
44
} from 'ts-morph' ;
44
45
import { name } from '..' ;
45
46
import { getConcreteModels , getDiscriminatorField } from '../../../utils/ast-utils' ;
@@ -503,16 +504,13 @@ export type Enhanced<Client> =
503
504
for ( const d of this . model . declarations . filter ( isDataModel ) ) {
504
505
const fileName = `${ prismaClientDir } /models/${ d . name } .ts` ;
505
506
const sf = project . addSourceFileAtPath ( fileName ) ;
506
- const sfNew = project . createSourceFile ( `${ prismaClientDir } /models/${ d . name } -fixed.ts` , undefined , {
507
- overwrite : true ,
508
- } ) ;
509
507
510
508
const syntaxList = sf . getChildren ( ) [ 0 ] ;
511
509
if ( ! Node . isSyntaxList ( syntaxList ) ) {
512
510
throw new PluginError ( name , `Unexpected syntax list structure in ${ fileName } ` ) ;
513
511
}
514
512
515
- sfNew . addStatements ( 'import $Types = runtime.Types;' ) ;
513
+ const statements : ( string | StatementStructures ) [ ] = [ 'import $Types = runtime.Types;' ] ;
516
514
517
515
// Add import for json-types if this model has JSON type fields
518
516
const modelWithJsonFields = this . modelsWithJsonTypeFields . find ( ( m ) => m . name === d . name ) ;
@@ -525,23 +523,35 @@ export type Enhanced<Client> =
525
523
const typeNames = [ ...new Set ( jsonFieldTypes . map ( ( field ) => field . type . reference ! . $refText ) ) ] ;
526
524
527
525
if ( typeNames . length > 0 ) {
528
- sfNew . addStatements ( `import type { ${ typeNames . join ( ', ' ) } } from "../../json-types";` ) ;
526
+ statements . push ( `import type { ${ typeNames . join ( ', ' ) } } from "../../json-types";` ) ;
529
527
}
530
528
}
531
529
532
530
syntaxList . getChildren ( ) . forEach ( ( node ) => {
533
531
if ( Node . isInterfaceDeclaration ( node ) ) {
534
- sfNew . addInterface ( this . transformInterface ( node , delegateInfo ) ) ;
532
+ statements . push ( this . transformInterface ( node , delegateInfo ) ) ;
535
533
} else if ( Node . isTypeAliasDeclaration ( node ) ) {
536
- sfNew . addTypeAlias ( this . transformTypeAlias ( node , delegateInfo ) ) ;
534
+ statements . push ( this . transformTypeAlias ( node , delegateInfo ) ) ;
537
535
} else {
538
- sfNew . addStatements ( node . getText ( ) ) ;
536
+ statements . push ( node . getText ( ) ) ;
539
537
}
540
538
} ) ;
541
539
542
- await sfNew . move ( sf . getFilePath ( ) , { overwrite : true } ) ;
540
+ const structure = sf . getStructure ( ) ;
541
+ structure . statements = statements ;
542
+
543
+ const sfNew = project . createSourceFile ( `${ prismaClientDir } /models/${ d . name } -fixed.ts` , structure , {
544
+ overwrite : true ,
545
+ } ) ;
543
546
await sfNew . save ( ) ;
544
547
}
548
+
549
+ for ( const d of this . model . declarations . filter ( isDataModel ) ) {
550
+ const fixedFileName = `${ prismaClientDir } /models/${ d . name } -fixed.ts` ;
551
+ const fileName = `${ prismaClientDir } /models/${ d . name } .ts` ;
552
+
553
+ fs . renameSync ( fixedFileName , fileName ) ;
554
+ }
545
555
}
546
556
547
557
private transformPrismaTypes ( sf : SourceFile , sfNew : SourceFile , delegateInfo : DelegateInfo ) {
@@ -641,6 +651,27 @@ export type Enhanced<Client> =
641
651
return structure ;
642
652
}
643
653
654
+ private transformVariableStatementProps ( variable : VariableStatement ) {
655
+ const structure = variable . getStructure ( ) ;
656
+
657
+ // remove `delegate_aux_*` fields from the variable's initializer
658
+ const auxFields = this . findAuxProps ( variable ) ;
659
+ if ( auxFields . length > 0 ) {
660
+ structure . declarations . forEach ( ( variable ) => {
661
+ if ( variable . initializer ) {
662
+ let source = variable . initializer ;
663
+ auxFields . forEach ( ( f ) => {
664
+ invariant ( typeof source === 'string' ) ;
665
+ source = this . removeFromSource ( source , f . getText ( ) ) ;
666
+ } ) ;
667
+ variable . initializer = source ;
668
+ }
669
+ } ) ;
670
+ }
671
+
672
+ return structure ;
673
+ }
674
+
644
675
private transformInterface ( iface : InterfaceDeclaration , delegateInfo : DelegateInfo ) {
645
676
const structure = iface . getStructure ( ) ;
646
677
@@ -958,6 +989,12 @@ export type Enhanced<Client> =
958
989
. filter ( ( n ) => n . getName ( ) . includes ( DELEGATE_AUX_RELATION_PREFIX ) ) ;
959
990
}
960
991
992
+ private findAuxProps ( node : Node ) {
993
+ return node
994
+ . getDescendantsOfKind ( SyntaxKind . PropertyAssignment )
995
+ . filter ( ( n ) => n . getName ( ) . includes ( DELEGATE_AUX_RELATION_PREFIX ) ) ;
996
+ }
997
+
961
998
private saveSourceFile ( sf : SourceFile ) {
962
999
if ( this . options . preserveTsFiles ) {
963
1000
saveSourceFile ( sf ) ;
@@ -974,7 +1011,7 @@ export type Enhanced<Client> =
974
1011
}
975
1012
976
1013
private trimEmptyLines ( source : string ) : string {
977
- return source . replace ( / ^ \s * [ \r \n ] / gm, '' ) ;
1014
+ return source . replace ( / ^ \s * , ? [ \r \n ] / gm, '' ) ;
978
1015
}
979
1016
980
1017
private get isNewPrismaClientGenerator ( ) {
0 commit comments