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' ) ;
77let io , server , browserWindows , ipc , apiProcess , loadURL ;
@@ -11,12 +11,25 @@ let commandLine, browserView;
1111let splashScreen , hostHook ;
1212
1313let manifestJsonFileName = 'electron.manifest.json' ;
14- if ( app . commandLine . hasSwitch ( 'manifest' ) ) {
14+ let watchable = false ;
15+ if ( app . commandLine . hasSwitch ( 'manifest' ) ) {
1516 manifestJsonFileName = app . commandLine . getSwitchValue ( 'manifest' ) ;
1617} ;
1718
18- const currentBinPath = path . join ( __dirname . replace ( 'app.asar' , '' ) , 'bin' ) ;
19- const manifestJsonFilePath = path . join ( currentBinPath , manifestJsonFileName ) ;
19+ if ( app . commandLine . hasSwitch ( 'watch' ) ) {
20+ watchable = true ;
21+ } ;
22+
23+ let currentBinPath = path . join ( __dirname . replace ( 'app.asar' , '' ) , 'bin' ) ;
24+ let manifestJsonFilePath = path . join ( currentBinPath , manifestJsonFileName ) ;
25+
26+ // if watch is enabled lets change the path
27+ if ( watchable ) {
28+ currentBinPath = path . join ( __dirname , '../../' ) ; // go to project directory
29+ currentBinPath = currentBinPath . substr ( 0 , currentBinPath . length - 1 ) ;
30+ manifestJsonFilePath = path . join ( currentBinPath , manifestJsonFileName ) ;
31+ }
32+
2033const manifestJsonFile = require ( manifestJsonFilePath ) ;
2134if ( manifestJsonFile . singleInstance || manifestJsonFile . aspCoreBackendPort ) {
2235 const mainInstance = app . requestSingleInstanceLock ( ) ;
@@ -106,7 +119,11 @@ function startSocketApiBridge(port) {
106119 server . on ( 'listening' , function ( ) {
107120 console . log ( 'Electron Socket started on port %s at %s' , server . address ( ) . port , server . address ( ) . address ) ;
108121 // Now that socket connection is established, we can guarantee port will not be open for portscanner
109- startAspCoreBackend ( port ) ;
122+ if ( watchable ) {
123+ startAspCoreBackendWithWatch ( port ) ;
124+ } else {
125+ startAspCoreBackend ( port ) ;
126+ }
110127 } ) ;
111128
112129 io . on ( 'connection' , ( socket ) => {
@@ -153,7 +170,7 @@ function isModuleAvailable(name) {
153170}
154171
155172function startAspCoreBackend ( electronPort ) {
156- if ( manifestJsonFile . aspCoreBackendPort ) {
173+ if ( manifestJsonFile . aspCoreBackendPort ) {
157174 startBackend ( manifestJsonFile . aspCoreBackendPort )
158175 } else {
159176 // hostname needs to be localhost, otherwise Windows Firewall will be triggered.
@@ -175,10 +192,40 @@ function startAspCoreBackend(electronPort) {
175192
176193 let binFilePath = path . join ( currentBinPath , binaryFile ) ;
177194 var options = { cwd : currentBinPath } ;
178- apiProcess = process ( binFilePath , parameters , options ) ;
195+ apiProcess = cProcess ( binFilePath , parameters , options ) ;
179196
180197 apiProcess . stdout . on ( 'data' , ( data ) => {
181198 console . log ( `stdout: ${ data . toString ( ) } ` ) ;
182199 } ) ;
183200 }
184201}
202+
203+ function startAspCoreBackendWithWatch ( electronPort ) {
204+ if ( manifestJsonFile . aspCoreBackendPort ) {
205+ startBackend ( manifestJsonFile . aspCoreBackendPort )
206+ } else {
207+ // hostname needs to be localhost, otherwise Windows Firewall will be triggered.
208+ portscanner . findAPortNotInUse ( electronPort + 1 , 65535 , 'localhost' , function ( error , electronWebPort ) {
209+ startBackend ( electronWebPort ) ;
210+ } ) ;
211+ }
212+
213+ function startBackend ( aspCoreBackendPort ) {
214+ console . log ( 'ASP.NET Core Watch Port: ' + aspCoreBackendPort ) ;
215+ loadURL = `http://localhost:${ aspCoreBackendPort } ` ;
216+ const parameters = [ 'watch' , 'run' , `--project ${ currentBinPath } ` , `/electronPort=${ electronPort } ` , `/electronWebPort=${ aspCoreBackendPort } ` ] ;
217+
218+ console . log ( currentBinPath ) ;
219+ var options = {
220+ cwd : currentBinPath ,
221+ env : {
222+ PATH : process . env . PATH
223+ }
224+ } ;
225+ apiProcess = cProcess ( 'dotnet' , parameters , options ) ;
226+
227+ apiProcess . stdout . on ( 'data' , ( data ) => {
228+ console . log ( `stdout: ${ data . toString ( ) } ` ) ;
229+ } ) ;
230+ }
231+ }
0 commit comments