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 path = require('path');
|
7 | var spawn = require('child_process').spawn;
|
8 |
|
9 | exports = module.exports = function (opts, cb) {
|
10 | if (typeof opts === 'function') {
|
11 | cb = opts;
|
12 | opts = {};
|
13 | }
|
14 | if (!opts) opts = {};
|
15 |
|
16 | config.read(opts.config, function (err, cfg, configDir) {
|
17 | if (err) return cb(err);
|
18 | if (!cfg) {
|
19 | exports.setup(configDir, function (err, cfg) {
|
20 | if (err) console.error(err)
|
21 | else cb(null, wrap(cfg))
|
22 | });
|
23 | }
|
24 | else cb(null, wrap(cfg))
|
25 | });
|
26 |
|
27 | function wrap (cfg) {
|
28 | var res = launcher.bind(null, cfg);
|
29 | res.browsers = cfg.browsers;
|
30 | return res;
|
31 | }
|
32 | };
|
33 | exports.detect = detect;
|
34 | exports.config = config;
|
35 |
|
36 | exports.setup = function (configDir, cb) {
|
37 | if (typeof configDir === 'function') {
|
38 | cb = configDir;
|
39 | configDir = path.dirname(config.defaultConfigFile);
|
40 | }
|
41 | detect(function (avail) {
|
42 | createProfiles(avail, configDir, function (err) {
|
43 | if (err) return cb(err);
|
44 | var cfg = { browsers : { local : avail } };
|
45 | config.write(cfg, function (err) {
|
46 | if (err) cb(err)
|
47 | else cb(null, cfg)
|
48 | });
|
49 | })
|
50 | });
|
51 | };
|
52 |
|
53 | function launcher (cfg, uri, opts, cb) {
|
54 | if (typeof opts === 'string') {
|
55 | opts = { browser : opts };
|
56 | }
|
57 | if (!opts) opts = {};
|
58 |
|
59 | var version = opts.version || opts.browser.split('/')[1] || '*';
|
60 | var name = opts.browser.split('/')[0];
|
61 |
|
62 | var runner = run(cfg, name, version);
|
63 | if (!runner) return cb('no matches for ' + name + '/' + version);
|
64 | runner(uri, opts, cb);
|
65 | }
|