Adding CoffeeScript as a globally-available object, when invoked from the coffee command-line, only.

This commit is contained in:
Jeremy Ashkenas
2010-08-08 09:54:18 -04:00
parent b902377304
commit 08506f160d
2 changed files with 8 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
(function() {
var BANNER, CoffeeScript, EventEmitter, SWITCHES, _a, _b, _c, compileOptions, compileScript, compileScripts, compileStdio, exec, fs, helpers, lint, optionParser, options, optparse, parseOptions, path, printTokens, sources, spawn, usage, version, watch, writeJs;
var BANNER, CoffeeScript, EventEmitter, SWITCHES, _a, _b, _c, compileOptions, compileScript, compileScripts, compileStdio, exec, fs, helpers, lint, localPath, optionParser, options, optparse, parseOptions, path, printTokens, sources, spawn, usage, version, watch, writeJs;
fs = require('fs');
path = require('path');
optparse = require('./optparse');
@@ -12,11 +12,13 @@
_c = require('events');
EventEmitter = _c.EventEmitter;
helpers.extend(CoffeeScript, new EventEmitter());
global.CoffeeScript = CoffeeScript;
BANNER = 'coffee compiles CoffeeScript source files into JavaScript.\n\nUsage:\n coffee path/to/script.coffee';
SWITCHES = [['-c', '--compile', 'compile to JavaScript and save as .js files'], ['-i', '--interactive', 'run an interactive CoffeeScript REPL'], ['-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'], ['-s', '--stdio', 'listen for and compile scripts over stdio'], ['-e', '--eval', 'compile a string from the command line'], ['-r', '--require [FILE*]', 'require a library before executing your script'], ['--no-wrap', 'compile without the top-level function wrapper'], ['-t', '--tokens', 'print the tokens that the lexer produces'], ['-n', '--nodes', 'print the parse tree that Jison produces'], ['-v', '--version', 'display CoffeeScript version'], ['-h', '--help', 'display this help message']];
options = {};
sources = [];
optionParser = null;
localPath = /^.\//;
exports.run = function() {
var flags, separator;
parseOptions();
@@ -101,7 +103,7 @@
_e = o.require;
for (_d = 0, _f = _e.length; _d < _f; _d++) {
file = _e[_d];
require(file);
require(file.replace(localPath, process.cwd() + '/'));
}
}
try {

View File

@@ -13,7 +13,9 @@ CoffeeScript = require './coffee-script'
{spawn, exec} = require 'child_process'
{EventEmitter} = require 'events'
# Allow CoffeeScript to emit Node.js events, and add it to global scope.
helpers.extend CoffeeScript, new EventEmitter
global.CoffeeScript = CoffeeScript
# The help banner that is printed when `coffee` is called without arguments.
BANNER = '''
@@ -45,6 +47,7 @@ SWITCHES = [
options = {}
sources = []
optionParser = null
localPath = /^.\//
# Run `coffee` by parsing passed options and determining what action to take.
# Many flags cause us to divert before compiling anything. Flags passed after
@@ -94,7 +97,7 @@ compileScript = (source, code, base) ->
o = options
codeOpts = compileOptions source
if o.require
require file for file in o.require
require file.replace(localPath, process.cwd() + '/') for file in o.require
try
CoffeeScript.emit 'compile', {source, code, base, options}
if o.tokens then printTokens CoffeeScript.tokens code