Run task only on gulp --production

@jakus I finally worked it out!

First create a .ftppass.json file in your theme root with your FTP credentials:

{
  "host":       "ftp.mydomain.com",
  "user":       "username",
  "password":   "password"
}

Then add these 2 globals at he top of your gulpfile.js:

var vinylftp     = require('vinyl-ftp');
var ftppass      = require('./.ftppass.json');

Then in your upload task, check if the --production flag is set and if so, run a sequence of tasks:

// ### Upload
gulp.task('upload', function(callback) {
  if (argv.production === true) {
    runSequence('rmdirdist', ['ftpupload'], callback);
  }  
});

First remove the remote dist folder with the rmdirdist task:

// ### Remove remote dist directory
gulp.task( 'rmdirdist', function (cb) {
  var conn = vinylftp.create( ftppass );
  conn.rmdir( '/public_html/web/app/themes/sage/dist', cb );
});

After that, upload only new files (including the freshly created dist directory) with the ftpupload task:

// ### Upload Vinyl FTP
gulp.task('ftpupload', function (callback) {
  var conn = vinylftp.create({
    host:       ftppass.host,
    user:       ftppass.user,
    password:   ftppass.password,
    log:        gutil.log
  });
  var globs = [
    '*',
    '*.php',
    'dist/**',
    'lang/**',
    'templates/*.php',
    'lib/*.php',
    '!*.json',
    '!*.md',
    '!*.xml',
    '!assets',
    '!bower_components',
    '!node_modules',
  ];
  // using base = '.' will transfer everything to /public_html correctly
  // turn off buffering in gulp.src for best performance
  return gulp.src( globs, { base: '.', buffer: false } )
    .pipe( conn.newer( '/public_html/web/app/themes/sage' ) ) // only upload newer files
    .pipe( conn.dest( '/public_html/web/app/themes/sage' ) );
});

Finally, add the upload task to the build sequence task:

// ### Build
gulp.task('build', function(callback) {
  runSequence('styles',
              'scripts',
              ['fonts', 'images', 'svg'],
              'upload',
              callback);
}); 

This should work now!
Happy :smile:

2 Likes