1
1
const { app } = require ( 'electron' ) ;
2
2
const { BrowserWindow } = require ( 'electron' ) ;
3
3
const path = require ( 'path' ) ;
4
- const process = require ( 'child_process' ) . spawn ;
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 ;
10
11
let commandLine , browserView ;
11
12
let splashScreen , hostHook ;
13
+ let mainWindowId ;
12
14
13
15
let manifestJsonFileName = 'electron.manifest.json' ;
14
- if ( app . commandLine . hasSwitch ( 'manifest' ) ) {
16
+ let watchable = false ;
17
+ if ( app . commandLine . hasSwitch ( 'manifest' ) ) {
15
18
manifestJsonFileName = app . commandLine . getSwitchValue ( 'manifest' ) ;
16
19
} ;
17
20
18
- const currentBinPath = path . join ( __dirname . replace ( 'app.asar' , '' ) , 'bin' ) ;
19
- const manifestJsonFilePath = path . join ( currentBinPath , manifestJsonFileName ) ;
21
+ if ( app . commandLine . hasSwitch ( 'watch' ) ) {
22
+ watchable = true ;
23
+ } ;
24
+
25
+ let currentBinPath = path . join ( __dirname . replace ( 'app.asar' , '' ) , 'bin' ) ;
26
+ let manifestJsonFilePath = path . join ( currentBinPath , manifestJsonFileName ) ;
27
+
28
+ // if watch is enabled lets change the path
29
+ if ( watchable ) {
30
+ currentBinPath = path . join ( __dirname , '../../' ) ; // go to project directory
31
+ manifestJsonFilePath = path . join ( currentBinPath , manifestJsonFileName ) ;
32
+ }
33
+
20
34
const manifestJsonFile = require ( manifestJsonFilePath ) ;
21
35
if ( manifestJsonFile . singleInstance || manifestJsonFile . aspCoreBackendPort ) {
22
36
const mainInstance = app . requestSingleInstanceLock ( ) ;
@@ -42,7 +56,7 @@ app.on('ready', () => {
42
56
43
57
// hostname needs to belocalhost, otherwise Windows Firewall will be triggered.
44
58
portscanner . findAPortNotInUse ( 8000 , 65535 , 'localhost' , function ( error , port ) {
45
- console . log ( 'Electron Socket IO Port: ' + port ) ;
59
+ console . log ( chalk . blue ( 'Electron Socket IO Port: ' + port ) ) ;
46
60
startSocketApiBridge ( port ) ;
47
61
} ) ;
48
62
@@ -62,8 +76,8 @@ function startSplashScreen() {
62
76
let imageFile = path . join ( currentBinPath , manifestJsonFile . splashscreen . imageFile ) ;
63
77
imageSize ( imageFile , ( error , dimensions ) => {
64
78
if ( error ) {
65
- console . log ( `load splashscreen error:` ) ;
66
- console . log ( error ) ;
79
+ console . log ( chalk . bold . red ( `load splashscreen error:` ) ) ;
80
+ console . log ( chalk . bold . red ( error ) ) ;
67
81
68
82
throw new Error ( error . message ) ;
69
83
}
@@ -104,15 +118,46 @@ function startSocketApiBridge(port) {
104
118
105
119
server . listen ( port , 'localhost' ) ;
106
120
server . on ( 'listening' , function ( ) {
107
- 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 ) ) ;
108
122
// Now that socket connection is established, we can guarantee port will not be open for portscanner
109
- startAspCoreBackend ( port ) ;
123
+ if ( watchable ) {
124
+ startAspCoreBackendWithWatch ( port ) ;
125
+ } else {
126
+ startAspCoreBackend ( port ) ;
127
+ }
110
128
} ) ;
111
129
130
+ // prototype
131
+ app [ 'mainWindowURL' ] = "" ;
132
+ app [ 'mainWindow' ] = null ;
133
+
112
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
+
113
158
global [ 'electronsocket' ] = socket ;
114
159
global [ 'electronsocket' ] . setMaxListeners ( 0 ) ;
115
- console . log ( 'ASP.NET Core Application connected...' , 'global.electronsocket' , global [ 'electronsocket' ] . id , new Date ( ) ) ;
160
+ console . log ( chalk . bold . bgCyan ( 'ASP.NET Core Application connected...' , 'global.electronsocket' , global [ 'electronsocket' ] . id , new Date ( ) ) ) ;
116
161
117
162
appApi = require ( './api/app' ) ( socket , app ) ;
118
163
browserWindows = require ( './api/browserWindows' ) ( socket , app ) ;
@@ -130,6 +175,8 @@ function startSocketApiBridge(port) {
130
175
clipboard = require ( './api/clipboard' ) ( socket ) ;
131
176
browserView = require ( './api/browserView' ) ( socket ) ;
132
177
178
+
179
+
133
180
try {
134
181
const hostHookScriptFilePath = path . join ( __dirname , 'ElectronHostHook' , 'index.js' ) ;
135
182
@@ -139,7 +186,7 @@ function startSocketApiBridge(port) {
139
186
hostHook . onHostReady ( ) ;
140
187
}
141
188
} catch ( error ) {
142
- console . log ( error . message ) ;
189
+ console . log ( chalk . bold . red ( error . message ) ) ;
143
190
}
144
191
} ) ;
145
192
}
@@ -153,7 +200,7 @@ function isModuleAvailable(name) {
153
200
}
154
201
155
202
function startAspCoreBackend ( electronPort ) {
156
- if ( manifestJsonFile . aspCoreBackendPort ) {
203
+ if ( manifestJsonFile . aspCoreBackendPort ) {
157
204
startBackend ( manifestJsonFile . aspCoreBackendPort )
158
205
} else {
159
206
// hostname needs to be localhost, otherwise Windows Firewall will be triggered.
@@ -175,10 +222,37 @@ function startAspCoreBackend(electronPort) {
175
222
176
223
let binFilePath = path . join ( currentBinPath , binaryFile ) ;
177
224
var options = { cwd : currentBinPath } ;
178
- apiProcess = process ( binFilePath , parameters , options ) ;
225
+ apiProcess = cProcess ( binFilePath , parameters , options ) ;
179
226
180
227
apiProcess . stdout . on ( 'data' , ( data ) => {
181
228
console . log ( `stdout: ${ data . toString ( ) } ` ) ;
182
229
} ) ;
183
230
}
184
231
}
232
+
233
+ function startAspCoreBackendWithWatch ( electronPort ) {
234
+ if ( manifestJsonFile . aspCoreBackendPort ) {
235
+ startBackend ( manifestJsonFile . aspCoreBackendPort )
236
+ } else {
237
+ // hostname needs to be localhost, otherwise Windows Firewall will be triggered.
238
+ portscanner . findAPortNotInUse ( electronPort + 1 , 65535 , 'localhost' , function ( error , electronWebPort ) {
239
+ startBackend ( electronWebPort ) ;
240
+ } ) ;
241
+ }
242
+
243
+ function startBackend ( aspCoreBackendPort ) {
244
+ console . log ( 'ASP.NET Core Watch Port: ' + aspCoreBackendPort ) ;
245
+ loadURL = `http://localhost:${ aspCoreBackendPort } ` ;
246
+ const parameters = [ 'watch' , 'run' , `/electronPort=${ electronPort } ` , `/electronWebPort=${ aspCoreBackendPort } ` ] ;
247
+
248
+ var options = {
249
+ cwd : currentBinPath ,
250
+ env : process . env ,
251
+ } ;
252
+ apiProcess = cProcess ( 'dotnet' , parameters , options ) ;
253
+
254
+ apiProcess . stdout . on ( 'data' , ( data ) => {
255
+ console . log ( chalk . bold . blue ( `${ data . toString ( ) } ` ) ) ;
256
+ } ) ;
257
+ }
258
+ }
0 commit comments