5

I'm using coffeescript with --watch option to rebuild javascript on changes to .coffee files.

Is it safe to combine that with node-supervisor to restart Node on changes to the compiled javascript?

I'm worried it won't be robust because of atomicity when coffeescript is recompiling multiple files. node-supervisor could jump the gun and restart Node on detecting the first filesystem change. Is it robust enough to realize there were additional changes while it was busy restarting Node?

Is there a better way? Ideally I'd have only one filesystem watcher recompile my coffeescript and restart node.

Nils
  • 5,612
  • 4
  • 34
  • 37
  • How do you watch coffee files recursively? perhaps you could give me ahand with this question: http://stackoverflow.com/questions/15470334/autocompile-coffee-files-and-reload-project – opensas Mar 18 '13 at 05:51

5 Answers5

7

Create a JavaScript launcher, i.e. run.js, like this:

require('coffee-script');
require('./launch');

Then run this file with supervisor and appropriate options:

supervisor -e "node|js|coffee" run.js

This worked well for me on Windows.

Lemming
  • 834
  • 1
  • 7
  • 16
  • No need to create separate js file. You can specify executable with `-x` option. See [my answer](http://stackoverflow.com/a/18373394/93988). – Ivan Nevostruev Aug 22 '13 at 06:42
6

You can use nodemon, it even has a delay feature (to restart server after a number of seconds have passed), for example:

nodemon --debug ./server.coffee 80

Another good feature of nodemon is ignoring files, example:

# this is my ignore file with a nice comment at the top

/vendor/*     # ignore all external submodules
/public/*     # static files
./README.md   # a specific file
*.css         # ignore any CSS files too

Other than that, read the documentation on the github repo and watch this Nodetuts video about nodemon: http://nodetuts.com/tutorials/14-some-nodejs-tools.html

alessioalex
  • 62,577
  • 16
  • 155
  • 122
  • Thanks, I wasn't aware of the delay function. I'll play around with this. – Nils Dec 24 '11 at 00:43
  • Note that by default nodemon ignores .coffee files exceptiong specified app file. So if you don't precompile your .coffee files, you will have a problem. I solved it by adding .nodemonignore file to project root, it rewrites default rules and nodemon started to watch all required .coffee files – Nayjest Jan 04 '13 at 14:16
  • I had to prepend --nodejs before --debug to get this to work with coffeescript: nodemon --nodejs --debug ./server.coffee – cbaigorri Mar 26 '14 at 15:59
4

You can use supervisor with -x option set to coffee. This will enable it to run script with right executable:

supervisor -x coffee your-script.coffee

Inspired by Lemming's answer.

Community
  • 1
  • 1
Ivan Nevostruev
  • 28,143
  • 8
  • 66
  • 82
1

In some of my Cakefiles, such as the one for connect-assets, I do the watching myself and simply spawn coffee -co lib src each time something changes, then restart the server when that child process finishes. That gets around the atomicity issue. If every .coffee file changes at once (or if you upgrade the coffee runtime), all of the JS files will update at once as well.

Trevor Burnham
  • 76,828
  • 33
  • 160
  • 196
  • Nice! I don't even fully understand your cakefile yet. But that looks like a good idea. – Nils Dec 24 '11 at 00:37
0

My foreman centric solution looks like this:

Procfile.dev

web: ./node_modules/supervisor/lib/cli-wrapper.js -n exit server.js
watch: ./node_modules/iced-coffee-script/bin/coffee --watch --compile server.iced

and then merely foreman start -f Procfile.dev

Then gitignore the resulting .js files. I like this approach because it keeps a constantly-updating vanilla JS file alongside my .iced files, so I can doublecheck my work as I go (I definitely make mistakes in coffeescript that I might not in vanilla).

Vincent Woo
  • 2,650
  • 1
  • 20
  • 21