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.

Answers


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.


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
}


Need Your Help

block third party cookies - workaround (facebook apps etc)

javascript facebook actionscript-3 safari local-shared-object

Safari on a Mac has a Block cookies set to From third parties and advertisers by default.

Can I use SharePoint Online user in SharePoint OnPremise?

sharepoint sharepoint-2010 sharepoint-2013 sharepoint-online

I have a requirement to make a Single Sign-On between the SharePoint Online and SharePoint on-Premise. I have read some articles on google which shows the process of synching the On-Premise users t...