|
1 | 1 | import { DELEGATE_AUX_RELATION_PREFIX } from '@zenstackhq/runtime'; |
2 | | -import { upperCaseFirst } from '@zenstackhq/runtime/local-helpers'; |
| 2 | +import { invariant, upperCaseFirst } from '@zenstackhq/runtime/local-helpers'; |
3 | 3 | import { |
4 | 4 | PluginError, |
5 | 5 | getAttribute, |
@@ -569,6 +569,32 @@ export type Enhanced<Client> = |
569 | 569 | private async processClientTypesNewPrismaGenerator(prismaClientDir: string, delegateInfo: DelegateInfo) { |
570 | 570 | const project = new Project(); |
571 | 571 |
|
| 572 | + // remove delegate_aux_* fields from the prismaNamespace.ts |
| 573 | + const internalFilename = `${prismaClientDir}/internal/prismaNamespace.ts` |
| 574 | + const internalFilenameFixed = `${prismaClientDir}/internal/prismaNamespace-fixed.ts` |
| 575 | + const internalSf = project.addSourceFileAtPath(internalFilename); |
| 576 | + const syntaxList = internalSf.getChildren()[0]; |
| 577 | + if (!Node.isSyntaxList(syntaxList)) { |
| 578 | + throw new PluginError(name, `Unexpected syntax list structure in ${internalFilename}`); |
| 579 | + } |
| 580 | + const statements: (string | StatementStructures)[] = []; |
| 581 | + |
| 582 | + syntaxList.getChildren().forEach((node) => { |
| 583 | + if (Node.isVariableStatement(node)) { |
| 584 | + statements.push(this.transformVariableStatementProps(node)); |
| 585 | + } else { |
| 586 | + statements.push(node.getText()); |
| 587 | + } |
| 588 | + }); |
| 589 | + const structure = internalSf.getStructure(); |
| 590 | + structure.statements = statements; |
| 591 | + |
| 592 | + const internalSfNew = project.createSourceFile(internalFilenameFixed, structure, { |
| 593 | + overwrite: true, |
| 594 | + }); |
| 595 | + await internalSfNew.save(); |
| 596 | + fs.renameSync(internalFilenameFixed, internalFilename); |
| 597 | + |
572 | 598 | // Create a shared file for all JSON fields type definitions |
573 | 599 | const jsonFieldsFile = project.createSourceFile(path.join(this.outDir, 'json-types.ts'), undefined, { |
574 | 600 | overwrite: true, |
@@ -727,6 +753,27 @@ export type Enhanced<Client> = |
727 | 753 | return structure; |
728 | 754 | } |
729 | 755 |
|
| 756 | + private transformVariableStatementProps(variable: VariableStatement) { |
| 757 | + const structure = variable.getStructure(); |
| 758 | + |
| 759 | + // remove `delegate_aux_*` fields from the variable's initializer |
| 760 | + const auxFields = this.findAuxProps(variable); |
| 761 | + if (auxFields.length > 0) { |
| 762 | + structure.declarations.forEach((variable) => { |
| 763 | + if (variable.initializer) { |
| 764 | + let source = variable.initializer; |
| 765 | + auxFields.forEach((f) => { |
| 766 | + invariant(typeof source === 'string'); |
| 767 | + source = this.removeFromSource(source, f.getText()); |
| 768 | + }); |
| 769 | + variable.initializer = source; |
| 770 | + } |
| 771 | + }); |
| 772 | + } |
| 773 | + |
| 774 | + return structure; |
| 775 | + } |
| 776 | + |
730 | 777 | private transformInterface(iface: InterfaceDeclaration, delegateInfo: DelegateInfo) { |
731 | 778 | const structure = iface.getStructure(); |
732 | 779 |
|
|
0 commit comments