# Compile gulp to temp folder then move to dist?

**URL:** https://discourse.roots.io/t/compile-gulp-to-temp-folder-then-move-to-dist/10185
**Category:** sage
**Tags:** sage8, gulp
**Created:** 2017-08-10T00:28:51Z
**Posts:** 4

## Post 1 by @stranskydesign — 2017-08-10T00:28:52Z

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`?

---

## Post 2 by @mikespainhower — 2017-08-10T02:42:54Z

related: [this comment from a couple years ago](https://discourse.roots.io/t/sage-and-augmenting-my-workflow/2965/3) – see: “3. Your original idea”

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

---

## Post 3 by @stranskydesign — 2017-08-10T15:54:58Z

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.

---

## Post 4 by @stranskydesign — 2017-10-05T23:00:17Z

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`
