import deepmerge from "deepmerge" ; import { Json } from "@chainsafe/ssz" ; import { defaultOptions , IBeaconNodeOptions } from "@chainsafe/lodestar" ; import { isPlainObject , RecursivePartial } from "@chainsafe/lodestar-utils" ; import { writeFile , readFile } from "../util" ; import { getInjectableBootEnrs , getNetworkBeaconNodeOptions , NetworkName } from "../networks" ; export class BeaconNodeOptions { private beaconNodeOptions : RecursivePartial < IBeaconNodeOptions > ; /**
     * Reads, parses and merges BeaconNodeOptions from (in order)
     * - Network options (diff)
     * - existing options file
     * - CLI flags
     */ constructor ( { network , configFile , bootnodesFile , beaconNodeOptionsCli , } ) { this . beaconNodeOptions = mergeBeaconNodeOptions ( network ? getNetworkBeaconNodeOptions ( network ) : { } , configFile ? readBeaconNodeOptions ( configFile ) : { } , bootnodesFile ? getInjectableBootEnrs ( bootnodesFile ) : { } , beaconNodeOptionsCli ) ; } /**
     * Returns current options
     */ get ( ) { return this . beaconNodeOptions ; } /**
     * Returns merged current options with defaultOptions
     */ getWithDefaults ( ) { return mergeBeaconNodeOptionsWithDefaults ( defaultOptions , this . beaconNodeOptions ) ; } set ( beaconNodeOptionsPartial ) { this . beaconNodeOptions = mergeBeaconNodeOptions ( this . beaconNodeOptions , beaconNodeOptionsPartial ) ; } writeTo ( filepath ) { writeFile ( filepath , this . beaconNodeOptions as Json ) ; } } export function writeBeaconNodeOptions ( filename , config ) { writeFile ( filename , config as Json ) ; } /**
 * This needs to be a synchronous function because it will be run as part of the yargs 'build' step
 * If the config file is not found, the default values will apply.
 */ export function readBeaconNodeOptions ( filepath ) { return readFile ( filepath ) ; } /**
 * Typesafe wrapper to merge partial IBeaconNodeOptions objects
 */ export function mergeBeaconNodeOptions ( ... partialOptionsArr ) { return partialOptionsArr . reduce ( ( mergedBeaconOptions , options ) => { return deepmerge ( mergedBeaconOptions , options , { arrayMerge : overwriteTargetArrayIfItems , isMergeableObject : isPlainObject , } ) ; } , partialOptionsArr [ 0 ] ) ; } /**
 * Typesafe wrapper to merge IBeaconNodeOptions objects
 */ export function mergeBeaconNodeOptionsWithDefaults ( defaultOptions , ... partialOptionsArr ) { return mergeBeaconNodeOptions ( defaultOptions , ... partialOptionsArr ) as IBeaconNodeOptions ; } param target param source function overwriteTargetArrayIfItems ( target , source ) { if ( source . length === 0 ) { return target ; } return source ; }