-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
130 lines (109 loc) · 4.6 KB
/
index.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
'use strict';
var headlessBrowsers = [
'Chrome',
'Chromium',
'ChromeCanary',
'Firefox',
'FirefoxDeveloper',
'FirefoxAurora',
'FirefoxNightly',
];
var DetectBrowsers = function (config, logger) {
var fs = require('fs'),
os = require('os'),
which = require('which'),
browsers = require('./browsers'),
log = logger.create('framework.detect-browsers');
/**
* Returns all browser names found on the current system.
*
* @param {!Object} browsers The object with all browsers fro the browsers directory.
* @returns {Array}
*/
function getInstalledBrowsers (browsers) {
var i, length,
browserNames = Object.keys(browsers),
result = [];
// only use one firefox version on linux
if (process.platform === 'linux') {
browserNames = browserNames.filter(function (name) {
return name !== 'firefoxAurora' && name !== 'firefoxNightly';
});
}
// iterate over all browsers in the browsers folder
for (i = 0, length = browserNames.length; i < length; i++) {
var browser = browsers[browserNames[i]],
browserPaths = browser.DEFAULT_CMD[process.platform] || [],
y, paths = browserPaths.length;
if (process.env[browser.ENV_CMD]) {
log.info('which.sync(process.env[browser.ENV_CMD]): ', which.sync(process.env[browser.ENV_CMD]));
}
if (process.env[browser.ENV_CMD] && which.sync(process.env[browser.ENV_CMD])) {
result.push(browser.name);
continue;
}
// iterate over all browser paths
for (y = 0; y < paths; y++) {
try {
var browserLocated = fs.existsSync(browserPaths[y]) || which.sync(browserPaths[y]);
// don't use Edge on operating systems other than Windows 10
// (the launcher would be found, but would fail to run)
var useBrowser = browser.name !== 'Edge' || process.platform === 'win32' && /^1\d/.test(os.release());
if (browserLocated && useBrowser && result.indexOf(browser.name) < 0) {
// add browser when found in file system or when env variable is set
result.push(browser.name);
// set env variable on win32 when it does not exist yet
if (process.platform === 'win32') {
process.env[browser.ENV_CMD] = browserPaths[y];
}
break;
}
} catch (e) {
// which.sync() failed to find the browser.
}
}
}
return result;
}
config = config || {};
config.detectBrowsers = config.detectBrowsers || {};
config.plugins = config.plugins || [];
if (config.detectBrowsers.enabled === false) {
log.info('Detecting browsers is disabled. The browsers of the browsers array are used.');
return;
}
var availableBrowsers = getInstalledBrowsers(browsers);
if (config.detectBrowsers.preferHeadless) {
availableBrowsers = availableBrowsers.map(function (browser) {
return headlessBrowsers.indexOf(browser) >= 0 ? browser + 'Headless' : browser;
});
}
// override the browsers in the config only when browsers where find by this plugin
if (availableBrowsers.length >= 0) {
// check for PhantomJS option
if (config.detectBrowsers.usePhantomJS !== false) {
availableBrowsers.push('PhantomJS');
}
if (config.detectBrowsers.postDetection && typeof config.detectBrowsers.postDetection === 'function') {
//Add specific process to manage browsers list
availableBrowsers = config.detectBrowsers.postDetection(availableBrowsers);
}
var browserList = config.browsers || [];
if (availableBrowsers.length > 0) {
config.browsers = browserList.concat(
availableBrowsers.filter(function (browser) {
return browserList.indexOf(browser) === -1;
})
);
}
log.info('The following browsers will be used:', config.browsers);
} else {
log.warn('No browsers were detected. The browsers of the browsers array are used.');
}
};
// inject karma runner config
DetectBrowsers.$inject = ['config', 'logger'];
// PUBLISH DI MODULE
module.exports = {
'framework:detectBrowsers': ['factory', DetectBrowsers]
};