Created
November 4, 2015 11:42
-
-
Save akalongman/5c9f7049ad7c534b057b to your computer and use it in GitHub Desktop.
Gulp task for uploading files to ftp/sftp server based on Sublime Text sftp-config.json configuration file
This file contains hidden or 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
const gutil = require('gulp-util'); | |
const ftp = require( 'vinyl-ftp' ); | |
const sftp = require('gulp-sftp'); | |
// task for deploying files on the server | |
gulp.task('deploy', function() { | |
const config = require('./sftp-config.json'); | |
const globs = [ | |
'folder/file', | |
'folder/file', | |
'folder/file', | |
]; | |
if (config.type == 'ftp') { | |
// FTP version | |
const conn = ftp.create( { | |
host: config.host, | |
user: config.user, | |
password: config.password, | |
port: config.port, | |
parallel: 10, | |
reload: true, | |
debug: function(d){console.log(d);}, | |
log: gutil.log | |
}); | |
return gulp.src( globs, { base: '.', buffer: false } ) | |
.pipe( conn.newer( '/dest_folder/' ) ) // only upload newer files | |
.pipe( conn.dest( '/dest_folder/' ) ); | |
} else { | |
// SFTP version | |
const conn = sftp({ | |
host: config.host, | |
user: config.user, | |
pass: config.password, | |
port: config.port, | |
remotePath: config.remote_path, | |
}); | |
return gulp.src(globs, { base: '.', buffer: false } ) | |
.pipe(conn); | |
} | |
}); |
After a few days I now recommend everyone to drop my previous regex and use strip-json-comments
from here for this. Example:
var cleanJSON = require("strip-json-comments");
var config = fs.readFileSync("./sftp-config.json", "utf8");
config = JSON.parse(cleanJSON(config));
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If your sublime files include comments (like mine) you can replace
require('./sftp-config.json')
with:It will remove all indentation and lines starting with
//
so the file can be parsed.