Skip to content

Commit adeace8

Browse files
committed
Merge branch 'issue/875' of git://github.com/StanAngeloff/coffee-script
2 parents 4447180 + a9e264d commit adeace8

File tree

3 files changed

+40
-23
lines changed

3 files changed

+40
-23
lines changed

lib/command.js

Lines changed: 18 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/command.coffee

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ CoffeeScript = require './coffee-script'
1616
# Allow CoffeeScript to emit Node.js events.
1717
helpers.extend CoffeeScript, new EventEmitter
1818

19+
printLine = (line) -> process.stdout.write line + '\n'
20+
printWarn = (line) -> process.binding('stdio').writeError line + '\n'
21+
1922
# The help banner that is printed when `coffee` is called without arguments.
2023
BANNER = '''
2124
Usage: coffee [options] path/to/script.coffee
@@ -103,19 +106,19 @@ compileScript = (file, input, base) ->
103106
t = task = {file, input, options}
104107
CoffeeScript.emit 'compile', task
105108
if o.tokens then printTokens CoffeeScript.tokens t.input
106-
else if o.nodes then console.log CoffeeScript.nodes(t.input).toString().trim()
109+
else if o.nodes then printLine CoffeeScript.nodes(t.input).toString().trim()
107110
else if o.run then CoffeeScript.run t.input, t.options
108111
else
109112
t.output = CoffeeScript.compile t.input, t.options
110113
CoffeeScript.emit 'success', task
111-
if o.print then console.log t.output.trim()
114+
if o.print then printLine t.output.trim()
112115
else if o.compile then writeJs t.file, t.output, base
113116
else if o.lint then lint t.file, t.output
114117
catch err
115118
CoffeeScript.emit 'failure', err, task
116119
return if CoffeeScript.listeners('failure').length
117-
return console.log err.message if o.watch
118-
console.error err.stack
120+
return printLine err.message if o.watch
121+
printWarn err.stack
119122
process.exit 1
120123

121124
# Attach the appropriate listeners to compile scripts incoming over **stdin**,
@@ -150,15 +153,15 @@ writeJs = (source, js, base) ->
150153
compile = ->
151154
js = ' ' if js.length <= 0
152155
fs.writeFile jsPath, js, (err) ->
153-
if err then console.log err.message
154-
else if opts.compile and opts.watch then console.log "Compiled #{source}"
156+
if err then printLine err.message
157+
else if opts.compile and opts.watch then printLine "Compiled #{source}"
155158
path.exists dir, (exists) ->
156159
if exists then compile() else exec "mkdir -p #{dir}", compile
157160

158161
# Pipe compiled JS through JSLint (requires a working `jsl` command), printing
159162
# any errors or warnings that arise.
160163
lint = (file, js) ->
161-
printIt = (buffer) -> console.log file + ':\t' + buffer.toString().trim()
164+
printIt = (buffer) -> printLine file + ':\t' + buffer.toString().trim()
162165
conf = __dirname + '/../extras/jsl.conf'
163166
jsl = spawn 'jsl', ['-nologo', '-stdin', '-conf', conf]
164167
jsl.stdout.on 'data', printIt
@@ -171,7 +174,7 @@ printTokens = (tokens) ->
171174
strings = for token in tokens
172175
[tag, value] = [token[0], token[1].toString().replace(/\n/, '\\n')]
173176
"[#{tag} #{value}]"
174-
console.log strings.join(' ')
177+
printLine strings.join(' ')
175178

176179
# Use the [OptionParser module](optparse.html) to extract all options from
177180
# `process.argv` that are specified in `SWITCHES`.
@@ -183,18 +186,18 @@ parseOptions = ->
183186
o.print = !! (o.print or (o.eval or o.stdio and o.compile))
184187
sources = o.arguments
185188
if opts['no-wrap']
186-
console.warn '--no-wrap is deprecated; please use --bare instead.'
189+
printWarn '--no-wrap is deprecated; please use --bare instead.'
187190

188191
# The compile-time options to pass to the CoffeeScript compiler.
189192
compileOptions = (fileName) -> {fileName, bare: opts.bare or opts['no-wrap']}
190193

191194
# Print the `--help` usage message and exit. Deprecated switches are not
192195
# shown.
193196
usage = ->
194-
console.log (new optparse.OptionParser SWITCHES, BANNER).help()
197+
printLine (new optparse.OptionParser SWITCHES, BANNER).help()
195198
process.exit 0
196199

197200
# Print the `--version` message and exit.
198201
version = ->
199-
console.log "CoffeeScript version #{CoffeeScript.VERSION}"
202+
printLine "CoffeeScript version #{CoffeeScript.VERSION}"
200203
process.exit 0

test/test_compilation.coffee

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
{ exec } = require 'child_process'
2+
13
# Ensure that carriage returns don't break compilation on Windows.
24
eq CoffeeScript.compile('one\r\ntwo', bare: on), 'one;\ntwo;'
35

@@ -12,3 +14,9 @@ catch e then eq e.message, 'unclosed CALL_START on line 1'
1214

1315
eq CoffeeScript.compile('for all k of o then', bare: on, globals: on),
1416
'for (k in o) {}'
17+
18+
#875: %d and %s in strings causes node.js to apply formatting
19+
cmd = process.argv[1].replace /cake$/, 'coffee'
20+
exec "#{cmd} -bpe \"'%d isnt %s'\"", (error, stdout, stderr) ->
21+
throw error if error
22+
eq stdout.trim(), "'%d isnt %s';"

0 commit comments

Comments
 (0)