From ab8ef85ae4db60e8535a086c74e51f19658a5a07 Mon Sep 17 00:00:00 2001 From: Mitchell Hentges Date: Sun, 28 Feb 2016 16:06:09 +0100 Subject: [PATCH 01/11] Set up wanted structure --- lib/detect.js | 77 +++++++++++++++++++-------------------------------- 1 file changed, 29 insertions(+), 48 deletions(-) diff --git a/lib/detect.js b/lib/detect.js index 6fdf258..eeaf382 100644 --- a/lib/detect.js +++ b/lib/detect.js @@ -3,64 +3,45 @@ var spawn = require( 'child_process' ).spawn, darwin = require( './darwin' ), extend = require( 'lodash' ).extend, browsers = { - 'google-chrome': { - name: 'chrome', - re: /Google Chrome (\S+)/, - type: 'chrome', - profile: true - }, - 'google-chrome-canary': { - name: 'canary', - re: /Google Chrome (\S+)/, - type: 'chrome', - profile: true - }, - 'chromium': { - name: 'chromium', - re: /Chromium (\S+)/, - type: 'chrome', - profile: true + chrome: { + regex: /Google Chrome (\S+)/, + profile: true, + variants: { + 'chrome': [ 'google-chrome', 'google-chrome-stable' ], + 'chrome-beta': 'google-chrome-beta', + 'chrome-canary': 'google-chrome-canary' + } }, - 'chromium-browser': { - name: 'chromium', - re: /Chromium (\S+)/, - type: 'chrome', - profile: true + chromium: { + regex: /Chromium (\S+)/, + profile: true, + variants: { + 'chromium': [ 'chromium', 'chromium-browser' ] + } }, - 'firefox': { - name: 'firefox', - re: /Mozilla Firefox (\S+)/, - type: 'firefox', - profile: true + firefox: { + regex: /Mozilla Firefox (\S+)/, + profile: true, + variants: { + 'firefox': 'firefox', + 'firefox-developer': 'firefox-developer' + } }, - 'phantomjs': { - name: 'phantomjs', - re: /(\S+)/, - type: 'phantom', - headless: true, - profile: false + phantomjs: { + regex: /(\S+)/, + profile: false, + headless: true }, - 'safari': { - name: 'safari', - type: 'safari', + safari: { profile: false }, - 'ie': { - name: 'ie', - type: 'ie', + ie: { profile: false }, - 'opera': { - name: 'opera', - re: /Opera (\S+)/, - type: 'opera', - image: 'opera.exe', + opera: { + regex: /Opera (\S+)/, profile: true } - }, - winDetectMap = { - chrome: 'google-chrome', - chromium: 'chromium-browser' }; /** From bbea971dc5ba824f7695fadeab99ffed54af7474 Mon Sep 17 00:00:00 2001 From: Mitchell Hentges Date: Sun, 28 Feb 2016 17:00:13 +0100 Subject: [PATCH 02/11] Finish refactor, update breaking changes --- CHANGELOG.md | 3 +- index.js | 6 +- lib/detect.js | 167 +++++++++++++++++++++++++++----------------------- 3 files changed, 96 insertions(+), 80 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 99f0b73..16b5753 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,8 @@ ## Upcoming Release #### Breaking changes -- None yet +- Type `phantom` is now `phantomjs`, which is a little more consistent +- The property "name" is more descriptive for variant channels, e.g. "chrome-canary" #### User features - None yet diff --git a/index.js b/index.js index f81d529..44c2916 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,5 @@ var path = require( 'path' ), - _ = require( 'lodash' ), + pick = require( 'lodash/pick' ), configModule = require( './lib/config' ), detect = require( './lib/detect' ), run = require( './lib/run' ), @@ -75,14 +75,14 @@ function getLauncher( configFile, callback ) { getLauncher.detect = function( callback ) { detect( function( browsers ) { callback( browsers.map( function( browser ) { - return _.pick( browser, [ 'name', 'version', 'type', 'command' ] ); + 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 {String} configFile Path to the configuration file * @param {Function} callback Callback function */ getLauncher.update = function( configFile, callback ) { diff --git a/lib/detect.js b/lib/detect.js index eeaf382..40a0f54 100644 --- a/lib/detect.js +++ b/lib/detect.js @@ -1,30 +1,31 @@ var spawn = require( 'child_process' ).spawn, winDetect = require( 'win-detect-browsers' ), darwin = require( './darwin' ), - extend = require( 'lodash' ).extend, + assign = require('lodash/assign'), + omit = require('lodash/omit'), browsers = { chrome: { regex: /Google Chrome (\S+)/, profile: true, variants: { - 'chrome': [ 'google-chrome', 'google-chrome-stable' ], - 'chrome-beta': 'google-chrome-beta', - 'chrome-canary': 'google-chrome-canary' + 'chrome': ['google-chrome', 'google-chrome-stable'], + 'chrome-beta': ['google-chrome-beta'], + 'chrome-canary': ['google-chrome-canary'] } }, chromium: { regex: /Chromium (\S+)/, profile: true, variants: { - 'chromium': [ 'chromium', 'chromium-browser' ] + 'chromium': ['chromium', 'chromium-browser'] } }, firefox: { regex: /Mozilla Firefox (\S+)/, profile: true, variants: { - 'firefox': 'firefox', - 'firefox-developer': 'firefox-developer' + 'firefox': ['firefox'], + 'firefox-developer': ['firefox-developer'] } }, phantomjs: { @@ -72,32 +73,28 @@ function detectWindows( callback ) { * @param {Function} callback Callback function */ function checkDarwin( name, callback ) { - if ( darwin[ name ] ) { - if ( darwin[ name ].all ) { - darwin[ name ].all( function( err, available ) { - if ( err ) { - callback( 'failed to get version for ' + name ); - } else { - callback( err, available ); - } - } ); - } else { - darwin[ name ].version( function( err, version ) { - if ( version ) { - darwin[ name ].path( function( err, p ) { - if ( err ) { - return callback( 'failed to get path for ' + name ); - } - - callback( null, version, p ); - } ); - } else { - callback( 'failed to get version for ' + name ); - } - } ); - } + if ( darwin[ name ].all ) { + darwin[ name ].all( function( err, available ) { + if ( err ) { + callback( 'failed to get version for ' + name ); + } else { + callback( err, available ); + } + } ); } else { - checkOthers( name, callback ); + darwin[ name ].version( function( err, version ) { + if ( version ) { + darwin[ name ].path( function( err, p ) { + if ( err ) { + return callback( 'failed to get path for ' + name ); + } + + callback( null, version, p ); + } ); + } else { + callback( 'failed to get version for ' + name ); + } + } ); } } @@ -105,11 +102,11 @@ function checkDarwin( name, callback ) { * Check if the given browser is available (on Unix systems). * Pass its version to the callback function if found. * @param {String} name Name of a browser + * @param {RegExp} regex Extracts version from command output * @param {Function} callback Callback function */ -function checkOthers( name, callback ) { +function checkOthers( name, regex, callback ) { var process = spawn( name, [ '--version' ] ), - re = browsers[ name ].re, data = ''; process.stdout.on( 'data', function( buf ) { @@ -130,65 +127,83 @@ function checkOthers( name, callback ) { return callback( 'not installed' ); } - var m = re.exec( data ); + var match = regex.exec( data ); - if ( m ) { - callback( null, m[ 1 ] ); + if ( match ) { + callback( null, match[ 1 ] ); } else { callback( null, data.trim() ); } } ); } +function flattenBrowsers(shouldUseDarwinDetector) { + var flatBrowsers = []; + + Object.keys(browsers).forEach(function(type) { + var variants = browsers[type].variants; + var config = omit(browsers[type], 'variants'); + + if (!variants) { + return flatBrowsers.push(assign({type: type, name: type, command: type}, config)); + } + + Object.keys(variants).map(function(name) { + if (shouldUseDarwinDetector(name)) { + return flatBrowsers.push(assign({type, name}, config)); // `command` not needed, will use darwin detector + } + + return variants[name].map(function(command) { + flatBrowsers.push(assign({type, name, command}, config)); + }); + }); + }); + + return flatBrowsers; +} + /** * Detect all available web browsers. * Pass an array of available browsers to the callback function when done. * @param {Function} callback Callback function */ -module.exports = function detect( callback ) { - var available = [], - names, - check; - - if ( process.platform === 'win32' ) { - return detectWindows( callback ); - } else if ( process.platform === 'darwin' ) { - check = checkDarwin; - } else { - check = checkOthers; +module.exports = function detect(callback) { + if (process.platform === 'win32') { + return detectWindows(callback); } - names = Object.keys( browsers ); - - function next() { - var name = names.shift(); - - if ( !name ) { - return callback( available ); - } + function shouldUseDarwinDetector(browserName) { + return process.platform === 'darwin' && darwin[browserName]; + } - var br = browsers[ name ]; - - check( name, function( err, v, p ) { - if ( err === null ) { - if ( Array.isArray( v ) ) { - v.forEach( function( item ) { - available.push( extend( {}, br, { - command: item.path, - version: item.version - } ) ); - } ); + var available = [], + detectAttempts = 0, + flattened = flattenBrowsers(shouldUseDarwinDetector); + flattened.forEach(function(flatBrowser) { + function cb(err, version, path) { + detectAttempts++; + if (!err) { + if (Array.isArray(version)) { + version.forEach(function(item) { + flatBrowser.command = item.path || flatBrowser.command; + flatBrowser.version = item.version; + available.push(flatBrowser); + }); } else { - available.push( extend( {}, br, { - command: p || name, - version: v - } ) ); + flatBrowser.command = path || flatBrowser.command; + flatBrowser.version = version; + available.push(flatBrowser); } } - next(); - } ); - } + if (detectAttempts === flattened.length) { + callback(available); + } + } - next(); -}; + if (shouldUseDarwinDetector(flatBrowser.name)) { + return checkDarwin(flatBrowser.name, cb); + } + checkOthers(flatBrowser.command, flatBrowser.regex, cb); + }); +}; \ No newline at end of file From 98bd1f2abe36834c13f4923aaa2b5d9781cda810 Mon Sep 17 00:00:00 2001 From: Mitchell Hentges Date: Sun, 28 Feb 2016 17:02:47 +0100 Subject: [PATCH 03/11] Fix refactor on windows --- lib/detect.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/detect.js b/lib/detect.js index 40a0f54..6a8012c 100644 --- a/lib/detect.js +++ b/lib/detect.js @@ -50,20 +50,20 @@ var spawn = require( 'child_process' ).spawn, * Pass an array of detected browsers to the callback function when done. * @param {Function} callback Callback function */ -function detectWindows( callback ) { - winDetect( function( found ) { - var available = found.map( function( browser ) { - var br = browsers[ winDetectMap[ browser.name ] || browser.name ]; +function detectWindows(callback) { + winDetect(function (found) { + var available = found.map(function (browser) { + var br = omit(browsers[browser.name], 'variants'); - return extend( {}, { + return extend({ name: browser.name, command: browser.path, version: browser.version - }, br || {} ); - } ); + }, br); + }); - callback( available ); - } ); + callback(available); + }); } /** From 3174ee990401d81925c848e3e4e91f2655e1545a Mon Sep 17 00:00:00 2001 From: Mitchell Hentges Date: Sun, 28 Feb 2016 17:03:12 +0100 Subject: [PATCH 04/11] Extend is now assign in Lodash --- lib/detect.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/detect.js b/lib/detect.js index 6a8012c..a720cc7 100644 --- a/lib/detect.js +++ b/lib/detect.js @@ -55,7 +55,7 @@ function detectWindows(callback) { var available = found.map(function (browser) { var br = omit(browsers[browser.name], 'variants'); - return extend({ + return assign({ name: browser.name, command: browser.path, version: browser.version From 90a4b2a8a90759306c54757d075dc535bb1ff63e Mon Sep 17 00:00:00 2001 From: Mitchell Hentges Date: Sun, 28 Feb 2016 17:03:58 +0100 Subject: [PATCH 05/11] Missing type on Windows --- lib/detect.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/detect.js b/lib/detect.js index a720cc7..0a88ec2 100644 --- a/lib/detect.js +++ b/lib/detect.js @@ -56,6 +56,7 @@ function detectWindows(callback) { var br = omit(browsers[browser.name], 'variants'); return assign({ + type: browser.name, name: browser.name, command: browser.path, version: browser.version From 20bcacd682ea4be88e26380a375ec09629d6ca25 Mon Sep 17 00:00:00 2001 From: Mitchell Hentges Date: Sun, 28 Feb 2016 21:20:12 +0100 Subject: [PATCH 06/11] Properly support older JS version Code reduction --- lib/detect.js | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/lib/detect.js b/lib/detect.js index 0a88ec2..57c1605 100644 --- a/lib/detect.js +++ b/lib/detect.js @@ -151,11 +151,11 @@ function flattenBrowsers(shouldUseDarwinDetector) { Object.keys(variants).map(function(name) { if (shouldUseDarwinDetector(name)) { - return flatBrowsers.push(assign({type, name}, config)); // `command` not needed, will use darwin detector + return flatBrowsers.push(assign({type: type, name: name}, config)); // `command` not needed, will use darwin detector } return variants[name].map(function(command) { - flatBrowsers.push(assign({type, name, command}, config)); + flatBrowsers.push(assign({type: type, name: name, command: command}, config)); }); }); }); @@ -184,17 +184,15 @@ module.exports = function detect(callback) { function cb(err, version, path) { detectAttempts++; if (!err) { - if (Array.isArray(version)) { - version.forEach(function(item) { - flatBrowser.command = item.path || flatBrowser.command; - flatBrowser.version = item.version; - available.push(flatBrowser); - }); - } else { - flatBrowser.command = path || flatBrowser.command; - flatBrowser.version = version; - available.push(flatBrowser); + if (!Array.isArray(version)) { + version = [{path: path, version: version}]; } + + version.forEach(function(item) { + flatBrowser.command = item.path || flatBrowser.command; + flatBrowser.version = item.version; + available.push(flatBrowser); + }); } if (detectAttempts === flattened.length) { @@ -207,4 +205,4 @@ module.exports = function detect(callback) { } checkOthers(flatBrowser.command, flatBrowser.regex, cb); }); -}; \ No newline at end of file +}; From 53c3b7754baf675fdaee83f0ea92ae69f1dc7ec1 Mon Sep 17 00:00:00 2001 From: Mitchell Hentges Date: Sun, 28 Feb 2016 21:25:00 +0100 Subject: [PATCH 07/11] Update darwin detectors --- lib/darwin/{canary.js => chrome-canary.js} | 0 lib/darwin/index.js | 4 ++-- 2 files changed, 2 insertions(+), 2 deletions(-) rename lib/darwin/{canary.js => chrome-canary.js} (100%) diff --git a/lib/darwin/canary.js b/lib/darwin/chrome-canary.js similarity index 100% rename from lib/darwin/canary.js rename to lib/darwin/chrome-canary.js diff --git a/lib/darwin/index.js b/lib/darwin/index.js index c92c4ca..d83a3b7 100644 --- a/lib/darwin/index.js +++ b/lib/darwin/index.js @@ -1,7 +1,7 @@ exports.safari = require( './safari' ); exports.firefox = require( './firefox' ); -exports.chrome = exports[ 'google-chrome' ] = require( './chrome' ); +exports.chrome = require( './chrome' ); exports.chromium = require( './chromium' ); -exports.canary = exports[ 'chrome-canary' ] = exports[ 'google-chrome-canary' ] = require( './canary' ); +exports[ 'chrome-canary' ] = require( './chrome-canary' ); exports.opera = require( './opera' ); exports.phantomjs = require( './phantomjs' ); From 69849d63bb23252037a1ebfe74872302e7a9d994 Mon Sep 17 00:00:00 2001 From: Mitchell Hentges Date: Sun, 28 Feb 2016 21:27:44 +0100 Subject: [PATCH 08/11] All flatBrowsers should have name, type, command (+ config) --- lib/detect.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/detect.js b/lib/detect.js index 57c1605..b32994e 100644 --- a/lib/detect.js +++ b/lib/detect.js @@ -151,7 +151,7 @@ function flattenBrowsers(shouldUseDarwinDetector) { Object.keys(variants).map(function(name) { if (shouldUseDarwinDetector(name)) { - return flatBrowsers.push(assign({type: type, name: name}, config)); // `command` not needed, will use darwin detector + return flatBrowsers.push(assign({type: type, name: name, command: command}, config)); } return variants[name].map(function(command) { From 7fd09e5e83fe6e0d34b94eec67429ae752fc3de7 Mon Sep 17 00:00:00 2001 From: Mitchell Hentges Date: Sun, 28 Feb 2016 21:37:50 +0100 Subject: [PATCH 09/11] Flatten checkDarwin --- lib/detect.js | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/lib/detect.js b/lib/detect.js index b32994e..33ee32c 100644 --- a/lib/detect.js +++ b/lib/detect.js @@ -73,30 +73,30 @@ function detectWindows(callback) { * @param {String} name Name of a browser * @param {Function} callback Callback function */ -function checkDarwin( name, callback ) { - if ( darwin[ name ].all ) { - darwin[ name ].all( function( err, available ) { - if ( err ) { - callback( 'failed to get version for ' + name ); - } else { - callback( err, available ); +function checkDarwin(name, callback) { + if (darwin[name].all) { + darwin[name].all(function (err, available) { + if (err) { + return callback('failed to get version for ' + name); } - } ); - } else { - darwin[ name ].version( function( err, version ) { - if ( version ) { - darwin[ name ].path( function( err, p ) { - if ( err ) { - return callback( 'failed to get path for ' + name ); - } - - callback( null, version, p ); - } ); - } else { - callback( 'failed to get version for ' + name ); - } - } ); + callback(undefined, available); + }); + return; } + + darwin[name].version(function (err, version) { + if (err) { + return callback('failed to get version for ' + name); + } + + darwin[name].path(function (err, path) { + if (err) { + return callback('failed to get path for ' + name); + } + + callback(undefined, version, path); + }); + }); } /** From cc897e27cc7e47b9de812c12564ee69ac00aee88 Mon Sep 17 00:00:00 2001 From: Mitchell Hentges Date: Sun, 28 Feb 2016 21:52:34 +0100 Subject: [PATCH 10/11] Embrace immutability in setting version and path --- lib/detect.js | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/lib/detect.js b/lib/detect.js index 33ee32c..6ef7f02 100644 --- a/lib/detect.js +++ b/lib/detect.js @@ -79,7 +79,7 @@ function checkDarwin(name, callback) { if (err) { return callback('failed to get version for ' + name); } - callback(undefined, available); + callback(null, available); }); return; } @@ -94,7 +94,7 @@ function checkDarwin(name, callback) { return callback('failed to get path for ' + name); } - callback(undefined, version, path); + callback(null, version, path); }); }); } @@ -129,12 +129,8 @@ function checkOthers( name, regex, callback ) { } var match = regex.exec( data ); - - if ( match ) { - callback( null, match[ 1 ] ); - } else { - callback( null, data.trim() ); - } + var version = match ? match[1] : data.trim(); + callback(null, version); } ); } @@ -189,9 +185,10 @@ module.exports = function detect(callback) { } version.forEach(function(item) { - flatBrowser.command = item.path || flatBrowser.command; - flatBrowser.version = item.version; - available.push(flatBrowser); + available.push(assign({ + command: item.path || flatBrowser.command, + version: item.version + }, flatBrowser)); }); } From 5413e5c574f6f7368cbcf6467fe6cabac1e05ee3 Mon Sep 17 00:00:00 2001 From: Mitchell Hentges Date: Sun, 28 Feb 2016 22:07:48 +0100 Subject: [PATCH 11/11] Fix usage of assign --- lib/detect.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/detect.js b/lib/detect.js index 6ef7f02..e32a67d 100644 --- a/lib/detect.js +++ b/lib/detect.js @@ -185,10 +185,10 @@ module.exports = function detect(callback) { } version.forEach(function(item) { - available.push(assign({ + available.push(assign({}, flatBrowser, { command: item.path || flatBrowser.command, version: item.version - }, flatBrowser)); + })); }); }