监控 grunt-contrib-watch 的子任务

4
我有以下的Gruntfile.coffee文件。如下所示,我正在监视watch任务以查看文件更改,然后将更改后的文件编译为CoffeeScript。
# Watch task
watch:
 coffee:
  files: ['client/**/*.coffee','server/**/*/.coffee']
  options:
   nospawn: true
   livereload: true

# Watch changed files
grunt.event.on 'watch', (action, filepath) ->
 cwd = 'client/'
 filepath = filepath.replace(cwd,'')
 grunt.config.set('coffee',
  changed:
   expand: true
   cwd: cwd
   src: filepath
   dest: 'client-dist/'
   ext: '.js'
 )
 grunt.task.run('coffee:changed')

然而,我想添加另一个监视任务来复制非咖啡文件。我该如何监视这些更改?

我考虑执行:

# Watch copy task
grunt.event.on 'watch:copy', (action,filepath) -> ...
# Watch coffee task
grunt.event.on 'watch:coffee', (action,filepath) -> ...

但似乎这并不起作用。有什么想法吗?
2个回答

2

我的解决方案-能够完成任务但不够完美。欢迎更好的答案。

基本上,我匹配传入文件的路径
如果是 .coffee 文件,则运行一个 coffee 编译任务
如果是 .* 文件,则运行一个复制任务

# Watch changed files
grunt.event.on 'watch', (action, filepath) ->

 # Determine server or client folder
 path = if filepath.indexOf('client') isnt -1 then 'client' else 'server'
 cwd = "#{path}/"
 filepath = filepath.replace(cwd,'')        

 # Minimatch for coffee files
 if minimatch filepath, '**/*.coffee'
  # Compile changed file
  grunt.config.set('coffee',
   changed:
    expand: true
    cwd: cwd
    src: filepath
    dest: "#{path}-dist/"
    ext: '.js'
  )
  grunt.task.run('coffee:changed')  

 # Minimatch for all others
 if minimatch filepath, '**/*.!(coffee)'
  # Copy changed file
  grunt.config.set('copy',
   changed:
    files: [
     expand: true
     cwd: cwd
     src: filepath
     dest: "#{path}-dist/"                      
    ]
  )
  grunt.task.run("copy:changed")

1

1
我只想要更改的文件...监视任务在这些文件夹中的所有文件上运行编译。 - imrane
然后继续在watch事件中使用grunt.config.set()。只是不要在watch事件中使用grunt.task.run()。这就是tasks的作用。 - Kyle Robinson Young
我想在过滤掉已更改的文件后运行任务...那我该怎么做呢? - imrane

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接