UNPKG

1.72 kBJavaScriptView Raw
1/*
2
3browser_launcher.js
4===================
5
6This file more or less figures out how to launch any browser on any platform.
7
8*/
9'use strict';
10
11var Bluebird = require('bluebird');
12
13var fileutils = require('./utils/fileutils');
14var envWithLocalPath = require('./utils/env-with-local-path');
15
16var executableExists = function(exe, config) {
17 return fileutils.executableExists(exe, { env: envWithLocalPath(config) });
18};
19var fileExists = fileutils.fileExists;
20
21// Returns the available browsers on the current machine.
22function getAvailableBrowsers(config, browsers, cb) {
23 browsers.forEach(function(b) {
24 b.protocol = 'browser';
25 });
26
27 return Bluebird.filter(browsers, function(browser) {
28 return isInstalled(browser, config).then(function(result) {
29 if (!result) {
30 return false;
31 }
32
33 browser.exe = result;
34 return true;
35 });
36 }).asCallback(cb);
37}
38
39function isInstalled(browser, config) {
40 return checkBrowser(browser, 'possiblePath', fileExists).then(function(result) {
41 if (result) {
42 return result;
43 }
44
45 return checkBrowser(browser, 'possibleExe', function(exe) {
46 return executableExists(exe, config);
47 });
48 });
49}
50
51function checkBrowser(browser, property, method) {
52 if (!browser[property]) {
53 return Bluebird.resolve(false);
54 }
55
56 if (Array.isArray(browser[property])) {
57 return Bluebird.filter(browser[property], method).then(function(result) {
58 if (result.length === 0) {
59 return false;
60 }
61
62 return result[0];
63 });
64 }
65
66 return method(browser[property]).then(function(result) {
67 if (!result) {
68 return false;
69 }
70
71 return browser[property];
72 });
73}
74
75exports.getAvailableBrowsers = getAvailableBrowsers;