目前我已经设置了一个 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 个文件夹
最佳答案
您可以使用 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
...
关于node.js - 在开发过程中,我如何查看/复制 Cakefile 中的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13820236/