Use Grunt newer with custom task

I am trying to use grunt-newer to view files from a folder and, if any, launches a custom task.

I have something similar in my Gruntfile.js:

grunt.initConfig({
   watch: {
      widgets: {
         files: "/somepath/*.js",
         tasks: ['newer:mycustomtask']
      }
   }
});

grunt.registerTask("mycustomtask", ["description of my task"], function() {
  console.log("me has been triggered");
});

Whenever I run "grunt watch", I have this output:

Running "watch" task
Waiting...
File "/somepath/WidgetA.js" changed.
Running "newer:mycustomtask" (newer) task
Fatal error: The "newer" prefix is not supported for aliases

I googled but found nothing about it. Does anyone know how I can implement this? I need now in my "customtask" whose files have been modified

+4
source share
3 answers

( , ), , , .

, .

+1

, , , . , :

Gruntfile.js
package.json
src/
    config.js
    data.js
tasks/
    customtask.js

src , watch, tasks/customtask.js. :

var fs = require('fs');
var path = require('path');

module.exports = function(grunt) {
    grunt.registerMultiTask('customtask', function() {
        var done = this.async();

        if(!this.files){ done(); return; }

        this.files[0].src.forEach(file_name => {
            console.log(file_name);
        });

        done();
    });
};

Gruntfile.js :

module.exports = function(grunt) {

    const files = ['src/config.js', 'src/data.js'];

    grunt.initConfig({

        pkg: grunt.file.readJSON('package.json'),

        customtask: {
            release: {
                src: files
            }
        },

        watch: {
            data: {
                files: files,
                tasks: ['customtask:release']
            },
            options: {
                spawn: false
            }
        }

    });

    grunt.loadTasks('tasks');
    grunt.loadNpmTasks('grunt-contrib-watch');

    var changedFiles = Object.create(null);
    var onChange = grunt.util._.debounce(function() {
        grunt.config('customtask.release.src', Object.keys(changedFiles));
        changedFiles = Object.create(null);
    }, 200);
    grunt.event.on('watch', function(action, filepath) {
        changedFiles[filepath] = action;
        onChange();
    });

    grunt.registerTask('build', ['watch:data']);
};

, , :

  • ['src/config.js', 'src/data.js']
  • customtask ( , )
  • watch customtask:release , -
  • grunt.loadTasks('tasks') " " tasks, .. customtask
  • grunt.registerTask('build', ['watch:data']) "" watch:data

, customtask , , " ". , src customtask .

grunt build "". , touch src/*.js, :

Running "watch:data" (watch) task
Waiting...
>> File "src/config.js" changed.
>> File "src/data.js" changed.

Running "customtask:release" (customtask) task
src/config.js
src/data.js

customtask...

0

You just need to have a configuration entry (even empty) for your task:

grunt.initConfig({

  mycustomtask: {
  },

  watch: {
    widgets: {
      files: "/somepath/*.js",
      tasks: ['newer:mycustomtask']
    }
  }
});
-1
source

Source: https://habr.com/ru/post/1607809/


All Articles