forked from node-migrator-bot/browser-launcher
-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
115 lines (99 loc) · 2.83 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
var path = require( 'path' ),
_ = require( 'lodash' ),
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 ) {
getLauncher.update( 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
getLauncher.update( 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' ] );
} ) );
} );
};
/**
* Update the browsers cache and create new profiles if necessary
* @param {String} configDir Path to the configuration file
* @param {Function} callback Callback function
*/
getLauncher.update = function( configFile, callback ) {
if ( typeof configFile === 'function' ) {
callback = configFile;
configFile = configModule.defaultConfigFile;
}
detect( function( browsers ) {
createProfiles( browsers, path.dirname( configFile ), function( err ) {
if ( err ) {
return callback( err );
}
var config = {
browsers: browsers
};
configModule.write( configFile, config, function( err ) {
if ( err ) {
callback( err );
} else {
callback( null, config );
}
} );
} );
} );
};
module.exports = getLauncher;