import type { GeneratorConfig , BinaryTargetsEnvValue } from '@prisma/generator-helper' ; import indent from 'indent-string' ; export function printGeneratorConfig ( config ) { return String ( new GeneratorConfigClass ( config ) ) ; } export class GeneratorConfigClass { constructor ( private readonly config ) { } toString ( ) { const { config } = this ; const provider = config . provider . fromEnvVar ? `env("${ config . provider . fromEnvVar }")` : config . provider . value ; const obj = JSON . parse ( JSON . stringify ( { provider , binaryTargets : getOriginalBinaryTargetsValue ( config . binaryTargets ) , } ) ) ; return `generator ${ config . name } {
${ indent ( printDatamodelObject ( obj ) , 2 ) }
}` ; } } export function getOriginalBinaryTargetsValue ( binaryTargets ) { let value ; if ( binaryTargets . length > 0 ) { const binaryTargetsFromEnvVar = binaryTargets . find ( ( object ) => object . fromEnvVar !== null ) ; if ( binaryTargetsFromEnvVar ) { value = `env("${ binaryTargetsFromEnvVar . fromEnvVar }")` ; } else { value = binaryTargets . map ( ( object ) => object . value ) ; } } else { value = undefined ; } return value ; } export function printDatamodelObject ( obj ) { const maxLength = Object . keys ( obj ) . reduce ( ( max , curr ) => Math . max ( max , curr . length ) , 0 ) ; return Object . entries ( obj ) . map ( ( [ key , value ] ) => `${ key . padEnd ( maxLength ) } = ${ niceStringify ( value ) }` ) . join ( '\n' ) ; } function niceStringify ( value ) { return JSON . parse ( JSON . stringify ( value , ( _ , value ) => { if ( Array . isArray ( value ) ) { return `[${ value . map ( ( element ) => JSON . stringify ( element ) ) . join ( ', ' ) }]` ; } return JSON . stringify ( value ) ; } ) ) ; }