-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
144 lines (125 loc) · 4.07 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
var path = require('path'),
pick = require('lodash/pick'),
configModule = require('./lib/config'),
detect = require('./lib/detect'),
run = require('./lib/run'),
createProfiles = require('./lib/create_profiles');
/**
* Check the configuration and prepare a launcher function.
* If there's no config ready, detect available browsers first.
* Finally, pass a launcher function to the callback.
* @param {String} [configFile] Path to a configuration file
* @param {Function} callback Callback function
*/
function getLauncher(configFile, callback) {
if (typeof configFile === 'function') {
callback = configFile;
configFile = configModule.defaultConfigFile;
}
configModule.read(configFile, function (err, config) {
if (!config) {
safeConfigUpdate(configFile, function (err, config) {
if (err) {
callback(err);
} else {
callback(null, wrap(config));
}
});
} else {
callback(null, wrap(config));
}
});
function wrap(config) {
var res = launch.bind(null, config);
res.browsers = config.browsers;
return res;
}
function launch(config, uri, options, callback) {
if (typeof options === 'string') {
options = {
browser: options
};
}
options = options || {};
var version = options.version || options.browser.split('/')[1] || '*',
name = options.browser.toLowerCase().split('/')[0],
runner = run(config, name, version);
if (!runner) {
// update the list of available browsers and retry
safeConfigUpdate(configFile, function (err, config) {
if (!(runner = run(config, name, version))) {
return callback(name + ' is not installed in your system.');
}
runner(uri, options, callback);
});
} else {
runner(uri, options, callback);
}
}
}
/**
* Detect available browsers
* @param {Function} callback Callback function
*/
getLauncher.detect = function (callback) {
detect(function (browsers) {
callback(browsers.map(function (browser) {
return pick(browser, ['name', 'version', 'type', 'command']);
}));
});
};
/**
* Detect the available browsers and build appropriate profiles if necessary
*/
function buildConfig(configDir, callback) {
detect(function (browsers) {
createProfiles(browsers, configDir, function (err) {
if (err) {
return callback(err);
}
callback(null, {
browsers: browsers
});
});
});
}
function safeConfigUpdate(configFile, callback) {
// Detect browssers etc, and try to update the config file, but return the
// detected config regardless of whether the config file actually works
buildConfig(path.dirname(configFile), (err, config) => {
if (err) {
return callback(err);
}
configModule.write(configFile, config, function (err) {
if (err) {
console.warn(err);
}
callback(null, config);
});
});
}
/**
* Detect the available browsers and build appropriate profiles if necessary,
* and update the config file with their details.
* @param {String} configFile Path to the configuration file
* @param {Function} callback Callback function
*/
getLauncher.update = function (configFile, callback) {
if (typeof configFile === 'function') {
callback = configFile;
configFile = configModule.defaultConfigFile;
}
buildConfig(path.dirname(configFile), (err, config) => {
if (err) {
return callback(err);
}
configModule.write(configFile, config, function (err) {
if (err) {
callback(err);
} else {
callback(null, config);
}
});
});
};
module.exports = getLauncher;