2
2
3
3
const path = require ( 'path' )
4
4
const fs = require ( 'graceful-fs' )
5
- const BB = require ( 'bluebird ' )
5
+ const { promisify } = require ( 'util ' )
6
6
const gentleFs = require ( 'gentle-fs' )
7
- const linkIfExists = BB . promisify ( gentleFs . linkIfExists )
8
- const gentleFsBinLink = BB . promisify ( gentleFs . binLink )
9
- const open = BB . promisify ( fs . open )
10
- const close = BB . promisify ( fs . close )
11
- const read = BB . promisify ( fs . read , { multiArgs : true } )
12
- const chmod = BB . promisify ( fs . chmod )
13
- const readFile = BB . promisify ( fs . readFile )
14
- const writeFileAtomic = BB . promisify ( require ( 'write-file-atomic' ) )
7
+ const linkIfExists = promisify ( gentleFs . linkIfExists )
8
+ const gentleFsBinLink = promisify ( gentleFs . binLink )
9
+ const open = promisify ( fs . open )
10
+ const close = promisify ( fs . close )
11
+ const read = promisify ( fs . read )
12
+ const chmod = promisify ( fs . chmod )
13
+ const readFile = promisify ( fs . readFile )
14
+ const writeFileAtomic = promisify ( require ( 'write-file-atomic' ) )
15
15
const normalize = require ( 'npm-normalize-package-bin' )
16
16
17
- module . exports = BB . promisify ( binLinks )
17
+ module . exports = binLinks
18
18
19
- function binLinks ( pkg , folder , global , opts , cb ) {
19
+ function binLinks ( pkg , folder , global , opts ) {
20
20
pkg = normalize ( pkg )
21
21
folder = path . resolve ( folder )
22
22
@@ -33,20 +33,28 @@ function binLinks (pkg, folder, global, opts, cb) {
33
33
if ( gnm ) opts . log . silly ( 'linkStuff' , opts . pkgId , 'is installed into a global node_modules' )
34
34
if ( gtop ) opts . log . silly ( 'linkStuff' , opts . pkgId , 'is installed into the top-level global node_modules' )
35
35
36
- return BB . join (
36
+ return Promise . all ( [
37
37
linkBins ( pkg , folder , parent , gtop , opts ) ,
38
38
linkMans ( pkg , folder , parent , gtop , opts )
39
- ) . asCallback ( cb )
39
+ ] )
40
40
}
41
41
42
42
function isHashbangFile ( file ) {
43
+ /* istanbul ignore next */
44
+ const FALSE = ( ) => false
43
45
return open ( file , 'r' ) . then ( fileHandle => {
44
- return read ( fileHandle , Buffer . alloc ( 2 ) , 0 , 2 , 0 ) . spread ( ( _ , buf ) => {
45
- if ( ! hasHashbang ( buf ) ) return [ ]
46
- return read ( fileHandle , Buffer . alloc ( 2048 ) , 0 , 2048 , 0 )
47
- } ) . spread ( ( _ , buf ) => buf && hasCR ( buf ) , /* istanbul ignore next */ ( ) => false )
48
- . finally ( ( ) => close ( fileHandle ) )
49
- } ) . catch ( /* istanbul ignore next */ ( ) => false )
46
+ const buf = Buffer . alloc ( 2 )
47
+ return read ( fileHandle , buf , 0 , 2 , 0 ) . then ( ( ...args ) => {
48
+ if ( ! hasHashbang ( buf ) ) {
49
+ return [ ]
50
+ }
51
+ const line = Buffer . alloc ( 2048 )
52
+ return read ( fileHandle , line , 0 , 2048 , 0 )
53
+ . then ( ( bytes ) => close ( fileHandle ) . then ( ( ) => bytes && hasCR ( line ) ) )
54
+ } )
55
+ // don't leak a fd if the read fails
56
+ . catch ( /* istanbul ignore next */ ( ) => close ( fileHandle ) . then ( FALSE , FALSE ) )
57
+ } ) . catch ( FALSE )
50
58
}
51
59
52
60
function hasHashbang ( buf ) {
@@ -78,14 +86,14 @@ function linkBins (pkg, folder, parent, gtop, opts) {
78
86
: path . resolve ( parent , '.bin' )
79
87
opts . log . verbose ( 'linkBins' , [ pkg . bin , binRoot , gtop ] )
80
88
81
- return BB . map ( Object . keys ( pkg . bin ) , bin => {
89
+ return Promise . all ( Object . keys ( pkg . bin ) . map ( bin => {
82
90
var dest = path . resolve ( binRoot , bin )
83
91
var src = path . resolve ( folder , pkg . bin [ bin ] )
84
92
85
93
/* istanbul ignore if - that unpossible */
86
94
if ( src . indexOf ( folder ) !== 0 ) {
87
- throw new Error ( 'invalid bin entry for package ' +
88
- pkg . _id + '. key=' + bin + ', value=' + pkg . bin [ bin ] )
95
+ return Promise . reject ( new Error ( 'invalid bin entry for package ' +
96
+ pkg . _id + '. key=' + bin + ', value=' + pkg . bin [ bin ] ) )
89
97
}
90
98
91
99
return linkBin ( src , dest , linkOpts ) . then ( ( ) => {
@@ -115,7 +123,7 @@ function linkBins (pkg, folder, parent, gtop, opts) {
115
123
if ( err . code === 'ENOENT' && opts . ignoreScripts ) return
116
124
throw err
117
125
} )
118
- } )
126
+ } ) )
119
127
}
120
128
121
129
function linkBin ( from , to , opts ) {
@@ -150,15 +158,15 @@ function linkMans (pkg, folder, parent, gtop, opts) {
150
158
return set [ path . basename ( man ) ] === cleanMan
151
159
} )
152
160
153
- return BB . map ( manpages , man => {
161
+ return Promise . all ( manpages . map ( man => {
154
162
opts . log . silly ( 'linkMans' , 'preparing to link' , man )
155
163
var parseMan = man . match ( / ( .* \. ( [ 0 - 9 ] + ) ( \. g z ) ? ) $ / )
156
164
if ( ! parseMan ) {
157
- throw new Error (
165
+ return Promise . reject ( new Error (
158
166
man + ' is not a valid name for a man file. ' +
159
167
'Man files must end with a number, ' +
160
168
'and optionally a .gz suffix if they are compressed.'
161
- )
169
+ ) )
162
170
}
163
171
164
172
var stem = parseMan [ 1 ]
@@ -167,8 +175,8 @@ function linkMans (pkg, folder, parent, gtop, opts) {
167
175
var manSrc = path . resolve ( folder , man )
168
176
/* istanbul ignore if - that unpossible */
169
177
if ( manSrc . indexOf ( folder ) !== 0 ) {
170
- throw new Error ( 'invalid man entry for package ' +
171
- pkg . _id + '. man=' + manSrc )
178
+ return Promise . reject ( new Error ( 'invalid man entry for package ' +
179
+ pkg . _id + '. man=' + manSrc ) )
172
180
}
173
181
174
182
var manDest = path . join ( manRoot , 'man' + sxn , bn )
@@ -179,5 +187,5 @@ function linkMans (pkg, folder, parent, gtop, opts) {
179
187
opts . clobberLinkGently = true
180
188
181
189
return linkIfExists ( manSrc , manDest , getLinkOpts ( opts , gtop && folder ) )
182
- } )
190
+ } ) )
183
191
}
0 commit comments