mecano
Version:
Common functions for system deployment.
66 lines (58 loc) • 1.75 kB
JavaScript
// Generated by CoffeeScript 1.7.1
var child, conditions, each, fs, misc, write;
fs = require('ssh2-fs');
each = require('each');
misc = require('./misc');
conditions = require('./misc/conditions');
child = require('./misc/child');
write = require('./write');
module.exports = function(goptions, options, callback) {
var finish, result, _ref;
_ref = misc.args(arguments), goptions = _ref[0], options = _ref[1], callback = _ref[2];
result = child();
finish = function(err, modified) {
if (callback) {
callback(err, modified);
}
return result.end(err, modified);
};
return misc.options(options, function(err, options) {
var modified;
if (err) {
return finish(err);
}
modified = 0;
return each(options).on('item', function(options, next) {
var destination, mode, ssh;
ssh = options.ssh, destination = options.destination, mode = options.mode;
if (!destination) {
return next(new Error("Missing destination: " + destination));
}
if (typeof options.log === "function") {
options.log("Check if exists: " + destination);
}
return fs.exists(ssh, destination, function(err, exists) {
if (err) {
return next(err);
}
if (exists) {
return next();
}
options.source = null;
options.content = '';
if (typeof options.log === "function") {
options.log("Create a new empty file");
}
return write(options, function(err, written) {
if (err) {
return next(err);
}
modified++;
return next();
});
});
}).on('both', function(err) {
return finish(err, modified);
});
});
};