import { readFileSync , writeFileSync } from 'fs' ; import * as path from 'path' ; import { app , BrowserWindow , ipcMain } from 'electron' ; import { FlossEvent } from './common' ; import * as chalk from 'chalk' ; const configPath = path . join ( app . getPath ( 'userData' ) , 'config.json' ) ; /**
 * Restore the bounds of the window.
 */ const restoreBounds = ( ) : Partial < Electron . Rectangle > => { try { return JSON . parse ( readFileSync ( configPath , 'utf8' ) ) ; } catch ( e ) { return { width : 1024 , height : 768 } ; } } ; const createWindow = ( ) => { const args = JSON . parse ( process . argv . slice ( 2 ) [ 0 ] ) ; const options = { ... restoreBounds ( ) , show : args . debug , background : '#fff' , webPreferences : { nodeIntegration : true , enableRemoteModule : true , contextIsolation : false , } , } ; const mainWindow = new BrowserWindow ( options ) ; ipcMain . on ( FlossEvent . Done , ( ) => process . exit ( 0 ) ) . on ( FlossEvent . Error , ( _event , message ) => { if ( message ) { console . error ( chalk . red ( `\n[floss] Error: ${ message }\n` ) ) ; } process . exit ( 1 ) ; } ) ; mainWindow . loadFile ( path . join ( __dirname , 'index.html' ) ) ; const { webContents } = mainWindow ; if ( args . debug ) { webContents . openDevTools ( { mode : 'bottom' } ) ; } webContents . on ( 'did-finish-load' , ( ) => { webContents . send ( FlossEvent . Start , JSON . stringify ( args ) ) ; } ) ; mainWindow . on ( 'close' , ( ) => { writeFileSync ( configPath , JSON . stringify ( mainWindow . getBounds ( ) ) ) ; } ) ; } ; app . whenReady ( ) . then ( createWindow ) ; app . on ( 'window-all-closed' , ( ) => app . quit ( ) ) ;