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