简体   繁体   English

将Grunt更新用于自定义任务

[英]Use Grunt newer with custom task

I'm trying to use grunt-newer to watch files from a folder and if any is changed, trigger a custom task. 我正在尝试使用grunt-newer来查看文件夹中的文件,如果有任何更改,则触发自定义任务。

I have something like this in my Gruntfile.js: 我的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: 每当我运行“grunt watch”时,我都会输出:

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 didn't found anything about this. 我用Google搜索但没有发现任何相关信息。 Anyone knows how could I implement this? 任何人都知道我怎么能实现这个? I need to now in my "customtask" which files have been changed 我现在需要在我的“customtask”中更改了哪些文件

If you reference a task (inside watch or concurrent eg) which is either not installed or not configured you get this error output. 如果您引用未安装或未配置的任务(内部监视或并发),则会收到此错误输出。

This happens often when you copy-paste a watch config from a different project. 当您从其他项目复制粘贴监视配置时,通常会发生这种情况。

I came across a similar requirement and the solution I ended up with is roughly as follows. 我遇到了类似的要求,我最终得到的解决方案大致如下。 Let's assume that the project structure is: 我们假设项目结构是:

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

Here, the src directory contains data which will be monitored by watch , while the definition of the custom task is stored in tasks/customtask.js . 这里, src目录包含将由watch监视的数据,而自定义任务的定义存储在tasks/customtask.js For the purpose of this example, this task will only print the file names of the changed files: 出于此示例的目的,此任务仅打印已更改文件的文件名:

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();
    });
};

Now, Gruntfile.js looks like: 现在, 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']);
};

here, it specifies that: 在这里,它指定:

  1. the files of interest are ['src/config.js', 'src/data.js'] 感兴趣的文件是['src/config.js', 'src/data.js']
  2. that our customtask operates in principle on these files (in case it would be invoked directly) 我们的customtask原则上对这些文件进行操作(如果它将被直接调用)
  3. that watch is supposed to observe these files and launch customtask:release whenever something changes watch应该观察这些文件并启动customtask:release只要有变化就会customtask:release
  4. grunt.loadTasks('tasks') loads all "tasks definitions" from the directory tasks , ie, here only the customtask grunt.loadTasks('tasks')加载所有从目录“任务定义” tasks ,即,这里只customtask
  5. grunt.registerTask('build', ['watch:data']) defines a "shortcut" for watch:data grunt.registerTask('build', ['watch:data'])定义了watch:data的“快捷方式” watch:data

Finally, in order to invoke customtask only for the changed files, this example uses the strategy employed in the documentation in the section "Compiling files as needed". 最后,为了仅为已更改的文件调用customtask ,此示例使用“根据需要编译文件”一节中的文档中使用的策略。 In loose terms, it assembles all changed files in an object the keys of which are then used to modify the src property of the customtask on-the-fly. 在松散的术语,它装配在一个对象然后的键被用来修改所有改变的文件src所述的属性customtask上即时。

Running grunt build then initiates the "watch". 运行grunt build然后启动“watch”。 If one runs in another terminal window for example touch src/*.js , the output is: 如果在另一个终端窗口中运行,例如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

where the last two lines come from customtask ... 最后两行来自customtask ...

You just need to have a config entry (even an empty one) for your task: 您只需要为您的任务配置一个配置条目(即使是空条目):

grunt.initConfig({

  mycustomtask: {
  },

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM