import Debug from '@prisma/debug' ; import findUp from 'find-up' ; import path from 'path' ; import fs from 'fs' ; import { getSchemaPathFromPackageJsonSync } from '../cli/getSchema' ; import { exists } from './tryLoadEnvs' ; const debug = Debug ( 'prisma:loadEnv' ) ; export type EnvPaths = { rootEnvPath : string | null ; schemaEnvPath : string | undefined ; } ; returns export function getEnvPaths ( schemaPath ? , opts = { cwd : process . cwd ( ) } ) { const rootEnvPath = getProjectRootEnvPath ( { cwd : opts . cwd } ) ?? null ; const schemaEnvPathFromArgs = schemaPathToEnvPath ( schemaPath ) ; const schemaEnvPathFromPkgJson = schemaPathToEnvPath ( readSchemaPathFromPkgJson ( ) ) ; const schemaEnvPaths = [ schemaEnvPathFromArgs , schemaEnvPathFromPkgJson , './prisma/.env' , './.env' , ] ; const schemaEnvPath = schemaEnvPaths . find ( exists ) ; return { rootEnvPath , schemaEnvPath } ; } function readSchemaPathFromPkgJson ( ) { try { return getSchemaPathFromPackageJsonSync ( process . cwd ( ) ) ; } catch { return null ; } } function getProjectRootEnvPath ( opts ) { const pkgJsonPath = findUp . sync ( ( dir ) => { const pkgPath = path . join ( dir , 'package.json' ) ; if ( findUp . sync . exists ( pkgPath ) ) { try { const pkg = JSON . parse ( fs . readFileSync ( pkgPath , 'utf8' ) ) ; if ( pkg [ 'name' ] !== '.prisma/client' ) { debug ( `project root found at ${ pkgPath }` ) ; return pkgPath ; } } catch ( e ) { debug ( `skipping package.json at ${ pkgPath }` ) ; } } return undefined ; } , opts ) ; if ( ! pkgJsonPath ) { return null ; } const candidate = path . join ( path . dirname ( pkgJsonPath ) , '.env' ) ; if ( ! fs . existsSync ( candidate ) ) { return null ; } return candidate ; } function schemaPathToEnvPath ( schemaPath ) { if ( ! schemaPath ) return null ; return path . join ( path . dirname ( schemaPath ) , '.env' ) ; }