autocompile .coffee files and reload project

2019-05-28 09:53发布

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?

3条回答
霸刀☆藐视天下
2楼-- · 2019-05-28 10:11

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

查看更多
Root(大扎)
3楼-- · 2019-05-28 10:13

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

查看更多
Ridiculous、
4楼-- · 2019-05-28 10:33

This should work:

coffee --watch --compile ./

Also, you can shorten the flags to -wc.

查看更多
登录 后发表回答