From c98fae59fceb060b73d194c4bb4b74a19a991e40 Mon Sep 17 00:00:00 2001 From: Michael Smith Date: Thu, 28 Feb 2013 17:51:55 -0800 Subject: [PATCH] Patch child_process.fork to run .coffee files --- lib/coffee-script/coffee-script.js | 17 ++++++++++++++++- src/coffee-script.coffee | 22 ++++++++++++++++------ test/cluster.coffee | 14 ++++++++++++++ test/test.html | 1 + 4 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 test/cluster.coffee diff --git a/lib/coffee-script/coffee-script.js b/lib/coffee-script/coffee-script.js index bed2722f..023a7f62 100644 --- a/lib/coffee-script/coffee-script.js +++ b/lib/coffee-script/coffee-script.js @@ -1,12 +1,14 @@ // Generated by CoffeeScript 1.5.0 (function() { - var Lexer, compile, ext, fs, helpers, lexer, loadFile, parser, path, vm, _i, _len, _ref, + var Lexer, child_process, compile, ext, fs, helpers, lexer, loadFile, parser, path, vm, _i, _len, _ref, __hasProp = {}.hasOwnProperty; fs = require('fs'); path = require('path'); + child_process = require('child_process'); + Lexer = require('./lexer').Lexer; parser = require('./parser').parser; @@ -33,6 +35,19 @@ } } + (function(fork) { + return child_process.fork = function(modulePath) { + var oldExecPath, output; + oldExecPath = process.execPath; + if (helpers.isCoffee(modulePath)) { + process.execPath = 'coffee'; + } + output = fork.apply(this, arguments); + process.execPath = oldExecPath; + return output; + }; + })(child_process.fork); + exports.VERSION = '1.5.0'; exports.helpers = require('./helpers'); diff --git a/src/coffee-script.coffee b/src/coffee-script.coffee index 1047bfd3..d07c8e9b 100644 --- a/src/coffee-script.coffee +++ b/src/coffee-script.coffee @@ -6,12 +6,13 @@ # If included on a webpage, it will automatically sniff out, compile, and # execute all scripts present in `text/coffeescript` tags. -fs = require 'fs' -path = require 'path' -{Lexer} = require './lexer' -{parser} = require './parser' -helpers = require './helpers' -vm = require 'vm' +fs = require 'fs' +path = require 'path' +child_process = require 'child_process' +{Lexer} = require './lexer' +{parser} = require './parser' +helpers = require './helpers' +vm = require 'vm' # Load and run a CoffeeScript file for Node, stripping any `BOM`s. loadFile = (module, filename) -> @@ -23,6 +24,15 @@ if require.extensions for ext in ['.coffee', '.litcoffee', '.md', '.coffee.md'] require.extensions[ext] = loadFile +# Patch child_process.fork to properly run .coffee files +do (fork = child_process.fork) -> + child_process.fork = (modulePath) -> + oldExecPath = process.execPath + process.execPath = 'coffee' if helpers.isCoffee modulePath + output = fork.apply this, arguments + process.execPath = oldExecPath + output + # The current CoffeeScript version number. exports.VERSION = '1.5.0' diff --git a/test/cluster.coffee b/test/cluster.coffee new file mode 100644 index 00000000..cc6b5555 --- /dev/null +++ b/test/cluster.coffee @@ -0,0 +1,14 @@ +# Cluster Module +# --------- + +unless window? or testingBrowser? + cluster = require('cluster') + + if cluster.isMaster + test "#2737 - cluster module can spawn workers from a coffeescript process", -> + cluster.once 'exit', (worker, code) -> + eq code, 0 + + cluster.fork() + else + process.exit 0 diff --git a/test/test.html b/test/test.html index a09692c9..d5a568be 100644 --- a/test/test.html +++ b/test/test.html @@ -88,6 +88,7 @@ 'assignment' 'booleans' 'classes' + 'cluster' 'comments' 'compilation' 'comprehensions'