|
| 1 | +var debug = require('debug')('api:launch') |
| 2 | +var Promise = require('es6-promise').Promise; |
| 3 | +var launch = require('launchpad'); |
| 4 | +var portscanner = require('portscanner') |
| 5 | + |
| 6 | +function Launch(gateway) { |
| 7 | + |
| 8 | + var openBrowsers = []; |
| 9 | + |
| 10 | + var api = gateway.api |
| 11 | + var logger = gateway.logger |
| 12 | + |
| 13 | + api.launchBrowser = function (browserName, url) { |
| 14 | + |
| 15 | + return new Promise(function(resolve, reject) { |
| 16 | + |
| 17 | + debug('command.launchBrowser', { |
| 18 | + browser: browserName, |
| 19 | + url: url |
| 20 | + }) |
| 21 | + |
| 22 | + switch (browserName) { |
| 23 | + case 'chrome': |
| 24 | + findPort().then(function(debugPort) { |
| 25 | + console.log('debugPort', debugPort) |
| 26 | + launch.local(function(err, local) { |
| 27 | + var options = { |
| 28 | + clean: true, |
| 29 | + args: ['--remote-debugging-port=' + debugPort] |
| 30 | + } |
| 31 | + |
| 32 | + local.chrome(url, options, function(err, instance) { |
| 33 | + if(err) reject(err) |
| 34 | + |
| 35 | + openBrowsers.push(instance) |
| 36 | + setTimeout(function() { |
| 37 | + resolve({ |
| 38 | + debugPort: debugPort, |
| 39 | + debugUrl: 'localhost:' + debugPort |
| 40 | + }) |
| 41 | + }, 2000) |
| 42 | + }) |
| 43 | + }) |
| 44 | + }) |
| 45 | + break; |
| 46 | + |
| 47 | + case 'canary': |
| 48 | + var debugPort = 9222 |
| 49 | + // findPort().then(function(debugPort) { |
| 50 | + |
| 51 | + launch.local(function(err, local) { |
| 52 | + var options = { |
| 53 | + clean: true, |
| 54 | + args: ['--remote-debugging-port=' + debugPort] |
| 55 | + } |
| 56 | + |
| 57 | + local.canary(url, options, function(err, instance) { |
| 58 | + if(err) reject(err) |
| 59 | + |
| 60 | + openBrowsers.push(instance) |
| 61 | + setTimeout(function() { |
| 62 | + resolve({ |
| 63 | + debugPort: debugPort, |
| 64 | + debugUrl: 'localhost:' + debugPort |
| 65 | + }) |
| 66 | + }, 2000) |
| 67 | + |
| 68 | + }) |
| 69 | + }) |
| 70 | + // }) |
| 71 | + break; |
| 72 | + |
| 73 | + case 'opera': |
| 74 | + var debugPort = 9223 |
| 75 | + |
| 76 | + launch.local(function(err, local) { |
| 77 | + var options = { |
| 78 | + clean: true, |
| 79 | + args: ['--remote-debugging-port=' + debugPort] |
| 80 | + } |
| 81 | + |
| 82 | + local.opera(url, options, function(err, instance) { |
| 83 | + if(err) reject(err) |
| 84 | + |
| 85 | + openBrowsers.push(instance) |
| 86 | + setTimeout(function() { |
| 87 | + resolve({ |
| 88 | + debugPort: debugPort, |
| 89 | + debugUrl: 'localhost:' + debugPort |
| 90 | + }) |
| 91 | + }, 2000); |
| 92 | + }) |
| 93 | + }) |
| 94 | + // }) |
| 95 | + break; |
| 96 | + } |
| 97 | + }) |
| 98 | + } |
| 99 | + |
| 100 | + api.closeOpenBrowsers = function() { |
| 101 | + openBrowsers.forEach(function(b) { |
| 102 | + b.stop() |
| 103 | + }) |
| 104 | + } |
| 105 | + |
| 106 | + var findPort = function() { |
| 107 | + return new Promise(function(resolve, reject) { |
| 108 | + portscanner.findAPortNotInUse(9222, 9300, '127.0.0.1', function(err, port) { |
| 109 | + if(err) reject(err) |
| 110 | + resolve(port) |
| 111 | + }) |
| 112 | + }) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +module.exports = Launch |
0 commit comments