繁体   English   中英

Gruntjs观看不同的文件夹并执行任务

[英]Gruntjs watch different folders and execute tasks

我想知道是否可以配置监视任务来观察两个不同的文件夹并在每个文件夹上执行不同的任务。 例如,每当更改/ folder1时,都应该执行task1,只要在/ folder2中更改了某些内容,就应该执行task2。

文件夹结构具有以下形式:root | -folder1 | -folder2

Watch的行为类似于多任务,因此您可以将其配置为观察不同的文件集并执行不同的任务

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']);      

没有测试但它应该工作。

如果您希望监视任务同时运行。 RobW在这里有一个很好的解决方案如何同时运行两个grunt监视任务

我花了一些时间来解决问题,所以这里是该解决方案的片段。

在自定义任务中动态编写配置对象有效。

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

有最好和唯一可行的解​​决方案: 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
                }
            }
        }

然后你使用焦点:来源或焦点:testu作为您的方便。

JM。

暂无
暂无

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

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