import fs from 'fs' ; import Module from 'module' ; import pkgUp from 'pkg-up' ; import { promisify } from 'util' ; const readFileAsync = promisify ( fs . readFile ) ; /**
 * Try reading the installed Prisma Client version
 */ export async function getInstalledPrismaClientVersion ( cwd = process . cwd ( ) ) { return ( await getPrismaClientVersionFromNodeModules ( cwd ) ) ?? ( await getPrismaClientVersionFromLocalPackageJson ( cwd ) ) ; } /**
 * Try reading the Prisma Client version from its package.json
 */ async function getPrismaClientVersionFromNodeModules ( cwd = process . cwd ( ) ) { try { const pkgJsonPath = requireResolveFrom ( '@prisma/client/package.json' , cwd ) ; if ( ! pkgJsonPath ) { return null ; } const pkgJsonString = await readFileAsync ( pkgJsonPath , 'utf-8' ) ; const pkgJson = JSON . parse ( pkgJsonString ) ; if ( ! pkgJson . version ) { return null ; } return pkgJson . version ; } catch { return null ; } } /**
 * Try reading the Prisma Client version from the local package.json
 */ async function getPrismaClientVersionFromLocalPackageJson ( cwd = process . cwd ( ) ) { try { const pkgJsonPath = await pkgUp ( { cwd } ) ; if ( ! pkgJsonPath ) { return null ; } const pkgJsonString = await readFileAsync ( pkgJsonPath , 'utf-8' ) ; const pkgJson = JSON . parse ( pkgJsonString ) ; const clientVersion = pkgJson . dependencies ?. [ '@prisma/client' ] ?? pkgJson . devDependencies ?. [ '@prisma/client' ] ; if ( ! clientVersion ) { return null ; } return clientVersion ; } catch { return null ; } } /**
 * Run require resolve from a given path
 */ function requireResolveFrom ( moduleId , fromDir ) { try { const resolvedPath = require . resolve ( moduleId , { paths : ( Module as any ) . _nodeModulePaths ( fromDir ) , } ) ; return resolvedPath ; } catch { return null ; } }