-
Notifications
You must be signed in to change notification settings - Fork 64
/
index.js
65 lines (57 loc) · 1.79 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
var config = require('./lib/config');
var detect = require('./lib/detect');
var run = require('./lib/run');
var createProfiles = require('./lib/create_profiles');
var path = require('path');
var spawn = require('child_process').spawn;
exports = module.exports = function (opts, cb) {
if (typeof opts === 'function') {
cb = opts;
opts = {};
}
if (!opts) opts = {};
config.read(opts.config, function (err, cfg, configDir) {
if (err) return cb(err);
if (!cfg) {
exports.setup(configDir, function (err, cfg) {
if (err) console.error(err)
else cb(null, wrap(cfg))
});
}
else cb(null, wrap(cfg))
});
function wrap (cfg) {
var res = launcher.bind(null, cfg);
res.browsers = cfg.browsers;
return res;
}
};
exports.detect = detect;
exports.config = config;
exports.setup = function (configDir, cb) {
if (typeof configDir === 'function') {
cb = configDir;
configDir = path.dirname(config.defaultConfigFile);
}
detect(function (avail) {
createProfiles(avail, configDir, function (err) {
if (err) return cb(err);
var cfg = { browsers : { local : avail } };
config.write(cfg, function (err) {
if (err) cb(err)
else cb(null, cfg)
});
})
});
};
function launcher (cfg, uri, opts, cb) {
if (typeof opts === 'string') {
opts = { browser : opts };
}
if (!opts) opts = {};
var version = opts.version || opts.browser.split('/')[1] || '*';
var name = opts.browser.split('/')[0];
var runner = run(cfg, name, version);
if (!runner) return cb('no matches for ' + name + '/' + version);
runner(uri, opts, cb);
}