11const { app } = require ( 'electron' ) ;
22const { BrowserWindow } = require ( 'electron' ) ;
33const path = require ( 'path' ) ;
4- const process = require ( 'child_process' ) . spawn ;
4+ const 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 ;
1011let commandLine , browserView ;
1112let splashScreen , hostHook ;
13+ let mainWindowId ;
1214
1315let manifestJsonFileName = 'electron.manifest.json' ;
14- if ( app . commandLine . hasSwitch ( 'manifest' ) ) {
16+ let watchable = false ;
17+ if ( app . commandLine . hasSwitch ( 'manifest' ) ) {
1518 manifestJsonFileName = app . commandLine . getSwitchValue ( 'manifest' ) ;
1619} ;
1720
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+
2034const manifestJsonFile = require ( manifestJsonFilePath ) ;
2135if ( manifestJsonFile . singleInstance || manifestJsonFile . aspCoreBackendPort ) {
2236 const mainInstance = app . requestSingleInstanceLock ( ) ;
@@ -42,7 +56,7 @@ app.on('ready', () => {
4256
4357 // hostname needs to belocalhost, otherwise Windows Firewall will be triggered.
4458 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 ) ) ;
4660 startSocketApiBridge ( port ) ;
4761 } ) ;
4862
@@ -62,8 +76,8 @@ function startSplashScreen() {
6276 let imageFile = path . join ( currentBinPath , manifestJsonFile . splashscreen . imageFile ) ;
6377 imageSize ( imageFile , ( error , dimensions ) => {
6478 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 ) ) ;
6781
6882 throw new Error ( error . message ) ;
6983 }
@@ -104,15 +118,46 @@ function startSocketApiBridge(port) {
104118
105119 server . listen ( port , 'localhost' ) ;
106120 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 ) ) ;
108122 // 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+ }
110128 } ) ;
111129
130+ // prototype
131+ app [ 'mainWindowURL' ] = "" ;
132+ app [ 'mainWindow' ] = null ;
133+
112134 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+
113158 global [ 'electronsocket' ] = socket ;
114159 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 ( ) ) ) ;
116161
117162 appApi = require ( './api/app' ) ( socket , app ) ;
118163 browserWindows = require ( './api/browserWindows' ) ( socket , app ) ;
@@ -130,6 +175,8 @@ function startSocketApiBridge(port) {
130175 clipboard = require ( './api/clipboard' ) ( socket ) ;
131176 browserView = require ( './api/browserView' ) ( socket ) ;
132177
178+
179+
133180 try {
134181 const hostHookScriptFilePath = path . join ( __dirname , 'ElectronHostHook' , 'index.js' ) ;
135182
@@ -139,7 +186,7 @@ function startSocketApiBridge(port) {
139186 hostHook . onHostReady ( ) ;
140187 }
141188 } catch ( error ) {
142- console . log ( error . message ) ;
189+ console . log ( chalk . bold . red ( error . message ) ) ;
143190 }
144191 } ) ;
145192}
@@ -153,7 +200,7 @@ function isModuleAvailable(name) {
153200}
154201
155202function startAspCoreBackend ( electronPort ) {
156- if ( manifestJsonFile . aspCoreBackendPort ) {
203+ if ( manifestJsonFile . aspCoreBackendPort ) {
157204 startBackend ( manifestJsonFile . aspCoreBackendPort )
158205 } else {
159206 // hostname needs to be localhost, otherwise Windows Firewall will be triggered.
@@ -175,10 +222,37 @@ function startAspCoreBackend(electronPort) {
175222
176223 let binFilePath = path . join ( currentBinPath , binaryFile ) ;
177224 var options = { cwd : currentBinPath } ;
178- apiProcess = process ( binFilePath , parameters , options ) ;
225+ apiProcess = cProcess ( binFilePath , parameters , options ) ;
179226
180227 apiProcess . stdout . on ( 'data' , ( data ) => {
181228 console . log ( `stdout: ${ data . toString ( ) } ` ) ;
182229 } ) ;
183230 }
184231}
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