Skip to content

Commit db16c7c

Browse files
committed
support more than one instance
1 parent e8b6bfa commit db16c7c

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

helpers/syncthing/syncthing.js

+20-16
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,36 @@ class Syncthing extends EventEmitter {
1919
this._apiKey = 'hkubectl';
2020
this._headers = { 'X-API-KEY': this._apiKey }
2121
}
22-
async _createConfig() {
22+
async _copyDependencies() {
2323
await fs.ensureDir(this._configDir);
2424
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
2626
await this._copy(path.join(__dirname, 'syncthing'), this._command);
27+
await this._copy(path.join(__dirname, './config.xml'), path.join(this._configDir, 'config.xml'));
2728
await fs.chmod(this._command, '775')
2829
}
2930
async start({ envs = {}, port = 8384, tunnelUrl, tunnelPort } = {}) {
3031
this._restPort = port;
3132
this._tunnelPort = tunnelPort;
3233
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 } });
3638

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+
}
4852
this._local = new Api({ apiKey: this._apiKey, baseUrl: `http://localhost:${this._restPort}`, name: 'local' })
4953
await this._local.isReady();
5054
await this._local._getEvents();

0 commit comments

Comments
 (0)