import type Readline from "readline" ; import Ganache , { ServerStatus } from "@ganache/core" ; import args from "./args" ; import { EthereumFlavorName , FilecoinFlavorName } from "@ganache/flavors" ; import initializeEthereum from "./initialize/ethereum" ; import initializeFilecoin from "./initialize/filecoin" ; import type { Provider as FilecoinProvider } from "@ganache/filecoin" ; import type { Provider as EthereumProvider } from "@ganache/ethereum" ; const logAndForceExit = ( messages , exitCode = 0 ) => { if ( ( process . stdout as any ) . _handle ) { ( process . stdout as any ) . _handle . setBlocking ( true ) ; } try { messages . forEach ( message => console . log ( message ) ) ; } catch ( e ) { console . log ( e ) ; } process . exit ( exitCode ) ; } ; const version = process . env . VERSION || "DEV" ; const cliVersion = process . env . CLI_VERSION || "DEV" ; const coreVersion = process . env . CORE_VERSION || "DEV" ; const detailedVersion = `ganache v${ version } (@ganache/cli: ${ cliVersion }, @ganache/core: ${ coreVersion })` ; const isDocker = "DOCKER" in process . env && process . env . DOCKER . toLowerCase ( ) === "true" ; const argv = args ( detailedVersion , isDocker ) ; const flavor = argv . flavor ; const cliSettings = argv . server ; console . log ( detailedVersion ) ; let server ; try { server = Ganache . server ( argv ) ; } catch ( error ) { console . error ( error . message ) ; process . exit ( 1 ) ; } let started = false ; process . on ( "uncaughtException" , function ( e ) { if ( started ) { logAndForceExit ( [ e ] , 1 ) ; } else { logAndForceExit ( [ e . stack ] , 1 ) ; } } ) ; let receivedShutdownSignal = false ; const handleSignal = async ( signal ) => { console . log ( `\nReceived shutdown signal: ${ signal }` ) ; closeHandler ( ) ; } ; const closeHandler = async ( ) => { try { switch ( server . status ) { case ServerStatus . opening : receivedShutdownSignal = true ; console . log ( "Server is currently starting; waiting…" ) ; return ; case ServerStatus . open : console . log ( "Shutting down…" ) ; await server . close ( ) ; console . log ( "Server has been shut down" ) ; break ; } process . exitCode = 0 ; } catch ( err ) { logAndForceExit ( [ "\nReceived an error while attempting to shut down the server: " , err . stack || err ] , 1 ) ; } } ; if ( process . platform === "win32" ) { const rl = ( require ( "readline" ) as typeof Readline ) . createInterface ( { input : process . stdin , output : process . stdout } ) . on ( "SIGINT" , ( ) => { rl . close ( ) ; handleSignal ( "SIGINT" ) ; } ) ; } process . on ( "SIGINT" , handleSignal ) ; process . on ( "SIGTERM" , handleSignal ) ; process . on ( "SIGHUP" , handleSignal ) ; async function startGanache ( err ) { if ( err ) { console . log ( err ) ; process . exitCode = 1 ; return ; } else if ( receivedShutdownSignal ) { closeHandler ( ) ; return ; } started = true ; switch ( flavor ) { case FilecoinFlavorName : { await initializeFilecoin ( server . provider as FilecoinProvider , cliSettings ) ; break ; } case EthereumFlavorName : default : { initializeEthereum ( server . provider as EthereumProvider , cliSettings ) ; break ; } } } console . log ( "Starting RPC server" ) ; server . listen ( cliSettings . port , cliSettings . host , startGanache ) ;