@@ -4,6 +4,7 @@ const path = require('path');
44const cProcess = require ( 'child_process' ) . spawn ;
55const portscanner = require ( 'portscanner' ) ;
66const imageSize = require ( 'image-size' ) ;
7+ const chalk = require ( 'chalk' ) ;
78let io , server , browserWindows , ipc , apiProcess , loadURL ;
89let appApi , menu , dialogApi , notification , tray , webContents ;
910let globalShortcut , shellApi , screen , clipboard , autoUpdater ;
@@ -55,17 +56,12 @@ app.on('ready', () => {
5556
5657 // hostname needs to belocalhost, otherwise Windows Firewall will be triggered.
5758 portscanner . findAPortNotInUse ( 8000 , 65535 , 'localhost' , function ( error , port ) {
58- console . log ( 'Electron Socket IO Port: ' + port ) ;
59+ console . log ( chalk . blue ( 'Electron Socket IO Port: ' + port ) ) ;
5960 startSocketApiBridge ( port ) ;
6061 } ) ;
6162
6263} ) ;
6364
64- app . on ( "mainWindow" , id => {
65- mainWindowId = id ;
66- console . log ( ` Main Window ID = ${ id } ` ) ;
67- } )
68-
6965function isSplashScreenEnabled ( ) {
7066 if ( manifestJsonFile . hasOwnProperty ( 'splashscreen' ) ) {
7167 if ( manifestJsonFile . splashscreen . hasOwnProperty ( 'imageFile' ) ) {
@@ -80,8 +76,8 @@ function startSplashScreen() {
8076 let imageFile = path . join ( currentBinPath , manifestJsonFile . splashscreen . imageFile ) ;
8177 imageSize ( imageFile , ( error , dimensions ) => {
8278 if ( error ) {
83- console . log ( `load splashscreen error:` ) ;
84- console . log ( error ) ;
79+ console . log ( chalk . bold . red ( `load splashscreen error:` ) ) ;
80+ console . log ( chalk . bold . red ( error ) ) ;
8581
8682 throw new Error ( error . message ) ;
8783 }
@@ -122,7 +118,7 @@ function startSocketApiBridge(port) {
122118
123119 server . listen ( port , 'localhost' ) ;
124120 server . on ( 'listening' , function ( ) {
125- console . log ( 'Electron Socket started on port %s at %s' , server . address ( ) . port , server . address ( ) . address ) ;
121+ console . log ( chalk . bgGreenBright ( 'Electron Socket started on port %s at %s' , server . address ( ) . port , server . address ( ) . address ) ) ;
126122 // Now that socket connection is established, we can guarantee port will not be open for portscanner
127123 if ( watchable ) {
128124 startAspCoreBackendWithWatch ( port ) ;
@@ -131,15 +127,37 @@ function startSocketApiBridge(port) {
131127 }
132128 } ) ;
133129
130+ // prototype
131+ app [ 'mainWindowURL' ] = "" ;
132+ app [ 'mainWindow' ] = null ;
133+
134134 io . on ( 'connection' , ( socket ) => {
135+
136+ // we need to remove previously cache instances
137+ // otherwise it will fire the same event multiple depends how many time
138+ // live reload watch happen.
139+ socket . on ( 'disconnect' , function ( ) {
140+ console . log ( chalk . bold . red ( 'Got disconnect!' ) ) ;
141+ delete require . cache [ require . resolve ( './api/app' ) ] ;
142+ delete require . cache [ require . resolve ( './api/browserWindows' ) ] ;
143+ delete require . cache [ require . resolve ( './api/commandLine' ) ] ;
144+ delete require . cache [ require . resolve ( './api/autoUpdater' ) ] ;
145+ delete require . cache [ require . resolve ( './api/ipc' ) ] ;
146+ delete require . cache [ require . resolve ( './api/menu' ) ] ;
147+ delete require . cache [ require . resolve ( './api/dialog' ) ] ;
148+ delete require . cache [ require . resolve ( './api/notification' ) ] ;
149+ delete require . cache [ require . resolve ( './api/tray' ) ] ;
150+ delete require . cache [ require . resolve ( './api/webContents' ) ] ;
151+ delete require . cache [ require . resolve ( './api/globalShortcut' ) ] ;
152+ delete require . cache [ require . resolve ( './api/shell' ) ] ;
153+ delete require . cache [ require . resolve ( './api/screen' ) ] ;
154+ delete require . cache [ require . resolve ( './api/clipboard' ) ] ;
155+ delete require . cache [ require . resolve ( './api/browserView' ) ] ;
156+ } ) ;
157+
135158 global [ 'electronsocket' ] = socket ;
136159 global [ 'electronsocket' ] . setMaxListeners ( 0 ) ;
137- console . log ( 'ASP.NET Core Application connected...' , 'global.electronsocket' , global [ 'electronsocket' ] . id , new Date ( ) ) ;
138-
139- // send signal to reload
140- if ( mainWindowId != undefined ) {
141- app . emit ( "hot-reload" , mainWindowId ) ;
142- }
160+ console . log ( chalk . bold . bgCyan ( 'ASP.NET Core Application connected...' , 'global.electronsocket' , global [ 'electronsocket' ] . id , new Date ( ) ) ) ;
143161
144162 appApi = require ( './api/app' ) ( socket , app ) ;
145163 browserWindows = require ( './api/browserWindows' ) ( socket , app ) ;
@@ -157,6 +175,8 @@ function startSocketApiBridge(port) {
157175 clipboard = require ( './api/clipboard' ) ( socket ) ;
158176 browserView = require ( './api/browserView' ) ( socket ) ;
159177
178+
179+
160180 try {
161181 const hostHookScriptFilePath = path . join ( __dirname , 'ElectronHostHook' , 'index.js' ) ;
162182
@@ -166,7 +186,7 @@ function startSocketApiBridge(port) {
166186 hostHook . onHostReady ( ) ;
167187 }
168188 } catch ( error ) {
169- console . log ( error . message ) ;
189+ console . log ( chalk . bold . red ( error . message ) ) ;
170190 }
171191 } ) ;
172192}
@@ -225,15 +245,14 @@ function startAspCoreBackendWithWatch(electronPort) {
225245 loadURL = `http://localhost:${ aspCoreBackendPort } ` ;
226246 const parameters = [ 'watch' , 'run' , `/electronPort=${ electronPort } ` , `/electronWebPort=${ aspCoreBackendPort } ` ] ;
227247
228- console . log ( currentBinPath ) ;
229248 var options = {
230249 cwd : currentBinPath ,
231250 env : process . env ,
232251 } ;
233252 apiProcess = cProcess ( 'dotnet' , parameters , options ) ;
234253
235254 apiProcess . stdout . on ( 'data' , ( data ) => {
236- console . log ( `stdout: ${ data . toString ( ) } `) ;
255+ console . log ( chalk . bold . blue ( ` ${ data . toString ( ) } `) ) ;
237256 } ) ;
238257 }
239258}
0 commit comments