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

First draft of --join.

This commit is contained in:
Jeremy Ashkenas 2010-12-22 21:26:15 -08:00
parent 482626b9b8
commit 6495508194
2 changed files with 30 additions and 4 deletions

View file

@ -1,5 +1,5 @@
(function() { (function() {
var BANNER, CoffeeScript, EventEmitter, SWITCHES, compileOptions, compileScript, compileScripts, compileStdio, exec, forkNode, fs, helpers, lint, optionParser, optparse, opts, parseOptions, path, printLine, printTokens, printWarn, sources, spawn, usage, util, version, watch, writeJs, _ref; var BANNER, CoffeeScript, EventEmitter, SWITCHES, compileJoin, compileOptions, compileScript, compileScripts, compileStdio, contents, exec, forkNode, fs, helpers, lint, optionParser, optparse, opts, parseOptions, path, printLine, printTokens, printWarn, sources, spawn, usage, util, version, watch, writeJs, _ref;
fs = require('fs'); fs = require('fs');
path = require('path'); path = require('path');
util = require('util'); util = require('util');
@ -16,9 +16,10 @@
return process.binding('stdio').writeError(line + '\n'); return process.binding('stdio').writeError(line + '\n');
}; };
BANNER = 'Usage: coffee [options] path/to/script.coffee'; BANNER = 'Usage: coffee [options] 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'], ['-b', '--bare', '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'], ['--nodejs [ARGS]', 'pass options through to the "node" binary'], ['-v', '--version', 'display CoffeeScript version'], ['-h', '--help', 'display this help message']]; 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'], ['-j', '--join', 'concatenate the scripts before compiling'], ['-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'], ['-b', '--bare', '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'], ['--nodejs [ARGS]', 'pass options through to the "node" binary'], ['-v', '--version', 'display CoffeeScript version'], ['-h', '--help', 'display this help message']];
opts = {}; opts = {};
sources = []; sources = [];
contents = [];
optionParser = null; optionParser = null;
exports.run = function() { exports.run = function() {
parseOptions(); parseOptions();
@ -73,7 +74,14 @@
}); });
} else if (topLevel || path.extname(source) === '.coffee') { } else if (topLevel || path.extname(source) === '.coffee') {
fs.readFile(source, function(err, code) { fs.readFile(source, function(err, code) {
return compileScript(source, code.toString(), base); if (opts.join) {
contents[sources.indexOf(source)] = code.toString();
if (helpers.compact(contents).length === sources.length) {
return compileJoin();
}
} else {
return compileScript(source, code.toString(), base);
}
}); });
if (opts.watch) { if (opts.watch) {
return watch(source, base); return watch(source, base);
@ -146,6 +154,11 @@
return compileScript(null, code); return compileScript(null, code);
}); });
}; };
compileJoin = function() {
var code;
code = contents.join('\n');
return compileScript("concat", code, "concat");
};
watch = function(source, base) { watch = function(source, base) {
return fs.watchFile(source, { return fs.watchFile(source, {
persistent: true, persistent: true,

View file

@ -30,6 +30,7 @@ SWITCHES = [
['-c', '--compile', 'compile to JavaScript and save as .js files'] ['-c', '--compile', 'compile to JavaScript and save as .js files']
['-i', '--interactive', 'run an interactive CoffeeScript REPL'] ['-i', '--interactive', 'run an interactive CoffeeScript REPL']
['-o', '--output [DIR]', 'set the directory for compiled JavaScript'] ['-o', '--output [DIR]', 'set the directory for compiled JavaScript']
['-j', '--join', 'concatenate the scripts before compiling']
['-w', '--watch', 'watch scripts for changes, and recompile'] ['-w', '--watch', 'watch scripts for changes, and recompile']
['-p', '--print', 'print the compiled JavaScript to stdout'] ['-p', '--print', 'print the compiled JavaScript to stdout']
['-l', '--lint', 'pipe the compiled JavaScript through JSLint'] ['-l', '--lint', 'pipe the compiled JavaScript through JSLint']
@ -47,6 +48,7 @@ SWITCHES = [
# Top-level objects shared by all the functions. # Top-level objects shared by all the functions.
opts = {} opts = {}
sources = [] sources = []
contents = []
optionParser = null optionParser = null
# Run `coffee` by parsing passed options and determining what action to take. # Run `coffee` by parsing passed options and determining what action to take.
@ -81,7 +83,12 @@ compileScripts = ->
for file in files for file in files
compile path.join(source, file) compile path.join(source, file)
else if topLevel or path.extname(source) is '.coffee' else if topLevel or path.extname(source) is '.coffee'
fs.readFile source, (err, code) -> compileScript(source, code.toString(), base) fs.readFile source, (err, code) ->
if opts.join
contents[sources.indexOf source] = code.toString()
compileJoin() if helpers.compact(contents).length is sources.length
else
compileScript(source, code.toString(), base)
watch source, base if opts.watch watch source, base if opts.watch
compile source, true compile source, true
@ -122,6 +129,12 @@ compileStdio = ->
stdin.on 'end', -> stdin.on 'end', ->
compileScript null, code compileScript null, code
# After all of the source files are done being read, concatenate and compile
# them together.
compileJoin = ->
code = contents.join '\n'
compileScript "concatenation", code, "concatenation"
# Watch a source CoffeeScript file using `fs.watchFile`, recompiling it every # Watch a source CoffeeScript file using `fs.watchFile`, recompiling it every
# time the file is updated. May be used in combination with other options, # time the file is updated. May be used in combination with other options,
# such as `--lint` or `--print`. # such as `--lint` or `--print`.