@@ -19,32 +19,36 @@ class Syncthing extends EventEmitter {
19
19
this . _apiKey = 'hkubectl' ;
20
20
this . _headers = { 'X-API-KEY' : this . _apiKey }
21
21
}
22
- async _createConfig ( ) {
22
+ async _copyDependencies ( ) {
23
23
await fs . ensureDir ( this . _configDir ) ;
24
24
await fs . ensureDir ( this . _binDir ) ;
25
- await this . _copy ( path . join ( __dirname , './config.xml' ) , path . join ( this . _configDir , 'config.xml' ) ) ;
25
+ // try to copy the exe first. If it fails, the server is already running
26
26
await this . _copy ( path . join ( __dirname , 'syncthing' ) , this . _command ) ;
27
+ await this . _copy ( path . join ( __dirname , './config.xml' ) , path . join ( this . _configDir , 'config.xml' ) ) ;
27
28
await fs . chmod ( this . _command , '775' )
28
29
}
29
30
async start ( { envs = { } , port = 8384 , tunnelUrl, tunnelPort } = { } ) {
30
31
this . _restPort = port ;
31
32
this . _tunnelPort = tunnelPort ;
32
33
console . log ( `starting local sync server` )
33
- await this . _createConfig ( ) ;
34
- this . _args = [ "-gui-address" , `localhost:${ this . _restPort } ` , "-gui-apikey" , this . _apiKey , "-home" , this . _configDir , "-no-browser" ]
35
- this . _proc = spawn ( this . _command , this . _args , { env : { ...process . env , ...envs } } ) ;
34
+ try {
35
+ await this . _copyDependencies ( ) ;
36
+ this . _args = [ "-gui-address" , `localhost:${ this . _restPort } ` , "-gui-apikey" , this . _apiKey , "-home" , this . _configDir , "-no-browser" ]
37
+ this . _proc = spawn ( this . _command , this . _args , { env : { ...process . env , ...envs } } ) ;
36
38
37
- this . _proc . stdout . on ( 'data' , async ( d ) => {
38
- // console.log(d.toString());
39
- } ) ;
40
- this . _proc . stderr . on ( 'data' , ( d ) => {
41
- console . error ( d . toString ( ) ) ;
42
- } ) ;
43
- this . _proc . on ( 'close' , ( code ) => {
44
- } ) ;
45
- this . _proc . on ( 'error' , ( err ) => {
46
- console . error ( err . message || err )
47
- } ) ;
39
+ this . _proc . stderr . on ( 'data' , ( d ) => {
40
+ console . error ( d . toString ( ) ) ;
41
+ } ) ;
42
+ this . _proc . on ( 'close' , ( code ) => {
43
+ } ) ;
44
+ this . _proc . on ( 'error' , ( err ) => {
45
+ console . error ( err . message || err )
46
+ } ) ;
47
+ }
48
+ catch ( error ) {
49
+ console . error ( error . message || error ) ;
50
+ console . log ( `Unable to start local server. Server is probably already working` ) ;
51
+ }
48
52
this . _local = new Api ( { apiKey : this . _apiKey , baseUrl : `http://localhost:${ this . _restPort } ` , name : 'local' } )
49
53
await this . _local . isReady ( ) ;
50
54
await this . _local . _getEvents ( ) ;
0 commit comments