1 | var config = require('./lib/config');
|
2 | var detect = require('./lib/detect');
|
3 | var run = require('./lib/run');
|
4 | var createProfiles = require('./lib/create_profiles');
|
5 |
|
6 | var spawn = require('child_process').spawn;
|
7 |
|
8 | exports = module.exports = function (opts, cb) {
|
9 | if (typeof opts === 'function') {
|
10 | cb = opts;
|
11 | opts = {};
|
12 | }
|
13 | if (!opts) opts = {};
|
14 |
|
15 | config.read(opts.config, function (err, cfg, configDir) {
|
16 | if (err) return cb(err);
|
17 | if (!cfg) {
|
18 | detect(function (avail) {
|
19 | createProfiles(avail, configDir, function (err) {
|
20 | if (err) cb(err)
|
21 | else write({ browsers : { local : avail } });
|
22 | })
|
23 | });
|
24 | }
|
25 | else cb(null, wrap(cfg))
|
26 | });
|
27 |
|
28 | function wrap (cfg) {
|
29 | var res = launcher.bind(null, cfg);
|
30 | res.browsers = cfg.browsers;
|
31 | return res;
|
32 | }
|
33 |
|
34 | function write (cfg) {
|
35 | config.write(cfg, function (err) {
|
36 | if (err) cb(err)
|
37 | else cb(null, wrap(cfg))
|
38 | })
|
39 | }
|
40 | };
|
41 | exports.detect = detect;
|
42 | exports.config = config;
|
43 |
|
44 | function launcher (cfg, uri, opts, cb) {
|
45 | if (typeof opts === 'string') {
|
46 | opts = { browser : opts };
|
47 | }
|
48 | if (!opts) opts = {};
|
49 |
|
50 | var version = opts.version || opts.browser.split('/')[1] || '*';
|
51 | var name = opts.browser.split('/')[0];
|
52 |
|
53 | var runner = run(cfg, name, version);
|
54 | if (!runner) return cb('no matches for ' + name + '/' + version);
|
55 | runner(uri, opts, cb);
|
56 | }
|