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

Battery of patches for compatibility with Node v0.1.90

This commit is contained in:
Jeremy Ashkenas 2010-04-10 18:05:35 -04:00
parent 065bf54094
commit 8317960f81
8 changed files with 81 additions and 73 deletions

View file

@ -1,10 +1,11 @@
fs: require 'fs'
helpers: require('./lib/helpers').helpers
fs: require 'fs'
helpers: require('./lib/helpers').helpers
CoffeeScript: require './lib/coffee-script'
{spawn: spawn, exec: exec}: require('child_process')
# Run a CoffeeScript through our node/coffee interpreter.
run: (args) ->
proc: process.createChildProcess 'bin/coffee', args
proc: spawn 'bin/coffee', args
proc.addListener 'error', (err) -> if err then puts err

View file

@ -6,7 +6,7 @@ http: require 'http'
server: http.createServer (req, res) ->
res.writeHeader 200, {'Content-Type': 'text/plain'}
res.write 'Hello, World!'
res.close()
res.end()
server.listen 3000

View file

@ -100,7 +100,7 @@
};
// Print an error and exit when attempting to all an undefined task.
no_such_task = function no_such_task(task) {
process.stdio.writeError(("No such task: \"" + task + "\"\n"));
puts(("No such task: \"" + task + "\"\n"));
return process.exit(1);
};
})();

View file

@ -1,5 +1,5 @@
(function(){
var BANNER, CoffeeScript, SWITCHES, compile_options, compile_script, compile_scripts, compile_stdio, fs, lint, option_parser, options, optparse, parse_options, path, print_tokens, sources, usage, version, watch_scripts, write_js;
var BANNER, CoffeeScript, SWITCHES, _a, compile_options, compile_script, compile_scripts, compile_stdio, exec, fs, lint, option_parser, options, optparse, parse_options, path, print_tokens, sources, spawn, usage, version, watch_scripts, write_js;
// The `coffee` utility. Handles command-line compilation of CoffeeScript
// into various forms: saved into `.js` files or printed to stdout, piped to
// [JSLint](http://javascriptlint.com/) or recompiled every time the source is
@ -10,6 +10,9 @@
path = require('path');
optparse = require('./optparse');
CoffeeScript = require('./coffee-script');
_a = require('child_process');
spawn = _a.spawn;
exec = _a.exec;
// The help banner that is printed when `coffee` is called without arguments.
BANNER = 'coffee compiles CoffeeScript source files into JavaScript.\n\nUsage:\n coffee path/to/script.coffee';
// The list of all the valid option flags that `coffee` knows how to handle.
@ -57,7 +60,7 @@
// Asynchronously read in each CoffeeScript in a list of source files and
// compile them.
compile_scripts = function compile_scripts() {
var _a, _b, _c, _d, compile, source;
var compile, run;
compile = function compile(source) {
return path.exists(source, function(exists) {
if (!(exists)) {
@ -68,12 +71,19 @@
});
});
};
_a = []; _c = sources;
for (_b = 0, _d = _c.length; _b < _d; _b++) {
source = _c[_b];
_a.push(compile(source));
run = function run() {
var _b, _c, _d, _e, source;
_b = []; _d = sources;
for (_c = 0, _e = _d.length; _c < _e; _c++) {
source = _d[_c];
_b.push(compile(source));
}
return _b;
};
if (!(options.output && options.compile)) {
return run();
}
return _a;
return exec(("mkdir -p " + options.output), run);
};
// Compile a single source script, containing the given code, according to the
// requested options. Both compile_scripts and watch_scripts share this method
@ -93,7 +103,7 @@
} else {
js = CoffeeScript.compile(code, code_opts);
if (o.print) {
return process.stdio.write(js);
return print(js);
} else if (o.compile) {
return write_js(source, js);
} else if (o.lint) {
@ -111,15 +121,15 @@
// Attach the appropriate listeners to compile scripts incoming over **stdin**,
// and write them back to **stdout**.
compile_stdio = function compile_stdio() {
var code;
var code, stdin;
code = '';
process.stdio.open();
process.stdio.addListener('data', function(string) {
if (string) {
return code += string;
stdin = process.openStdin();
stdin.addListener('data', function(buffer) {
if (buffer) {
return code += buffer.toString();
}
});
return process.stdio.addListener('close', function() {
return stdin.addListener('end', function() {
return compile_script('stdio', code);
});
};
@ -127,7 +137,7 @@
// them every time the files are updated. May be used in combination with other
// options, such as `--lint` or `--print`.
watch_scripts = function watch_scripts() {
var _a, _b, _c, _d, source, watch;
var _b, _c, _d, _e, source, watch;
watch = function watch(source) {
return fs.watchFile(source, {
persistent: true,
@ -141,12 +151,12 @@
});
});
};
_a = []; _c = sources;
for (_b = 0, _d = _c.length; _b < _d; _b++) {
source = _c[_b];
_a.push(watch(source));
_b = []; _d = sources;
for (_c = 0, _e = _d.length; _c < _e; _c++) {
source = _d[_c];
_b.push(watch(source));
}
return _a;
return _b;
};
// Write out a JavaScript source file with the compiled code. By default, files
// are written out in `cwd` as `.js` files with the same name, but the output
@ -161,36 +171,31 @@
// Pipe compiled JS through JSLint (requires a working `jsl` command), printing
// any errors or warnings that arise.
lint = function lint(js) {
var jsl;
jsl = process.createChildProcess('jsl', ['-nologo', '-stdin']);
jsl.addListener('output', function(result) {
if (result) {
return puts(result.replace(/\n/g, ''));
}
});
jsl.addListener('error', function(result) {
if (result) {
return puts(result);
}
});
jsl.write(js);
return jsl.close();
var jsl, print_it;
print_it = function print_it(buffer) {
return puts(buffer.toString());
};
jsl = spawn('jsl', ['-nologo', '-stdin']);
jsl.stdout.addListener('data', print_it);
jsl.stderr.addListener('data', print_it);
jsl.stdin.write(js);
return jsl.stdin.end();
};
// Pretty-print a stream of tokens.
print_tokens = function print_tokens(tokens) {
var _a, _b, _c, _d, _e, strings, tag, token, value;
var _b, _c, _d, _e, _f, strings, tag, token, value;
strings = (function() {
_a = []; _c = tokens;
for (_b = 0, _d = _c.length; _b < _d; _b++) {
token = _c[_b];
_a.push((function() {
_e = [token[0], token[1].toString().replace(/\n/, '\\n')];
tag = _e[0];
value = _e[1];
_b = []; _d = tokens;
for (_c = 0, _e = _d.length; _c < _e; _c++) {
token = _d[_c];
_b.push((function() {
_f = [token[0], token[1].toString().replace(/\n/, '\\n')];
tag = _f[0];
value = _f[1];
return "[" + tag + " " + value + "]";
})());
}
return _a;
return _b;
})();
return puts(strings.join(' '));
};

View file

@ -1,5 +1,5 @@
(function(){
var CoffeeScript, helpers, prompt, run;
var CoffeeScript, helpers, prompt, run, stdin;
// A very simple Read-Eval-Print-Loop. Compiles one line at a time to JavaScript
// and evaluates it. Good for simple tests, or poking around the **Node.js** API.
// Using it looks like this:
@ -18,10 +18,10 @@
// The main REPL function. **run** is called every time a line of code is entered.
// Attempt to evaluate the command. If there's an exception, print it out instead
// of exiting.
run = function run(code) {
run = function run(buffer) {
var val;
try {
val = CoffeeScript.run(code, {
val = CoffeeScript.run(buffer.toString(), {
no_wrap: true,
globals: true,
source: 'repl'
@ -34,8 +34,8 @@
}
return print(prompt);
};
// Start up the REPL by opening **stdio** and listening for input.
process.stdio.addListener('data', run);
process.stdio.open();
// Start up the REPL by opening **stdin** and listening for input.
stdin = process.openStdin();
stdin.addListener('data', run);
print(prompt);
})();

View file

@ -66,5 +66,5 @@ print_tasks: ->
# Print an error and exit when attempting to all an undefined task.
no_such_task: (task) ->
process.stdio.writeError "No such task: \"$task\"\n"
puts "No such task: \"$task\"\n"
process.exit 1

View file

@ -9,6 +9,7 @@ fs: require 'fs'
path: require 'path'
optparse: require './optparse'
CoffeeScript: require './coffee-script'
{spawn: spawn, exec: exec}: require('child_process')
# The help banner that is printed when `coffee` is called without arguments.
BANNER: '''
@ -67,7 +68,9 @@ compile_scripts: ->
path.exists source, (exists) ->
throw new Error "File not found: $source" unless exists
fs.readFile source, (err, code) -> compile_script(source, code)
compile(source) for source in sources
run: -> compile(source) for source in sources
return run() unless options.output and options.compile
exec "mkdir -p $options.output", run
# Compile a single source script, containing the given code, according to the
# requested options. Both compile_scripts and watch_scripts share this method
@ -82,7 +85,7 @@ compile_script: (source, code) ->
else if o.run then CoffeeScript.run code, code_opts
else
js: CoffeeScript.compile code, code_opts
if o.print then process.stdio.write js
if o.print then print js
else if o.compile then write_js source, js
else if o.lint then lint js
catch err
@ -92,10 +95,10 @@ compile_script: (source, code) ->
# and write them back to **stdout**.
compile_stdio: ->
code: ''
process.stdio.open()
process.stdio.addListener 'data', (string) ->
code: + string if string
process.stdio.addListener 'close', ->
stdin: process.openStdin()
stdin.addListener 'data', (buffer) ->
code: + buffer.toString() if buffer
stdin.addListener 'end', ->
compile_script 'stdio', code
# Watch a list of source CoffeeScript files using `fs.watchFile`, recompiling
@ -120,13 +123,12 @@ write_js: (source, js) ->
# Pipe compiled JS through JSLint (requires a working `jsl` command), printing
# any errors or warnings that arise.
lint: (js) ->
jsl: process.createChildProcess('jsl', ['-nologo', '-stdin'])
jsl.addListener 'output', (result) ->
puts result.replace(/\n/g, '') if result
jsl.addListener 'error', (result) ->
puts result if result
jsl.write js
jsl.close()
print_it: (buffer) -> puts buffer.toString()
jsl: spawn 'jsl', ['-nologo', '-stdin']
jsl.stdout.addListener 'data', print_it
jsl.stderr.addListener 'data', print_it
jsl.stdin.write js
jsl.stdin.end()
# Pretty-print a stream of tokens.
print_tokens: (tokens) ->

View file

@ -19,15 +19,15 @@ helpers.extend global, {
# The main REPL function. **run** is called every time a line of code is entered.
# Attempt to evaluate the command. If there's an exception, print it out instead
# of exiting.
run: (code) ->
run: (buffer) ->
try
val: CoffeeScript.run code, {no_wrap: true, globals: true, source: 'repl'}
val: CoffeeScript.run buffer.toString(), {no_wrap: true, globals: true, source: 'repl'}
p val if val isnt undefined
catch err
puts err.stack or err.toString()
print prompt
# Start up the REPL by opening **stdio** and listening for input.
process.stdio.addListener 'data', run
process.stdio.open()
# Start up the REPL by opening **stdin** and listening for input.
stdin: process.openStdin()
stdin.addListener 'data', run
print prompt