1010
1111// handles _node and _coffee command line options
1212var fs = require ( 'fs' ) ;
13- var path = require ( 'path' ) ;
13+ var fsp = require ( 'path' ) ;
1414var util = require ( './util' ) ;
1515var register = require ( './register' ) ;
1616var 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+
7395function 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 ( ! / \. _ ? ( j s | c o f f e e ) $ / . test ( ext ) ) ext = / ^ _ c o f f e e / . test ( path . basename ( process . argv [ 1 ] ) ) ? '._coffee' : '._js' ;
120+ var ext = fsp . extname ( filename ) ;
121+ if ( ! / \. _ ? ( j s | c o f f e e ) $ / . test ( ext ) ) ext = / ^ _ c o f f e e / . 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 ) ;
0 commit comments