Skip to content

Instantly share code, notes, and snippets.

@bradberger
Created November 24, 2014 11:05
Show Gist options
  • Save bradberger/31872575de555a147773 to your computer and use it in GitHub Desktop.
Save bradberger/31872575de555a147773 to your computer and use it in GitHub Desktop.
Uploading files via sftp using Gulp
var gulp = require("gulp"),
sftp = require("gulp-sftp"),
path = require("path"),
changed = require("gulp-changed"),
rename = require("gulp-rename"),
tap = require("gulp-tap"),
notify = require("gulp-notify"),
remoteBaseDir = "/base/dir/on/server",
watchedFilesToUpload = ["css/**/*.css", "js/**/*.js"];
// See gulp-sftp options for the meaning of auth: "privateKeyCustom" or similar.
// https://www.npmjs.org/package/gulp-sftp
function sftpOpts(opts) {
var defaults = { host: "remote.host.com", auth: "privateKeyCustom", remotePath: "" };
if (opts) {
for(var attr in opts) {
if(opts.hasOwnProperty(attr)) {
defaults[attr] = opts[attr];
}
}
}
return defaults;
}
function localPath(str) {
return __dirname + (str.charAt(0) === "/" ? "" : "/") + str;
}
function upload(event) {
var path = event.path, remoteFile;
gulp.src(path)
.pipe(tap(function (file, t) {
remoteFile = file.base.replace(__dirname,remoteBaseDir);
gulp.src(file.path)
.pipe(sftp(sftpOpts({
remotePath: remoteFile
})))
.pipe(notify({
onLast: true,
message: "Uploaded " + file.relative + " to " + remoteFile
}));
}));
};
gulp.task("watch:upload", function() {
gulp.watch(watchedFilesToUpload, upload);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment