UNPKG

mecano

Version:

Common functions for system deployment.

175 lines (168 loc) 5.02 kB
// Generated by CoffeeScript 1.11.1 var each, fs, glob, misc, path; module.exports = function(options, callback) { var do_copy, do_directory, dstStat, modified, srcStat; options.log({ message: "Entering copy", level: 'DEBUG', module: 'mecano/lib/copy' }); if (!options.source) { return callback(Error('Missing source')); } if (!options.target) { return callback(Error('Missing target')); } modified = false; srcStat = null; dstStat = null; options.log({ message: "Stat source file", level: 'DEBUG', module: 'mecano/lib/copy' }); fs.stat(options.ssh, options.source, function(err, stat) { if (err) { return callback(err); } srcStat = stat; options.log({ message: "Stat target file", level: 'DEBUG', module: 'mecano/lib/copy' }); return fs.stat(options.ssh, options.target, function(err, stat) { var sourceEndWithSlash; if (err && err.code !== 'ENOENT') { return callback(err); } dstStat = stat; sourceEndWithSlash = options.source.lastIndexOf('/') === options.source.length - 1; if (srcStat.isDirectory() && dstStat && !sourceEndWithSlash) { options.target = path.resolve(options.target, path.basename(options.source)); } if (srcStat.isDirectory()) { return do_directory(options.source, function(err) { return callback(err, modified); }); } else { return do_copy(options.source, function(err) { return callback(err, modified); }); } }); }); do_directory = function(dir, callback) { options.log({ message: "Source is a directory", level: 'INFO', module: 'mecano/lib/copy' }); return glob(options.ssh, dir + "/**", { dot: true }, function(err, files) { if (err) { return callback(err); } return each(files).call(function(file, callback) { return do_copy(file, callback); }).then(callback); }); }; return do_copy = (function(_this) { return function(source, callback) { var do_chown_chmod, do_copy_dir, do_copy_file, do_end, target; if (srcStat.isDirectory()) { target = path.resolve(options.target, path.relative(options.source, source)); } else if (!srcStat.isDirectory() && (dstStat != null ? dstStat.isDirectory() : void 0)) { target = path.resolve(options.target, path.basename(source)); } else { target = options.target; } fs.stat(options.ssh, source, function(err, stat) { if (err) { return callback(err); } if (stat.isDirectory()) { return do_copy_dir(source, target); } else { return do_copy_file(source, target); } }); do_copy_dir = function(source, target) { options.log({ message: "Create directory " + target, level: 'WARN', module: 'mecano/lib/copy' }); return fs.mkdir(options.ssh, target, function(err) { if ((err != null ? err.code : void 0) === 'EEXIST') { return callback(); } if (err) { return callback(err); } modified = true; return do_end(); }); }; do_copy_file = function(source, target) { return misc.file.compare(options.ssh, [source, target], function(err, md5) { if (err && err.message.indexOf('Does not exist') !== 0) { return callback(err); } if (md5) { return do_chown_chmod(target); } options.log({ message: "Copy file from " + source + " into " + target, level: 'WARN', module: 'mecano/lib/copy' }); return misc.file.copyFile(options.ssh, source, target, function(err) { if (err) { return callback(err); } modified = true; return do_chown_chmod(target); }); }); }; do_chown_chmod = function(target) { _this.chown({ target: target, uid: options.uid, gid: options.gid, "if": (options.uid != null) || (options.gid != null) }); _this.chmod({ target: target, mode: options.mode, "if": options.mode != null }); return _this.then(function(err, status) { if (err) { return callback(err); } if (status) { modified = true; } return do_end(); }); }; return do_end = function() { options.log({ message: "File " + source + " copied", level: 'DEBUG', module: 'mecano/lib/copy' }); return callback(null, modified); }; }; })(this); }; fs = require('ssh2-fs'); path = require('path'); each = require('each'); misc = require('../misc'); glob = require('../misc/glob');