diff --git a/src/test/create-generator-options.ts b/src/test/create-generator-options.ts deleted file mode 100644 index dc5e5faf..00000000 --- a/src/test/create-generator-options.ts +++ /dev/null @@ -1,71 +0,0 @@ -import { GeneratorOptions } from '@prisma/generator-helper'; -import { exec } from 'child_process'; -import crypto from 'crypto'; -import fs from 'fs'; -import cachePath from 'temp-dir'; - -import { DMMF } from '../types'; - -const { - dependencies: { '@prisma/generator-helper': generatorVersion }, - // eslint-disable-next-line @typescript-eslint/no-var-requires -} = require('../../package.json'); - -/** - * Get generator options after run prisma generate. - */ -export async function createGeneratorOptions( - schema: string, - options?: string[], -): Promise { - const schemaHeader = ` - datasource db { - provider = "postgresql" - url = env("DATABASE_URL") - } - generator client { - provider = "prisma-client-js" - previewFeatures = ["filterJson"] - } - `; - // eslint-disable-next-line prefer-rest-params - const hash = createHash(generatorVersion, schemaHeader, arguments); - const cacheFile = `${cachePath}/options-${hash}.js`; - if (!fs.existsSync(cacheFile)) { - const schemaFile = `${cachePath}/schema-${hash}.prisma`; - const schemaContent = ` - ${schemaHeader} - generator proxy { - provider = "node -r ts-node/register/transpile-only src/test/proxy-generator.ts" - output = "." - hash = "${hash}" - ${options?.join('\n') || ''} - } - ${schema} - `; - fs.writeFileSync(schemaFile, schemaContent); - - await new Promise((resolve, reject) => { - const proc = exec( - `node node_modules/prisma/build/index.js generate --schema=${schemaFile}`, - ); - if (!proc.stderr) { - throw new Error('Generate error'); - } - proc.stdout?.pipe(process.stdout); - proc.stderr.on('data', data => { - reject(String(data)); - }); - proc.on('error', reject); - proc.on('exit', code => { - code === 0 ? resolve(0) : reject(); - }); - }); - } - // eslint-disable-next-line @typescript-eslint/no-unsafe-return - return require(cacheFile); -} - -function createHash(...data: unknown[]) { - return crypto.createHash('md5').update(JSON.stringify(data)).digest('hex'); -} diff --git a/src/test/test-generate.ts b/src/test/test-generate.ts index f7480b01..26ac0214 100644 --- a/src/test/test-generate.ts +++ b/src/test/test-generate.ts @@ -1,13 +1,21 @@ -/* eslint-disable sonarjs/cognitive-complexity */ +import { GeneratorOptions } from '@prisma/generator-helper'; import { ok } from 'assert'; import AwaitEventEmitter from 'await-event-emitter/types'; +import { exec } from 'child_process'; +import crypto from 'crypto'; +import fs from 'fs'; import { uniq } from 'lodash'; +import cachePath from 'temp-dir'; import { ImportSpecifierStructure, Project } from 'ts-morph'; import { generate } from '../generate'; import { generateFileName } from '../helpers/generate-file-name'; -import { EventArguments } from '../types'; -import { createGeneratorOptions } from './create-generator-options'; +import { DMMF, EventArguments } from '../types'; + +const { + dependencies: { '@prisma/generator-helper': generatorVersion }, + // eslint-disable-next-line @typescript-eslint/no-var-requires +} = require('../../package.json'); export async function testGenerate(args: { schema: string; @@ -23,7 +31,6 @@ export async function testGenerate(args: { let project: Project | undefined; const connectCallback = (emitter: AwaitEventEmitter) => { onConnect && onConnect(emitter); - emitter.off('GenerateFiles'); if (createSouceFile) { emitter.on( 'PostBegin', @@ -106,3 +113,62 @@ export async function testGenerate(args: { return { project, sourceFiles }; } + +/** + * Get generator options after run prisma generate. + */ +async function createGeneratorOptions( + schema: string, + options?: string[], +): Promise { + const schemaHeader = ` + datasource db { + provider = "postgresql" + url = env("DATABASE_URL") + } + generator client { + provider = "prisma-client-js" + previewFeatures = ["filterJson", "mongoDb", "fullTextSearch", "referentialIntegrity", "extendedIndexes", "fullTextIndex"] + } + `; + // eslint-disable-next-line prefer-rest-params + const hash = createHash(generatorVersion, schemaHeader, arguments); + const cacheFile = `${cachePath}/options-${hash}.js`; + if (!fs.existsSync(cacheFile)) { + const schemaFile = `${cachePath}/schema-${hash}.prisma`; + const schemaContent = ` + ${schemaHeader} + generator proxy { + provider = "node -r ts-node/register/transpile-only src/test/proxy-generator.ts" + output = "." + hash = "${hash}" + ${options?.join('\n') || ''} + } + ${schema} + `; + fs.writeFileSync(schemaFile, schemaContent); + + await new Promise((resolve, reject) => { + const proc = exec( + `node node_modules/prisma/build/index.js generate --schema=${schemaFile}`, + ); + if (!proc.stderr) { + throw new Error('Generate error'); + } + proc.stdout?.pipe(process.stdout); + proc.stderr.on('data', data => { + reject(String(data)); + }); + proc.on('error', reject); + proc.on('exit', code => { + code === 0 ? resolve(0) : reject(); + }); + }); + } + // eslint-disable-next-line @typescript-eslint/no-unsafe-return + return require(cacheFile); +} + +function createHash(...data: unknown[]) { + return crypto.createHash('md5').update(JSON.stringify(data)).digest('hex'); +}