我在开发时如何在Cakefile中观看/复制文件?

时间:2012-12-11 12:15:54

标签: node.js

目前我已设置cake任务来观察和编译CoffeeScript,Jade,Stylus,如:

task "startdev", "Starts server with nodemon and watch files for changes", ->
    # start nodemon server
    nodemon = spawn procExec("nodemon"), ["server.coffee"]
    processOutput nodemon

    # watch and compile CoffeeScript
    coffee = spawn procExec("coffee"), ["-o", "public/js/app", "-cw", "client/coffee"]
    processOutput coffee

    # watch and compile Stylus
    stylus = spawn procExec("stylus"), ["client/stylus/styles.styl", "-I", "public/css/","-l", "-w", "-u", "./node_modules/nib", "-o", "public/css/app"]
    processOutput stylus

    # watch and compile jade
    jade = spawn procExec("jade"), ["client/jade/index.jade", "--out", "public"]
    processOutput jade

现在,我想要查看文件夹中的文件更改并将其复制到另一个文件夹(也称为同步文件夹,将文件从src复制到public)。我怎么能这样做?我认为如果解决方案不是特定于Node / JS,那就好了,只要我不必下载一大堆设置就可以使它工作。

经过一些研究,或许我应该使用类似jake的构建?但是如何使用它来同步2个文件夹

1 个答案:

答案 0 :(得分:0)

您可以使用手表执行此操作:

watch = require 'watch'

task 'watch', 'watches for file and copy/compiles them', ->
  # watch file changes
  watch.watchTree 'src', (f, curr, prev) ->
    return if typeof f is "object" && prev is null && curr is null
    return if f.match(/\.(coffee)$/)
    dest = 'lib' + f[3..]
    if curr.nlink is 0
      fs.unlinkSync dest if fs.existsSync dest
      log "removed " + dest
    else 
      oldFile = fs.createReadStream f
      newFile = fs.createWriteStream dest
      oldFile.pipe newFile
      log "copied " + f + ' to ' + dest
  log 'Watching to copy files', green  
  # watch_coffee
  ...