mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-02-17 19:11:22 -05:00
merging node into master -- you can now pass the --narwhal flag to use narwhal instead. All tests are executing successfully against both Node.js and Narwhal/Rhino backends
This commit is contained in:
50
lib/coffee_script/coffee-script.js
Normal file
50
lib/coffee_script/coffee-script.js
Normal file
@@ -0,0 +1,50 @@
|
||||
(function(){
|
||||
var compiler, path;
|
||||
// Executes the `coffee` Ruby program to convert from CoffeeScript to JavaScript.
|
||||
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.
|
||||
exports.compile = function compile(code, callback) {
|
||||
var coffee, js;
|
||||
js = '';
|
||||
coffee = process.createChildProcess(compiler, ['--eval', '--no-wrap', '--globals']);
|
||||
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();
|
||||
};
|
||||
// Compile a list of CoffeeScript files on disk.
|
||||
exports.compile_files = function compile_files(paths, callback) {
|
||||
var coffee, exit_ran, js;
|
||||
js = '';
|
||||
coffee = process.createChildProcess(compiler, ['--print'].concat(paths));
|
||||
coffee.addListener('output', function(results) {
|
||||
if ((typeof results !== "undefined" && results !== null)) {
|
||||
return js += results;
|
||||
}
|
||||
});
|
||||
// NB: we have to add a mutex to make sure it doesn't get called twice.
|
||||
exit_ran = false;
|
||||
coffee.addListener('exit', function() {
|
||||
if (exit_ran) {
|
||||
return null;
|
||||
}
|
||||
exit_ran = true;
|
||||
return callback(js);
|
||||
});
|
||||
return coffee.addListener('error', function(message) {
|
||||
if (!(message)) {
|
||||
return null;
|
||||
}
|
||||
puts(message);
|
||||
throw new Error("CoffeeScript compile error");
|
||||
});
|
||||
};
|
||||
})();
|
||||
Reference in New Issue
Block a user