Skip to content

Commit 0aa92cd

Browse files
authored
Update Local.js
1 parent 440aa80 commit 0aa92cd

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

lib/Local.js

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function Local(){
3737

3838
const binaryPath = this.getBinaryPath(null, options['bs-host']);
3939
that.binaryPath = binaryPath;
40-
childProcess.exec('echo "" > ' + that.logfile);
40+
fs.writeFileSync(that.logfile, '');
4141
that.opcode = 'start';
4242
if(!this.binaryPath){
4343
return new LocalError('Couldn\'t find binary file');
@@ -86,9 +86,46 @@ function Local(){
8686

8787
this.getBinaryPath(function(binaryPath){
8888
that.binaryPath = binaryPath;
89-
childProcess.exec('echo "" > ' + that.logfile);
89+
fs.writeFile(that.logfile, '', function(err) {
90+
if (err) {
91+
return callback(new LocalError('Failed to create log file: ' + err.message));
92+
}
93+
that.opcode = 'start';
94+
that.tunnel = childProcess.execFile(that.binaryPath, that.getBinaryArgs(), function(error, stdout, stderr){
95+
if(error) {
96+
const binaryDownloadErrorMessage = `Error while trying to execute binary: ${util.format(error)}`;
97+
console.error(binaryDownloadErrorMessage);
98+
if(that.retriesLeft > 0) {
99+
console.log('Retrying Binary Download. Retries Left', that.retriesLeft);
100+
that.retriesLeft -= 1;
101+
fs.unlinkSync(that.binaryPath);
102+
delete(that.binaryPath);
103+
process.env.BINARY_DOWNLOAD_ERROR_MESSAGE = binaryDownloadErrorMessage;
104+
process.env.BINARY_DOWNLOAD_FALLBACK_ENABLED = true;
105+
that.start(options, callback);
106+
return;
107+
} else {
108+
callback(new LocalError(error.toString()));
109+
}
110+
}
90111

91-
that.opcode = 'start';
112+
var data = {};
113+
if(stdout)
114+
data = JSON.parse(stdout);
115+
else if(stderr)
116+
data = JSON.parse(stderr);
117+
else
118+
callback(new LocalError('No output received'));
119+
120+
if(data['state'] != 'connected'){
121+
callback(new LocalError(data['message']['message']));
122+
} else {
123+
that.pid = data['pid'];
124+
that.isProcessRunning = true;
125+
callback();
126+
}
127+
});
128+
});
92129
that.tunnel = childProcess.execFile(that.binaryPath, that.getBinaryArgs(), function(error, stdout, stderr){
93130
if(error) {
94131
const binaryDownloadErrorMessage = `Error while trying to execute binary: ${util.format(error)}`;

0 commit comments

Comments
 (0)