@@ -2,6 +2,7 @@ const { spawn } = require('child_process');
22const { app, BrowserWindow, ipcMain, shell } = require ( 'electron' ) ;
33const isDevMode = require ( 'electron-is-dev' ) ;
44const path = require ( 'path' ) ;
5+ const fs = require ( 'fs' ) ;
56
67let pythonServer ;
78
@@ -27,21 +28,34 @@ function startPythonServer() {
2728 return ;
2829 }
2930
31+ const logPath = path . join ( path . dirname ( process . execPath ) , 'LiSA.log' ) ;
32+ fs . writeFileSync ( logPath , '' , { encoding : 'utf8' } ) ; // clear logs
33+
3034 pythonServer = spawn ( cmd , {
3135 shell : true ,
32- detached : isDevMode ,
36+ detached : false ,
37+ stdio : [ 'ignore' , 'pipe' , 'pipe' ]
38+ } ) ;
39+
40+ const logStream = fs . createWriteStream ( logPath , { flags : 'a' } ) ;
41+ pythonServer . stdout . pipe ( logStream ) ;
42+ pythonServer . stderr . pipe ( logStream ) ;
43+
44+ pythonServer . on ( 'close' , ( _ ) => {
45+ logStream . end ( ) ;
3346 } ) ;
3447}
3548
49+
3650function killPythonServer ( ) {
3751 if ( ! pythonServer ) return ;
3852
3953 if ( process . platform === 'win32' ) {
40- const killCmd = `taskkill /pid ${ pythonServer . pid } /f /t ` ;
54+ const killCmd = `tskill LiSA ` ;
4155 spawn ( 'cmd.exe' , [ '/c' , killCmd ] ) ;
42- } else pythonServer . kill ( 'SIGINT' ) ;
43-
44- console . log ( 'Killed python server, PID: ' , pythonServer . pid ) ;
56+ } else {
57+ process . kill ( - pythonServer . pid , 'SIGINT' ) ;
58+ }
4559
4660 pythonServer = null ;
4761}
@@ -191,7 +205,7 @@ app.whenReady().then(async () => {
191205
192206app . on ( 'activate' , ( ) => {
193207 /**
194- * On macOS it's common to re-create a window in the app when the
208+ * On macOS, it's common to re-create a window in the app when the
195209 * dock icon is clicked and there are no other windows open.
196210 */
197211 if ( BrowserWindow . getAllWindows ( ) . length === 0 ) createWindow ( ) ;
@@ -206,8 +220,7 @@ app.on('window-all-closed', () => {
206220 if ( process . platform !== 'darwin' ) app . quit ( ) ;
207221} ) ;
208222
209- app . on ( 'quit' , function ( ) {
210- // Clean up the Python server when the app quits
223+ app . on ( 'quit' , ( event ) => {
211224 killPythonServer ( ) ;
212225} ) ;
213226
0 commit comments