DieserJonas
DieserJonas

Reputation: 149

Copy files with gulp while preserving modification time

Situation:

I currently have a little gulp task that prepares my project files to be deployed via FTP. It's nothing fancy, but this simple project doesn't need more, at this time.

The whole deploy task has a "sub task" which simply grabs a list of paths, and copies all files to a __deploy directory:

/*
    Path: Folder path for a prepared deploy
 */
var deployBasePath = '__deploy/';  


/*
    Paths: Include all paths into deploy and excluded unneeded files. 
 */    
var deployIncludePaths = [
    '**/*',
    '.htaccess',
    '!{__deploy,__deploy/**}',
    '!{.git,.git/**}',
    '!assets/{js-sources,js-sources/**}',
    '!assets/{scss,scss/**}',
    '!assets/{vendor,vendor/**}',
    '!{node_modules,node_modules/**}',
    '!{panel,panel/**}',
    '!thumbs/**',
    '!.bowerrc',
    '!.gitignore',
    '!.gitmodules',
    '!bower.json',
    '!composer.{json,lock}',
    '!gulpfile.js',
    '!package.json',
    '!readme.md'
];


gulp.task('deploy-copy', ['deploy-cleanup', 'css', 'js'], function() {
    return gulp.src(deployIncludePaths)
        .pipe(gulp.dest(deployBasePath));
});

This will copy the project files to a __deploy directory and exclude all the gulp, bower, composer config files as well as SCSS and JS sources. The deploy-cleanup task it's calling simply cleans out the deploy directory.


Problem:

There is a part of the projects code that uses file modification dates to create sitemaps, etc. Unfortunately, this behavior cannot be changed.

Therefor it would be helpful, if there was a way to copy the files just like the above task does, however while preserving all files modification dates (or at least files from a specified directory).

Is there a way to accomplish this?

Upvotes: 9

Views: 1878

Answers (2)

Richard
Richard

Reputation: 30618

As I needed this too, but could not find anything suitable, I've written the gulp-preservetime plugin to do it.

Example:

var gulp = require('gulp');
var preservetime = require('gulp-preservetime');

gulp.task('default', function() {
    gulp.src('./src/**/*')
        .pipe(gulp.dest('./dest'))
        .pipe(preservetime());
});

Upvotes: 12

codevision
codevision

Reputation: 5520

You could use gulp-newer for that.

Here the example from it:

var gulp = require('gulp');
var newer = require('gulp-newer');
var imagemin = require('gulp-imagemin');

var imgSrc = 'src/img/**';
var imgDest = 'build/img';

// Minify any new images 
gulp.task('images', function() {

  // Add the newer pipe to pass through newer images only 
  return gulp.src(imgSrc)
      .pipe(newer(imgDest))
      .pipe(imagemin())
      .pipe(gulp.dest(imgDest));

});

gulp.task('default', function() {
  gulp.watch(imgSrc, ['images']);
});

Upvotes: 2

Related Questions