Skip to content

Instantly share code, notes, and snippets.

@gillesdemey
Last active August 29, 2015 14:18
Show Gist options
  • Save gillesdemey/c6f2d7d33b4cbe3c4f6c to your computer and use it in GitHub Desktop.
Save gillesdemey/c6f2d7d33b4cbe3c4f6c to your computer and use it in GitHub Desktop.
var _ = require('lodash');
var exec = require('child_process').exec;
/**
* Get a list of NFS mounted drives
*/
function getMountedDrives(types, callback) {
function parseOut(err, stdout, stderr) {
if (err || stderr)
throw err || stderr;
var lines = stdout.split('\n');
var drives = [];
lines.forEach(function(line, index) {
var paths = line.match(/(?:.*\:)?(.*) on (\/(?:[a-zA-Z0-9\-]+)?)/);
if (!paths)
return;
var mountPath = paths[1];
var realPath = paths[2];
drives.push({
mountPath : mountPath,
realPath : realPath
});
});
callback(null, drives);
}
exec('mount -t ' + types.join(','), parseOut);
}
/**
* Checks if a path is on a mounted drive
*/
function isOnNetworkDrive(path, callback) {
getMountedDrives(['nfs'], function(err, drives) {
// reduce drives to array of realPaths
var paths = _.reduce(drives, function(result, drive) {
result.push(drive.realPath);
return result;
}, []);
var match = false;
// figure out of path starts with one of the realPaths
_.each(paths, function(p) {
if (path.indexOf(p) === 0) {
match = true;
}
});
callback(err, match);
});
}
isOnNetworkDrive(process.cwd(), function(err, bool) {
console.log(err, bool);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment