Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Gulp task for uploading files to ftp/sftp server based on Sublime Text sftp-config.json configuration file
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);
}
});
@TCB13

This comment has been minimized.

Copy link

@TCB13 TCB13 commented Sep 27, 2016

If your sublime files include comments (like mine) you can replace require('./sftp-config.json') with:

var config = fs.readFileSync("./sftp-config.json", "utf8").replace(/  +/g, '').replace(/\/\/.+/g, '')
config = JSON.parse(config);

It will remove all indentation and lines starting with // so the file can be parsed.

@TCB13

This comment has been minimized.

Copy link

@TCB13 TCB13 commented Oct 5, 2016

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