Last active
May 3, 2018 09:05
-
-
Save piotr-cz/cc7c82e6a8161e425cae3a5bacfb7d6a to your computer and use it in GitHub Desktop.
Gulp - FTP deploy updated files
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
{ | |
"env": { | |
"staging": { | |
"ftpConfig": { | |
"host": "xxx", | |
"user": "xxx", | |
"pass": "xxx" | |
}, | |
"path": { | |
"local": "./public", | |
"remote": "/www" | |
}, | |
"since": null | |
} | |
}, | |
"global": { | |
"patterns": [ | |
"public/**", | |
"public/.*", | |
"!.editorconfig", | |
"!.gitattributes" | |
] | |
} | |
} |
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
/*jscs:enable esnext */ | |
/*jshint esversion: 6 */ | |
var gulp = require('gulp'); | |
// Deploy tools | |
var ftp = require('vinyl-ftp'); | |
var fs = require('fs'); | |
var vinylFilterSince = require('vinyl-filter-since'); | |
// Variables | |
var deployConfigFile = './deploy.json'; | |
/** | |
* Deploy | |
* | |
* usage: gulp deploy [--env production] | |
* global variables: deployConfigFile, util, console, ftp | |
*/ | |
gulp.task('deploy', (cb) => { | |
var conn; | |
var deployConfig = require(deployConfigFile); | |
var args = process.argv.slice(3); | |
var targetEnv = (args.length && args[0] == '--env') ? args[1] : 'staging'; | |
var envOptions; | |
// Check that environment options exists | |
if (!deployConfig.env.hasOwnProperty(targetEnv)) { | |
console.error( | |
'No config for "%s1" environment.\nAvailable environments are: %a1.' | |
.replace('%s1', targetEnv) | |
.replace('%a1', Object.keys(deployConfig.env)) | |
); | |
return; | |
} | |
// envOptions = deployConfig.env[targetEnv]; | |
envOptions = Object.assign({}, deployConfig.env[targetEnv], deployConfig.global); | |
// Configure logger | |
if (!envOptions.ftpConfig.hasOwnProperty('log')) { | |
envOptions.ftpConfig.log = util.log; | |
} | |
console.info('Transferring files...\n'); | |
// Create new connection | |
conn = ftp.create(envOptions.ftpConfig); | |
gulp.src(envOptions.patterns, {base: envOptions.path.local, buffer: false}) | |
.pipe(vinylFilterSince(new Date(envOptions.since || 0))) | |
.pipe(conn.dest(envOptions.path.remote)) | |
.on('end', function() { | |
console.info('Transfer complete\n'); | |
// Store timestamp | |
deployConfig.env[targetEnv].since = new Date(); | |
fs.writeFile(deployConfigFile, JSON.stringify(deployConfig, null, 4), cb); | |
}) | |
.on('error', swallowError) | |
; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment