|
| 1 | +var childProcess = require('child_process'), |
| 2 | + fs = require('fs'), |
| 3 | + path = require('path'), |
| 4 | + running = require('is-running'), |
| 5 | + LocalBinary = require('./LocalBinary'), |
| 6 | + LocalError = require('./LocalError'); |
| 7 | + |
| 8 | + |
| 9 | +function Local(){ |
| 10 | + this.pid = undefined; |
| 11 | + this.key = process.env.BROWSERSTACK_ACCESS_KEY; |
| 12 | + this.logfile = path.join(process.cwd(), 'local.log'); |
| 13 | + this.exitCallback; |
| 14 | + |
| 15 | + this.errorRegex = /\*\*\* Error\: [^\n]+/i; |
| 16 | + this.doneRegex = /Press Ctrl-C to exit/i; |
| 17 | + |
| 18 | + this.start = function(options, callback){ |
| 19 | + var that = this; |
| 20 | + this.addArgs(options); |
| 21 | + |
| 22 | + if(typeof options['onlyCommand'] !== 'undefined') |
| 23 | + return callback(); |
| 24 | + |
| 25 | + this.getBinaryPath(function(binaryPath){ |
| 26 | + that.binaryPath = binaryPath; |
| 27 | + childProcess.exec('echo "" > ' + that.logfile); |
| 28 | + |
| 29 | + that.tunnel = childProcess.spawn(binaryPath, that.getBinaryArgs()); |
| 30 | + that.tunnel.on('exit', function(){ |
| 31 | + that.tunnel = undefined; |
| 32 | + if(that.exitCallback) that.exitCallback(); |
| 33 | + }); |
| 34 | + |
| 35 | + that.stdout = fs.openSync(that.logfile, 'r'); |
| 36 | + var chunkSize = 512, |
| 37 | + buffer = new Buffer(81920), |
| 38 | + bytesRead = 0, |
| 39 | + error = undefined; |
| 40 | + |
| 41 | + while(true){ |
| 42 | + var bytes = fs.readSync(that.stdout, buffer, bytesRead, chunkSize, bytesRead); |
| 43 | + if(bytes == 0) continue; |
| 44 | + |
| 45 | + var buffRead = buffer.slice(bytesRead, bytesRead+bytes); |
| 46 | + bytesRead += bytes; |
| 47 | + |
| 48 | + var data = buffRead.toString(); |
| 49 | + //console.log(data); |
| 50 | + |
| 51 | + if(data.match(that.errorRegex)){ |
| 52 | + fs.closeSync(that.stdout); |
| 53 | + error = data.match(that.errorRegex)[0].trim(); |
| 54 | + break; |
| 55 | + } |
| 56 | + |
| 57 | + if(data.match(that.doneRegex)){ |
| 58 | + fs.closeSync(that.stdout); |
| 59 | + break; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + if(error) throw new LocalError(data); |
| 64 | + callback(); |
| 65 | + }); |
| 66 | + }; |
| 67 | + |
| 68 | + this.isRunning = function(){ |
| 69 | + return this.tunnel && running(this.tunnel.pid); |
| 70 | + }; |
| 71 | + |
| 72 | + this.stop = function (callback) { |
| 73 | + if (this.tunnel) { |
| 74 | + if(callback) this.exitCallback = callback; |
| 75 | + this.tunnel.kill(); |
| 76 | + } |
| 77 | + else if(callback) callback(); |
| 78 | + }; |
| 79 | + |
| 80 | + this.addArgs = function(options){ |
| 81 | + for(var key in options){ |
| 82 | + var value = options[key]; |
| 83 | + |
| 84 | + switch(key){ |
| 85 | + case 'key': |
| 86 | + this.key = value; |
| 87 | + break; |
| 88 | + |
| 89 | + case 'v': |
| 90 | + if(value) |
| 91 | + this.verboseFlag = '-vvv'; |
| 92 | + break; |
| 93 | + |
| 94 | + case 'force': |
| 95 | + if(value) |
| 96 | + this.forceFlag = '-force'; |
| 97 | + break; |
| 98 | + |
| 99 | + case 'only': |
| 100 | + if(value) |
| 101 | + this.onlyFlag = '-only'; |
| 102 | + break; |
| 103 | + |
| 104 | + case 'onlyAutomate': |
| 105 | + if(value) |
| 106 | + this.onlyAutomateFlag = '-onlyAutomate'; |
| 107 | + break; |
| 108 | + |
| 109 | + case 'forcelocal': |
| 110 | + if(value) |
| 111 | + this.forceLocalFlag = '-forcelocal'; |
| 112 | + break; |
| 113 | + |
| 114 | + case 'localIdentifier': |
| 115 | + if(value) |
| 116 | + this.localIdentifierFlag = '-localIdentifier ' + value; |
| 117 | + break; |
| 118 | + |
| 119 | + case 'f': |
| 120 | + if(value){ |
| 121 | + this.folderFlag = '-f'; |
| 122 | + this.folderPath = value; |
| 123 | + } |
| 124 | + break; |
| 125 | + |
| 126 | + case 'proxyHost': |
| 127 | + if(value) |
| 128 | + this.proxyHost = '-proxyHost ' + value; |
| 129 | + break; |
| 130 | + |
| 131 | + case 'proxyPort': |
| 132 | + if(value) |
| 133 | + this.proxyPort = '-proxyPort ' + value; |
| 134 | + break; |
| 135 | + |
| 136 | + case 'proxyUser': |
| 137 | + if(value) |
| 138 | + this.proxyUser = '-proxyUser ' + value; |
| 139 | + break; |
| 140 | + |
| 141 | + case 'proxyPass': |
| 142 | + if(value) |
| 143 | + this.proxyPass = '-proxyPass ' + value; |
| 144 | + break; |
| 145 | + |
| 146 | + case 'hosts': |
| 147 | + if(value) |
| 148 | + this.hosts = value; |
| 149 | + break; |
| 150 | + |
| 151 | + case 'logfile': |
| 152 | + if(value) |
| 153 | + this.logfile = value; |
| 154 | + break; |
| 155 | + |
| 156 | + case 'binarypath': |
| 157 | + if(value) |
| 158 | + this.binaryPath = value; |
| 159 | + break; |
| 160 | + |
| 161 | + default: |
| 162 | + break; |
| 163 | + } |
| 164 | + } |
| 165 | + }; |
| 166 | + |
| 167 | + this.getBinaryPath = function(callback){ |
| 168 | + if(typeof(this.binaryPath) == 'undefined'){ |
| 169 | + this.binary = new LocalBinary(); |
| 170 | + this.binary.binaryPath(callback); |
| 171 | + } else { |
| 172 | + callback(this.binaryPath); |
| 173 | + } |
| 174 | + }; |
| 175 | + |
| 176 | + this.getBinaryArgs = function(){ |
| 177 | + var args = ['-logFile', this.logfile]; |
| 178 | + if(this.folderFlag) |
| 179 | + args.push(this.folderFlag); |
| 180 | + args.push(this.key); |
| 181 | + if(this.folderPath) |
| 182 | + args.push(this.folderPath); |
| 183 | + if(this.forceLocalFlag) |
| 184 | + args.push(this.forceLocalFlag); |
| 185 | + if(this.localIdentifierFlag) |
| 186 | + args.push(this.localIdentifierFlag); |
| 187 | + if(this.onlyFlag) |
| 188 | + args.push(this.onlyFlag); |
| 189 | + if(this.onlyAutomateFlag) |
| 190 | + args.push(this.onlyAutomateFlag); |
| 191 | + if(this.proxyHost) |
| 192 | + args.push(this.proxyHost); |
| 193 | + if(this.proxyPort) |
| 194 | + args.push(this.proxyPort); |
| 195 | + if(this.proxyUser) |
| 196 | + args.push(this.proxyUser); |
| 197 | + if(this.proxyPass) |
| 198 | + args.push(this.proxyPass); |
| 199 | + if(this.forceFlag) |
| 200 | + args.push(this.forceFlag); |
| 201 | + if(this.verboseFlag) |
| 202 | + args.push(this.verboseFlag); |
| 203 | + if(this.hosts) |
| 204 | + args.push(this.hosts); |
| 205 | + return args; |
| 206 | + }; |
| 207 | +} |
| 208 | + |
| 209 | +module.exports = Local; |
0 commit comments