Using console.log and console.error in command.coffee (fixes #1798)

This commit is contained in:
Trevor Burnham 2011-10-24 14:39:55 -04:00
parent 913171f708
commit 41f2d2f789
3 changed files with 26 additions and 36 deletions

View File

@ -1,5 +1,5 @@
(function() {
var BANNER, CoffeeScript, EventEmitter, SWITCHES, compileJoin, compileOptions, compileScript, compileScripts, compileStdio, contents, exec, forkNode, fs, helpers, lint, loadRequires, optionParser, optparse, opts, parseOptions, path, printLine, printTokens, printWarn, sources, spawn, usage, version, watch, writeJs, _ref;
var BANNER, CoffeeScript, EventEmitter, SWITCHES, compileJoin, compileOptions, compileScript, compileScripts, compileStdio, contents, exec, forkNode, fs, helpers, lint, loadRequires, optionParser, optparse, opts, parseOptions, path, printTokens, sources, spawn, usage, version, watch, writeJs, _ref;
fs = require('fs');
@ -17,14 +17,6 @@
helpers.extend(CoffeeScript, new EventEmitter);
printLine = function(line) {
return process.stdout.write(line + '\n');
};
printWarn = function(line) {
return process.binding('stdio').writeError(line + '\n');
};
BANNER = 'Usage: coffee [options] path/to/script.coffee\n\nIf called without options, `coffee` will run your script.';
SWITCHES = [['-b', '--bare', 'compile without a top-level function wrapper'], ['-c', '--compile', 'compile to JavaScript and save as .js files'], ['-e', '--eval', 'pass a string from the command line as input'], ['-h', '--help', 'display this help message'], ['-i', '--interactive', 'run an interactive CoffeeScript REPL'], ['-j', '--join [FILE]', 'concatenate the source CoffeeScript before compiling'], ['-l', '--lint', 'pipe the compiled JavaScript through JavaScript Lint'], ['-n', '--nodes', 'print out the parse tree that the parser produces'], ['--nodejs [ARGS]', 'pass options directly to the "node" binary'], ['-o', '--output [DIR]', 'set the output directory for compiled JavaScript'], ['-p', '--print', 'print out the compiled JavaScript'], ['-r', '--require [FILE*]', 'require a library before executing your script'], ['-s', '--stdio', 'listen for and compile scripts over stdio'], ['-t', '--tokens', 'print out the tokens that the lexer/rewriter produce'], ['-v', '--version', 'display the version number'], ['-w', '--watch', 'watch scripts for changes and rerun commands']];
@ -142,14 +134,14 @@
if (o.tokens) {
return printTokens(CoffeeScript.tokens(t.input));
} else if (o.nodes) {
return printLine(CoffeeScript.nodes(t.input).toString().trim());
return console.log(CoffeeScript.nodes(t.input).toString().trim());
} else if (o.run) {
return CoffeeScript.run(t.input, t.options);
} else {
t.output = CoffeeScript.compile(t.input, t.options);
CoffeeScript.emit('success', task);
if (o.print) {
return printLine(t.output.trim());
return console.log(t.output.trim());
} else if (o.compile) {
return writeJs(t.file, t.output, base);
} else if (o.lint) {
@ -159,8 +151,8 @@
} catch (err) {
CoffeeScript.emit('failure', err, task);
if (CoffeeScript.listeners('failure').length) return;
if (o.watch) return printLine(err.message);
printWarn(err instanceof Error && err.stack || ("ERROR: " + err));
if (o.watch) return console.log(err.message);
console.error(err instanceof Error && err.stack || ("ERROR: " + err));
return process.exit(1);
}
};
@ -221,7 +213,7 @@
if (js.length <= 0) js = ' ';
return fs.writeFile(jsPath, js, function(err) {
if (err) {
return printLine(err.message);
return console.log(err.message);
} else if (opts.compile && opts.watch) {
return console.log("" + ((new Date).toLocaleTimeString()) + " - compiled " + source);
}
@ -239,7 +231,7 @@
lint = function(file, js) {
var conf, jsl, printIt;
printIt = function(buffer) {
return printLine(file + ':\t' + buffer.toString().trim());
return console.log(file + ':\t' + buffer.toString().trim());
};
conf = __dirname + '/../extras/jsl.conf';
jsl = spawn('jsl', ['-nologo', '-stdin', '-conf', conf]);
@ -261,7 +253,7 @@
}
return _results;
})();
return printLine(strings.join(' '));
return console.log(strings.join(' '));
};
parseOptions = function() {
@ -294,11 +286,11 @@
};
usage = function() {
return printLine((new optparse.OptionParser(SWITCHES, BANNER)).help());
return console.log((new optparse.OptionParser(SWITCHES, BANNER)).help());
};
version = function() {
return printLine("CoffeeScript version " + CoffeeScript.VERSION);
return console.log("CoffeeScript version " + CoffeeScript.VERSION);
};
}).call(this);

View File

@ -107,14 +107,15 @@
};
Lexer.prototype.numberToken = function() {
var is_binary, match, number, numlen;
var binaryLiteral, lexedLength, match, number;
if (!(match = NUMBER.exec(this.chunk))) return 0;
number = match[0];
numlen = number.length;
is_binary = /0b([01]+)/.exec(number);
if (is_binary) number = (parseInt(is_binary[1], 2)).toString();
lexedLength = number.length;
if (binaryLiteral = /0b([01]+)/.exec(number)) {
number = (parseInt(binaryLiteral[1], 2)).toString();
}
this.token('NUMBER', number);
return numlen;
return lexedLength;
};
Lexer.prototype.stringToken = function() {

View File

@ -16,9 +16,6 @@ CoffeeScript = require './coffee-script'
# Allow CoffeeScript to emit Node.js events.
helpers.extend CoffeeScript, new EventEmitter
printLine = (line) -> process.stdout.write line + '\n'
printWarn = (line) -> process.binding('stdio').writeError line + '\n'
# The help banner that is printed when `coffee` is called without arguments.
BANNER = '''
Usage: coffee [options] path/to/script.coffee
@ -130,19 +127,19 @@ compileScript = (file, input, base) ->
t = task = {file, input, options}
CoffeeScript.emit 'compile', task
if o.tokens then printTokens CoffeeScript.tokens t.input
else if o.nodes then printLine CoffeeScript.nodes(t.input).toString().trim()
else if o.nodes then console.log CoffeeScript.nodes(t.input).toString().trim()
else if o.run then CoffeeScript.run t.input, t.options
else
t.output = CoffeeScript.compile t.input, t.options
CoffeeScript.emit 'success', task
if o.print then printLine t.output.trim()
if o.print then console.log t.output.trim()
else if o.compile then writeJs t.file, t.output, base
else if o.lint then lint t.file, t.output
catch err
CoffeeScript.emit 'failure', err, task
return if CoffeeScript.listeners('failure').length
return printLine err.message if o.watch
printWarn err instanceof Error and err.stack or "ERROR: #{err}"
return console.log err.message if o.watch
console.error err instanceof Error and err.stack or "ERROR: #{err}"
process.exit 1
# Attach the appropriate listeners to compile scripts incoming over **stdin**,
@ -191,7 +188,7 @@ writeJs = (source, js, base) ->
js = ' ' if js.length <= 0
fs.writeFile jsPath, js, (err) ->
if err
printLine err.message
console.log err.message
else if opts.compile and opts.watch
console.log "#{(new Date).toLocaleTimeString()} - compiled #{source}"
path.exists dir, (exists) ->
@ -200,7 +197,7 @@ writeJs = (source, js, base) ->
# Pipe compiled JS through JSLint (requires a working `jsl` command), printing
# any errors or warnings that arise.
lint = (file, js) ->
printIt = (buffer) -> printLine file + ':\t' + buffer.toString().trim()
printIt = (buffer) -> console.log file + ':\t' + buffer.toString().trim()
conf = __dirname + '/../extras/jsl.conf'
jsl = spawn 'jsl', ['-nologo', '-stdin', '-conf', conf]
jsl.stdout.on 'data', printIt
@ -213,7 +210,7 @@ printTokens = (tokens) ->
strings = for token in tokens
[tag, value] = [token[0], token[1].toString().replace(/\n/, '\\n')]
"[#{tag} #{value}]"
printLine strings.join(' ')
console.log strings.join(' ')
# Use the [OptionParser module](optparse.html) to extract all options from
# `process.argv` that are specified in `SWITCHES`.
@ -242,8 +239,8 @@ forkNode = ->
# Print the `--help` usage message and exit. Deprecated switches are not
# shown.
usage = ->
printLine (new optparse.OptionParser SWITCHES, BANNER).help()
console.log (new optparse.OptionParser SWITCHES, BANNER).help()
# Print the `--version` message and exit.
version = ->
printLine "CoffeeScript version #{CoffeeScript.VERSION}"
console.log "CoffeeScript version #{CoffeeScript.VERSION}"