【问题标题】:Gruntjs watch different folders and execute tasksGruntjs 监视不同的文件夹并执行任务
【发布时间】:2012-11-29 18:28:27
【问题描述】:

我想知道是否可以配置一个监视任务来监视两个不同的文件夹并在每个文件夹上执行不同的任务。例如,每当 /folder1 发生变化时,就应该执行 task1,每当 /folder2 发生变化时,就应该执行 task2。

文件夹结构如下: 根 |-文件夹1 |-文件夹2

【问题讨论】:

    标签: javascript node.js watch gruntjs


    【解决方案1】:

    观看就像一个多任务,所以是的,你可以配置它来观看不同的文件集并执行不同的任务

    watch:{
      set1: {
        files: [ 'folder1/**/*' ],  //<- this watch all files (even sub-folders)
        tasks: ['task1']
      },
      set2: {
        files: ['folder2/**/*'],
        tasks: ['task2']
      }
    },
    

    然后你可以运行一个监视任务或同时运行两个

    grunt.registerTask('watchSet1', ['watch:set1']);
    grunt.registerTask('watchSet1And2', ['watch:set1', 'watch:set2']);      
    

    尚未测试,但应该可以。

    【讨论】:

    • 那么 watchSet1and2 会在两个任务都完成后执行?
    • 不,这是关于如何运行这两个任务的示例。你可以从命令行调用grunt watchSet1And2,两个手表都会启动。
    • 谢谢,工作得很好。我还注意到只运行监视任务(grunt watch)是可以的。如果文件在任何被监视的文件夹中被修改,它将运行正确的任务。
    • 我在这里对这个问题进行了很好的跟进:stackoverflow.com/questions/15691804/…
    • 这会导致一些问题......在我的情况下,watch:set1 无限期运行,而 watch:set2 永远不会运行......正确的答案可以在这里找到:stackoverflow.com/questions/17585385/…
    【解决方案2】:

    最好且唯一可行的解​​决方案是:https://npmjs.org/package/grunt-focus 添加此插件,然后:

    focus: {
                sources: {
                    include: ['js', 'html', 'css', 'grunt']
                },
                testu: {
                    include: ['js', 'html', 'css', 'testu', 'grunt']
                },
                testi: {
                    include: ['js', 'html', 'css', 'testu', 'testi', 'grunt']
                }
            },
            watch: {
                js: {
                    files: paths.js,
                    tasks: ['jshint'],
                    options: {
                        livereload: true
                    }
                },
                html: {
                    files: paths.html,
                    options: {
                        livereload: true
                    }
                },
                css: {
                    files: paths.css,
                    tasks: ['csslint'],
                    options: {
                        livereload: true
                    }
                },
                testu: {
                    files: ['test/**/*.js', 'test/**/*.css'],
                    tasks: ['mochaTest'],
                    options: {}
                },
                testi: {
                    files: ['test/**/*.js', 'test/**/*.css'],
                    tasks: ['exec:cleanTestDB', 'protractor_webdriver', 'protractor'],
                    options: {}
                },
                grunt: {
                    files: ['Gruntfile.js', 'server/config/env/*.js'],
                    options: {
                        reload: true
                    }
                }
            }
    

    然后您可以使用 focus:sources 或 focus:testu 来方便。

    JM.

    【讨论】:

      【解决方案3】:

      如果您希望监视任务同时运行。 RobW 这里有一个很好的解决方案How to run two grunt watch tasks simultaneously

      我花了一些时间找到解决方案,所以这是该解决方案的 sn-p。

      在自定义任务中动态编写配置对象是可行的。

      grunt.registerTask('watch:test', function() {
        // Configuration for watch:test tasks.
        var config = {
          options: {
            interrupt: true
          },
          unit: {
            files: [
              'test/unit/**/*.spec.coffee'
            ],
            tasks: ['karma:unit']
          },
          integration: {
            files: [
              'test/integration/**/*.rb',
              '.tmp/scripts/**/*.js'
            ],
            tasks: ['exec:rspec']
          }
        };
      
        grunt.config('watch', config);
        grunt.task.run('watch');
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-22
        • 1970-01-01
        • 2018-02-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多