1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

Big commit. First draft of stripping comments from generated JS output. Issue #41

This commit is contained in:
Jeremy Ashkenas 2010-06-27 12:59:54 -04:00
parent 8eedfe4bc6
commit ec570c46bf
20 changed files with 4473 additions and 1245 deletions

View file

@ -1,11 +1,5 @@
(function(){
var BANNER, CoffeeScript, SWITCHES, _a, compileOptions, compileScript, compileScripts, compileStdio, exec, fs, lint, optionParser, options, optparse, parseOptions, path, printTokens, sources, spawn, usage, version, watch, writeJs;
// The `coffee` utility. Handles command-line compilation of CoffeeScript
// into various forms: saved into `.js` files or printed to stdout, piped to
// [JSLint](http://javascriptlint.com/) or recompiled every time the source is
// saved, printed as a token stream or as the syntax tree, or launch an
// interactive REPL.
// External dependencies.
fs = require('fs');
path = require('path');
optparse = require('./optparse');
@ -13,17 +7,11 @@
_a = require('child_process');
spawn = _a.spawn;
exec = _a.exec;
// The help banner that is printed when `coffee` is called without arguments.
BANNER = 'coffee compiles CoffeeScript source files into JavaScript.\n\nUsage:\n coffee path/to/script.coffee';
// The list of all the valid option flags that `coffee` knows how to handle.
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'], ['--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']];
// Top-level objects shared by all the functions.
options = {};
sources = [];
optionParser = null;
// Run `coffee` by parsing passed options and determining what action to take.
// Many flags cause us to divert before compiling anything. Flags passed after
// `--` will be passed verbatim to your script as arguments in `process.argv`
exports.run = function() {
var flags, separator;
parseOptions();
@ -54,9 +42,6 @@
process.ARGV = (process.argv = flags);
return compileScripts();
};
// Asynchronously read in each CoffeeScript in a list of source files and
// compile them. If a directory is passed, recursively compile all
// '.coffee' extension source files in it and all subdirectories.
compileScripts = function() {
var _b, _c, _d, _e;
_b = []; _d = sources;
@ -99,9 +84,6 @@
}
return _b;
};
// Compile a single source script, containing the given code, according to the
// requested options. If evaluating the script directly sets `__filename`,
// `__dirname` and `module.filename` to be correct relative to the script's path.
compileScript = function(source, code, base) {
var codeOpts, js, o;
o = options;
@ -130,8 +112,6 @@
return puts(err.message);
}
};
// Attach the appropriate listeners to compile scripts incoming over **stdin**,
// and write them back to **stdout**.
compileStdio = function() {
var code, stdin;
code = '';
@ -145,9 +125,6 @@
return compileScript('stdio', code);
});
};
// Watch a source CoffeeScript file using `fs.watchFile`, recompiling it every
// time the file is updated. May be used in combination with other options,
// such as `--lint` or `--print`.
watch = function(source, base) {
return fs.watchFile(source, {
persistent: true,
@ -161,9 +138,6 @@
});
});
};
// Write out a JavaScript source file with the compiled code. By default, files
// are written out in `cwd` as `.js` files with the same name, but the output
// directory can be customized with `--output`.
writeJs = function(source, js, base) {
var baseDir, compile, dir, filename, jsPath, srcDir;
filename = path.basename(source, path.extname(source)) + '.js';
@ -186,8 +160,6 @@
}
});
};
// Pipe compiled JS through JSLint (requires a working `jsl` command), printing
// any errors or warnings that arise.
lint = function(js) {
var jsl, printIt;
printIt = function(buffer) {
@ -199,7 +171,6 @@
jsl.stdin.write(js);
return jsl.stdin.end();
};
// Pretty-print a stream of tokens.
printTokens = function(tokens) {
var _b, _c, _d, _e, _f, strings, tag, token, value;
strings = (function() {
@ -217,8 +188,6 @@
})();
return puts(strings.join(' '));
};
// Use the [OptionParser module](optparse.html) to extract all options from
// `process.argv` that are specified in `SWITCHES`.
parseOptions = function() {
var o;
optionParser = new optparse.OptionParser(SWITCHES, BANNER);
@ -228,7 +197,6 @@
sources = options.arguments.slice(2, options.arguments.length);
return sources;
};
// The compile-time options to pass to the CoffeeScript compiler.
compileOptions = function(source) {
var o;
o = {
@ -237,12 +205,10 @@
o['no_wrap'] = options['no-wrap'];
return o;
};
// Print the `--help` usage message and exit.
usage = function() {
puts(optionParser.help());
return process.exit(0);
};
// Print the `--version` message and exit.
version = function() {
puts(("CoffeeScript version " + CoffeeScript.VERSION));
return process.exit(0);