unifying the CoffeeScript.compile and CoffeeScript.run apis to be the same -- source code and options hash.

This commit is contained in:
Jeremy Ashkenas
2010-03-07 22:17:45 -05:00
parent 5b9ebd19d5
commit 1cf0326183
9 changed files with 31 additions and 28 deletions

View File

@@ -1,5 +1,5 @@
fs: require 'fs' fs: require 'fs'
coffee: require 'coffee-script' CoffeeScript: require 'coffee-script'
# Run a CoffeeScript through our node/coffee interpreter. # Run a CoffeeScript through our node/coffee interpreter.
run: (args) -> run: (args) ->
@@ -74,4 +74,4 @@ task 'test', 'run the CoffeeScript language test suite', ->
fs.readdir 'test', (err, files) -> fs.readdir 'test', (err, files) ->
for file in files for file in files
fs.readFile 'test/' + file, (err, code) -> fs.readFile 'test/' + file, (err, code) ->
coffee.run code, file CoffeeScript.run code, {source: file}

View File

@@ -1,5 +1,5 @@
(function(){ (function(){
var coffee, fs, no_such_task, oparse, options, optparse, path, print_tasks, switches, tasks; var CoffeeScript, fs, no_such_task, oparse, options, optparse, path, print_tasks, switches, tasks;
var __hasProp = Object.prototype.hasOwnProperty; var __hasProp = Object.prototype.hasOwnProperty;
// `cake` is a simplified version of [Make](http://www.gnu.org/software/make/) // `cake` is a simplified version of [Make](http://www.gnu.org/software/make/)
// ([Rake](http://rake.rubyforge.org/), [Jake](http://github.com/280north/jake)) // ([Rake](http://rake.rubyforge.org/), [Jake](http://github.com/280north/jake))
@@ -10,8 +10,8 @@
// External dependencies. // External dependencies.
fs = require('fs'); fs = require('fs');
path = require('path'); path = require('path');
coffee = require('coffee-script');
optparse = require('optparse'); optparse = require('optparse');
CoffeeScript = require('coffee-script');
// Keep track of the list of defined tasks, the accepted options, and so on. // Keep track of the list of defined tasks, the accepted options, and so on.
tasks = {}; tasks = {};
options = {}; options = {};
@@ -52,7 +52,9 @@
throw new Error("Cakefile not found in " + (process.cwd())); throw new Error("Cakefile not found in " + (process.cwd()));
} }
args = process.argv.slice(2, process.argv.length); args = process.argv.slice(2, process.argv.length);
coffee.run(fs.readFileSync('Cakefile'), 'Cakefile'); CoffeeScript.run(fs.readFileSync('Cakefile'), {
source: 'Cakefile'
});
oparse = new optparse.OptionParser(switches); oparse = new optparse.OptionParser(switches);
if (!(args.length)) { if (!(args.length)) {
return print_tasks(); return print_tasks();

View File

@@ -43,11 +43,10 @@
}; };
// Compile and execute a string of CoffeeScript (on the server), correctly // Compile and execute a string of CoffeeScript (on the server), correctly
// setting `__filename`, `__dirname`, and relative `require()`. // setting `__filename`, `__dirname`, and relative `require()`.
exports.run = function run(code, source, options) { exports.run = function run(code, options) {
var __dirname, __filename; var __dirname, __filename;
options = options || {}; module.filename = (__filename = options.source);
module.filename = (__filename = (options.source = source)); __dirname = path.dirname(__filename);
__dirname = path.dirname(source);
return eval(exports.compile(code, options)); return eval(exports.compile(code, options));
}; };
// The real Lexer produces a generic stream of tokens. This object provides a // The real Lexer produces a generic stream of tokens. This object provides a

View File

@@ -37,7 +37,7 @@
return compile_stdio(); return compile_stdio();
} }
if (options.eval) { if (options.eval) {
return compile_script('unknown', sources[0]); return compile_script('console', sources[0]);
} }
if (!(sources.length)) { if (!(sources.length)) {
return usage(); return usage();
@@ -80,17 +80,18 @@
// in common. If evaluating the script directly sets `__filename`, `__dirname` // in common. If evaluating the script directly sets `__filename`, `__dirname`
// and `module.filename` to be correct relative to the script's path. // and `module.filename` to be correct relative to the script's path.
compile_script = function compile_script(source, code) { compile_script = function compile_script(source, code) {
var js, o; var code_opts, js, o;
o = options; o = options;
code_opts = compile_options(source);
try { try {
if (o.tokens) { if (o.tokens) {
return print_tokens(CoffeeScript.tokens(code)); return print_tokens(CoffeeScript.tokens(code));
} else if (o.nodes) { } else if (o.nodes) {
return puts(CoffeeScript.nodes(code).toString()); return puts(CoffeeScript.nodes(code).toString());
} else if (o.run) { } else if (o.run) {
return CoffeeScript.run(code, source, compile_options()); return CoffeeScript.run(code, code_opts);
} else { } else {
js = CoffeeScript.compile(code, compile_options(source)); js = CoffeeScript.compile(code, code_opts);
if (o.compile) { if (o.compile) {
return write_js(source, js); return write_js(source, js);
} else if (o.lint) { } else if (o.lint) {

View File

@@ -20,9 +20,10 @@
run = function run(code) { run = function run(code) {
var val; var val;
try { try {
val = CoffeeScript.run(code, 'repl', { val = CoffeeScript.run(code, {
no_wrap: true, no_wrap: true,
globals: true globals: true,
source: 'repl'
}); });
if (val !== undefined) { if (val !== undefined) {
p(val); p(val);

View File

@@ -7,10 +7,10 @@
# current directory's Cakefile. # current directory's Cakefile.
# External dependencies. # External dependencies.
fs: require 'fs' fs: require 'fs'
path: require 'path' path: require 'path'
coffee: require 'coffee-script' optparse: require 'optparse'
optparse: require 'optparse' CoffeeScript: require 'coffee-script'
# Keep track of the list of defined tasks, the accepted options, and so on. # Keep track of the list of defined tasks, the accepted options, and so on.
tasks: {} tasks: {}
@@ -46,7 +46,7 @@ exports.run: ->
path.exists 'Cakefile', (exists) -> path.exists 'Cakefile', (exists) ->
throw new Error("Cakefile not found in ${process.cwd()}") unless exists throw new Error("Cakefile not found in ${process.cwd()}") unless exists
args: process.argv[2...process.argv.length] args: process.argv[2...process.argv.length]
coffee.run fs.readFileSync('Cakefile'), 'Cakefile' CoffeeScript.run fs.readFileSync('Cakefile'), {source: 'Cakefile'}
oparse: new optparse.OptionParser switches oparse: new optparse.OptionParser switches
return print_tasks() unless args.length return print_tasks() unless args.length
options: oparse.parse(args) options: oparse.parse(args)

View File

@@ -41,10 +41,9 @@ exports.nodes: (code) ->
# Compile and execute a string of CoffeeScript (on the server), correctly # Compile and execute a string of CoffeeScript (on the server), correctly
# setting `__filename`, `__dirname`, and relative `require()`. # setting `__filename`, `__dirname`, and relative `require()`.
exports.run: (code, source, options) -> exports.run: (code, options) ->
options ||= {} module.filename: __filename: options.source
module.filename: __filename: options.source: source __dirname: path.dirname __filename
__dirname: path.dirname source
eval exports.compile code, options eval exports.compile code, options
# The real Lexer produces a generic stream of tokens. This object provides a # The real Lexer produces a generic stream of tokens. This object provides a

View File

@@ -49,7 +49,7 @@ exports.run: ->
return version() if options.version return version() if options.version
return require 'repl' if options.interactive return require 'repl' if options.interactive
return compile_stdio() if options.stdio return compile_stdio() if options.stdio
return compile_script 'unknown', sources[0] if options.eval return compile_script 'console', sources[0] if options.eval
return usage() unless sources.length return usage() unless sources.length
separator: sources.indexOf '--' separator: sources.indexOf '--'
flags: [] flags: []
@@ -75,12 +75,13 @@ compile_scripts: ->
# and `module.filename` to be correct relative to the script's path. # and `module.filename` to be correct relative to the script's path.
compile_script: (source, code) -> compile_script: (source, code) ->
o: options o: options
code_opts: compile_options source
try try
if o.tokens then print_tokens CoffeeScript.tokens code if o.tokens then print_tokens CoffeeScript.tokens code
else if o.nodes then puts CoffeeScript.nodes(code).toString() else if o.nodes then puts CoffeeScript.nodes(code).toString()
else if o.run then CoffeeScript.run code, source, compile_options() else if o.run then CoffeeScript.run code, code_opts
else else
js: CoffeeScript.compile code, compile_options(source) js: CoffeeScript.compile code, code_opts
if o.compile then write_js source, js if o.compile then write_js source, js
else if o.lint then lint js else if o.lint then lint js
else if o.print or o.eval then print js else if o.print or o.eval then print js

View File

@@ -20,7 +20,7 @@ process.mixin {
# of exiting. # of exiting.
run: (code) -> run: (code) ->
try try
val: CoffeeScript.run code, 'repl', {no_wrap: true, globals: true} val: CoffeeScript.run code, {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()