11import path from 'path' ;
22import process from 'process' ;
33import fs from 'fs' ;
4- import { spawn } from 'child_process' ;
4+ import { spawn , execFile } from 'child_process' ;
55import { app , dialog } from 'electron' ;
66import { publishServerInfo , publishServerStderr } from '../ipc/ipc_main' ;
77import { ServerInfo } from './types' ;
@@ -15,8 +15,12 @@ function findServer() {
1515 // In development
1616 path . join ( process . cwd ( ) , 'server' , 'server' ) ,
1717 path . join ( process . cwd ( ) , 'server' , 'server.exe' ) ,
18+
19+ path . join ( 'server' , 'server' ) ,
20+ path . join ( 'server' , 'server.exe' ) ,
1821 ] ;
1922 for ( const path of possibilities ) {
23+ console . log ( 'trying' , path , 'exists:' , fs . existsSync ( path ) ) ;
2024 if ( fs . existsSync ( path ) ) {
2125 return path ;
2226 }
@@ -45,8 +49,7 @@ function getServerProcess() {
4549 return null ;
4650 }
4751 console . log ( 'Starting server from' , path ) ;
48- return spawn ( path , {
49- stdio : 'pipe' ,
52+ return execFile ( path , {
5053 env : { ...process . env } ,
5154 } ) ;
5255}
@@ -73,21 +76,24 @@ function startServer() {
7376 if ( ! serverProcess ) {
7477 return ;
7578 }
76- serverProcess . stdout . on ( 'data' , ( data : Buffer ) => {
79+ serverProcess . stdout ? .on ( 'data' , ( data : Buffer ) => {
7780 console . log ( 'server-stdout' , data . toString ( ) ) ;
78- try {
79- const parsed_data : ServerStartingMessage | ServerStartedMessage = JSON . parse ( data . toString ( ) ) ;
80- if ( parsed_data . msg == 'server_starting' ) {
81- publishServerInfo ( { state : 'starting' , port : parsed_data . port } ) ;
82- } else if ( parsed_data . msg == 'server_started' ) {
83- publishServerInfo ( { state : 'running' , token : parsed_data . token } ) ;
81+ for ( const line of data . toString ( ) . trim ( ) . split ( / \r ? \n / ) ) {
82+ console . log ( 'line' , line ) ;
83+ try {
84+ const parsed_data : ServerStartingMessage | ServerStartedMessage = JSON . parse ( line ) ;
85+ if ( parsed_data . msg == 'server_starting' ) {
86+ publishServerInfo ( { state : 'starting' , port : parsed_data . port } ) ;
87+ } else if ( parsed_data . msg == 'server_started' ) {
88+ publishServerInfo ( { state : 'running' , token : parsed_data . token } ) ;
89+ }
90+ } catch ( e ) {
91+ console . log ( 'error decoding stdout json' , e ) ;
8492 }
85- } catch ( e ) {
86- console . log ( 'error decoding stdout json' , e ) ;
8793 }
8894 } ) ;
8995
90- serverProcess . stderr . on ( 'data' , ( data : Buffer ) => {
96+ serverProcess . stderr ? .on ( 'data' , ( data : Buffer ) => {
9197 console . log ( `server-stderr: \n${ data } ` ) ;
9298 publishServerStderr ( data . toString ( ) ) ;
9399 } ) ;
0 commit comments