1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | 'use strict';
|
10 |
|
11 | var Bluebird = require('bluebird');
|
12 |
|
13 | var fileutils = require('./utils/fileutils');
|
14 | var envWithLocalPath = require('./utils/env-with-local-path');
|
15 |
|
16 | var executableExists = function(exe, config) {
|
17 | return fileutils.executableExists(exe, { env: envWithLocalPath(config) });
|
18 | };
|
19 | var fileExists = fileutils.fileExists;
|
20 |
|
21 |
|
22 | function 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 |
|
39 | function 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 |
|
51 | function 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 |
|
75 | exports.getAvailableBrowsers = getAvailableBrowsers;
|