<commit_before>import { GeneratorConfig } from '@prisma/generator-helper'
import { Platform } from '@prisma/get-platform'
import { getEnvPaths } from '@prisma/sdk/dist/utils/getEnvPaths'
import indent from 'indent-string'
import { klona } from 'klona'
import path from 'path'
import { DMMFClass } from '../../runtime/dmmf'
import { DMMF } from '../../runtime/dmmf-types'
import { GetPrismaClientOptions } from '../../runtime/getPrismaClient'
import { InternalDatasource } from '../../runtime/utils/printDatasources'
import { getClientEngineType } from '../../runtime/utils/getClientEngineType'
import { buildNFTEngineAnnotations } from '../utils'
import { DatasourceOverwrite } from './../extractSqliteSources'
import { commonCodeJS, commonCodeTS } from './common'
import { Count } from './Count'
import { Enum } from './Enum'
import { Generatable } from './Generatable'
import { escapeJson, ExportCollector } from './helpers'
import { InputType } from './Input'
import { Model } from './Model'
import { PrismaClientClass } from './PrismaClient'

export interface TSClientOptions {
  projectRoot: string
  clientVersion: string
  engineVersion: string
  document: DMMF.Document
  runtimePath: string
  browser?: boolean
  datasources: InternalDatasource[]
  generator?: GeneratorConfig
  platforms?: string[]
  sqliteDatasourceOverrides?: DatasourceOverwrite[]
  schemaDir: string
  outputDir: string
  activeProvider: string
}

export class TSClient implements Generatable {
  protected readonly dmmf: DMMFClass
  protected readonly dmmfString: string
  constructor(protected readonly options: TSClientOptions) {
    this.dmmfString = escapeJson(JSON.stringify(options.document))
    this.dmmf = new DMMFClass(klona(options.document))
  }
  public toJS(): string {
    const { generator, sqliteDatasourceOverrides, outputDir, schemaDir } =
      this.options
    const schemaPath = path.join(schemaDir, 'prisma.schema')
    const envPaths = getEnvPaths(schemaPath, { cwd: outputDir })

    const relativeEnvPaths = {
      rootEnvPath:
        envPaths.rootEnvPath && path.relative(outputDir, envPaths.rootEnvPath),
      schemaEnvPath:
        envPaths.schemaEnvPath &&
        path.relative(outputDir, envPaths.schemaEnvPath),
    }

    const config: Omit<GetPrismaClientOptions, 'document' | 'dirname'> = {
      generator,
      relativeEnvPaths,
      sqliteDatasourceOverrides,
      relativePath: path.relative(outputDir, schemaDir),
      clientVersion: this.options.clientVersion,
      engineVersion: this.options.engineVersion,
      datasourceNames: this.options.datasources.map((d) => d.name),
      activeProvider: this.options.activeProvider,
    }
    // This ensures that any engine override is propagated to the generated clients config
    const clientEngineType = getClientEngineType(config.generator!)
    if (config.generator) {
      config.generator.config.engineType = clientEngineType
    }

    // get relative output dir for it to be preserved even after bundling, or
    // being moved around as long as we keep the same project dir structure.
    const relativeOutputDir = path.relative(process.cwd(), outputDir)

    // on serverless envs, relative output dir can be one step lower because of
    // where and how the code is packaged into the lambda like with a build step
    // with platforms like Vercel or Netlify. We want to check this as well.
    const slsRelativeOutputDir = path
      .relative(process.cwd(), outputDir)
      .split(path.sep)
      .slice(1)
      .join(path.sep)

    const code = `${commonCodeJS({ ...this.options, browser: false })}

// folder where the generated client is found
const dirname = findSync(process.cwd(), [
  '${JSON.stringify(relativeOutputDir)}',
  '${JSON.stringify(slsRelativeOutputDir)}',
], ['d'], ['d'], 1)[0] || __dirname

/**
 * Enums
 */
// Based on
// https://github.com/microsoft/TypeScript/issues/3192#issuecomment-261720275
function makeEnum(x) { return x; }

${this.dmmf.schema.enumTypes.prisma
  .map((type) => new Enum(type, true).toJS())
  .join('\n\n')}
${
  this.dmmf.schema.enumTypes.model
    ?.map((type) => new Enum(type, false).toJS())
    .join('\n\n') ?? ''
}

${new Enum(
  {
    name: 'ModelName',
    values: this.dmmf.mappings.modelOperations.map((m) => m.model),
  },
  true,
).toJS()}
<commit_msg>fix: emitted patterns for engine resolution (#9295)<commit_after>import { GeneratorConfig } from '@prisma/generator-helper'
import { Platform } from '@prisma/get-platform'
import { getEnvPaths } from '@prisma/sdk/dist/utils/getEnvPaths'
import indent from 'indent-string'
import { klona } from 'klona'
import path from 'path'
import { DMMFClass } from '../../runtime/dmmf'
import { DMMF } from '../../runtime/dmmf-types'
import { GetPrismaClientOptions } from '../../runtime/getPrismaClient'
import { InternalDatasource } from '../../runtime/utils/printDatasources'
import { getClientEngineType } from '../../runtime/utils/getClientEngineType'
import { buildNFTEngineAnnotations } from '../utils'
import { DatasourceOverwrite } from './../extractSqliteSources'
import { commonCodeJS, commonCodeTS } from './common'
import { Count } from './Count'
import { Enum } from './Enum'
import { Generatable } from './Generatable'
import { escapeJson, ExportCollector } from './helpers'
import { InputType } from './Input'
import { Model } from './Model'
import { PrismaClientClass } from './PrismaClient'

export interface TSClientOptions {
  projectRoot: string
  clientVersion: string
  engineVersion: string
  document: DMMF.Document
  runtimePath: string
  browser?: boolean
  datasources: InternalDatasource[]
  generator?: GeneratorConfig
  platforms?: string[]
  sqliteDatasourceOverrides?: DatasourceOverwrite[]
  schemaDir: string
  outputDir: string
  activeProvider: string
}

export class TSClient implements Generatable {
  protected readonly dmmf: DMMFClass
  protected readonly dmmfString: string
  constructor(protected readonly options: TSClientOptions) {
    this.dmmfString = escapeJson(JSON.stringify(options.document))
    this.dmmf = new DMMFClass(klona(options.document))
  }
  public toJS(): string {
    const { generator, sqliteDatasourceOverrides, outputDir, schemaDir } =
      this.options
    const schemaPath = path.join(schemaDir, 'prisma.schema')
    const envPaths = getEnvPaths(schemaPath, { cwd: outputDir })

    const relativeEnvPaths = {
      rootEnvPath:
        envPaths.rootEnvPath && path.relative(outputDir, envPaths.rootEnvPath),
      schemaEnvPath:
        envPaths.schemaEnvPath &&
        path.relative(outputDir, envPaths.schemaEnvPath),
    }

    const config: Omit<GetPrismaClientOptions, 'document' | 'dirname'> = {
      generator,
      relativeEnvPaths,
      sqliteDatasourceOverrides,
      relativePath: path.relative(outputDir, schemaDir),
      clientVersion: this.options.clientVersion,
      engineVersion: this.options.engineVersion,
      datasourceNames: this.options.datasources.map((d) => d.name),
      activeProvider: this.options.activeProvider,
    }
    // This ensures that any engine override is propagated to the generated clients config
    const clientEngineType = getClientEngineType(config.generator!)
    if (config.generator) {
      config.generator.config.engineType = clientEngineType
    }

    // get relative output dir for it to be preserved even after bundling, or
    // being moved around as long as we keep the same project dir structure.
    const relativeOutputDir = path.relative(process.cwd(), outputDir)

    // on serverless envs, relative output dir can be one step lower because of
    // where and how the code is packaged into the lambda like with a build step
    // with platforms like Vercel or Netlify. We want to check this as well.
    const slsRelativeOutputDir = path
      .relative(process.cwd(), outputDir)
      .split(path.sep)
      .slice(1)
      .join(path.sep)

    const code = `${commonCodeJS({ ...this.options, browser: false })}

// folder where the generated client is found
const dirname = findSync(process.cwd(), [
  ${JSON.stringify(relativeOutputDir)},
  ${JSON.stringify(slsRelativeOutputDir)},
], ['d'], ['d'], 1)[0] || __dirname

/**
 * Enums
 */
// Based on
// https://github.com/microsoft/TypeScript/issues/3192#issuecomment-261720275
function makeEnum(x) { return x; }

${this.dmmf.schema.enumTypes.prisma
  .map((type) => new Enum(type, true).toJS())
  .join('\n\n')}
${
  this.dmmf.schema.enumTypes.model
    ?.map((type) => new Enum(type, false).toJS())
    .join('\n\n') ?? ''
}

${new Enum(
  {
    name: 'ModelName',
    values: this.dmmf.mappings.modelOperations.map((m) => m.model),
  },
  true,
).toJS()}

{'fraction_non_alphanumeric': 0.12297734627831715, 'fraction_numerical': 0.004160887656033287, 'mean_word_length': 3.8367803242034655, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3429330', 'n_tokens_mistral': 2591, 'n_tokens_neox': 2555, 'n_words': 676}