Removing the mixed-in sys/util module. Switching from 'puts' to console.log

This commit is contained in:
Jeremy Ashkenas 2010-10-24 12:48:42 -04:00
parent aed0e8790e
commit 26a115adcf
23 changed files with 86 additions and 89 deletions

View File

@ -11,12 +11,12 @@ reset = '\033[0m'
# Run a CoffeeScript through our node/coffee interpreter. # Run a CoffeeScript through our node/coffee interpreter.
run = (args) -> run = (args) ->
proc = spawn 'bin/coffee', args proc = spawn 'bin/coffee', args
proc.stderr.on 'data', (buffer) -> puts buffer.toString() proc.stderr.on 'data', (buffer) -> console.log buffer.toString()
proc.on 'exit', (status) -> process.exit(1) if status != 0 proc.on 'exit', (status) -> process.exit(1) if status != 0
# Log a message with a color. # Log a message with a color.
log = (message, color, explanation) -> log = (message, color, explanation) ->
puts color + message + reset + ' ' + (explanation or '') console.log color + message + reset + ' ' + (explanation or '')
option '-p', '--prefix [DIR]', 'set the installation prefix for `cake install`' option '-p', '--prefix [DIR]', 'set the installation prefix for `cake install`'
@ -25,9 +25,9 @@ task 'install', 'install CoffeeScript into /usr/local (or --prefix)', (options)
lib = "#{base}/lib/coffee-script" lib = "#{base}/lib/coffee-script"
bin = "#{base}/bin" bin = "#{base}/bin"
node = "~/.node_libraries/coffee-script" node = "~/.node_libraries/coffee-script"
puts "Installing CoffeeScript to #{lib}" console.log "Installing CoffeeScript to #{lib}"
puts "Linking to #{node}" console.log "Linking to #{node}"
puts "Linking 'coffee' to #{bin}/coffee" console.log "Linking 'coffee' to #{bin}/coffee"
exec([ exec([
"mkdir -p #{lib} #{bin}" "mkdir -p #{lib} #{bin}"
"cp -rf bin lib LICENSE README package.json src #{lib}" "cp -rf bin lib LICENSE README package.json src #{lib}"

View File

@ -4,5 +4,4 @@ var path = require('path');
var fs = require('fs'); var fs = require('fs');
var lib = path.join(path.dirname(fs.realpathSync(__filename)), '../lib'); var lib = path.join(path.dirname(fs.realpathSync(__filename)), '../lib');
require(lib + '/helpers').extend(global, require(/^v0\.[012]/.test(process.version) ? 'sys' : 'util'));
require(lib + '/cake').run(); require(lib + '/cake').run();

View File

@ -4,5 +4,4 @@ var path = require('path');
var fs = require('fs'); var fs = require('fs');
var lib = path.join(path.dirname(fs.realpathSync(__filename)), '../lib'); var lib = path.join(path.dirname(fs.realpathSync(__filename)), '../lib');
require(lib + '/helpers').extend(global, require(/^v0\.[012]/.test(process.version) ? 'sys' : 'util'));
require(lib + '/command').run(); require(lib + '/command').run();

View File

@ -11,6 +11,6 @@ index = (list, target) ->
if val < target then low = mid + 1 else high = mid if val < target then low = mid + 1 else high = mid
return -1 return -1
puts 2 is index [10, 20, 30, 40, 50], 30 console.log 2 is index [10, 20, 30, 40, 50], 30
puts 4 is index [-97, 35, 67, 88, 1200], 1200 console.log 4 is index [-97, 35, 67, 88, 1200], 1200
puts 0 is index [0, 45, 70], 0 console.log 0 is index [0, 45, 70], 0

View File

@ -8,6 +8,6 @@ runtime = (N) ->
t = n - 1 + sum / n t = n - 1 + sum / n
t t
puts runtime(3) is 2.6666666666666665 console.log runtime(3) is 2.6666666666666665
puts runtime(5) is 7.4 console.log runtime(5) is 7.4
puts runtime(8) is 16.92142857142857 console.log runtime(8) is 16.92142857142857

View File

@ -26,9 +26,9 @@ match_star = (c, regexp, text) ->
return false unless text and (text[0] is c or c is '.') return false unless text and (text[0] is c or c is '.')
text = text.slice(1) text = text.slice(1)
puts match("ex", "some text") console.log match("ex", "some text")
puts match("s..t", "spit") console.log match("s..t", "spit")
puts match("^..t", "buttercup") console.log match("^..t", "buttercup")
puts match("i..$", "cherries") console.log match("i..$", "cherries")
puts match("o*m", "vrooooommm!") console.log match("o*m", "vrooooommm!")
puts match("^hel*o$", "hellllllo") console.log match("^hel*o$", "hellllllo")

View File

@ -11,7 +11,7 @@ append = (location, data) ->
throw new Error("Location does not exist") unless path.exists() throw new Error("Location does not exist") unless path.exists()
File.open path, 'a', (file) -> File.open path, 'a', (file) ->
file.puts YAML.dump data file.console.log YAML.dump data
data data
@ -35,7 +35,7 @@ write = (location, data) ->
File.open path, 'w', (file) -> File.open path, 'w', (file) ->
return false if Digest.MD5.hexdigest(file.read()) is data.hash() return false if Digest.MD5.hexdigest(file.read()) is data.hash()
file.puts YAML.dump data file.console.log YAML.dump data
true true

View File

@ -19,7 +19,7 @@ binary_search = (items, value) ->
# Test the function. # Test the function.
puts 2 is binary_search [10, 20, 30, 40, 50], 30 console.log 2 is binary_search [10, 20, 30, 40, 50], 30
puts 4 is binary_search [-97, 35, 67, 88, 1200], 1200 console.log 4 is binary_search [-97, 35, 67, 88, 1200], 1200
puts 0 is binary_search [0, 45, 70], 0 console.log 0 is binary_search [0, 45, 70], 0
puts(-1 is binary_search [0, 45, 70], 10) console.log(-1 is binary_search [0, 45, 70], 10)

View File

@ -7,5 +7,5 @@ bubble_sort = (list) ->
# Test the function. # Test the function.
puts bubble_sort([3, 2, 1]).join(' ') is '1 2 3' console.log bubble_sort([3, 2, 1]).join(' ') is '1 2 3'
puts bubble_sort([9, 2, 7, 0, 1]).join(' ') is '0 1 2 7 9' console.log bubble_sort([9, 2, 7, 0, 1]).join(' ') is '0 1 2 7 9'

View File

@ -93,16 +93,16 @@ class LinkedList
list = new LinkedList list = new LinkedList
list.add("Hi") list.add("Hi")
puts list.size() is 1 console.log list.size() is 1
puts list.item(0) is "Hi" console.log list.item(0) is "Hi"
puts list.item(1) is null console.log list.item(1) is null
list = new LinkedList list = new LinkedList
list.add("zero").add("one").add("two") list.add("zero").add("one").add("two")
puts list.size() is 3 console.log list.size() is 3
puts list.item(2) is "two" console.log list.item(2) is "two"
puts list.remove(1) is "one" console.log list.remove(1) is "one"
puts list.item(0) is "zero" console.log list.item(0) is "zero"
puts list.item(1) is "two" console.log list.item(1) is "two"
puts list.size() is 2 console.log list.size() is 2
puts list.item(-10) is null console.log list.item(-10) is null

View File

@ -31,6 +31,6 @@ is_valid_identifier = (identifier) ->
# Tests. # Tests.
puts is_valid_identifier("49927398716") is true console.log is_valid_identifier("49927398716") is true
puts is_valid_identifier("4408041234567893") is true console.log is_valid_identifier("4408041234567893") is true
puts is_valid_identifier("4408041234567890") is false console.log is_valid_identifier("4408041234567890") is false

View File

@ -15,5 +15,5 @@ merge_sort = (list) ->
# Test the function. # Test the function.
puts merge_sort([3, 2, 1]).join(' ') is '1 2 3' console.log merge_sort([3, 2, 1]).join(' ') is '1 2 3'
puts merge_sort([9, 2, 7, 0, 1]).join(' ') is '0 1 2 7 9' console.log merge_sort([9, 2, 7, 0, 1]).join(' ') is '0 1 2 7 9'

View File

@ -19,5 +19,5 @@ selection_sort = (list) ->
# Test the function. # Test the function.
puts selection_sort([3, 2, 1]).join(' ') is '1 2 3' console.log selection_sort([3, 2, 1]).join(' ') is '1 2 3'
puts selection_sort([9, 2, 7, 0, 1]).join(' ') is '0 1 2 7 9' console.log selection_sort([9, 2, 7, 0, 1]).join(' ') is '0 1 2 7 9'

View File

@ -76,29 +76,29 @@ WishScanner =
# p_up = rand( charisma ) # p_up = rand( charisma )
# if p_up % 9 == 7 # if p_up % 9 == 7
# @life += p_up / 4 # @life += p_up / 4
# puts "[#{ self.class } magick powers up #{ p_up }!]" # console.log "[#{ self.class } magick powers up #{ p_up }!]"
# end # end
# @life -= damage # @life -= damage
# puts "[#{ self.class } has died.]" if @life <= 0 # console.log "[#{ self.class } has died.]" if @life <= 0
# end # end
# #
# # This method takes one turn in a fight. # # This method takes one turn in a fight.
# def fight( enemy, weapon ) # def fight( enemy, weapon )
# if life <= 0 # if life <= 0
# puts "[#{ self.class } is too dead to fight!]" # console.log "[#{ self.class } is too dead to fight!]"
# return # return
# end # end
# #
# # Attack the opponent # # Attack the opponent
# your_hit = rand( strength + weapon ) # your_hit = rand( strength + weapon )
# puts "[You hit with #{ your_hit } points of damage!]" # console.log "[You hit with #{ your_hit } points of damage!]"
# enemy.hit( your_hit ) # enemy.hit( your_hit )
# #
# # Retaliation # # Retaliation
# p enemy # p enemy
# if enemy.life > 0 # if enemy.life > 0
# enemy_hit = rand( enemy.strength + enemy.weapon ) # enemy_hit = rand( enemy.strength + enemy.weapon )
# puts "[Your enemy hit with #{ enemy_hit } points of damage!]" # console.log "[Your enemy hit with #{ enemy_hit } points of damage!]"
# self.hit( enemy_hit ) # self.hit( enemy_hit )
# end # end
# end # end
@ -112,24 +112,24 @@ Creature =
p_up = Math.rand this.charisma p_up = Math.rand this.charisma
if p_up % 9 is 7 if p_up % 9 is 7
this.life += p_up / 4 this.life += p_up / 4
puts "[" + this.name + " magick powers up " + p_up + "!]" console.log "[" + this.name + " magick powers up " + p_up + "!]"
this.life -= damage this.life -= damage
if this.life <= 0 then puts "[" + this.name + " has died.]" if this.life <= 0 then console.log "[" + this.name + " has died.]"
# This method takes one turn in a fight. # This method takes one turn in a fight.
fight: (enemy, weapon) -> fight: (enemy, weapon) ->
if this.life <= 0 then return puts "[" + this.name + "is too dead to fight!]" if this.life <= 0 then return console.log "[" + this.name + "is too dead to fight!]"
# Attack the opponent. # Attack the opponent.
your_hit = Math.rand this.strength + weapon your_hit = Math.rand this.strength + weapon
puts "[You hit with " + your_hit + "points of damage!]" console.log "[You hit with " + your_hit + "points of damage!]"
enemy.hit your_hit enemy.hit your_hit
# Retaliation. # Retaliation.
puts enemy console.log enemy
if enemy.life > 0 if enemy.life > 0
enemy_hit = Math.rand enemy.strength + enemy.weapon enemy_hit = Math.rand enemy.strength + enemy.weapon
puts "[Your enemy hit with " + enemy_hit + "points of damage!]" console.log "[Your enemy hit with " + enemy_hit + "points of damage!]"
this.hit enemy_hit this.hit enemy_hit

View File

@ -1,6 +1,5 @@
# Contributed by Jason Huggins # Contributed by Jason Huggins
sys = require 'sys'
http = require 'http' http = require 'http'
server = http.createServer (req, res) -> server = http.createServer (req, res) ->
@ -10,4 +9,4 @@ server = http.createServer (req, res) ->
server.listen 3000 server.listen 3000
sys.puts "Server running at http://localhost:3000/" console.log "Server running at http://localhost:3000/"

View File

@ -57,18 +57,18 @@
}; };
printTasks = function() { printTasks = function() {
var _ref, desc, name, spaces, task; var _ref, desc, name, spaces, task;
puts(''); console.log('');
for (name in _ref = tasks) { for (name in _ref = tasks) {
task = _ref[name]; task = _ref[name];
spaces = 20 - name.length; spaces = 20 - name.length;
spaces = spaces > 0 ? Array(spaces + 1).join(' ') : ''; spaces = spaces > 0 ? Array(spaces + 1).join(' ') : '';
desc = task.description ? "# " + task.description : ''; desc = task.description ? "# " + task.description : '';
puts("cake " + name + spaces + " " + desc); console.log("cake " + name + spaces + " " + desc);
} }
return switches.length ? puts(oparse.help()) : undefined; return switches.length ? console.log(oparse.help()) : undefined;
}; };
missingTask = function(task) { missingTask = function(task) {
puts("No such task: \"" + task + "\""); console.log("No such task: \"" + task + "\"");
return process.exit(1); return process.exit(1);
}; };
}).call(this); }).call(this);

View File

@ -104,13 +104,13 @@
if (o.tokens) { if (o.tokens) {
return printTokens(CoffeeScript.tokens(t.input)); return printTokens(CoffeeScript.tokens(t.input));
} else if (o.nodes) { } else if (o.nodes) {
return puts(CoffeeScript.nodes(t.input).toString().trim()); return console.log(CoffeeScript.nodes(t.input).toString().trim());
} else if (o.run) { } else if (o.run) {
return CoffeeScript.run(t.input, t.options); return CoffeeScript.run(t.input, t.options);
} else { } else {
t.output = CoffeeScript.compile(t.input, t.options); t.output = CoffeeScript.compile(t.input, t.options);
CoffeeScript.emit('success', task); CoffeeScript.emit('success', task);
return o.print ? print(t.output) : o.compile ? writeJs(t.file, t.output, base) : o.lint ? lint(t.output) : undefined; return o.print ? console.log(t.output.trim()) : o.compile ? writeJs(t.file, t.output, base) : o.lint ? lint(t.output) : undefined;
} }
} catch (err) { } catch (err) {
CoffeeScript.emit('failure', err, task); CoffeeScript.emit('failure', err, task);
@ -118,7 +118,7 @@
return; return;
} }
if (o.watch) { if (o.watch) {
return puts(err.message); return console.log(err.message);
} }
error(err.stack); error(err.stack);
return process.exit(1); return process.exit(1);
@ -163,7 +163,7 @@
js = ' '; js = ' ';
} }
return fs.writeFile(jsPath, js, function(err) { return fs.writeFile(jsPath, js, function(err) {
return err ? puts(err.message) : opts.compile && opts.watch ? puts("Compiled " + source) : undefined; return err ? console.log(err.message) : opts.compile && opts.watch ? console.log("Compiled " + source) : undefined;
}); });
}; };
return path.exists(dir, function(exists) { return path.exists(dir, function(exists) {
@ -173,7 +173,7 @@
lint = function(js) { lint = function(js) {
var conf, jsl, printIt; var conf, jsl, printIt;
printIt = function(buffer) { printIt = function(buffer) {
return puts(buffer.toString().trim()); return console.log(buffer.toString().trim());
}; };
conf = __dirname + '/../extras/jsl.conf'; conf = __dirname + '/../extras/jsl.conf';
jsl = spawn('jsl', ['-nologo', '-stdin', '-conf', conf]); jsl = spawn('jsl', ['-nologo', '-stdin', '-conf', conf]);
@ -193,7 +193,7 @@
} }
return _result; return _result;
})(); })();
return puts(strings.join(' ')); return console.log(strings.join(' '));
}; };
parseOptions = function() { parseOptions = function() {
var o; var o;
@ -211,11 +211,11 @@
}; };
}; };
usage = function() { usage = function() {
puts(optionParser.help()); console.log(optionParser.help());
return process.exit(0); return process.exit(0);
}; };
version = function() { version = function() {
puts("CoffeeScript version " + CoffeeScript.VERSION); console.log("CoffeeScript version " + CoffeeScript.VERSION);
return process.exit(0); return process.exit(0);
}; };
}).call(this); }).call(this);

View File

@ -18,10 +18,10 @@
fileName: 'repl' fileName: 'repl'
}); });
if (val !== undefined) { if (val !== undefined) {
puts(inspect(val)); console.log(inspect(val));
} }
} catch (err) { } catch (err) {
puts(err.stack || err.toString()); console.log(err.stack || err.toString());
} }
return repl.prompt(); return repl.prompt();
}; };

View File

@ -55,15 +55,15 @@ exports.run = ->
# Display the list of Cake tasks in a format similar to `rake -T` # Display the list of Cake tasks in a format similar to `rake -T`
printTasks = -> printTasks = ->
puts '' console.log ''
for all name, task of tasks for all name, task of tasks
spaces = 20 - name.length spaces = 20 - name.length
spaces = if spaces > 0 then Array(spaces + 1).join(' ') else '' spaces = if spaces > 0 then Array(spaces + 1).join(' ') else ''
desc = if task.description then "# #{task.description}" else '' desc = if task.description then "# #{task.description}" else ''
puts "cake #{name}#{spaces} #{desc}" console.log "cake #{name}#{spaces} #{desc}"
puts oparse.help() if switches.length console.log oparse.help() if switches.length
# Print an error and exit when attempting to all an undefined task. # Print an error and exit when attempting to all an undefined task.
missingTask = (task) -> missingTask = (task) ->
puts "No such task: \"#{task}\"" console.log "No such task: \"#{task}\""
process.exit 1 process.exit 1

View File

@ -100,12 +100,12 @@ compileScript = (file, input, base) ->
t = task = {file, input, options} t = task = {file, input, options}
CoffeeScript.emit 'compile', task CoffeeScript.emit 'compile', task
if o.tokens then printTokens CoffeeScript.tokens t.input if o.tokens then printTokens CoffeeScript.tokens t.input
else if o.nodes then puts 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 if o.run then CoffeeScript.run t.input, t.options
else else
t.output = CoffeeScript.compile t.input, t.options t.output = CoffeeScript.compile t.input, t.options
CoffeeScript.emit 'success', task CoffeeScript.emit 'success', task
if o.print then print t.output if o.print then console.log t.output.trim()
else if o.compile then writeJs t.file, t.output, base else if o.compile then writeJs t.file, t.output, base
else if o.lint then lint t.output else if o.lint then lint t.output
catch err catch err
@ -113,7 +113,7 @@ compileScript = (file, input, base) ->
# node will print a stack trace and exit the program. # node will print a stack trace and exit the program.
CoffeeScript.emit 'failure', err, task CoffeeScript.emit 'failure', err, task
return if CoffeeScript.listeners('failure').length return if CoffeeScript.listeners('failure').length
return puts err.message if o.watch return console.log err.message if o.watch
error err.stack error err.stack
process.exit 1 process.exit 1
@ -149,15 +149,15 @@ writeJs = (source, js, base) ->
compile = -> compile = ->
js = ' ' if js.length <= 0 js = ' ' if js.length <= 0
fs.writeFile jsPath, js, (err) -> fs.writeFile jsPath, js, (err) ->
if err then puts err.message if err then console.log err.message
else if opts.compile and opts.watch then puts "Compiled #{source}" else if opts.compile and opts.watch then console.log "Compiled #{source}"
path.exists dir, (exists) -> path.exists dir, (exists) ->
if exists then compile() else exec "mkdir -p #{dir}", compile if exists then compile() else exec "mkdir -p #{dir}", compile
# Pipe compiled JS through JSLint (requires a working `jsl` command), printing # Pipe compiled JS through JSLint (requires a working `jsl` command), printing
# any errors or warnings that arise. # any errors or warnings that arise.
lint = (js) -> lint = (js) ->
printIt = (buffer) -> puts buffer.toString().trim() printIt = (buffer) -> console.log buffer.toString().trim()
conf = __dirname + '/../extras/jsl.conf' conf = __dirname + '/../extras/jsl.conf'
jsl = spawn 'jsl', ['-nologo', '-stdin', '-conf', conf] jsl = spawn 'jsl', ['-nologo', '-stdin', '-conf', conf]
jsl.stdout.on 'data', printIt jsl.stdout.on 'data', printIt
@ -170,7 +170,7 @@ printTokens = (tokens) ->
strings = for token in tokens strings = for token in tokens
[tag, value] = [token[0], token[1].toString().replace(/\n/, '\\n')] [tag, value] = [token[0], token[1].toString().replace(/\n/, '\\n')]
"[#{tag} #{value}]" "[#{tag} #{value}]"
puts strings.join(' ') console.log strings.join(' ')
# Use the [OptionParser module](optparse.html) to extract all options from # Use the [OptionParser module](optparse.html) to extract all options from
# `process.argv` that are specified in `SWITCHES`. # `process.argv` that are specified in `SWITCHES`.
@ -187,10 +187,10 @@ compileOptions = (fileName) -> {fileName, bare: opts.bare}
# Print the `--help` usage message and exit. # Print the `--help` usage message and exit.
usage = -> usage = ->
puts optionParser.help() console.log optionParser.help()
process.exit 0 process.exit 0
# Print the `--version` message and exit. # Print the `--version` message and exit.
version = -> version = ->
puts "CoffeeScript version #{CoffeeScript.VERSION}" console.log "CoffeeScript version #{CoffeeScript.VERSION}"
process.exit 0 process.exit 0

View File

@ -2,7 +2,7 @@
# and evaluates it. Good for simple tests, or poking around the **Node.js** API. # and evaluates it. Good for simple tests, or poking around the **Node.js** API.
# Using it looks like this: # Using it looks like this:
# #
# coffee> puts "#{num} bottles of beer" for num in [99..1] # coffee> console.log "#{num} bottles of beer" for num in [99..1]
# Require the **coffee-script** module to get access to the compiler. # Require the **coffee-script** module to get access to the compiler.
CoffeeScript = require './coffee-script' CoffeeScript = require './coffee-script'
@ -21,9 +21,9 @@ helpers.extend global, quit: -> process.exit(0)
run = (buffer) -> run = (buffer) ->
try try
val = CoffeeScript.eval buffer.toString(), bare: on, globals: on, fileName: 'repl' val = CoffeeScript.eval buffer.toString(), bare: on, globals: on, fileName: 'repl'
puts inspect val if val isnt undefined console.log inspect val if val isnt undefined
catch err catch err
puts err.stack or err.toString() console.log err.stack or err.toString()
repl.prompt() repl.prompt()
# Create the REPL by listening to **stdin**. # Create the REPL by listening to **stdin**.

View File

@ -10,7 +10,7 @@
class exports.Rewriter class exports.Rewriter
# Helpful snippet for debugging: # Helpful snippet for debugging:
# puts (t[0] + '/' + t[1] for t in @tokens).join ' ' # console.log (t[0] + '/' + t[1] for t in @tokens).join ' '
# Rewrite the token stream in multiple passes, one logical filter at # Rewrite the token stream in multiple passes, one logical filter at
# a time. This could certainly be changed into a single pass through the # a time. This could certainly be changed into a single pass through the

View File

@ -4,7 +4,7 @@ callWithLambda = (l) -> null
for i in array for i in array
result = callWithLambda(->) result = callWithLambda(->)
if i == 2 if i == 2
puts "i = 2" console.log "i = 2"
else else
break break