mecano
Version:
Common functions for system deployment.
86 lines (76 loc) • 1.94 kB
JavaScript
// Generated by CoffeeScript 1.11.1
var Minimatch, exec, getprefix, glob, path, string;
path = require('path');
glob = require('glob');
Minimatch = require('minimatch').Minimatch;
exec = require('ssh2-exec');
string = require('./string');
getprefix = function(pattern) {
var n, prefix;
prefix = null;
n = 0;
while (typeof pattern[n] === "string") {
n++;
}
switch (n) {
case pattern.length:
prefix = pattern.join('/');
return prefix;
case 0:
return null;
default:
prefix = pattern.slice(0, n);
prefix = prefix.join('/');
return prefix;
}
};
/*
Important: for now, only the "dot" options has been tested.
*/
module.exports = function(ssh, pattern, options, callback) {
var cmd, i, len, minimatch, prefix, ref, s;
if (arguments.length === 3) {
callback = options;
options = {};
}
if (ssh) {
pattern = path.normalize(pattern);
minimatch = new Minimatch(pattern, options);
cmd = "find";
ref = minimatch.set;
for (i = 0, len = ref.length; i < len; i++) {
s = ref[i];
prefix = getprefix(s);
cmd += " " + prefix;
}
return exec(ssh, cmd, function(err, stdout) {
var files, j, len1, n, ref1;
if (err) {
return callback(null, []);
}
files = string.lines(stdout.trim());
files = files.filter(function(file) {
return minimatch.match(file);
});
ref1 = minimatch.set;
for (j = 0, len1 = ref1.length; j < len1; j++) {
s = ref1[j];
n = 0;
while (typeof s[n] === "string") {
n++;
}
if (s[n] === Minimatch.GLOBSTAR) {
prefix = getprefix(s);
if (prefix) {
files.unshift(prefix);
}
}
}
return callback(err, files);
});
} else {
return glob("" + pattern, options, function(err, files) {
return callback(err, files);
});
}
};