CoffeeScript编译器API
我正在使用CoffeeScript(编写一个Cakefile)。 我想编译一些其他的CoffeeScript文件,la
coffee -o lib -c src
我可以在subprocess中启动上述命令,但是这种方法存在跨平台的问题,使得error handling变得困难。 我宁愿使用一个API。
我很乐意使用command.coffee的确切函数,但是我无法解决这个问题。
附录:我看到require('coffee-script').compile
,编译一个string到另一个string。 这仍然让我做循环的文件和子文件夹和写输出的繁重的工作。
您正在寻找的API在coffee-script.coffee中 。 它导出一个compile
函数,它能够完成它在锡上所说的内容。
要直接使用command.coffee的run
函数,你必须首先用你在命令行上传递的选项来覆盖process.argv
。
只需使用节点的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."
还要看看coffeescript自己的Cakefile(使用subprocess): https : //github.com/jashkenas/coffee-script/blob/master/Cakefile
感谢乔丹和莱纳斯我写道:
command = require('iced-coffee-script/lib/coffee-script/command') process.argv[2..]=['-o','lib','-c','src'] command.run()
突出的问题: run
函数返回提前,没有callback报告错误:\