Compile gulp to temp folder then move to dist?

gulp
sage8

#1

I have a live site built with Sage 8.4.2 that I need to run gulp --production on but don’t want the site to be without it’s assets while it’s building. How might I go about creating a temp folder like /dist-<date()> and when it’s done, replace the contents of /dist?


#2

related: this comment from a couple years ago – see: “3. Your original idea”

edit: I assume you’ll test gulp --production in a different environment first!


#3

Thanks @mikespainhower, but that doesn’t address the bulk of my question which is how to create a temp-dist folder, compile into it and then replace the contents of /dist with the contents of /temp-dist and then delete /temp-dist.


#4

I ended up solving this by adding new vars to manifest.paths, altering a couple tasks and creating a new one.

// first require some file system tools
var del = require('del');
var fs = require('fs');

// then save the original dist location and replace original
// if production build, create a {dist}-temp folder to compile into
if ( argv.production ) {
  manifest.paths.dist_prod = manifest.paths.dist;
  manifest.paths.dist = manifest.paths.dist.replace(/\/$/, "") + "-" + (new Date()).getTime() + "/";
}

// prevent the clean task from deleting dist if doing a --production build
gulp.task('clean', function() {
  if ( !argv.production ) {
    return del([path.dist]);
  }
});

// create new task called publish and add it to the build task
gulp.task('build', function(callback) {
  runSequence('styles',
              'templates',
              'scripts',
              ['fonts', 'images'],
              'publish',
              callback);
});

gulp.task('publish', function() {
  if ( argv.production ) {
    // new files are ready, rename existing `/dist` to `/dist-old`
    fs.rename(path.dist_prod, path.dist_prod.replace(/\/$/, "") + "-old/", function(err) {
      // if error is anything but 'missing' halt, otherwise keep going
      if (err && err.code !== 'ENOENT') {
        throw err;
      } else {
        // rename our `/dist-[temp]` to `/dist`
        fs.rename(path.dist, path.dist_prod, function(err) {
          // any error here is bad
          if (err) {
            throw err;
          }
          // delete `/dist-old` if exists or fail silently
          del([path.dist_prod.replace(/\/$/, "") + "-old/"]);
        });
      }
    });
  }
});

That’s pretty much it. ymmv if you already have a customized gulpfile.js