var gulp = require('gulp'), watch = require('gulp-watch'), Rsync = require('rsync'); var rsync = new Rsync(); var parseArgs = function(args) { var argsMap = {}; var mainPos = 1; while (args[mainPos].indexOf('--') > 0) { mainPos++; } argsMap.main = args[mainPos]; for (var i = (mainPos + 1); i < args.length; i++) { var arg = args[i]; var sep = arg.indexOf('='); var key = arg.slice(2, sep); var value = arg.slice(sep + 1); if (!isNaN(Number(value)) && (value.indexOf('.') < 0)) { value = Number(value); } argsMap[key] = value; } return argsMap; }; var args = parseArgs(process.argv); // Build the command rsync.shell('ssh -p ' + args.port) .flags('avzrP') .destination(args.username + '@' + args.host + ':/data/code/i.haitiand.cn/' + args.username + '/' . args.path) .delete() .exclude([ 'gulpfile.js', '.git', '.DS_Store', 'node_modules' ]); gulp.task('rsync', function() { rsync.source('./dist/') .execute(function(error, code, cmd) { // we're done console.log(error, code, cmd); }, function(data) { // do things like parse progress }, function(data) { // do things like parse error output console.log(data); }); }); gulp.task('default', ['rsync'], function() { gulp.watch([ './dist/**/*', ], ['rsync']).on('change', function(event) { console.log('File ' + event.path + ' was ' + event.type + ', running tasks...'); }); });