22

I'm looking for simplest possible way to automatically recompile coffee scripts into JS.

Reading documentation but still having troubles to get exactly what I want.

I need it to watch folder src/ for any *.coffee files modifications and compile them into concatenated javascript file into lib/something.js.

Somehow can't combine watching, compiling and concatenating together. :/

Arnis Lapsa
  • 45,880
  • 29
  • 115
  • 195
  • - https://github.com/jashkenas/coffee-script/issues/1075 - https://github.com/jashkenas/coffee-script/issues/1076 – matyr Feb 21 '11 at 05:18

11 Answers11

21

The coffee script documentation provides an example for this:

Watch a file for changes, and recompile it every time the file is saved:

coffee --watch --compile experimental.coffee

If you have a particular script you want to execute, you could use the linux command dnotify: http://linux.die.net/man/1/dnotify

dnotify --all src/ --execute=command

Edit: I had some problems with the --execute part of dnotify - might be a bug, but this is what I got working:

dnotify --all . -e `coffee -o lib/ --join --compile *.coffee`

That executed the compile command each time a file was modified.

If you append the command with an ampersand, like this:

dnotify --all . -e `coffee -o lib/ --join --compile *.coffee` &

it will be started in a separate process. To get the process ID, you can use this:

ps ux | awk '/dnotify/ && !/awk/ {print $2}'

And then, you can kill the process by using something like this:

kill `ps ux | awk '/dnotify/ && !/awk/ {print $2}'`

But if that's your objective (to kill by process name), you can do it in a simpler way by using:

killall dnotify
arnorhs
  • 10,383
  • 2
  • 35
  • 38
  • Yap. Managed to compile 1 file. Have problems with multiple. I guess `coffee -o lib/ --join --compile src/*.coffee` + dnotify would do the trick. – Arnis Lapsa Feb 21 '11 at 00:06
  • I think it should, but can't figure it out. :) – Arnis Lapsa Feb 21 '11 at 00:23
  • I edited my question to include the command. I see you also added your own answer, which probably works as well :) – arnorhs Feb 21 '11 at 00:37
  • @arnorhs yeah. problem solved. I'm really unfamiliar with all this including linux in general. thanks for helping. – Arnis Lapsa Feb 21 '11 at 00:44
  • I've never tried coffee script before, it looks interesting. So thanks for introducing me to it :) – arnorhs Feb 21 '11 at 00:52
  • @amorhs still got one problem - dnotify `--background` switch doesn't want to work. nothing happens when I try to add it. – Arnis Lapsa Feb 21 '11 at 00:53
  • Is there an equivalent to dnotify for Mac OS X? – Philip Walton Feb 07 '12 at 02:40
  • Look at http://stackoverflow.com/questions/1515730/is-there-a-command-like-watch-or-inotifywait-on-the-mac for examples of how to configure mac os x to do something similar to dnotify: http://stackoverflow.com/questions/1515730/is-there-a-command-like-watch-or-inotifywait-on-the-mac – arnorhs Dec 28 '13 at 16:25
7

Try jitter

https://github.com/TrevorBurnham/jitter

It watches a directory of .coffee files, and when it detects that a file has changed it automatically recompiles it to .js

jitter /path/to/coffee/dir /path/to/js/dir

I've been trying it out with a project using coffescript and sencha touch, it seems to work pretty well. Doesn't take care of the concatenation problem, but it's really simple to use for someone who just needs auto-compilation.

Steve
  • 71
  • 1
  • 3
4

This helped me (-o output directory, -j join to project.js, -cw compile and watch coffeescript directory in full depth):

coffee -o web/js -j project.js -cw coffeescript
Ikrom
  • 4,785
  • 5
  • 52
  • 76
3

Well coffee --watch has 2 major flaws:

  • New files created after command has been issued aren't being watched
  • Requires manual initiation so there can be a chance you forget to do it, which doesn't sound more brilliant than forget to compile before you git commit it

The solution I came up with is a rather simple Bash script that takes coffee --watch a few steps further which will allow your working directory tree to be watched ever since system login, and automatically get compiled into JavaScript on each file save/change or new file creation:

http://blog.gantrithor.com/post/11609373640/carefree-coffeescript-auto-compiler

There may be more elegant way to do this, but this implementation works great =)

gsklee
  • 4,774
  • 4
  • 39
  • 55
2

Being one level above /src, this will work for all .coffee files found no matter the depth.

coffee -w -c src/
zrooda
  • 3,610
  • 1
  • 30
  • 38
  • I've run into situations where if node_modules is contained in the same directory, I'll run into this error: https://github.com/jashkenas/coffee-script/issues/2004 – Casey Flynn Mar 16 '13 at 03:39
  • That's related to watching too many files, I don't see how that makes my reply wrong. – zrooda Mar 20 '13 at 09:51
2

I have found the command-line coffeescript compiler to be poorly suited for complex project structures.

If you and your build process are as needy as I am, check out Grunt - http://gruntjs.com/

It allows for highly complex build processes - for example, you might

  1. concatenate coffee to new file(s)
  2. compile
  3. concatenate some additional JS
  4. minify

Tasks can be strung together, and watched files/folders are highly customizable as well.

Vikdor
  • 23,934
  • 10
  • 61
  • 84
ckpcw
  • 106
  • 2
2
find -type f | grep .coffee | xargs ls -t | head -n 1 | xargs coffee -cw

find last modifed coffee script and put it in compile-watch mode

Dino Reic
  • 3,586
  • 3
  • 22
  • 11
1
find `pwd` | grep .coffee | xargs coffee -w -c

try this one in root directory of the application

AAA
  • 63
  • 1
  • 5
1

The short answer to your question is that the coffee utility wasn't designed for this; combining file-watching and concatenation is actually pretty complex. Expect more sophisticated build tools for CoffeeScript in the near future; until then, you might want to do your project's builds by writing a Cakefile or Ruby Watchr script. Then you can also throw in minification, documentation-generation, and whatever else you need for your particular project (not to mention guaranteeing a particular concatenation order).

Trevor Burnham
  • 76,828
  • 33
  • 160
  • 196
0

nodemon -x coffee server.coffee does it for me.

Install nodemon using npm install -g nodemon

Matej
  • 9,548
  • 8
  • 49
  • 66
0

Changed mind about concatenation.
Created small compiler.sh file which contains:

dnotify -M src/ -e coffee -o lib/ -c src/ &
dnotify -M spec/ -e coffee -o lib/ -c spec/ &

Kind a suits my needs.

Arnis Lapsa
  • 45,880
  • 29
  • 115
  • 195
  • nice. Don't you have to append an & to those commands, so that they'll both get executed? The way it's set up, only the first one will get executed and the script will wait till the command quits. It might also be a good idea to grab the process IDs so you'll be able to stop the processes later... – arnorhs Feb 21 '11 at 00:54
  • @arnorhs another newbie question - how to grab pid? :) – Arnis Lapsa Feb 21 '11 at 01:05
  • 1
    You could do it like this: ps ux | awk '/dnotify/ && !/awk/ {print $2}' this will display the process ID for all dnotify processes running ont he machine.. so you can call: kill `ps ux | awk '/dnotify/ && !/awk/ {print $2}'` – arnorhs Feb 21 '11 at 01:13