diff --git a/lib/Local.js b/lib/Local.js index ad35fd4..25a85c2 100644 --- a/lib/Local.js +++ b/lib/Local.js @@ -37,7 +37,7 @@ function Local(){ const binaryPath = this.getBinaryPath(null, options['bs-host']); that.binaryPath = binaryPath; - childProcess.exec('echo "" > ' + that.logfile); + fs.writeFileSync(that.logfile, ''); that.opcode = 'start'; if(!this.binaryPath){ return new LocalError('Couldn\'t find binary file'); @@ -86,9 +86,46 @@ function Local(){ this.getBinaryPath(function(binaryPath){ that.binaryPath = binaryPath; - childProcess.exec('echo "" > ' + that.logfile); + fs.writeFile(that.logfile, '', function(err) { + if (err) { + return callback(new LocalError('Failed to create log file: ' + err.message)); + } + that.opcode = 'start'; + that.tunnel = childProcess.execFile(that.binaryPath, that.getBinaryArgs(), function(error, stdout, stderr){ + if(error) { + const binaryDownloadErrorMessage = `Error while trying to execute binary: ${util.format(error)}`; + console.error(binaryDownloadErrorMessage); + if(that.retriesLeft > 0) { + console.log('Retrying Binary Download. Retries Left', that.retriesLeft); + that.retriesLeft -= 1; + fs.unlinkSync(that.binaryPath); + delete(that.binaryPath); + process.env.BINARY_DOWNLOAD_ERROR_MESSAGE = binaryDownloadErrorMessage; + process.env.BINARY_DOWNLOAD_FALLBACK_ENABLED = true; + that.start(options, callback); + return; + } else { + callback(new LocalError(error.toString())); + } + } - that.opcode = 'start'; + var data = {}; + if(stdout) + data = JSON.parse(stdout); + else if(stderr) + data = JSON.parse(stderr); + else + callback(new LocalError('No output received')); + + if(data['state'] != 'connected'){ + callback(new LocalError(data['message']['message'])); + } else { + that.pid = data['pid']; + that.isProcessRunning = true; + callback(); + } + }); + }); that.tunnel = childProcess.execFile(that.binaryPath, that.getBinaryArgs(), function(error, stdout, stderr){ if(error) { const binaryDownloadErrorMessage = `Error while trying to execute binary: ${util.format(error)}`;