From 73aaf127c84d2b7fbf95006af5477fd8e9e4cce7 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Thu, 24 Dec 2009 23:57:27 -0800 Subject: [PATCH] removed bin/cs in favor of a more comprehensive coffee-script command ... now with --interactive and --run --- Rakefile | 5 ++--- bin/cs | 3 --- lib/coffee_script/command_line.rb | 21 ++++++++++++++++++- lib/coffee_script/narwhal/coffee-script.cs | 2 +- .../narwhal/js}/coffee-script.js | 4 ++-- lib/coffee_script/narwhal/js/launcher.js | 3 +++ .../coffee_script/narwhal/js}/loader.js | 2 +- lib/coffee_script/narwhal/launcher.cs | 1 + package.json | 4 ++-- test/unit/test_execution.rb | 2 +- 10 files changed, 33 insertions(+), 14 deletions(-) delete mode 100755 bin/cs rename {lib-js => lib/coffee_script/narwhal/js}/coffee-script.js (94%) create mode 100644 lib/coffee_script/narwhal/js/launcher.js rename {lib-js/coffee-script => lib/coffee_script/narwhal/js}/loader.js (99%) create mode 100644 lib/coffee_script/narwhal/launcher.cs diff --git a/Rakefile b/Rakefile index 553194a9..6f508bde 100644 --- a/Rakefile +++ b/Rakefile @@ -17,10 +17,9 @@ namespace :build do sh "racc #{args[:extra_args]} -o lib/coffee_script/parser.rb lib/coffee_script/grammar.y" end - desc "Compile the Narwhal interface for bin/cs" + desc "Compile the Narwhal interface for --interactive and --run" task :narwhal do - sh "bin/coffee-script lib/coffee_script/narwhal/coffee-script.cs --print > lib-js/coffee-script.js" - sh "bin/coffee-script lib/coffee_script/narwhal/loader.cs --print > lib-js/coffee-script/loader.js" + sh "bin/coffee-script lib/coffee_script/narwhal/*.cs -o lib/coffee_script/narwhal/js" end end diff --git a/bin/cs b/bin/cs deleted file mode 100755 index b59bb2ab..00000000 --- a/bin/cs +++ /dev/null @@ -1,3 +0,0 @@ -#!/usr/bin/env narwhal - -require("coffee-script").run(system.args); diff --git a/lib/coffee_script/command_line.rb b/lib/coffee_script/command_line.rb index 7499cf81..f7bf6c94 100644 --- a/lib/coffee_script/command_line.rb +++ b/lib/coffee_script/command_line.rb @@ -23,8 +23,10 @@ Usage: def initialize @mtimes = {} parse_options + return launch_repl if @options[:interactive] return eval_scriptlet if @options[:eval] check_sources + return run_scripts if @options[:run] @sources.each {|source| compile_javascript(source) } watch_coffee_scripts if @options[:watch] end @@ -100,6 +102,17 @@ Usage: puts js end + # Use Narwhal to run an interactive CoffeeScript session. + def launch_repl + exec "narwhal lib/coffee_script/narwhal/js/launcher.js" + end + + # Use Narwhal to compile and execute CoffeeScripts. + def run_scripts + sources = @sources.join(' ') + exec "narwhal lib/coffee_script/narwhal/js/launcher.js #{sources}" + end + # Print the tokens that the lexer generates from a source script. def tokens(script) puts Lexer.new.tokenize(script).inspect @@ -134,6 +147,12 @@ Usage: def parse_options @options = {} @option_parser = OptionParser.new do |opts| + opts.on('-i', '--interactive', 'run a CoffeeScript REPL (requires Narwhal)') do |i| + @options[:interactive] = true + end + opts.on('-r', '--run', 'compile and run a script (requires Narwhal)') do |r| + @options[:run] = true + end opts.on('-o', '--output [DIR]', 'set the directory for compiled JavaScript') do |d| @options[:output] = d FileUtils.mkdir_p(d) unless File.exists?(d) @@ -147,7 +166,7 @@ Usage: opts.on('-l', '--lint', 'pipe the compiled JavaScript through JSLint') do |l| @options[:lint] = true end - opts.on('-e', '--eval', 'eval a little scriptlet or read from stdin') do |e| + opts.on('-e', '--eval', 'compile a cli scriptlet or read from stdin') do |e| @options[:eval] = true end opts.on('-t', '--tokens', 'print the tokens that the lexer produces') do |t| diff --git a/lib/coffee_script/narwhal/coffee-script.cs b/lib/coffee_script/narwhal/coffee-script.cs index 09706312..fe474a7f 100644 --- a/lib/coffee_script/narwhal/coffee-script.cs +++ b/lib/coffee_script/narwhal/coffee-script.cs @@ -9,7 +9,7 @@ File: require('file') Readline: require('readline') # The path to the CoffeeScript Compiler. -coffeePath: File.path(module.path).dirname().dirname().join('bin', 'coffee-script') +coffeePath: File.path(module.path).dirname().dirname().dirname().dirname().dirname().join('bin', 'coffee-script') # Our general-purpose error handler. checkForErrors: coffeeProcess => diff --git a/lib-js/coffee-script.js b/lib/coffee_script/narwhal/js/coffee-script.js similarity index 94% rename from lib-js/coffee-script.js rename to lib/coffee_script/narwhal/js/coffee-script.js index 0f1e2e22..34f16f6a 100644 --- a/lib-js/coffee-script.js +++ b/lib/coffee_script/narwhal/js/coffee-script.js @@ -6,7 +6,7 @@ var File = require('file'); var Readline = require('readline'); // The path to the CoffeeScript Compiler. - var coffeePath = File.path(module.path).dirname().dirname().join('bin', 'coffee-script'); + var coffeePath = File.path(module.path).dirname().dirname().dirname().dirname().dirname().join('bin', 'coffee-script'); // Our general-purpose error handler. var checkForErrors = function(coffeeProcess) { if (coffeeProcess.wait() === 0) { @@ -62,4 +62,4 @@ return eval("(" + factoryText + ")"); } }; -})(); +})(); \ No newline at end of file diff --git a/lib/coffee_script/narwhal/js/launcher.js b/lib/coffee_script/narwhal/js/launcher.js new file mode 100644 index 00000000..953c677e --- /dev/null +++ b/lib/coffee_script/narwhal/js/launcher.js @@ -0,0 +1,3 @@ +(function(){ + require("coffee-script").run(system.args); +})(); \ No newline at end of file diff --git a/lib-js/coffee-script/loader.js b/lib/coffee_script/narwhal/js/loader.js similarity index 99% rename from lib-js/coffee-script/loader.js rename to lib/coffee_script/narwhal/js/loader.js index 6e08cd61..6db0e791 100644 --- a/lib-js/coffee-script/loader.js +++ b/lib/coffee_script/narwhal/js/loader.js @@ -17,4 +17,4 @@ } }; require.loader.loaders.unshift([".cs", loader]); -})(); +})(); \ No newline at end of file diff --git a/lib/coffee_script/narwhal/launcher.cs b/lib/coffee_script/narwhal/launcher.cs new file mode 100644 index 00000000..26fe7ac3 --- /dev/null +++ b/lib/coffee_script/narwhal/launcher.cs @@ -0,0 +1 @@ +require("coffee-script").run(system.args) \ No newline at end of file diff --git a/package.json b/package.json index 4847572d..e81ae93d 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "coffee-script", - "lib": "lib-js", - "preload": ["coffee-script/loader"], + "lib": "lib/coffee_script/narwhal/js", + "preload": ["loader"], "description": "Unfancy JavaScript", "keywords": ["javascript", "language"], "author": "Jeremy Ashkenas", diff --git a/test/unit/test_execution.rb b/test/unit/test_execution.rb index 9ec4ab4d..26e7975b 100644 --- a/test/unit/test_execution.rb +++ b/test/unit/test_execution.rb @@ -7,7 +7,7 @@ class ExecutionTest < Test::Unit::TestCase def test_execution_of_coffeescript sources = ['test/fixtures/execution/*.cs'].join(' ') - assert `bin/cs #{sources}`.match(ALLS_WELL) + assert `bin/coffee-script -r #{sources}`.match(ALLS_WELL) end def test_lintless_coffeescript