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

added --node flag for passing options through directly to node

This commit is contained in:
Michael Ficarra 2010-12-17 02:39:39 -05:00
parent c3943d21d0
commit 805d03125b
2 changed files with 62 additions and 8 deletions

View file

@ -1,5 +1,6 @@
(function() {
var ALL_SWITCHES, BANNER, CoffeeScript, DEPRECATED_SWITCHES, EventEmitter, SWITCHES, compileOptions, compileScript, compileScripts, compileStdio, exec, fs, helpers, lint, optionParser, optparse, opts, parseOptions, path, printLine, printTokens, printWarn, sources, spawn, usage, util, version, watch, writeJs, _ref;
var ALL_SWITCHES, BANNER, CoffeeScript, DEPRECATED_SWITCHES, EventEmitter, SWITCHES, compileOptions, compileScript, compileScripts, compileStdio, exec, execl, fs, helpers, lint, optionParser, optparse, opts, parseOptions, path, printLine, printTokens, printWarn, sources, spawn, usage, util, version, watch, writeJs, _ref;
var __slice = Array.prototype.slice;
fs = require('fs');
path = require('path');
util = require('util');
@ -16,7 +17,7 @@
return process.binding('stdio').writeError(line + '\n');
};
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'], ['-v', '--version', 'display CoffeeScript version'], ['-h', '--help', 'display this help message']];
SWITCHES = [['--node [ARGS]', 'pass arguments through to the node binary'], ['-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'], ['-v', '--version', 'display CoffeeScript version'], ['-h', '--help', 'display this help message']];
DEPRECATED_SWITCHES = [['--no-wrap', 'compile without the top-level function wrapper']];
ALL_SWITCHES = SWITCHES.concat(DEPRECATED_SWITCHES);
opts = {};
@ -25,6 +26,9 @@
exports.run = function() {
var flags, separator;
parseOptions();
if (opts.node) {
return execl();
}
if (opts.help) {
return usage();
}
@ -242,6 +246,32 @@
bare: opts.bare || opts['no-wrap']
};
};
execl = function() {
var args, files, flags, idx, name, nodeArgs, value;
nodeArgs = opts.node.split(/\s+/);
files = opts.arguments;
flags = [];
args = [];
if ((idx = files.indexOf('--')) > -1) {
flags = ['--'].concat(__slice.call(files.slice(idx + 1)));
[].splice.apply(files, [idx, files.length].concat([]));
}
for (name in opts) {
value = opts[name];
if (!value || (name === 'node' || name === 'run' || name === 'arguments')) {
continue;
}
args.push('--' + name);
if (value !== true) {
args.push(value);
}
}
return spawn(process.execPath, __slice.call(nodeArgs).concat([process.argv[1]], __slice.call(args), __slice.call(files), __slice.call(flags)), {
cwd: process.cwd(),
env: process.env,
customFds: [0, 1, 2]
});
};
usage = function() {
printLine((new optparse.OptionParser(SWITCHES, BANNER)).help());
return process.exit(0);

View file

@ -27,6 +27,7 @@ BANNER = '''
# The list of all the valid option flags that `coffee` knows how to handle.
SWITCHES = [
[ '--node [ARGS]', 'pass arguments through to the node binary']
['-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']
@ -60,12 +61,13 @@ optionParser = null
# `--` will be passed verbatim to your script as arguments in `process.argv`
exports.run = ->
parseOptions()
return usage() if opts.help
return version() if opts.version
return require './repl' if opts.interactive
return compileStdio() if opts.stdio
return compileScript null, sources[0] if opts.eval
return require './repl' unless sources.length
return execl() if opts.node
return usage() if opts.help
return version() if opts.version
return require './repl' if opts.interactive
return compileStdio() if opts.stdio
return compileScript null, sources[0] if opts.eval
return require './repl' unless sources.length
separator = sources.indexOf '--'
flags = []
if separator >= 0
@ -192,6 +194,28 @@ parseOptions = ->
# The compile-time options to pass to the CoffeeScript compiler.
compileOptions = (fileName) -> {fileName, bare: opts.bare or opts['no-wrap']}
# Start up a new node instance with the arguments in opts.node passed to node,
# preserving the other options
execl = ->
nodeArgs = opts.node.split /\s+/
files = opts.arguments
flags = []
args = []
if (idx = files.indexOf '--') > -1
flags = ['--', files[(idx+1)..]...]
files[idx..] = []
for name, value of opts
# the three magic values listed below are the long-name of the argument that causes
# this function to be invoked, a generated option, and the non-option arguments
continue if !value or name in ['node','run','arguments']
args.push '--' + name
args.push value unless value is true
spawn process.execPath,
[nodeArgs..., process.argv[1], args..., files..., flags...]
cwd: process.cwd()
env: process.env
customFds: [0, 1, 2]
# Print the `--help` usage message and exit. Deprecated switches are not
# shown.
usage = ->