@@ -16,6 +16,9 @@ CoffeeScript = require './coffee-script'
1616# Allow CoffeeScript to emit Node.js events.
1717helpers .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.
2023BANNER = '''
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.
160163lint = (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.
189192compileOptions = (fileName ) -> {fileName, bare : opts .bare or opts[' no-wrap' ]}
190193
191194# Print the `--help` usage message and exit. Deprecated switches are not
192195# shown.
193196usage = ->
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.
198201version = ->
199- console . log " CoffeeScript version #{ CoffeeScript .VERSION } "
202+ printLine " CoffeeScript version #{ CoffeeScript .VERSION } "
200203 process .exit 0
0 commit comments