mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-02-18 03:21:20 -05:00
coffee compiles CoffeeScript source files into JavaScript.
Usage:
coffee path/to/script.coffee
-i, --interactive run a CoffeeScript REPL (requires Narwhal)
-r, --run compile and run a script (requires Narwhal)
-o, --output [DIR] set the directory for compiled JavaScript
-w, --watch watch scripts for changes, and recompile
-p, --print print the compiled JavaScript to stdout
-l, --lint pipe the compiled JavaScript through JSLint
-e, --eval compile a cli scriptlet or read from stdin
-t, --tokens print the tokens that the lexer produces
-v, --verbose print at every step of code generation
-n, --no-wrap raw output, no safety wrapper or vars
--install-bundle install the CoffeeScript TextMate bundle
--version display CoffeeScript version
-h, --help display this help message outside of the coffee-script directory
62 lines
2.1 KiB
CoffeeScript
62 lines
2.1 KiB
CoffeeScript
# This (javascript) file is generated from lib/coffee_script/narwhal/coffee-script.coffee
|
|
|
|
# Executes the `coffee` Ruby program to convert from CoffeeScript
|
|
# to Javascript. Eventually this will hopefully happen entirely within JS.
|
|
|
|
# Require external dependencies.
|
|
OS: require('os')
|
|
File: require('file')
|
|
Readline: require('readline')
|
|
|
|
# The path to the CoffeeScript Compiler.
|
|
coffeePath: File.path(module.path).dirname().dirname().dirname().dirname().dirname().join('bin', 'coffee')
|
|
|
|
# Our general-purpose error handler.
|
|
checkForErrors: coffeeProcess =>
|
|
return true if coffeeProcess.wait() is 0
|
|
system.stderr.print(coffeeProcess.stderr.read())
|
|
throw new Error("CoffeeScript compile error").
|
|
|
|
# Run a simple REPL, round-tripping to the CoffeeScript compiler for every
|
|
# command.
|
|
exports.run: args =>
|
|
args.shift()
|
|
if args.length
|
|
exports.evalCS(File.read(path)) for path in args.
|
|
return true.
|
|
|
|
while true
|
|
try
|
|
system.stdout.write('coffee> ').flush()
|
|
result: exports.evalCS(Readline.readline())
|
|
print(result) if result isnt undefined
|
|
catch e
|
|
print(e)...
|
|
|
|
# Compile a given CoffeeScript file into JavaScript.
|
|
exports.compileFile: path =>
|
|
coffee: OS.popen([coffeePath, "--print", "--no-wrap", path])
|
|
checkForErrors(coffee)
|
|
coffee.stdout.read().
|
|
|
|
# Compile a string of CoffeeScript into JavaScript.
|
|
exports.compile: source =>
|
|
coffee: OS.popen([coffeePath, "--eval", "--no-wrap"])
|
|
coffee.stdin.write(source).flush().close()
|
|
checkForErrors(coffee)
|
|
coffee.stdout.read().
|
|
|
|
# Evaluating a string of CoffeeScript first compiles it externally.
|
|
exports.evalCS: source =>
|
|
eval(exports.compile(source)).
|
|
|
|
# Make a factory for the CoffeeScript environment.
|
|
exports.makeNarwhalFactory: path =>
|
|
code: exports.compileFile(path)
|
|
factoryText: "function(require,exports,module,system,print){ 1 + 1 /**/\n}"
|
|
if system.engine is "rhino"
|
|
Packages.org.mozilla.javascript.Context.getCurrentContext().compileFunction(global, factoryText, path, 0, null)
|
|
else
|
|
# eval requires parenthesis, but parenthesis break compileFunction.
|
|
eval("(" + factoryText + ")")..
|