Pramodh
Pramodh

Reputation: 186

How to use gulp-newer?

I'm new to gulp and I tried to follow the documentation in https://www.npmjs.com/package/gulp-newer to understand how it works. However its not working as expected for the below task. I think I'm missing something obvious.

Here's the folder structure,

    temp
      file1.js
      file2.js
    new
      file1.js
      file3.js
    change
      <empty initially>

I want to compare temp folder with new folder and if there are any new files in new folder(which was not present in temp earlier) then move those files to change folder. This is just me trying to understand how gulp-newer works. Am I doing it right?

    gulp.task('newer', function() {
        return gulp.src('temp/*.js')
                .pipe(newer('new/*.js'))
                .pipe(gulp.dest('change')) 
        });

However when I run this task it just copy all the files in temp folder to change folder. So after task run change folder has file1.js and file2.js. I'm expecting just file3.js to be present in change(since that's a new file). Correct me if my understanding with the approach is incorrect.

Upvotes: 2

Views: 2402

Answers (2)

dman
dman

Reputation: 11064

May I also suggest gulp-newy in which you can manipulate the path and filename in your own function. Then, just use the function as the callback to the newy(). This gives you complete control of the files you would like to compare.

This will allow 1:1 or many to 1 compares.

newy(function(projectDir, srcFile, absSrcFile) {
  // do whatever you want to here. 
  // construct your absolute path, change filename suffix, etc. 
  // then return /foo/bar/filename.suffix as the file to compare against
}

enter image description here

Upvotes: 1

Wilmer SH
Wilmer SH

Reputation: 1417

From gulp-newer:

Using newer with many:1 source:dest mappings Plugins like gulp-concat take many source files and generate a single destination file. In this case, the newer stream will pass through all source files if any one of them is newer than the destination file. The newer plugin is configured with the destination file path.

and the sample code:

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

// Concatenate all if any are newer 
gulp.task('concat', function() {

  // Add the newer pipe to pass through all sources if any are newer 
  return gulp.src('lib/*.js')
      .pipe(newer('dist/all.js'))
      .pipe(concat('all.js'))
      .pipe(gulp.dest('dist'));

});

it seems that you need to pass in all the files already concatenated to newer. In your case:

gulp.task('newer', function() {
        return gulp.src('temp/*.js')
                .pipe(newer('new/*.js'))
                .pipe(concat('*.js'))
                .pipe(gulp.dest('change')) 
        });

Also, since newer checks the files modified date make sure that the files are actually newer. I know it's obvious, but I'm usually stuck on "obvious" stuff.

Upvotes: 3

Related Questions