[JavaScript] watch and compile, then concatenate them in CoffeeScript Cakefile

I am learning CoffeeScript.
I saw Cakefile, and learned it.
I found that it behaves like batch file.

Then I wanted it to use as these way.

1. It keeps watching src/*.coffee files
2. When they are changed, it compiles them
3. It concatenates compiled files to lib/main.js

I made below code.
Although I don’t know whether it is correct or not, it works.


{spawn} = require 'child_process'

task 'watch', 'watch src/ and concatenate them to lib/main.js', (callback) -> 
  watch = spawn 'coffee', ['-w', 'src/']
  watch.stderr.on 'data', (data) ->
    process.stderr.write data.toString()
  watch.stdout.on 'data', (data) ->
    console.log 'file changed'
    build = spawn 'coffee', ['-j', 'lib/main.js', '-cl', 'src/']
    build.stderr.on 'data', (data) ->
      process.stderr.write data.toString()
    build.on 'exit', (code) ->
      if code is 0
        console.log 'build complete'

How to use
In terminal type this

cake watch

I refered these pages. Thanks.

>> node.js – coffee script cakefile task not finishing – Stack Overflow
>> [HowTo] Compiling and Setting Up Build Tools – GitHub
>> The Little Book on CoffeeScript – Compiling
>> Cakefile(zsh) – podhmoの日記
>> CakeFileの使い方 -CoffeeScript- – プログラムdeタマゴ

Tags: ,

Comments are closed.