mecano
Version:
Common functions for system deployment.
116 lines (108 loc) • 3.33 kB
JavaScript
// Generated by CoffeeScript 1.7.1
var child, conditions, each, fs, misc, remove;
fs = require('ssh2-fs');
each = require('each');
misc = require('./misc');
conditions = require('./misc/conditions');
child = require('./misc/child');
remove = require('./remove');
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 moved;
if (err) {
return callback(err);
}
moved = 0;
return each(options).parallel(goptions.parallel).on('item', function(options, next) {
var do_chkhash, do_dsthash, do_exists, do_move, do_remove_dest, do_remove_src, do_srchash;
do_exists = function() {
return fs.stat(options.ssh, options.destination, function(err, stat) {
if ((err != null ? err.code : void 0) === 'ENOENT') {
return do_move();
}
if (err) {
return next(err);
}
if (options.force) {
return do_remove_dest();
} else {
return do_srchash();
}
});
};
do_srchash = function() {
if (options.source_md5) {
return do_dsthash();
}
return misc.file.hash(options.ssh, options.source, 'md5', function(err, hash) {
if (err) {
return next(err);
}
options.source_md5 = hash;
return do_dsthash();
});
};
do_dsthash = function() {
if (options.destination_md5) {
return do_chkhash();
}
return misc.file.hash(options.ssh, options.destination, 'md5', function(err, hash) {
if (err) {
return next(err);
}
options.destination_md5 = hash;
return do_chkhash();
});
};
do_chkhash = function() {
if (options.source_md5 === options.destination_md5) {
return do_remove_src();
} else {
return do_remove_dest();
}
};
do_remove_dest = function() {
if (typeof options.log === "function") {
options.log("Remove " + options.destination);
}
return remove({
ssh: options.ssh,
destination: options.destination
}, function(err, removed) {
if (err) {
return next(err);
}
return do_move();
});
};
do_move = function() {
if (typeof options.log === "function") {
options.log("Rename " + options.source + " to " + options.destination);
}
return fs.rename(options.ssh, options.source, options.destination, function(err) {
if (err) {
return next(err);
}
moved++;
return next();
});
};
do_remove_src = function() {
if (typeof options.log === "function") {
options.log("Remove " + options.source);
}
return remove({
ssh: options.ssh,
destination: options.source
}, function(err, removed) {
return next(err);
});
};
return conditions.all(options, next, do_exists);
}).on('both', function(err) {
return callback(err, moved);
});
});
};