mecano
Version:
Common functions for system deployment.
121 lines (112 loc) • 3.42 kB
JavaScript
// Generated by CoffeeScript 1.7.1
var child, conditions, each, execute, fs, misc, path;
fs = require('ssh2-fs');
path = require('path');
each = require('each');
misc = require('./misc');
conditions = require('./misc/conditions');
child = require('./misc/child');
execute = require('./execute');
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 updated;
if (err) {
return callback(err);
}
updated = 0;
return each(options).parallel(goptions.parallel).on('item', function(options, next) {
var checkout, clone, log, prepare, rev;
if (options.revision == null) {
options.revision = 'HEAD';
}
rev = null;
prepare = function() {
return fs.exists(options.ssh, options.destination, function(err, exists) {
var gitDir;
if (err) {
return next(err);
}
if (!exists) {
return clone();
}
gitDir = "" + options.destination + "/.git";
return fs.exists(options.ssh, gitDir, function(err, exists) {
if (!exists) {
return next(new Error("Not a git repository"));
}
return log();
});
});
};
clone = function() {
return execute({
ssh: options.ssh,
cmd: "git clone " + options.source + " " + options.destination,
cwd: path.dirname(options.destination),
log: options.log,
stdout: options.stdout,
stderr: options.stderr
}, function(err, executed, stdout, stderr) {
if (err) {
return next(err);
}
return checkout();
});
};
log = function() {
return execute({
ssh: options.ssh,
cmd: "git log --pretty=format:'%H' -n 1",
cwd: options.destination,
log: options.log,
stdout: options.stdout,
stderr: options.stderr
}, function(err, executed, stdout, stderr) {
var current;
if (err) {
return next(err);
}
current = stdout.trim();
return execute({
ssh: options.ssh,
cmd: "git rev-list --max-count=1 " + options.revision,
cwd: options.destination,
log: options.log,
stdout: options.stdout,
stderr: options.stderr
}, function(err, executed, stdout, stderr) {
if (err) {
return next(err);
}
if (stdout.trim() !== current) {
return checkout();
} else {
return next();
}
});
});
};
checkout = function() {
return execute({
ssh: options.ssh,
cmd: "git checkout " + options.revision,
cwd: options.destination,
log: options.log,
stdout: options.stdout,
stderr: options.stderr
}, function(err) {
if (err) {
return next(err);
}
updated++;
return next();
});
};
return conditions.all(options, next, prepare);
}).on('both', function(err) {
return callback(err, updated);
});
});
};