Currently I have setup a cake task to watch and compile CoffeeScript, Jade, Stylus like:
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
Now, I want to watch for file changes in a folder and copy it to another folder (aka sync folders, copy files from src to public). How might I do that? I think its fine if the solution is not specific to Node/JS, as long as I dont have to download a whole bunch and lots of setup to make it work.
After some research, perhaps I should use a build too like jake? But how do I use it to sync 2 folders
You may use watch to do this:
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
...