Skip to content

Commit a9f965b

Browse files
07souravkundafrancisf
authored andcommitted
add Local.startSync
1 parent 23c5827 commit a9f965b

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed

lib/Local.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,51 @@ function Local(){
2626
this.errorRegex = /\*\*\* Error: [^\r\n]*/i;
2727
this.doneRegex = /Press Ctrl-C to exit/i;
2828

29+
this.startSync = function(options) {
30+
this.userArgs = [];
31+
var that = this;
32+
this.addArgs(options);
33+
34+
if(typeof options['onlyCommand'] !== 'undefined')
35+
return;
36+
37+
const binaryPath = this.getBinaryPathSync();
38+
that.binaryPath = binaryPath;
39+
childProcess.exec('echo "" > ' + that.logfile);
40+
that.opcode = 'start';
41+
if(!this.binaryPath){
42+
return new LocalError("Couldn't find binary file");
43+
}
44+
try{
45+
const obj = childProcess.spawnSync(that.binaryPath, that.getBinaryArgs());
46+
var data = {};
47+
if(obj.stdout.length > 0)
48+
data = JSON.parse(obj.stdout);
49+
else if(obj.stderr.length > 0)
50+
data = JSON.parse(obj.stderr);
51+
else
52+
return new LocalError('No output received');
53+
if(data['state'] != 'connected'){
54+
return new LocalError(data['message']['message']);
55+
} else {
56+
that.pid = data['pid'];
57+
that.isProcessRunning = true;
58+
return;
59+
}
60+
}catch(error){
61+
console.error('Error while trying to execute binary', error);
62+
if(that.retriesLeft > 0) {
63+
console.log('Retrying Binary Download. Retries Left', that.retriesLeft);
64+
that.retriesLeft -= 1;
65+
fs.unlinkSync(that.binaryPath);
66+
delete(that.binaryPath);
67+
return that.startSync(options);
68+
} else {
69+
return new LocalError(error.toString());
70+
}
71+
}
72+
}
73+
2974
this.start = function(options, callback){
3075
this.userArgs = [];
3176
var that = this;
@@ -191,6 +236,21 @@ function Local(){
191236
}
192237
};
193238

239+
this.getBinaryPathSync = function(){
240+
if(typeof(this.binaryPath) == 'undefined'){
241+
this.binary = new LocalBinary();
242+
var conf = {};
243+
if(this.proxyHost && this.proxyPort){
244+
conf.proxyHost = this.proxyHost;
245+
conf.proxyPort = this.proxyPort;
246+
}
247+
return this.binary.binaryPathSync(conf);
248+
} else {
249+
console.log('BINARY PATH IS DEFINED');
250+
return this.binaryPath;
251+
}
252+
}
253+
194254
this.getBinaryPath = function(callback){
195255
if(typeof(this.binaryPath) == 'undefined'){
196256
this.binary = new LocalBinary();

lib/LocalBinary.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@ function LocalBinary(){
3939

4040
this.httpPath = this.getDownloadPath();
4141

42+
this.retryBinaryDownloadSync = function(conf, destParentDir, retries, binaryPath){
43+
var that = this;
44+
if(retries > 0) {
45+
console.log('Retrying Download. Retries left', retries);
46+
try {
47+
const stats = fs.statSync(binaryPath)
48+
fs.unlinkSync(binaryPath);
49+
} catch(er) {}
50+
51+
that.downloadSync(conf, destParentDir, retries - 1)
52+
53+
} else {
54+
console.error('Number of retries to download exceeded.');
55+
}
56+
}
57+
4258
this.retryBinaryDownload = function(conf, destParentDir, callback, retries, binaryPath) {
4359
var that = this;
4460
if(retries > 0) {
@@ -54,6 +70,56 @@ function LocalBinary(){
5470
}
5571
};
5672

73+
this.downloadSync = function(conf, destParentDir, retries) {
74+
console.log('downloading in sync')
75+
var that = this;
76+
if(!this.checkPath(destParentDir))
77+
fs.mkdirSync(destParentDir);
78+
79+
var destBinaryName = (this.windows) ? 'BrowserStackLocal.exe' : 'BrowserStackLocal';
80+
var binaryPath = path.join(destParentDir, destBinaryName);
81+
82+
var options = url.parse(this.httpPath);
83+
if(conf.proxyHost && conf.proxyPort) {
84+
options.agent = new HttpsProxyAgent({
85+
host: conf.proxyHost,
86+
port: conf.proxyPort
87+
});
88+
}
89+
90+
const cmd = `curl`;
91+
const opts = ['-o', binaryPath, options.href];
92+
93+
if(conf.proxyHost && conf.proxyPort) {
94+
opts.push('--proxy', `${conf.proxyHost}:${conf.proxyPort}`);
95+
}
96+
97+
try{
98+
const obj = childProcess.spawnSync(cmd, opts);
99+
let output;
100+
if(obj.stdout.length > 0) {
101+
output = Buffer.from(JSON.parse(JSON.stringify(obj.stderr)).data).toString();
102+
} else if(obj.stderr.length > 0) {
103+
output = Buffer.from(JSON.parse(JSON.stringify(obj.stderr)).data).toString();
104+
}
105+
if(output.includes('failed')){
106+
console.log('failed to download');
107+
that.retryBinaryDownloadSync(conf, destParentDir, retries, binaryPath);
108+
}else{
109+
if(fs.existsSync(binaryPath)){
110+
fs.chmodSync(binaryPath, '0755')
111+
return binaryPath;
112+
}else{
113+
console.log('failed to download');
114+
that.retryBinaryDownloadSync(conf, destParentDir, retries, binaryPath);
115+
}
116+
}
117+
} catch(err) {
118+
console.log('Download failed with error', err);
119+
that.retryBinaryDownloadSync(conf, destParentDir, retries, binaryPath);
120+
}
121+
}
122+
57123
this.download = function(conf, destParentDir, callback, retries){
58124
var that = this;
59125
if(!this.checkPath(destParentDir))
@@ -92,6 +158,17 @@ function LocalBinary(){
92158
});
93159
};
94160

161+
this.binaryPathSync = function(conf){
162+
var destParentDir = this.getAvailableDirs();
163+
var destBinaryName = (this.windows) ? 'BrowserStackLocal.exe' : 'BrowserStackLocal';
164+
var binaryPath = path.join(destParentDir, destBinaryName);
165+
if(this.checkPath(binaryPath, fs.X_OK)){
166+
return binaryPath;
167+
} else {
168+
return this.downloadSync(conf, destParentDir, 5);
169+
}
170+
}
171+
95172
this.binaryPath = function(conf, callback){
96173
var destParentDir = this.getAvailableDirs();
97174
var destBinaryName = (this.windows) ? 'BrowserStackLocal.exe' : 'BrowserStackLocal';

0 commit comments

Comments
 (0)