import Debug = require ( 'debug' ) ; import { app , EventEmitter , ipcMain } from 'electron' ; import { resolve } from 'path' ; import { install as installExifRotate } from 'electron-exif-rotate' ; import { WindowManager } from './window-manager' ; const debug = Debug ( 'app:app' ) ; debug ( `starting` , app . getName ( ) , 'version' , app . getVersion ( ) ) ; export const wm = new WindowManager ( ) ; if ( ! app . isDefaultProtocolClient ( app . getName ( ) . toLowerCase ( ) ) ) { app . setAsDefaultProtocolClient ( app . getName ( ) . toLowerCase ( ) ) ; } if ( ! app . isPackaged ) { debug ( 'starting auto reloader' ) ; import ( 'electron-reload' ) . then ( reloader => reloader ( resolve ( __dirname ) ) ) . catch ( console . error ) ; } const instanceLock = app . requestSingleInstanceLock ( ) ; if ( ! instanceLock ) { app . quit ( ) ; } app . on ( 'ready' , ( ) => { debug ( 'ready event' ) ; installExifRotate ( ) ; if ( ! app . isPackaged ) loadDevtoolExtensions ( ) ; wm . goToMonux ( ) ; } ) ; app . on ( 'open-url' , async ( _ , forwardedUrl ) => { debug ( 'open-url event' ) ; forwardAuthUrl ( forwardedUrl ) ; } ) ; app . on ( 'window-all-closed' , ( ) => { debug ( 'window-all-closed event' ) ; if ( process . platform !== 'darwin' ) app . quit ( ) ; } ) ; app . on ( 'activate' , ( ) => { debug ( 'activate event' ) ; if ( wm . hasMainWindow ( ) ) wm . focusMainWindow ( ) ; else wm . goToMonux ( ) ; } ) ; app . on ( 'second-instance' , ( _ev , argv , _ ) => { if ( wm . hasMainWindow ( ) && wm . mainWindow . isMinimized ( ) ) { wm . mainWindow . restore ( ) ; } wm . focusMainWindow ( ) ; if ( process . platform === 'win32' && argv . length > 1 ) { const authUrl = argv . find ( param => { return param . toLowerCase ( ) . startsWith ( 'monux://' ) ; } ) ; if ( authUrl ) { forwardAuthUrl ( authUrl ) ; } else { console . error ( 'Auth URL not found' ) ; throw new Error ( 'Auth URL not found' ) ; } } } ) ; ipcMain . on ( 'open-auth-window' , ( _ev , url ) => { debug ( 'opening auth request window' ) ; wm . openAuthRequest ( url ) ; } ) ; ipcMain . on ( 'auth-success:monzo' , ( ) => { wm . closeAuthRequest ( ) ; } ) ; function forwardAuthUrl ( forwardedUrl ) { wm . mainWindow . webContents . send ( 'auth-verify:monzo' , forwardedUrl ) ; } function loadDevtoolExtensions ( ) { debug ( 'loading devtool extensions' ) ; import ( 'devtron' ) . then ( ( { install } ) => install ( ) ) . catch ( console . error ) ; import ( 'electron-devtools-installer' ) . then ( ( { default : installExtension , REDUX_DEVTOOLS } ) => { const extensions = [ installExtension ( REDUX_DEVTOOLS ) ] ; return Promise . all ( extensions ) . then ( names => debug ( 'Added Extensions:' , names . join ( ', ' ) ) ) . catch ( err => debug ( 'An error occurred adding extension:' , err ) ) ; } ) . catch ( console . error ) ; }