@@ -4,6 +4,7 @@ const path = require('path');
4
4
const cProcess = require ( 'child_process' ) . spawn ;
5
5
const portscanner = require ( 'portscanner' ) ;
6
6
const imageSize = require ( 'image-size' ) ;
7
+ const chalk = require ( 'chalk' ) ;
7
8
let io , server , browserWindows , ipc , apiProcess , loadURL ;
8
9
let appApi , menu , dialogApi , notification , tray , webContents ;
9
10
let globalShortcut , shellApi , screen , clipboard , autoUpdater ;
@@ -55,17 +56,12 @@ app.on('ready', () => {
55
56
56
57
// hostname needs to belocalhost, otherwise Windows Firewall will be triggered.
57
58
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 ) ) ;
59
60
startSocketApiBridge ( port ) ;
60
61
} ) ;
61
62
62
63
} ) ;
63
64
64
- app . on ( "mainWindow" , id => {
65
- mainWindowId = id ;
66
- console . log ( ` Main Window ID = ${ id } ` ) ;
67
- } )
68
-
69
65
function isSplashScreenEnabled ( ) {
70
66
if ( manifestJsonFile . hasOwnProperty ( 'splashscreen' ) ) {
71
67
if ( manifestJsonFile . splashscreen . hasOwnProperty ( 'imageFile' ) ) {
@@ -80,8 +76,8 @@ function startSplashScreen() {
80
76
let imageFile = path . join ( currentBinPath , manifestJsonFile . splashscreen . imageFile ) ;
81
77
imageSize ( imageFile , ( error , dimensions ) => {
82
78
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 ) ) ;
85
81
86
82
throw new Error ( error . message ) ;
87
83
}
@@ -122,7 +118,7 @@ function startSocketApiBridge(port) {
122
118
123
119
server . listen ( port , 'localhost' ) ;
124
120
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 ) ) ;
126
122
// Now that socket connection is established, we can guarantee port will not be open for portscanner
127
123
if ( watchable ) {
128
124
startAspCoreBackendWithWatch ( port ) ;
@@ -131,15 +127,37 @@ function startSocketApiBridge(port) {
131
127
}
132
128
} ) ;
133
129
130
+ // prototype
131
+ app [ 'mainWindowURL' ] = "" ;
132
+ app [ 'mainWindow' ] = null ;
133
+
134
134
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
+
135
158
global [ 'electronsocket' ] = socket ;
136
159
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 ( ) ) ) ;
143
161
144
162
appApi = require ( './api/app' ) ( socket , app ) ;
145
163
browserWindows = require ( './api/browserWindows' ) ( socket , app ) ;
@@ -157,6 +175,8 @@ function startSocketApiBridge(port) {
157
175
clipboard = require ( './api/clipboard' ) ( socket ) ;
158
176
browserView = require ( './api/browserView' ) ( socket ) ;
159
177
178
+
179
+
160
180
try {
161
181
const hostHookScriptFilePath = path . join ( __dirname , 'ElectronHostHook' , 'index.js' ) ;
162
182
@@ -166,7 +186,7 @@ function startSocketApiBridge(port) {
166
186
hostHook . onHostReady ( ) ;
167
187
}
168
188
} catch ( error ) {
169
- console . log ( error . message ) ;
189
+ console . log ( chalk . bold . red ( error . message ) ) ;
170
190
}
171
191
} ) ;
172
192
}
@@ -225,15 +245,14 @@ function startAspCoreBackendWithWatch(electronPort) {
225
245
loadURL = `http://localhost:${ aspCoreBackendPort } ` ;
226
246
const parameters = [ 'watch' , 'run' , `/electronPort=${ electronPort } ` , `/electronWebPort=${ aspCoreBackendPort } ` ] ;
227
247
228
- console . log ( currentBinPath ) ;
229
248
var options = {
230
249
cwd : currentBinPath ,
231
250
env : process . env ,
232
251
} ;
233
252
apiProcess = cProcess ( 'dotnet' , parameters , options ) ;
234
253
235
254
apiProcess . stdout . on ( 'data' , ( data ) => {
236
- console . log ( `stdout: ${ data . toString ( ) } `) ;
255
+ console . log ( chalk . bold . blue ( ` ${ data . toString ( ) } `) ) ;
237
256
} ) ;
238
257
}
239
258
}
0 commit comments