import { GeneratorOptions } from "@prisma/generator-helper" ; import { DMMF as PrismaDMMF } from "@prisma/client/runtime" ; import { parseEnvValue } from "@prisma/sdk" ; import { promises as asyncFs } from "fs" ; import path from "path" ; import generateCode from "../generator/generate-code" ; import removeDir from "../utils/removeDir" ; import { ExternalGeneratorOptions , InternalGeneratorOptions , } from "../generator/options" ; import { toUnixPath } from "../generator/helpers" ; import { ALL_EMIT_BLOCK_KINDS } from "../generator/emit-block" ; import { parseStringBoolean , parseStringArray } from "./helpers" ; export async function generate ( options ) { const outputDir = parseEnvValue ( options . generator . output ! ) ; await asyncFs . mkdir ( outputDir , { recursive : true } ) ; await removeDir ( outputDir , true ) ; const prismaClientProvider = options . otherGenerators . find ( it => parseEnvValue ( it . provider ) === "prisma-client-js" ) ! ; const prismaClientPath = parseEnvValue ( prismaClientProvider . output ! ) ; const prismaClientDmmf = require ( prismaClientPath ) . dmmf as PrismaDMMF . Document ; const generatorConfig = options . generator . config ; const externalConfig = { emitDMMF : parseStringBoolean ( generatorConfig . emitDMMF ) , emitTranspiledCode : parseStringBoolean ( generatorConfig . emitTranspiledCode ) , simpleResolvers : parseStringBoolean ( generatorConfig . simpleResolvers ) , useOriginalMapping : parseStringBoolean ( generatorConfig . useOriginalMapping ) , useUncheckedScalarInputs : parseStringBoolean ( generatorConfig . useUncheckedScalarInputs ) , emitIdAsIDType : parseStringBoolean ( generatorConfig . emitIdAsIDType ) , emitOnly : parseStringArray ( generatorConfig . emitOnly , "emitOnly" , ALL_EMIT_BLOCK_KINDS ) , customPrismaImportPath : generatorConfig . customPrismaImportPath , } ; const internalConfig = { outputDirPath : outputDir , relativePrismaOutputPath : toUnixPath ( path . relative ( outputDir , prismaClientPath ) ) , absolutePrismaOutputPath : prismaClientPath . includes ( "node_modules" ) ? "@prisma/client" : undefined , } ; if ( externalConfig . emitDMMF ) { await Promise . all ( [ asyncFs . writeFile ( path . resolve ( outputDir , "./dmmf.json" ) , JSON . stringify ( options . dmmf , null , 2 ) ) , asyncFs . writeFile ( path . resolve ( outputDir , "./prisma-client-dmmf.json" ) , JSON . stringify ( prismaClientDmmf , null , 2 ) ) , ] ) ; } await generateCode ( prismaClientDmmf , { ... externalConfig , ... internalConfig , } ) ; return "" ; }