CoffeeScript Compiler API

I work in CoffeeScript (writing Cakefile). I would like to compile some other CoffeeScript files, ร  la

coffee -o lib -c src 

I can run the above command in a child process, but this approach has cross-platform problems and makes error handling more difficult. I would rather use the API.

I would be happy to use the exact functions from command.coffee , but I cannot figure out how to do this.

Application: I see require('coffee-script').compile , which compiles a string into another string. It would still leave me to do the rough work of cyclizing files and subfolders and writing output.

+8
coffeescript
source share
3 answers

The API you are looking for is in the coffee-script. coffee . It exports a compile function that does what it says on the gesture.

To use command.coffee run , you must first overwrite process.argv with the parameters that you would pass on the command line.

+11
source share

Just use node fs API + coffeescript.compile :

 fs = require 'fs' coffee = require 'coffee-script' fs.readFile 'source.coffee', 'utf8', (err, data) -> compiled = coffee.compile data fs.writeFile 'source.js', compiled, (err) -> console.log "Done." 

Also take a look at Cakefile's own coffeescript file (uses child processes): https://github.com/jashkenas/coffee-script/blob/master/Cakefile

+7
source share

Thanks to Jordan and Linus, I wrote:

  command = require('iced-coffee-script/lib/coffee-script/command') process.argv[2..]=['-o','lib','-c','src'] command.run() 

Exceptional problems: the run function returns earlier, and there is no callback for the error message: \

0
source share

All Articles