Skip to content

Commit 390827b

Browse files
committed
issue #291 - previous fix was incomplete - republishing as 1.0.4
1 parent c4d6f70 commit 390827b

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

lib/command.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
// handles _node and _coffee command line options
1212
var fs = require('fs');
13-
var path = require('path');
13+
var fsp = require('path');
1414
var util = require('./util');
1515
var register = require('./register');
1616
var Module = require('module');
@@ -70,6 +70,28 @@ function parseOptions(argv) {
7070
return util.getOptions(util.extend(util.envOptions(), options));
7171
};
7272

73+
// babel uses process.cwd() to locate its plugins.
74+
// We have to fool it so that globally installed _node / _coffee can load the streamline plugin.
75+
// Fortunately it caches the result of the first process.cwd() call (see tryRequire implementation)
76+
// So we monkey patch process.cwd, execute a dummy transform, and then restore process.cwd
77+
function dummyTransform() {
78+
var cwd = process.cwd;
79+
process.cwd = function() {
80+
return fsp.join(__dirname, '..');
81+
}
82+
try {
83+
require('babel').transform("(function(_) {})", {
84+
plugins: ['streamline'],
85+
extra: {
86+
streamline: {
87+
quiet: true,
88+
}
89+
}
90+
});
91+
} catch (ex) {}
92+
process.cwd = cwd;
93+
}
94+
7395
function runScript(options) {
7496
var filename = options.args[0];
7597

@@ -85,17 +107,18 @@ function runScript(options) {
85107
// helper functions to resolve these guys!
86108
// https://github.com/joyent/node/blob/master/lib/module.js
87109
// Except we need to tell Node that these are paths, not native modules.
88-
filename = path.resolve(filename || '.');
110+
filename = fsp.resolve(filename || '.');
89111
mainModule.filename = filename = Module._resolveFilename(filename);
90-
mainModule.paths = Module._nodeModulePaths(path.join(__dirname, '../node_modules'));
91-
112+
mainModule.paths = Module._nodeModulePaths(fsp.join(__dirname, '../node_modules'));
113+
dummyTransform();
114+
92115
//process.execPath = filename;
93116
// Load the target file and evaluate it as the main module.
94117
// The input path should have been resolved to a file, so use its extension.
95118
// If the file doesn't have an extension (e.g. scripts with a shebang),
96119
// go by what executable this was called as.
97-
var ext = path.extname(filename);
98-
if (!/\._?(js|coffee)$/.test(ext)) ext = /^_coffee/.test(path.basename(process.argv[1])) ? '._coffee' : '._js';
120+
var ext = fsp.extname(filename);
121+
if (!/\._?(js|coffee)$/.test(ext)) ext = /^_coffee/.test(fsp.basename(process.argv[1])) ? '._coffee' : '._js';
99122
// Update the process argv and execPath too.
100123
process.argv = [process.argv[1], filename].concat(options.args.slice(1));
101124
require.extensions[ext](mainModule, filename);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "streamline",
33
"description": "Asynchronous Javascript for dummies",
4-
"version": "1.0.2",
4+
"version": "1.0.4",
55
"license": "MIT",
66
"homepage": "http://github.com/Sage/streamlinejs",
77
"author": "Bruno Jouhier",

0 commit comments

Comments
 (0)