mecano
Version:
Common functions for system deployment.
79 lines (75 loc) • 2.2 kB
JavaScript
// Generated by CoffeeScript 1.9.1
var fs, path;
module.exports = function(options, callback) {
var creates, destination, ext, extract, format, ref, stat, success;
if (!options.source) {
return callback(new Error("Missing source: " + options.source));
}
destination = (ref = options.destination) != null ? ref : path.dirname(options.source);
if (options.format != null) {
format = options.format;
} else {
if (/\.(tar\.gz|tgz)$/.test(options.source)) {
format = 'tgz';
} else if (/\.tar$/.test(options.source)) {
format = 'tar';
} else if (/\.zip$/.test(options.source)) {
format = 'zip';
} else {
ext = path.extname(options.source);
return callback(Error("Unsupported extension, got " + (JSON.stringify(ext))));
}
}
stat = function() {
return fs.stat(options.ssh, options.source, function(err, stat) {
if (err) {
return callback(Error("File does not exist: " + options.source));
}
if (!stat.isFile()) {
return callback(Error("Not a File: " + options.source));
}
return extract();
});
};
extract = (function(_this) {
return function() {
var cmd;
cmd = null;
switch (format) {
case 'tgz':
cmd = "tar xzf " + options.source + " -C " + destination;
break;
case 'tar':
cmd = "tar xf " + options.source + " -C " + destination;
break;
case 'zip':
cmd = "unzip -u " + options.source + " -d " + destination;
}
return _this.execute({
cmd: cmd
}, function(err, created) {
if (err) {
return callback(err);
}
return creates();
});
};
})(this);
creates = function() {
if (options.creates == null) {
return success();
}
return fs.exists(options.ssh, options.creates, function(err, exists) {
if (!exists) {
return callback(new Error("Failed to create '" + (path.basename(options.creates)) + "'"));
}
return success();
});
};
success = function() {
return callback(null, true);
};
return stat();
};
fs = require('ssh2-fs');
path = require('path');