@@ -53,18 +53,41 @@ function remove(files) {
5353 } ) ;
5454}
5555
56+ // Add/update shebang (#!) line
57+ function shebang ( line , file ) {
58+ var content = fs . readFileSync ( file , 'utf8' ) ;
59+ var firstLine = content . split ( / \n / , 1 ) [ 0 ] ;
60+ var newContent ;
61+ if ( firstLine . slice ( 0 , 2 ) === '#!' ) {
62+ newContent = line + content . slice ( firstLine . length ) ;
63+ } else {
64+ newContent = line + '\n' + content ;
65+ }
66+ if ( content !== newContent ) {
67+ fs . writeFileSync ( file , newContent , 'utf8' ) ;
68+ }
69+ }
70+
5671// Run every command in queue, one-by-one
5772function run ( ) {
5873 var cmd = queue . shift ( ) ;
5974 if ( ! cmd ) return ;
6075
61- console . log ( cmd ) ;
62- exec ( cmd , function ( err , stdout , stderr ) {
63- if ( stderr ) console . error ( stderr ) ;
64- if ( err ) process . exit ( 1 ) ;
65-
76+ if ( Array . isArray ( cmd ) && cmd [ 0 ] instanceof Function ) {
77+ var func = cmd [ 0 ] ;
78+ var args = cmd . slice ( 1 ) ;
79+ console . log . apply ( null , [ func . name ] . concat ( args ) ) ;
80+ func . apply ( null , args ) ;
6681 run ( ) ;
67- } ) ;
82+ } else {
83+ console . log ( cmd ) ;
84+ exec ( cmd , function ( err , stdout , stderr ) {
85+ if ( stderr ) console . error ( stderr ) ;
86+ if ( err ) process . exit ( 1 ) ;
87+
88+ run ( ) ;
89+ } ) ;
90+ }
6891}
6992
7093if ( cmd === 'install' ) {
@@ -109,6 +132,8 @@ if (cmd === 'install') {
109132 copy ( 'deps/npm' , 'lib/node_modules/npm' ) ;
110133 queue . push ( 'ln -sf ../lib/node_modules/npm/bin/npm-cli.js ' +
111134 path . join ( node_prefix , 'bin/npm' ) ) ;
135+ queue . push ( [ shebang , '#!' + path . join ( node_prefix , 'bin/node' ) ,
136+ path . join ( node_prefix , 'lib/node_modules/npm/bin/npm-cli.js' ) ] ) ;
112137 }
113138} else {
114139 remove ( [
0 commit comments