Created
November 24, 2014 11:05
-
-
Save bradberger/31872575de555a147773 to your computer and use it in GitHub Desktop.
Uploading files via sftp using Gulp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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