import { MockoonServer } from '@mockoon/commons-server' ; import { app , BrowserWindow } from 'electron' ; import { catchErrors as logCatchErrors , error as logError , info as logInfo , transports as logTransports } from 'electron-log' ; import { join as pathJoin , resolve as pathResolve } from 'path' ; import { parseProtocolArgs , registerProtocol } from './libs/custom-protocol' ; import { clearIPCChannels , initIPCListeners } from './libs/ipc' ; import { initMainWindow , saveOpenUrlArgs } from './libs/main-window' ; import { checkForUpdate } from './libs/update' ; declare const isTesting ; declare const isDev ; const setAppAndLogPath = ( path ) => { app . setPath ( 'userData' , path ) ; logTransports . file . resolvePath = ( ) => pathJoin ( path , '/logs' , 'main.log' ) ; } ; if ( isTesting || isDev ) { setAppAndLogPath ( pathResolve ( './tmp' ) ) ; } const portableExecDir = process . env . PORTABLE_EXECUTABLE_DIR ; if ( portableExecDir ) { setAppAndLogPath ( pathJoin ( portableExecDir , 'mockoon-data' ) ) ; } logCatchErrors ( { onError : ( error ) => { logError ( error ) ; return false ; } } ) ; const runningServerInstances = { } ; let mainWindow ; const appLock = app . requestSingleInstanceLock ( ) ; const initApp = ( ) => { mainWindow = initMainWindow ( ) ; initIPCListeners ( mainWindow , runningServerInstances ) ; if ( isDev ) { import ( './libs/hot-reload' ) . then ( ( hotReloadModule ) => { hotReloadModule . hotReload ( ) ; } ) ; } } ; if ( ! appLock ) { logInfo ( '[MAIN]An instance of the application is already running. Stopping process.' ) ; app . quit ( ) ; } else { app . on ( 'second-instance' , ( event , args ) => { if ( process . platform === 'win32' || process . platform === 'linux' ) { parseProtocolArgs ( args , mainWindow ) ; } if ( mainWindow ) { if ( mainWindow . isMinimized ( ) ) { mainWindow . restore ( ) ; } mainWindow . focus ( ) ; } } ) ; app . on ( 'ready' , ( ) => { registerProtocol ( ) ; initApp ( ) ; checkForUpdate ( mainWindow ) ; app . on ( 'activate' , ( ) => { if ( BrowserWindow . getAllWindows ( ) . length === 0 ) { initApp ( ) ; } } ) ; } ) ; app . on ( 'window-all-closed' , ( ) => { Object . keys ( runningServerInstances ) . forEach ( ( runningEnvironmentUUID ) => { logInfo ( `[SERVICE][SERVER]Server ${ runningEnvironmentUUID } has been stopped` ) ; runningServerInstances [ runningEnvironmentUUID ] . stop ( ) ; } ) ; clearIPCChannels ( ) ; if ( process . platform !== 'darwin' || isTesting ) { app . quit ( ) ; } } ) ; app . on ( 'open-url' , ( event , url ) => { event . preventDefault ( ) ; if ( ! mainWindow ) { saveOpenUrlArgs ( [ url ] ) ; } else { parseProtocolArgs ( [ url ] , mainWindow ) ; } } ) ; }