import { generatorHandler , GeneratorOptions , DMMF , } from '@prisma/generator-helper' ; import { DMMF as PrismaClientDMMF } from '@prisma/client/runtime/dmmf-types' ; import mls from 'multilines' ; import * as path from 'path' ; import rimraf from 'rimraf' ; import { ModuleKind , ScriptTarget } from 'typescript' ; import { promisify } from 'util' ; import { VirtualFS , writeToFS , copyVFS , compileVFS } from './vfs' ; import { generateGeneratedSeedModelsType } from './intellisense/seed' ; import { generatePoolType } from './intellisense/pool' ; const del = promisify ( rimraf ) ; param options export async function generatePrismaTestUtils ( options ) { const client = options . otherGenerators . find ( og => og . provider === 'prisma-client-js' ) ! ; if ( ! client ) { throw new Error ( `You need to generate Prisma Client first.` ) ; } const photonPath = client . output ! ; const outputDir = options . generator . output ! ; try { await del ( outputDir , { } ) ; } catch ( err ) { console . log ( `Error while copying Runtime` ) ; console . error ( err ) ; return '' ; } const staticFs = { [ path . join ( outputDir , './static/index.js' ) ] : eval ( `path.join(
      __dirname,
      '../runtime/index.js',
    )` ) , [ path . join ( outputDir , './static/' ) ] : eval ( `path.join(
      __dirname,
      '../runtime',
    )` ) , } ; try { await copyVFS ( staticFs ) ; } catch ( err ) { console . log ( `Error while copying Runtime` ) ; console . error ( err ) ; return '' ; } /**
     * The generation process is separated into three parts:
     *  1. We use TS to build the dynamic parts of the library which refer to the
     *    static ones.
     *  2. We wire the functions from the static part with the dynamic properties.
     *  3. We write the files to the file system.
     */ const dmmf = require ( photonPath ) . dmmf as PrismaClientDMMF . Document ; const seedLib = mls `
  | import { PrismaClient, dmmf } from '${ photonPath }';
  | import { getSeed, SeedModels, SeedFunction } from './static';
  |
  | interface GeneratedSeedModels extends SeedModels {
  |   ${ generateGeneratedSeedModelsType ( dmmf ) }
  | }
  |
  | export const seed: SeedFunction<PrismaClient, GeneratedSeedModels>  = getSeed<PrismaClient, GeneratedSeedModels>(dmmf);
  | export default seed
  | export { GeneratedSeedModels }
  | export {
  |   SeedOptions,
  |   SeedModelsDefinition,
  |   SeedKit,
  |   SeedModels,
  |   SeedModel,
  |   ID,
  |   SeedModelFieldDefinition,
  |   SeedModelFieldRelationConstraint,
  | } from './static'
  ` ; const poolLib = mls `
  | import { dmmf } from '${ photonPath }';
  | import { getMySQLPool, getPostgreSQLPool, getSQLitePool } from './static';
  |
  | export default ${ generatePoolType ( options ) }(dmmf, "${ __dirname }");
  | export { MySQLPoolOptions, PostgreSQLPoolOptions, SQLitePoolOptions } from './static'
  | export { Pool, DBInstance } from './static';
  ` ; const dynamicFS = { [ path . join ( outputDir , './pool.ts' ) ] : poolLib , [ path . join ( outputDir , './seed.ts' ) ] : seedLib , } ; /**
     * Write dynamic files to file system.
     */ try { const compiledFS = await compileVFS ( dynamicFS , { module : ModuleKind . CommonJS , target : ScriptTarget . ES2016 , lib : [ 'lib.esnext.d.ts' , 'lib.dom.d.ts' ] , declaration : true , sourceMap : true , suppressOutputPathCheck : false , } ) ; await writeToFS ( compiledFS ) ; } catch ( err ) { console . log ( `Error in Test Utils generation.` ) ; console . error ( err ) ; } return '' ; } generatorHandler ( { onManifest ( ) { return { prettyName : 'Prisma Test Utils' , defaultOutput : 'node_modules/@prisma/test-utils' , requiresGenerators : [ 'prisma-client-js' ] , requiresEngines : [ 'queryEngine' , 'migrationEngine' ] , } ; } , async onGenerate ( options ) { return generatePrismaTestUtils ( options ) ; } , } ) ;