@@ -8,16 +8,19 @@ const BB = require('bluebird')
8
8
9
9
const cache = require ( './cache' )
10
10
const cacache = require ( 'cacache' )
11
+ const cp = require ( 'child_process' )
11
12
const deprCheck = require ( './utils/depr-check' )
12
- const fpm = BB . promisify ( require ( './fetch-package-metadata' ) )
13
+ const fpm = require ( './fetch-package-metadata' )
13
14
const fs = require ( 'graceful-fs' )
14
15
const install = require ( './install' )
15
16
const lifecycle = BB . promisify ( require ( './utils/lifecycle' ) )
17
+ const log = require ( 'npmlog' )
16
18
const move = require ( 'move-concurrently' )
17
19
const npm = require ( './npm' )
18
20
const output = require ( './utils/output' )
19
21
const pacoteOpts = require ( './config/pacote' )
20
22
const path = require ( 'path' )
23
+ const PassThrough = require ( 'stream' ) . PassThrough
21
24
const pathIsInside = require ( 'path-is-inside' )
22
25
const pipe = BB . promisify ( require ( 'mississippi' ) . pipe )
23
26
const prepublishWarning = require ( './utils/warn-deprecated' ) ( 'prepublish-on-install' )
@@ -53,7 +56,7 @@ function pack (args, silent, cb) {
53
56
54
57
// add to cache, then cp to the cwd
55
58
function pack_ ( pkg , dir ) {
56
- return fpm ( pkg , dir ) . then ( ( mani ) => {
59
+ return BB . fromNode ( ( cb ) => fpm ( pkg , dir , cb ) ) . then ( ( mani ) => {
57
60
let name = mani . name [ 0 ] === '@'
58
61
// scoped packages get special treatment
59
62
? mani . name . substr ( 1 ) . replace ( / \/ / g, '-' )
@@ -108,10 +111,111 @@ function prepareDirectory (dir) {
108
111
module . exports . packDirectory = packDirectory
109
112
function packDirectory ( mani , dir , target ) {
110
113
deprCheck ( mani )
111
- return cacache . tmp . withTmp ( npm . tmp , { tmpPrefix : 'packing' } , ( tmp ) => {
112
- const tmpTarget = path . join ( tmp , path . basename ( target ) )
113
- return tarPack ( tmpTarget , dir , mani ) . then ( ( ) => {
114
- return move ( tmpTarget , target , { Promise : BB , fs} )
115
- } ) . then ( ( ) => target )
114
+ return readJson ( path . join ( dir , 'package.json' ) ) . then ( ( pkg ) => {
115
+ return lifecycle ( pkg , 'prepack' , dir )
116
+ } ) . then ( ( ) => {
117
+ return readJson ( path . join ( dir , 'package.json' ) )
118
+ } ) . then ( ( pkg ) => {
119
+ return cacache . tmp . withTmp ( npm . tmp , { tmpPrefix : 'packing' } , ( tmp ) => {
120
+ const tmpTarget = path . join ( tmp , path . basename ( target ) )
121
+ return tarPack ( tmpTarget , dir , pkg ) . then ( ( ) => {
122
+ return move ( tmpTarget , target , { Promise : BB , fs} )
123
+ } ) . then ( ( ) => {
124
+ return lifecycle ( pkg , 'postpack' , dir )
125
+ } ) . then ( ( ) => target )
126
+ } )
116
127
} )
117
128
}
129
+
130
+ const PASSTHROUGH_OPTS = [
131
+ 'always-auth' ,
132
+ 'auth-type' ,
133
+ 'ca' ,
134
+ 'cafile' ,
135
+ 'cert' ,
136
+ 'git' ,
137
+ 'local-address' ,
138
+ 'maxsockets' ,
139
+ 'offline' ,
140
+ 'prefer-offline' ,
141
+ 'prefer-online' ,
142
+ 'proxy' ,
143
+ 'https-proxy' ,
144
+ 'registry' ,
145
+ 'send-metrics' ,
146
+ 'sso-poll-frequency' ,
147
+ 'sso-type' ,
148
+ 'strict-ssl'
149
+ ]
150
+
151
+ module . exports . packGitDep = packGitDep
152
+ function packGitDep ( manifest , dir ) {
153
+ const stream = new PassThrough ( )
154
+ readJson ( path . join ( dir , 'package.json' ) ) . then ( ( pkg ) => {
155
+ if ( pkg . scripts && pkg . scripts . prepare ) {
156
+ log . verbose ( 'prepareGitDep' , `${ manifest . _spec } : installing devDeps and running prepare script.` )
157
+ const cliArgs = PASSTHROUGH_OPTS . reduce ( ( acc , opt ) => {
158
+ if ( npm . config . get ( opt , 'cli' ) != null ) {
159
+ acc . push ( `--${ opt } =${ npm . config . get ( opt ) } ` )
160
+ }
161
+ return acc
162
+ } , [ ] )
163
+ const child = cp . spawn ( process . env . NODE || process . execPath , [
164
+ require . main . filename ,
165
+ 'install' ,
166
+ '--ignore-prepublish' ,
167
+ '--no-progress' ,
168
+ '--no-save'
169
+ ] . concat ( cliArgs ) , {
170
+ cwd : dir ,
171
+ env : process . env
172
+ } )
173
+ let errData = [ ]
174
+ let errDataLen = 0
175
+ let outData = [ ]
176
+ let outDataLen = 0
177
+ child . stdout . on ( 'data' , ( data ) => {
178
+ outData . push ( data )
179
+ outDataLen += data . length
180
+ log . gauge . pulse ( 'preparing git package' )
181
+ } )
182
+ child . stderr . on ( 'data' , ( data ) => {
183
+ errData . push ( data )
184
+ errDataLen += data . length
185
+ log . gauge . pulse ( 'preparing git package' )
186
+ } )
187
+ return BB . fromNode ( ( cb ) => {
188
+ child . on ( 'error' , cb )
189
+ child . on ( 'exit' , ( code , signal ) => {
190
+ if ( code > 0 ) {
191
+ const err = new Error ( `${ signal } : npm exited with code ${ code } while attempting to build ${ manifest . _requested } . Clone the repository manually and run 'npm install' in it for more information.` )
192
+ err . code = code
193
+ err . signal = signal
194
+ cb ( err )
195
+ } else {
196
+ cb ( )
197
+ }
198
+ } )
199
+ } ) . then ( ( ) => {
200
+ if ( outDataLen > 0 ) log . silly ( 'prepareGitDep' , '1>' , Buffer . concat ( outData , outDataLen ) . toString ( ) )
201
+ if ( errDataLen > 0 ) log . silly ( 'prepareGitDep' , '2>' , Buffer . concat ( errData , errDataLen ) . toString ( ) )
202
+ } , ( err ) => {
203
+ if ( outDataLen > 0 ) log . error ( 'prepareGitDep' , '1>' , Buffer . concat ( outData , outDataLen ) . toString ( ) )
204
+ if ( errDataLen > 0 ) log . error ( 'prepareGitDep' , '2>' , Buffer . concat ( errData , errDataLen ) . toString ( ) )
205
+ throw err
206
+ } )
207
+ }
208
+ } ) . then ( ( ) => {
209
+ return readJson ( path . join ( dir , 'package.json' ) )
210
+ } ) . then ( ( pkg ) => {
211
+ return cacache . tmp . withTmp ( npm . tmp , {
212
+ tmpPrefix : 'pacote-packing'
213
+ } , ( tmp ) => {
214
+ const tmpTar = path . join ( tmp , 'package.tgz' )
215
+ return packDirectory ( manifest , dir , tmpTar ) . then ( ( ) => {
216
+ return pipe ( fs . createReadStream ( tmpTar ) , stream )
217
+ } )
218
+ } )
219
+ } ) . catch ( ( err ) => stream . emit ( 'error' , err ) )
220
+ return stream
221
+ }
0 commit comments