import { enginesVersion } from '@prisma/engines' ; import type { BinaryDownloadConfiguration , DownloadOptions } from '@prisma/fetch-engine' ; import { download } from '@prisma/fetch-engine' ; import type { BinaryPaths , BinaryTargetsEnvValue } from '@prisma/generator-helper' ; import type { Platform } from '@prisma/get-platform' ; import makeDir from 'make-dir' ; import path from 'path' ; import { binaryTypeToEngineType } from '../utils/binaryTypeToEngineType' ; import { engineTypeToBinaryType } from '../utils/engineTypeToBinaryType' ; import type { GetBinaryPathsByVersionInput } from '../getGenerators' ; import { mapKeys } from '../../utils/mapKeys' ; export async function getBinaryPathsByVersion ( { neededVersions , platform , version , printDownloadProgress , skipDownload , binaryPathsOverride , } ) { const binaryPathsByVersion = Object . create ( null ) ; for ( const currentVersion in neededVersions ) { binaryPathsByVersion [ currentVersion ] = { } ; const neededVersion = neededVersions [ currentVersion ] ; if ( neededVersion . binaryTargets . length === 0 ) { neededVersion . binaryTargets = [ { fromEnvVar : null , value : platform } ] ; } if ( process . env . NETLIFY && ! neededVersion . binaryTargets . find ( ( object ) => object . value === 'rhel-openssl-1.0.x' ) ) { neededVersion . binaryTargets . push ( { fromEnvVar : null , value : 'rhel-openssl-1.0.x' , } ) ; } let binaryTargetBaseDir = eval ( `require('path').join(__dirname, '..')` ) ; if ( version !== currentVersion ) { binaryTargetBaseDir = path . join ( binaryTargetBaseDir , `./engines/${ currentVersion }/` ) ; await makeDir ( binaryTargetBaseDir ) . catch ( ( e ) => console . error ( e ) ) ; } const binariesConfig = neededVersion . engines . reduce ( ( acc , curr ) => { if ( ! binaryPathsOverride ?. [ curr ] ) { acc [ engineTypeToBinaryType ( curr ) ] = binaryTargetBaseDir ; } return acc ; } , Object . create ( null ) ) ; if ( Object . values ( binariesConfig ) . length > 0 ) { const platforms = neededVersion . binaryTargets . map ( ( binaryTarget ) => binaryTarget . value as Platform ) ; const downloadParams = { binaries : binariesConfig , binaryTargets : platforms , showProgress : typeof printDownloadProgress === 'boolean' ? printDownloadProgress : true , version : currentVersion && currentVersion !== 'latest' ? currentVersion : enginesVersion , skipDownload , } ; const binaryPathsWithEngineType = await download ( downloadParams ) ; const binaryPaths = mapKeys ( binaryPathsWithEngineType , binaryTypeToEngineType ) ; binaryPathsByVersion [ currentVersion ] = binaryPaths ; } if ( binaryPathsOverride ) { const overrideEngines = Object . keys ( binaryPathsOverride ) ; const enginesCoveredByOverride = neededVersion . engines . filter ( ( engine ) => overrideEngines . includes ( engine ) ) ; if ( enginesCoveredByOverride . length > 0 ) { for ( const engine of enginesCoveredByOverride ) { const enginePath = binaryPathsOverride [ engine ] ! ; binaryPathsByVersion [ currentVersion ] [ engine ] = { [ platform ] : enginePath , } ; } } } } return binaryPathsByVersion ; }