From 390827b7a40e5625662c5bf3cc927213ee6a88e0 Mon Sep 17 00:00:00 2001 From: Bruno Jouhier Date: Sun, 18 Oct 2015 00:45:49 +0200 Subject: [PATCH] issue #291 - previous fix was incomplete - republishing as 1.0.4 --- lib/command.js | 35 +++++++++++++++++++++++++++++------ package.json | 2 +- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/lib/command.js b/lib/command.js index e2a5271a..5b5aa493 100644 --- a/lib/command.js +++ b/lib/command.js @@ -10,7 +10,7 @@ // handles _node and _coffee command line options var fs = require('fs'); -var path = require('path'); +var fsp = require('path'); var util = require('./util'); var register = require('./register'); var Module = require('module'); @@ -70,6 +70,28 @@ function parseOptions(argv) { return util.getOptions(util.extend(util.envOptions(), options)); }; +// babel uses process.cwd() to locate its plugins. +// We have to fool it so that globally installed _node / _coffee can load the streamline plugin. +// Fortunately it caches the result of the first process.cwd() call (see tryRequire implementation) +// So we monkey patch process.cwd, execute a dummy transform, and then restore process.cwd +function dummyTransform() { + var cwd = process.cwd; + process.cwd = function() { + return fsp.join(__dirname, '..'); + } + try { + require('babel').transform("(function(_) {})", { + plugins: ['streamline'], + extra: { + streamline: { + quiet: true, + } + } + }); + } catch (ex) {} + process.cwd = cwd; +} + function runScript(options) { var filename = options.args[0]; @@ -85,17 +107,18 @@ function runScript(options) { // helper functions to resolve these guys! // https://github.com/joyent/node/blob/master/lib/module.js // Except we need to tell Node that these are paths, not native modules. - filename = path.resolve(filename || '.'); + filename = fsp.resolve(filename || '.'); mainModule.filename = filename = Module._resolveFilename(filename); - mainModule.paths = Module._nodeModulePaths(path.join(__dirname, '../node_modules')); - + mainModule.paths = Module._nodeModulePaths(fsp.join(__dirname, '../node_modules')); + dummyTransform(); + //process.execPath = filename; // Load the target file and evaluate it as the main module. // The input path should have been resolved to a file, so use its extension. // If the file doesn't have an extension (e.g. scripts with a shebang), // go by what executable this was called as. - var ext = path.extname(filename); - if (!/\._?(js|coffee)$/.test(ext)) ext = /^_coffee/.test(path.basename(process.argv[1])) ? '._coffee' : '._js'; + var ext = fsp.extname(filename); + if (!/\._?(js|coffee)$/.test(ext)) ext = /^_coffee/.test(fsp.basename(process.argv[1])) ? '._coffee' : '._js'; // Update the process argv and execPath too. process.argv = [process.argv[1], filename].concat(options.args.slice(1)); require.extensions[ext](mainModule, filename); diff --git a/package.json b/package.json index 1c412860..95905a45 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "streamline", "description": "Asynchronous Javascript for dummies", - "version": "1.0.2", + "version": "1.0.4", "license": "MIT", "homepage": "http://github.com/Sage/streamlinejs", "author": "Bruno Jouhier",