2010-01-29 22:51:51 -05:00
|
|
|
(function(){
|
2010-01-30 17:02:05 -05:00
|
|
|
var compiler, path;
|
2010-01-29 22:51:51 -05:00
|
|
|
// Executes the `coffee` Ruby program to convert from CoffeeScript to JavaScript.
|
2010-01-29 23:41:18 -05:00
|
|
|
path = require('path');
|
|
|
|
// The path to the CoffeeScript executable.
|
|
|
|
compiler = path.normalize(path.dirname(__filename) + '/../../bin/coffee');
|
|
|
|
// Compile a string over stdin, with global variables, for the REPL.
|
2010-01-29 22:51:51 -05:00
|
|
|
exports.compile = function compile(code, callback) {
|
|
|
|
var coffee, js;
|
|
|
|
js = '';
|
2010-01-29 23:41:18 -05:00
|
|
|
coffee = process.createChildProcess(compiler, ['--eval', '--no-wrap', '--globals']);
|
2010-01-29 22:51:51 -05:00
|
|
|
coffee.addListener('output', function(results) {
|
|
|
|
if ((typeof results !== "undefined" && results !== null)) {
|
|
|
|
return js += results;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
coffee.addListener('exit', function() {
|
|
|
|
return callback(js);
|
|
|
|
});
|
|
|
|
coffee.write(code);
|
|
|
|
return coffee.close();
|
|
|
|
};
|
2010-01-29 23:41:18 -05:00
|
|
|
// Compile a list of CoffeeScript files on disk.
|
2010-01-29 23:30:54 -05:00
|
|
|
exports.compile_files = function compile_files(paths, callback) {
|
2010-01-30 15:56:40 -05:00
|
|
|
var coffee, exit_ran, js;
|
2010-01-29 23:30:54 -05:00
|
|
|
js = '';
|
2010-01-29 23:41:18 -05:00
|
|
|
coffee = process.createChildProcess(compiler, ['--print'].concat(paths));
|
2010-01-29 23:30:54 -05:00
|
|
|
coffee.addListener('output', function(results) {
|
|
|
|
if ((typeof results !== "undefined" && results !== null)) {
|
|
|
|
return js += results;
|
|
|
|
}
|
|
|
|
});
|
2010-01-30 15:56:40 -05:00
|
|
|
// NB: we have to add a mutex to make sure it doesn't get called twice.
|
|
|
|
exit_ran = false;
|
2010-01-29 23:30:54 -05:00
|
|
|
return coffee.addListener('exit', function() {
|
2010-01-30 15:56:40 -05:00
|
|
|
if (exit_ran) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
exit_ran = true;
|
2010-01-29 23:30:54 -05:00
|
|
|
return callback(js);
|
|
|
|
});
|
|
|
|
};
|
2010-01-29 22:51:51 -05:00
|
|
|
})();
|