autocompile .coffee files and reload project

2019-05-28 10:05发布

问题:

I have a small web service developed in node, and I'd like to code some files in coffeescript, to start playing with it.

I'm using nodemon to run the app, like this

nodemon app.js 

And I have the following file structure

app.js controllers/ ping.coffee test.js

In nodemon's homepage it says that it supports coffeescript, but I change the file and it won't get reloaded.

Then I found this article, so I tried with

$ coffee --watch --compile ./controllers/*.coffee

And it works fine, but if I try with

$ coffee --watch --compile ./*.coffee 
File not found: ./*.coffee

So it seems like the watch option is not recursive.

Any idea how can I make nodemon pick coffeescript files changes, or have the coffee compiler pick files recursively?

回答1:

This should work:

coffee --watch --compile ./

Also, you can shorten the flags to -wc.



回答2:

Nodemon will properly watch the coffeescript files if you explicitly specify their extension with -e js,coffee. This is counter to what the docs state and I've filed a ticket on this issue here: https://github.com/remy/nodemon/issues/312



回答3:

What you're doing is pretty common in coffeescript libraries. Many libraries have a script to compile all coffeescript files in one directory to Javascript files in another. For example, the following Cakefile compiles from src/ to lib/. You can cake watch or cake build depending on what you want to do.

{print} = require 'util'
{spawn} = require 'child_process'

task 'build', 'Build lib/ from src/', ->
  coffee = spawn 'coffee', ['-c', '-o', 'lib', 'src']
  coffee.stderr.on 'data', (data) ->
    process.stderr.write data.toString()
  coffee.stdout.on 'data', (data) ->
    print data.toString()
  coffee.on 'exit', (code) ->
    callback?() if code is 0

task 'watch', 'Watch src/ for changes', ->
  coffee_src = spawn 'coffee', ['-w', '-c', '-o', 'lib', 'src']
  coffee_src.stderr.on 'data', (data) -> process.stderr.write data.toString()
  coffee_src.stdout.on 'data', (data) -> print data.toString()

However, if you're not running node but actually running a browser app, I would suggest using the fantastic hem. For that, I've also written a getting-started guide on here: https://stackoverflow.com/a/14993583/586086