Skip to content

Instantly share code, notes, and snippets.

@vankasteelj
Created November 17, 2015 01:17
Show Gist options
  • Save vankasteelj/dbf28309b0e18df5f8df to your computer and use it in GitHub Desktop.
Save vankasteelj/dbf28309b0e18df5f8df to your computer and use it in GitHub Desktop.
multiplatform code to find a local binary on user input.
var fs = require('fs');
var async = require('async');
var path = require('path')
var DEBUT = Date.now();
var USER_INPUT = process.argv[2],
FOUND_BINARY,
PATH_TO_BINARY;
var searchPaths = {
linux: [],
darwin: [],
win32: []
};
var addPath = function (path) {
if (fs.existsSync(path)) {
searchPaths[process.platform].push(path);
}
};
// linux
addPath('/usr/bin');
addPath('/usr/local/bin');
// darwin
addPath('/Applications');
addPath(process.env.HOME + '/Applications');
// win32
addPath(process.env.SystemDrive + '\\Program Files\\');
addPath(process.env.SystemDrive + '\\Program Files (x86)\\');
addPath(process.env.LOCALAPPDATA + '\\Apps\\2.0\\');
async.each(searchPaths[process.platform], function (folderName, pathcb) {
folderName = path.resolve(folderName);
console.log('Scanning: ' + folderName);
var appIndex = -1;
var fileStream = require('readdirp')({
root: folderName,
depth: 3
});
fileStream.on('data', function (d) {
var app = d.name.replace('.app', '').replace('.exe', '').toLowerCase();
var match = app === USER_INPUT.trim().toLowerCase();
if (match) {
FOUND_BINARY = app;
PATH_TO_BINARY = d.fullPath;
console.info('Found External Binary: ' + FOUND_BINARY + ' in ' + d.fullParentDir);
return;
}
});
fileStream.on('end', function () {
pathcb();
});
}, function (err) {
if (err) {
console.error('External Binaries: scan', err);
return;
} else {
console.log('External Binaries: scan finished in %s milliseconds', Date.now() - DEBUT);
if (!FOUND_BINARY) {
console.log('%s not found :(', USER_INPUT);
} else {
console.log('Full path to %s is: %s', FOUND_BINARY, PATH_TO_BINARY)
}
return;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment