diff --git a/README.md b/README.md index b42d95d..a0f0bb6 100644 --- a/README.md +++ b/README.md @@ -166,6 +166,18 @@ Type: `Object` return `stream`, copy the files to remote through sftp, acts similarly to Gulp dest, will make dirs if not exist. +#### options + +*Option* +Type: `Object` + +#### options.deleteRemote + +*Option* +Type: `Boolean` + +should gulp-ssh delete remote files when they are deleted locally + ## License MIT © [Teambition](https://www.teambition.com) diff --git a/index.js b/index.js index 32ad676..fbdfa09 100644 --- a/index.js +++ b/index.js @@ -214,6 +214,8 @@ GulpSSH.prototype.dest = function (destDir, options) { options = options || {} options.autoClose = false + var deleteRemote = options.deleteRemote || false + function getSftp (callback) { if (sftpClient) return callback(null, sftpClient) ssh.gulpReady(function () { @@ -237,7 +239,21 @@ GulpSSH.prototype.dest = function (destDir, options) { return through.obj(function (file, encoding, callback) { if (file.isNull()) { - gutil.log('"' + gutil.colors.cyan(file.path) + '" has no content. Skipping.') + if (deleteRemote) { + gutil.log('"' + gutil.colors.cyan(file.path) + '" has no content. Attempting delete.') + getSftp(function (err, sftp) { + if (err) return end(err, callback) + var baseRegexp = new RegExp('^' + file.base.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&')) + var outPath = path.join(destDir, file.path.replace(baseRegexp, '')).replace(/\\/g, '/') + gutil.log('Preparing to delete "' + gutil.colors.red(outPath) + '"') + + internalRemove(sftp, outPath, function (err) { + if (err) return end(err, callback) + }) + }) + } else { + gutil.log('"' + gutil.colors.cyan(file.path) + '" has no content. Skipping.') + } return callback() } getSftp(function (err, sftp) { @@ -319,6 +335,15 @@ GulpSSH.prototype.shell = function (commands, options) { return outStream } +// function internalRemove(sftp, filePath, callback) { +function internalRemove(sftp, filePath, callback) { + sftp.exists(filePath, function(result) { + if (!result) return + gutil.log('Deleting \'' + gutil.colors.red(filePath) + '\'') + sftp.unlink(filePath, callback) + }) +} + function internalMkDirs (sftp, filePath, callback) { var outPathDir = path.dirname(filePath).replace(/\\/g, '/')