Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup darwin section #19

Merged
merged 10 commits into from
Mar 2, 2016
Merged
6 changes: 0 additions & 6 deletions lib/darwin/chrome-canary.js

This file was deleted.

6 changes: 0 additions & 6 deletions lib/darwin/chrome.js

This file was deleted.

36 changes: 0 additions & 36 deletions lib/darwin/chromium.js

This file was deleted.

42 changes: 0 additions & 42 deletions lib/darwin/firefox.js

This file was deleted.

23 changes: 16 additions & 7 deletions lib/darwin/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
exports.safari = require( './safari' );
exports.firefox = require( './firefox' );
exports.chrome = require( './chrome' );
exports.chromium = require( './chromium' );
exports[ 'chrome-canary' ] = require( './chrome-canary' );
exports.opera = require( './opera' );
exports.phantomjs = require( './phantomjs' );
var util = require('./util');

function browser(id, versionKey) {
return {
path: util.find.bind(null, id),
version: util.getInfoKey.bind(null, id, versionKey)
};
}

exports.chrome = browser('com.google.Chrome', 'KSVersion');
exports[ 'chrome-canary' ] = browser('com.google.Chrome.canary', 'KSVersion');
exports.chromium = browser('org.chromium.Chromium', 'CFBundleShortVersionString');
exports.firefox = browser('org.mozilla.firefox', 'CFBundleShortVersionString');
exports['firefox-developer'] = browser('org.mozilla.firefoxdeveloperedition', 'CFBundleShortVersionString');
exports.safari = browser('com.apple.Safari', 'CFBundleShortVersionString');
exports.opera = browser('com.operasoftware.Opera', 'CFBundleVersion');
6 changes: 0 additions & 6 deletions lib/darwin/opera.js

This file was deleted.

32 changes: 0 additions & 32 deletions lib/darwin/phantomjs.js

This file was deleted.

6 changes: 0 additions & 6 deletions lib/darwin/safari.js

This file was deleted.

118 changes: 53 additions & 65 deletions lib/darwin/util.js
Original file line number Diff line number Diff line change
@@ -1,87 +1,75 @@
var exec = require( 'child_process' ).exec,
fs = require( 'fs' ),
path = require( 'path' ),
plist = require( 'plist' );
var exec = require('child_process').exec,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly just auto-indent going on here

fs = require('fs'),
path = require('path'),
plist = require('plist');

exports.exists = fs.exists;
exports.parse = parse;
exports.find = find;
exports.getInfoPath = getInfoPath;
exports.getInfoKey = getInfoKey;
exports.findBin = findBin;
exports.getBinVersion = getBinVersion;

var infoCache = Object.create(null);

function parse( file, callback ) {
if (infoCache[file] !== void 0) {
return callback(null, infoCache[file]);
}
fs.readFile( file, {
encoding: 'utf8'
}, function( err, data ) {
if ( !err ) {
infoCache[ file ] = data = plist.parse( data );
}

callback( err, data );
} );
}

var bundleCache = Object.create(null);

function find( id, callback ) {
if (bundleCache[id] !== void 0) {
return callback(null, bundleCache[id]);
}
var pathQuery = 'mdfind "kMDItemCFBundleIdentifier=="' + id + '"" | head -1';
function parse(file, callback) {
if (infoCache[file]) {
return callback(null, infoCache[file]);
}

exec( pathQuery, function( err, stdout ) {
var loc = stdout.trim();
fs.exists(file, function(exists) {
if (!exists) {
return callback('cannot parse non-existant plist', null);
}

if ( loc === '' ) {
loc = null;
err = 'not installed';
} else {
bundleCache[id] = loc;
}
fs.readFile(file, {
encoding: 'utf8'
}, function (err, data) {
if (!err) {
infoCache[file] = data = plist.parse(data);
}

callback( err, loc );
} );
callback(err, data);
});
});
}

function findBin(id, callback) {
exec('which ' + id, function( err, path ) {
callback( err, path ? path.trim() : path );
});
}
function find(id, callback) {
if (bundleCache[id]) {
return callback(null, bundleCache[id]);
}

var pathQuery = 'mdfind kMDItemCFBundleIdentifier=="' + id + '" | head -1';
exec(pathQuery, function (err, stdout) {
var loc = stdout.trim();

function getBinVersion(id, callback) {
exec(id + ' --version', function( err, version ) {
callback( err, version ? version.trim() : version );
});
if (loc === '') {
loc = null;
err = 'not installed';
} else {
bundleCache[id] = loc;
}

callback(err, loc);
});
}

function getInfoPath( p ) {
return path.join( p, 'Contents', 'Info.plist' );
function getInfoPath(p) {
return path.join(p, 'Contents', 'Info.plist');
}

function getInfoKey( bundleId, key, callback ) {
find( bundleId, function( err, path ) {
if ( err ) {
return callback( err, null );
}
function getInfoKey(bundleId, key, callback) {
find(bundleId, function (err, path) {
if (err) {
return callback(err, null);
}

var pl = getInfoPath( path );
parse(getInfoPath(path), function (err, data) {
if (err) {
return callback(err, null);
}

fs.exists( pl, function( exists ) {
if ( exists ) {
parse( pl, function( err, data ) {
callback( err, data[key] );
} );
} else {
callback( 'not installed', null );
}
} );
} );
}
callback(null, data[key]);
});
});
}
55 changes: 20 additions & 35 deletions lib/detect.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,19 @@ function detectWindows(callback) {
* @param {Function} callback Callback function
*/
function checkDarwin(name, callback) {
if (darwin[name].all) {
darwin[name].all(function (err, available) {
if (err) {
return callback('failed to get version for ' + name);
}
callback(null, available);
});
} else {
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(null, version, path);
});
});
}
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(null, version, path);
});
});
}

/**
Expand Down Expand Up @@ -145,19 +136,13 @@ module.exports = function detect(callback) {
function browserDone(err, version, path) {
detectAttempts++;
if (!err) {
if (!Array.isArray(version)) {
version = [{path: path, version: version}];
}

version.forEach(function(item) {
var config = browsers.typeConfig(browserPlatform.type);
available.push(assign({}, config, {
type: browserPlatform.type,
name: browserPlatform.darwin,
command: item.path,
version: item.version
}));
});
var config = browsers.typeConfig(browserPlatform.type);
available.push(assign({}, config, {
type: browserPlatform.type,
name: browserPlatform.darwin,
command: path,
version: version
}));
}

if (detectAttempts === browserPlatforms.length) {
Expand Down