Skip to content

Commit ee939c0

Browse files
committed
chore: Use native Promises instead of Bluebird
BREAKING CHANGE: Promises are now Promises, rather than blue birds. Fix #13
1 parent 32132c9 commit ee939c0

File tree

6 files changed

+46
-37
lines changed

6 files changed

+46
-37
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@ jump in if you'd like to, or even ask us questions if something isn't clear.
3838

3939
### API
4040

41-
#### <a name="binLinks"></a> `> binLinks(pkg, folder, global, opts, cb)`
41+
#### <a name="binLinks"></a> `> binLinks(pkg, folder, global, opts)`
42+
43+
Returns a Promise that resolves when the requisite things have been linked.
4244

4345
##### Example
4446

4547
```javascript
46-
binLinks(pkg, folder, global, opts, cb)
47-
```
48+
binLinks(pkg, folder, global, opts).then(() => console.log('bins linked!'))
49+
```

index.js

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22

33
const path = require('path')
44
const fs = require('graceful-fs')
5-
const BB = require('bluebird')
5+
const {promisify} = require('util')
66
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'))
1515
const normalize = require('npm-normalize-package-bin')
1616

17-
module.exports = BB.promisify(binLinks)
17+
module.exports = binLinks
1818

19-
function binLinks (pkg, folder, global, opts, cb) {
19+
function binLinks (pkg, folder, global, opts) {
2020
pkg = normalize(pkg)
2121
folder = path.resolve(folder)
2222

@@ -33,20 +33,28 @@ function binLinks (pkg, folder, global, opts, cb) {
3333
if (gnm) opts.log.silly('linkStuff', opts.pkgId, 'is installed into a global node_modules')
3434
if (gtop) opts.log.silly('linkStuff', opts.pkgId, 'is installed into the top-level global node_modules')
3535

36-
return BB.join(
36+
return Promise.all([
3737
linkBins(pkg, folder, parent, gtop, opts),
3838
linkMans(pkg, folder, parent, gtop, opts)
39-
).asCallback(cb)
39+
])
4040
}
4141

4242
function isHashbangFile (file) {
43+
/* istanbul ignore next */
44+
const FALSE = () => false
4345
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)
5058
}
5159

5260
function hasHashbang (buf) {
@@ -78,14 +86,14 @@ function linkBins (pkg, folder, parent, gtop, opts) {
7886
: path.resolve(parent, '.bin')
7987
opts.log.verbose('linkBins', [pkg.bin, binRoot, gtop])
8088

81-
return BB.map(Object.keys(pkg.bin), bin => {
89+
return Promise.all(Object.keys(pkg.bin).map(bin => {
8290
var dest = path.resolve(binRoot, bin)
8391
var src = path.resolve(folder, pkg.bin[bin])
8492

8593
/* istanbul ignore if - that unpossible */
8694
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]))
8997
}
9098

9199
return linkBin(src, dest, linkOpts).then(() => {
@@ -115,7 +123,7 @@ function linkBins (pkg, folder, parent, gtop, opts) {
115123
if (err.code === 'ENOENT' && opts.ignoreScripts) return
116124
throw err
117125
})
118-
})
126+
}))
119127
}
120128

121129
function linkBin (from, to, opts) {
@@ -150,15 +158,15 @@ function linkMans (pkg, folder, parent, gtop, opts) {
150158
return set[path.basename(man)] === cleanMan
151159
})
152160

153-
return BB.map(manpages, man => {
161+
return Promise.all(manpages.map(man => {
154162
opts.log.silly('linkMans', 'preparing to link', man)
155163
var parseMan = man.match(/(.*\.([0-9]+)(\.gz)?)$/)
156164
if (!parseMan) {
157-
throw new Error(
165+
return Promise.reject(new Error(
158166
man + ' is not a valid name for a man file. ' +
159167
'Man files must end with a number, ' +
160168
'and optionally a .gz suffix if they are compressed.'
161-
)
169+
))
162170
}
163171

164172
var stem = parseMan[1]
@@ -167,8 +175,8 @@ function linkMans (pkg, folder, parent, gtop, opts) {
167175
var manSrc = path.resolve(folder, man)
168176
/* istanbul ignore if - that unpossible */
169177
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))
172180
}
173181

174182
var manDest = path.join(manRoot, 'man' + sxn, bn)
@@ -179,5 +187,5 @@ function linkMans (pkg, folder, parent, gtop, opts) {
179187
opts.clobberLinkGently = true
180188

181189
return linkIfExists(manSrc, manDest, getLinkOpts(opts, gtop && folder))
182-
})
190+
}))
183191
}

package-lock.json

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
},
2929
"homepage": "https://github.com/npm/bin-links#readme",
3030
"dependencies": {
31-
"bluebird": "^3.5.3",
3231
"cmd-shim": "^3.0.0",
3332
"gentle-fs": "^2.3.0",
3433
"graceful-fs": "^4.1.15",

test/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const mkdirp = require('mkdirp')
66
const rimraf = require('rimraf')
77
const test = require('tap').test
88

9-
const binLinks = require('../index.js')
9+
const binLinks = require('../')
1010

1111
const log = {
1212
clearProgress () {},

test/link-bins.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const t = require('tap')
2-
const BB = require('bluebird')
3-
const binLinks = BB.promisify(require('../'))
2+
const binLinks = require('../')
43

54
// forking between cmd-shims and symlinks is already handled by
65
// the gentle-fs.binLink module. just test the unix handling here.

0 commit comments

Comments
 (0)