Skip to content

Commit

Permalink
issue #291 - previous fix was incomplete - republishing as 1.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
bjouhier committed Oct 17, 2015
1 parent c4d6f70 commit 390827b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
35 changes: 29 additions & 6 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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];

Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 390827b

Please sign in to comment.