From 1cf032618332c5a523423bbc2131dedb091ea9fb Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Sun, 7 Mar 2010 22:17:45 -0500 Subject: [PATCH] unifying the CoffeeScript.compile and CoffeeScript.run apis to be the same -- source code and options hash. --- Cakefile | 4 ++-- lib/cake.js | 8 +++++--- lib/coffee-script.js | 7 +++---- lib/command.js | 9 +++++---- lib/repl.js | 5 +++-- src/cake.coffee | 10 +++++----- src/coffee-script.coffee | 7 +++---- src/command.coffee | 7 ++++--- src/repl.coffee | 2 +- 9 files changed, 31 insertions(+), 28 deletions(-) diff --git a/Cakefile b/Cakefile index 46a0a623..ae66d322 100644 --- a/Cakefile +++ b/Cakefile @@ -1,5 +1,5 @@ fs: require 'fs' -coffee: require 'coffee-script' +CoffeeScript: require 'coffee-script' # Run a CoffeeScript through our node/coffee interpreter. run: (args) -> @@ -74,4 +74,4 @@ task 'test', 'run the CoffeeScript language test suite', -> fs.readdir 'test', (err, files) -> for file in files fs.readFile 'test/' + file, (err, code) -> - coffee.run code, file \ No newline at end of file + CoffeeScript.run code, {source: file} \ No newline at end of file diff --git a/lib/cake.js b/lib/cake.js index 4726e65d..e8664d54 100755 --- a/lib/cake.js +++ b/lib/cake.js @@ -1,5 +1,5 @@ (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; // `cake` is a simplified version of [Make](http://www.gnu.org/software/make/) // ([Rake](http://rake.rubyforge.org/), [Jake](http://github.com/280north/jake)) @@ -10,8 +10,8 @@ // External dependencies. fs = require('fs'); path = require('path'); - coffee = require('coffee-script'); optparse = require('optparse'); + CoffeeScript = require('coffee-script'); // Keep track of the list of defined tasks, the accepted options, and so on. tasks = {}; options = {}; @@ -52,7 +52,9 @@ throw new Error("Cakefile not found in " + (process.cwd())); } 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); if (!(args.length)) { return print_tasks(); diff --git a/lib/coffee-script.js b/lib/coffee-script.js index 5e7b2554..a024328c 100644 --- a/lib/coffee-script.js +++ b/lib/coffee-script.js @@ -43,11 +43,10 @@ }; // Compile and execute a string of CoffeeScript (on the server), correctly // setting `__filename`, `__dirname`, and relative `require()`. - exports.run = function run(code, source, options) { + exports.run = function run(code, options) { var __dirname, __filename; - options = options || {}; - module.filename = (__filename = (options.source = source)); - __dirname = path.dirname(source); + module.filename = (__filename = options.source); + __dirname = path.dirname(__filename); return eval(exports.compile(code, options)); }; // The real Lexer produces a generic stream of tokens. This object provides a diff --git a/lib/command.js b/lib/command.js index 56a0ac29..0100c18b 100644 --- a/lib/command.js +++ b/lib/command.js @@ -37,7 +37,7 @@ return compile_stdio(); } if (options.eval) { - return compile_script('unknown', sources[0]); + return compile_script('console', sources[0]); } if (!(sources.length)) { return usage(); @@ -80,17 +80,18 @@ // in common. If evaluating the script directly sets `__filename`, `__dirname` // and `module.filename` to be correct relative to the script's path. compile_script = function compile_script(source, code) { - var js, o; + var code_opts, js, o; o = options; + code_opts = compile_options(source); try { if (o.tokens) { return print_tokens(CoffeeScript.tokens(code)); } else if (o.nodes) { return puts(CoffeeScript.nodes(code).toString()); } else if (o.run) { - return CoffeeScript.run(code, source, compile_options()); + return CoffeeScript.run(code, code_opts); } else { - js = CoffeeScript.compile(code, compile_options(source)); + js = CoffeeScript.compile(code, code_opts); if (o.compile) { return write_js(source, js); } else if (o.lint) { diff --git a/lib/repl.js b/lib/repl.js index 68681a6d..fd751be0 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -20,9 +20,10 @@ run = function run(code) { var val; try { - val = CoffeeScript.run(code, 'repl', { + val = CoffeeScript.run(code, { no_wrap: true, - globals: true + globals: true, + source: 'repl' }); if (val !== undefined) { p(val); diff --git a/src/cake.coffee b/src/cake.coffee index 3b13cea7..07dec7d1 100644 --- a/src/cake.coffee +++ b/src/cake.coffee @@ -7,10 +7,10 @@ # current directory's Cakefile. # External dependencies. -fs: require 'fs' -path: require 'path' -coffee: require 'coffee-script' -optparse: require 'optparse' +fs: require 'fs' +path: require 'path' +optparse: require 'optparse' +CoffeeScript: require 'coffee-script' # Keep track of the list of defined tasks, the accepted options, and so on. tasks: {} @@ -46,7 +46,7 @@ exports.run: -> path.exists 'Cakefile', (exists) -> throw new Error("Cakefile not found in ${process.cwd()}") unless exists 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 return print_tasks() unless args.length options: oparse.parse(args) diff --git a/src/coffee-script.coffee b/src/coffee-script.coffee index 62d274d8..cec6e4f5 100644 --- a/src/coffee-script.coffee +++ b/src/coffee-script.coffee @@ -41,10 +41,9 @@ exports.nodes: (code) -> # Compile and execute a string of CoffeeScript (on the server), correctly # setting `__filename`, `__dirname`, and relative `require()`. -exports.run: (code, source, options) -> - options ||= {} - module.filename: __filename: options.source: source - __dirname: path.dirname source +exports.run: (code, options) -> + module.filename: __filename: options.source + __dirname: path.dirname __filename eval exports.compile code, options # The real Lexer produces a generic stream of tokens. This object provides a diff --git a/src/command.coffee b/src/command.coffee index 60bf0b42..899cd88c 100644 --- a/src/command.coffee +++ b/src/command.coffee @@ -49,7 +49,7 @@ exports.run: -> return version() if options.version return require 'repl' if options.interactive 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 separator: sources.indexOf '--' flags: [] @@ -75,12 +75,13 @@ compile_scripts: -> # and `module.filename` to be correct relative to the script's path. compile_script: (source, code) -> o: options + code_opts: compile_options source try if o.tokens then print_tokens CoffeeScript.tokens code 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 - js: CoffeeScript.compile code, compile_options(source) + js: CoffeeScript.compile code, code_opts if o.compile then write_js source, js else if o.lint then lint js else if o.print or o.eval then print js diff --git a/src/repl.coffee b/src/repl.coffee index d5276b88..f2f789cf 100644 --- a/src/repl.coffee +++ b/src/repl.coffee @@ -20,7 +20,7 @@ process.mixin { # of exiting. run: (code) -> 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 catch err puts err.stack or err.toString()