UNPKG

mecano

Version:

Common functions for system deployment.

87 lines (78 loc) 2.59 kB
// Generated by CoffeeScript 1.7.1 var child, conditions, each, exec, fs, misc, path; fs = require('ssh2-fs'); path = require('path'); each = require('each'); exec = require('ssh2-exec'); misc = require('./misc'); conditions = require('./misc/conditions'); child = require('./misc/child'); module.exports = function(goptions, options, callback) { var _ref; _ref = misc.args(arguments), goptions = _ref[0], options = _ref[1], callback = _ref[2]; return misc.options(options, function(err, options) { var extracted; if (err) { return callback(err); } extracted = 0; return each(options).parallel(goptions.parallel).on('item', function(options, next) { var creates, destination, ext, extract, format, success, _ref1; if (!options.source) { return next(new Error("Missing source: " + options.source)); } destination = (_ref1 = options.destination) != null ? _ref1 : path.dirname(options.source); if (options.format != null) { format = options.format; } else { if (/\.(tar\.gz|tgz)$/.test(options.source)) { format = 'tgz'; } else if (/\.zip$/.test(options.source)) { format = 'zip'; } else { ext = path.extname(options.source); return next(new Error("Unsupported extension, got " + (JSON.stringify(ext)))); } } extract = function() { var cmd; cmd = null; switch (format) { case 'tgz': cmd = "tar xzf " + options.source + " -C " + destination; break; case 'zip': cmd = "unzip -u " + options.source + " -d " + destination; } options.cmd = cmd; return exec(options, function(err, stdout, stderr) { if (err) { return next(err); } return creates(); }); }; creates = function() { if (options.creates == null) { return success(); } return fs.exists(options.ssh, options.creates, function(err, exists) { if (!exists) { return next(new Error("Failed to create '" + (path.basename(options.creates)) + "'")); } return success(); }); }; success = function() { extracted++; return next(); }; if (typeof options.should_exist === 'undefined') { options.should_exist = options.source; } return conditions.all(options, next, extract); }).on('both', function(err) { return callback(err, extracted); }); }); };