forked from benderjs/browser-launcher2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
92 lines (76 loc) · 2.03 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
var path = require( 'path' ),
_ = require( 'lodash' ),
configModule = require( './lib/config' ),
detect = require( './lib/detect' ),
run = require( './lib/run' ),
createProfiles = require( './lib/create_profiles' );
module.exports = function( configFile, callback ) {
if ( typeof configFile === 'function' ) {
callback = configFile;
configFile = null;
}
configModule.read( configFile, function( err, config, configDir ) {
if ( !config ) {
module.exports.update( configDir, 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 ) {
return callback( 'no matches for ' + name + '/' + version );
}
runner( uri, options, callback );
}
};
module.exports.detect = function( callback ) {
detect( function( browsers ) {
callback( browsers.map( function( browser ) {
return _.pick( browser, [ 'name', 'version', 'type', 'command' ] );
} ) );
} );
};
module.exports.update = function( configDir, callback ) {
if ( typeof configDir === 'function' ) {
callback = configDir;
configDir = path.dirname( configModule.defaultConfigFile );
}
detect( function( available ) {
createProfiles( available, configDir, function( err ) {
if ( err ) {
return callback( err );
}
var config = {
browsers: available
};
configModule.write( config, function( err ) {
if ( err ) {
callback( err );
} else {
callback( null, config );
}
} );
} );
} );
};
module.exports.config = configModule;