Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete files from dest if they are deleted locally #64

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
27 changes: 26 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The callback will be in conflict if deleteRemote on.

}
getSftp(function (err, sftp) {
Expand Down Expand Up @@ -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, '/')

Expand Down