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:
parent
065bf54094
commit
8317960f81
8 changed files with 81 additions and 73 deletions
7
Cakefile
7
Cakefile
|
@ -1,10 +1,11 @@
|
||||||
fs: require 'fs'
|
fs: require 'fs'
|
||||||
helpers: require('./lib/helpers').helpers
|
helpers: require('./lib/helpers').helpers
|
||||||
CoffeeScript: require './lib/coffee-script'
|
CoffeeScript: require './lib/coffee-script'
|
||||||
|
{spawn: spawn, exec: exec}: require('child_process')
|
||||||
|
|
||||||
# Run a CoffeeScript through our node/coffee interpreter.
|
# Run a CoffeeScript through our node/coffee interpreter.
|
||||||
run: (args) ->
|
run: (args) ->
|
||||||
proc: process.createChildProcess 'bin/coffee', args
|
proc: spawn 'bin/coffee', args
|
||||||
proc.addListener 'error', (err) -> if err then puts err
|
proc.addListener 'error', (err) -> if err then puts err
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@ http: require 'http'
|
||||||
server: http.createServer (req, res) ->
|
server: http.createServer (req, res) ->
|
||||||
res.writeHeader 200, {'Content-Type': 'text/plain'}
|
res.writeHeader 200, {'Content-Type': 'text/plain'}
|
||||||
res.write 'Hello, World!'
|
res.write 'Hello, World!'
|
||||||
res.close()
|
res.end()
|
||||||
|
|
||||||
server.listen 3000
|
server.listen 3000
|
||||||
|
|
||||||
|
|
|
@ -100,7 +100,7 @@
|
||||||
};
|
};
|
||||||
// Print an error and exit when attempting to all an undefined task.
|
// Print an error and exit when attempting to all an undefined task.
|
||||||
no_such_task = function no_such_task(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);
|
return process.exit(1);
|
||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
(function(){
|
(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
|
// The `coffee` utility. Handles command-line compilation of CoffeeScript
|
||||||
// into various forms: saved into `.js` files or printed to stdout, piped to
|
// into various forms: saved into `.js` files or printed to stdout, piped to
|
||||||
// [JSLint](http://javascriptlint.com/) or recompiled every time the source is
|
// [JSLint](http://javascriptlint.com/) or recompiled every time the source is
|
||||||
|
@ -10,6 +10,9 @@
|
||||||
path = require('path');
|
path = require('path');
|
||||||
optparse = require('./optparse');
|
optparse = require('./optparse');
|
||||||
CoffeeScript = require('./coffee-script');
|
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.
|
// 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';
|
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.
|
// 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
|
// Asynchronously read in each CoffeeScript in a list of source files and
|
||||||
// compile them.
|
// compile them.
|
||||||
compile_scripts = function compile_scripts() {
|
compile_scripts = function compile_scripts() {
|
||||||
var _a, _b, _c, _d, compile, source;
|
var compile, run;
|
||||||
compile = function compile(source) {
|
compile = function compile(source) {
|
||||||
return path.exists(source, function(exists) {
|
return path.exists(source, function(exists) {
|
||||||
if (!(exists)) {
|
if (!(exists)) {
|
||||||
|
@ -68,12 +71,19 @@
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
_a = []; _c = sources;
|
run = function run() {
|
||||||
for (_b = 0, _d = _c.length; _b < _d; _b++) {
|
var _b, _c, _d, _e, source;
|
||||||
source = _c[_b];
|
_b = []; _d = sources;
|
||||||
_a.push(compile(source));
|
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
|
// Compile a single source script, containing the given code, according to the
|
||||||
// requested options. Both compile_scripts and watch_scripts share this method
|
// requested options. Both compile_scripts and watch_scripts share this method
|
||||||
|
@ -93,7 +103,7 @@
|
||||||
} else {
|
} else {
|
||||||
js = CoffeeScript.compile(code, code_opts);
|
js = CoffeeScript.compile(code, code_opts);
|
||||||
if (o.print) {
|
if (o.print) {
|
||||||
return process.stdio.write(js);
|
return print(js);
|
||||||
} else if (o.compile) {
|
} else if (o.compile) {
|
||||||
return write_js(source, js);
|
return write_js(source, js);
|
||||||
} else if (o.lint) {
|
} else if (o.lint) {
|
||||||
|
@ -111,15 +121,15 @@
|
||||||
// Attach the appropriate listeners to compile scripts incoming over **stdin**,
|
// Attach the appropriate listeners to compile scripts incoming over **stdin**,
|
||||||
// and write them back to **stdout**.
|
// and write them back to **stdout**.
|
||||||
compile_stdio = function compile_stdio() {
|
compile_stdio = function compile_stdio() {
|
||||||
var code;
|
var code, stdin;
|
||||||
code = '';
|
code = '';
|
||||||
process.stdio.open();
|
stdin = process.openStdin();
|
||||||
process.stdio.addListener('data', function(string) {
|
stdin.addListener('data', function(buffer) {
|
||||||
if (string) {
|
if (buffer) {
|
||||||
return code += string;
|
return code += buffer.toString();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return process.stdio.addListener('close', function() {
|
return stdin.addListener('end', function() {
|
||||||
return compile_script('stdio', code);
|
return compile_script('stdio', code);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@ -127,7 +137,7 @@
|
||||||
// them every time the files are updated. May be used in combination with other
|
// them every time the files are updated. May be used in combination with other
|
||||||
// options, such as `--lint` or `--print`.
|
// options, such as `--lint` or `--print`.
|
||||||
watch_scripts = function watch_scripts() {
|
watch_scripts = function watch_scripts() {
|
||||||
var _a, _b, _c, _d, source, watch;
|
var _b, _c, _d, _e, source, watch;
|
||||||
watch = function watch(source) {
|
watch = function watch(source) {
|
||||||
return fs.watchFile(source, {
|
return fs.watchFile(source, {
|
||||||
persistent: true,
|
persistent: true,
|
||||||
|
@ -141,12 +151,12 @@
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
_a = []; _c = sources;
|
_b = []; _d = sources;
|
||||||
for (_b = 0, _d = _c.length; _b < _d; _b++) {
|
for (_c = 0, _e = _d.length; _c < _e; _c++) {
|
||||||
source = _c[_b];
|
source = _d[_c];
|
||||||
_a.push(watch(source));
|
_b.push(watch(source));
|
||||||
}
|
}
|
||||||
return _a;
|
return _b;
|
||||||
};
|
};
|
||||||
// Write out a JavaScript source file with the compiled code. By default, files
|
// 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
|
// 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
|
// Pipe compiled JS through JSLint (requires a working `jsl` command), printing
|
||||||
// any errors or warnings that arise.
|
// any errors or warnings that arise.
|
||||||
lint = function lint(js) {
|
lint = function lint(js) {
|
||||||
var jsl;
|
var jsl, print_it;
|
||||||
jsl = process.createChildProcess('jsl', ['-nologo', '-stdin']);
|
print_it = function print_it(buffer) {
|
||||||
jsl.addListener('output', function(result) {
|
return puts(buffer.toString());
|
||||||
if (result) {
|
};
|
||||||
return puts(result.replace(/\n/g, ''));
|
jsl = spawn('jsl', ['-nologo', '-stdin']);
|
||||||
}
|
jsl.stdout.addListener('data', print_it);
|
||||||
});
|
jsl.stderr.addListener('data', print_it);
|
||||||
jsl.addListener('error', function(result) {
|
jsl.stdin.write(js);
|
||||||
if (result) {
|
return jsl.stdin.end();
|
||||||
return puts(result);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
jsl.write(js);
|
|
||||||
return jsl.close();
|
|
||||||
};
|
};
|
||||||
// Pretty-print a stream of tokens.
|
// Pretty-print a stream of tokens.
|
||||||
print_tokens = function print_tokens(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() {
|
strings = (function() {
|
||||||
_a = []; _c = tokens;
|
_b = []; _d = tokens;
|
||||||
for (_b = 0, _d = _c.length; _b < _d; _b++) {
|
for (_c = 0, _e = _d.length; _c < _e; _c++) {
|
||||||
token = _c[_b];
|
token = _d[_c];
|
||||||
_a.push((function() {
|
_b.push((function() {
|
||||||
_e = [token[0], token[1].toString().replace(/\n/, '\\n')];
|
_f = [token[0], token[1].toString().replace(/\n/, '\\n')];
|
||||||
tag = _e[0];
|
tag = _f[0];
|
||||||
value = _e[1];
|
value = _f[1];
|
||||||
return "[" + tag + " " + value + "]";
|
return "[" + tag + " " + value + "]";
|
||||||
})());
|
})());
|
||||||
}
|
}
|
||||||
return _a;
|
return _b;
|
||||||
})();
|
})();
|
||||||
return puts(strings.join(' '));
|
return puts(strings.join(' '));
|
||||||
};
|
};
|
||||||
|
|
12
lib/repl.js
12
lib/repl.js
|
@ -1,5 +1,5 @@
|
||||||
(function(){
|
(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
|
// 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.
|
// and evaluates it. Good for simple tests, or poking around the **Node.js** API.
|
||||||
// Using it looks like this:
|
// Using it looks like this:
|
||||||
|
@ -18,10 +18,10 @@
|
||||||
// The main REPL function. **run** is called every time a line of code is entered.
|
// 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
|
// Attempt to evaluate the command. If there's an exception, print it out instead
|
||||||
// of exiting.
|
// of exiting.
|
||||||
run = function run(code) {
|
run = function run(buffer) {
|
||||||
var val;
|
var val;
|
||||||
try {
|
try {
|
||||||
val = CoffeeScript.run(code, {
|
val = CoffeeScript.run(buffer.toString(), {
|
||||||
no_wrap: true,
|
no_wrap: true,
|
||||||
globals: true,
|
globals: true,
|
||||||
source: 'repl'
|
source: 'repl'
|
||||||
|
@ -34,8 +34,8 @@
|
||||||
}
|
}
|
||||||
return print(prompt);
|
return print(prompt);
|
||||||
};
|
};
|
||||||
// Start up the REPL by opening **stdio** and listening for input.
|
// Start up the REPL by opening **stdin** and listening for input.
|
||||||
process.stdio.addListener('data', run);
|
stdin = process.openStdin();
|
||||||
process.stdio.open();
|
stdin.addListener('data', run);
|
||||||
print(prompt);
|
print(prompt);
|
||||||
})();
|
})();
|
||||||
|
|
|
@ -66,5 +66,5 @@ print_tasks: ->
|
||||||
|
|
||||||
# Print an error and exit when attempting to all an undefined task.
|
# Print an error and exit when attempting to all an undefined task.
|
||||||
no_such_task: (task) ->
|
no_such_task: (task) ->
|
||||||
process.stdio.writeError "No such task: \"$task\"\n"
|
puts "No such task: \"$task\"\n"
|
||||||
process.exit 1
|
process.exit 1
|
||||||
|
|
|
@ -9,6 +9,7 @@ fs: require 'fs'
|
||||||
path: require 'path'
|
path: require 'path'
|
||||||
optparse: require './optparse'
|
optparse: require './optparse'
|
||||||
CoffeeScript: require './coffee-script'
|
CoffeeScript: require './coffee-script'
|
||||||
|
{spawn: spawn, exec: exec}: require('child_process')
|
||||||
|
|
||||||
# The help banner that is printed when `coffee` is called without arguments.
|
# The help banner that is printed when `coffee` is called without arguments.
|
||||||
BANNER: '''
|
BANNER: '''
|
||||||
|
@ -67,7 +68,9 @@ compile_scripts: ->
|
||||||
path.exists source, (exists) ->
|
path.exists source, (exists) ->
|
||||||
throw new Error "File not found: $source" unless exists
|
throw new Error "File not found: $source" unless exists
|
||||||
fs.readFile source, (err, code) -> compile_script(source, code)
|
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
|
# Compile a single source script, containing the given code, according to the
|
||||||
# requested options. Both compile_scripts and watch_scripts share this method
|
# 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 if o.run then CoffeeScript.run code, code_opts
|
||||||
else
|
else
|
||||||
js: CoffeeScript.compile code, code_opts
|
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.compile then write_js source, js
|
||||||
else if o.lint then lint js
|
else if o.lint then lint js
|
||||||
catch err
|
catch err
|
||||||
|
@ -92,10 +95,10 @@ compile_script: (source, code) ->
|
||||||
# and write them back to **stdout**.
|
# and write them back to **stdout**.
|
||||||
compile_stdio: ->
|
compile_stdio: ->
|
||||||
code: ''
|
code: ''
|
||||||
process.stdio.open()
|
stdin: process.openStdin()
|
||||||
process.stdio.addListener 'data', (string) ->
|
stdin.addListener 'data', (buffer) ->
|
||||||
code: + string if string
|
code: + buffer.toString() if buffer
|
||||||
process.stdio.addListener 'close', ->
|
stdin.addListener 'end', ->
|
||||||
compile_script 'stdio', code
|
compile_script 'stdio', code
|
||||||
|
|
||||||
# Watch a list of source CoffeeScript files using `fs.watchFile`, recompiling
|
# 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
|
# 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) ->
|
||||||
jsl: process.createChildProcess('jsl', ['-nologo', '-stdin'])
|
print_it: (buffer) -> puts buffer.toString()
|
||||||
jsl.addListener 'output', (result) ->
|
jsl: spawn 'jsl', ['-nologo', '-stdin']
|
||||||
puts result.replace(/\n/g, '') if result
|
jsl.stdout.addListener 'data', print_it
|
||||||
jsl.addListener 'error', (result) ->
|
jsl.stderr.addListener 'data', print_it
|
||||||
puts result if result
|
jsl.stdin.write js
|
||||||
jsl.write js
|
jsl.stdin.end()
|
||||||
jsl.close()
|
|
||||||
|
|
||||||
# Pretty-print a stream of tokens.
|
# Pretty-print a stream of tokens.
|
||||||
print_tokens: (tokens) ->
|
print_tokens: (tokens) ->
|
||||||
|
|
|
@ -19,15 +19,15 @@ helpers.extend global, {
|
||||||
# The main REPL function. **run** is called every time a line of code is entered.
|
# 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
|
# Attempt to evaluate the command. If there's an exception, print it out instead
|
||||||
# of exiting.
|
# of exiting.
|
||||||
run: (code) ->
|
run: (buffer) ->
|
||||||
try
|
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
|
p val if val isnt undefined
|
||||||
catch err
|
catch err
|
||||||
puts err.stack or err.toString()
|
puts err.stack or err.toString()
|
||||||
print prompt
|
print prompt
|
||||||
|
|
||||||
# Start up the REPL by opening **stdio** and listening for input.
|
# Start up the REPL by opening **stdin** and listening for input.
|
||||||
process.stdio.addListener 'data', run
|
stdin: process.openStdin()
|
||||||
process.stdio.open()
|
stdin.addListener 'data', run
|
||||||
print prompt
|
print prompt
|
Loading…
Add table
Reference in a new issue