1

目前我已经设置了一个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

现在,我想监视文件夹中的文件更改并将其复制到另一个文件夹(又名同步文件夹,将文件从复制srcpublic)。我该怎么做?我认为如果解决方案不是特定于 Node/JS 的,我认为这很好,只要我不必下载一大堆和大量的设置来使其工作。

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

4

1 回答 1

0

您可以使用 watch 来执行此操作:

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
  ...
于 2013-08-08T20:14:06.827 回答