Skip to content

Instantly share code, notes, and snippets.

@kumaxim
Created March 4, 2017 18:52
Show Gist options
  • Select an option

  • Save kumaxim/40fcc8e1f7aaf66634c222821defd880 to your computer and use it in GitHub Desktop.

Select an option

Save kumaxim/40fcc8e1f7aaf66634c222821defd880 to your computer and use it in GitHub Desktop.
Simple sFTP task for sync the changes between local and remote machine
var gulp = require('gulp');
var plumber = require('gulp-plumber');
var sftp = require('gulp-sftp');
var sftpInfo = function () {
var info = {
host: '172.0.0.1',
port: '0',
user: 'secter_user_login',
pass: 'password',
remotePath: '/place/remote/path/here',
};
return info;
}
var settingProject = {
folder: 'relative/path/on/localhost/',
}
var extractRelativePath = function (filePath) {
// Remove file name at the end
var folder = filePath.substring(0, filePath.lastIndexOf('/'));
var splitedPath = folder.split(settingProject.folder);
return splitedPath[1];
}
var SFTPSettings = function(changedFile) {
var newSettings = sftpInfo();
var newRemotePath = newSettings.remotePath + '/' + extractRelativePath(changedFile);
newSettings.remotePath = newRemotePath.replace('//', '/');
return newSettings;
}
gulp.task('upload', function () {
return gulp.src(settingProject.folder + '/**/*')
.pipe(plumber())
.pipe(sftp(sftpInfo()));
});
gulp.task('default', function () {
var watcher = gulp.watch(settingProject.folder + '/**/*', function () {});
var uploader = function(properties) {
return gulp.src(properties.path)
.pipe(plumber())
.pipe(sftp(SFTPSettings(properties.path)));
}
watcher.on('added', function (event) { return uploader(event); });
watcher.on('change', function (event) { return uploader(event); });
// The delete event will need implement in gulp-rsync. The gulp-sftp is working wrong with it.
// watcher.on('deleted', function (event) { return uploader(event); });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment