Skip to content

Commit

Permalink
chore: Use native Promises instead of Bluebird
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Promises are now Promises, rather than blue birds.

Fix #13
  • Loading branch information
isaacs committed Jan 5, 2020
1 parent 32132c9 commit ee939c0
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 37 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ jump in if you'd like to, or even ask us questions if something isn't clear.

### API

#### <a name="binLinks"></a> `> binLinks(pkg, folder, global, opts, cb)`
#### <a name="binLinks"></a> `> binLinks(pkg, folder, global, opts)`

Returns a Promise that resolves when the requisite things have been linked.

##### Example

```javascript
binLinks(pkg, folder, global, opts, cb)
```
binLinks(pkg, folder, global, opts).then(() => console.log('bins linked!'))
```
66 changes: 37 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

const path = require('path')
const fs = require('graceful-fs')
const BB = require('bluebird')
const {promisify} = require('util')
const gentleFs = require('gentle-fs')
const linkIfExists = BB.promisify(gentleFs.linkIfExists)
const gentleFsBinLink = BB.promisify(gentleFs.binLink)
const open = BB.promisify(fs.open)
const close = BB.promisify(fs.close)
const read = BB.promisify(fs.read, {multiArgs: true})
const chmod = BB.promisify(fs.chmod)
const readFile = BB.promisify(fs.readFile)
const writeFileAtomic = BB.promisify(require('write-file-atomic'))
const linkIfExists = promisify(gentleFs.linkIfExists)
const gentleFsBinLink = promisify(gentleFs.binLink)
const open = promisify(fs.open)
const close = promisify(fs.close)
const read = promisify(fs.read)
const chmod = promisify(fs.chmod)
const readFile = promisify(fs.readFile)
const writeFileAtomic = promisify(require('write-file-atomic'))
const normalize = require('npm-normalize-package-bin')

module.exports = BB.promisify(binLinks)
module.exports = binLinks

function binLinks (pkg, folder, global, opts, cb) {
function binLinks (pkg, folder, global, opts) {
pkg = normalize(pkg)
folder = path.resolve(folder)

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

return BB.join(
return Promise.all([
linkBins(pkg, folder, parent, gtop, opts),
linkMans(pkg, folder, parent, gtop, opts)
).asCallback(cb)
])
}

function isHashbangFile (file) {
/* istanbul ignore next */
const FALSE = () => false
return open(file, 'r').then(fileHandle => {
return read(fileHandle, Buffer.alloc(2), 0, 2, 0).spread((_, buf) => {
if (!hasHashbang(buf)) return []
return read(fileHandle, Buffer.alloc(2048), 0, 2048, 0)
}).spread((_, buf) => buf && hasCR(buf), /* istanbul ignore next */ () => false)
.finally(() => close(fileHandle))
}).catch(/* istanbul ignore next */ () => false)
const buf = Buffer.alloc(2)
return read(fileHandle, buf, 0, 2, 0).then((...args) => {
if (!hasHashbang(buf)) {
return []
}
const line = Buffer.alloc(2048)
return read(fileHandle, line, 0, 2048, 0)
.then((bytes) => close(fileHandle).then(() => bytes && hasCR(line)))
})
// don't leak a fd if the read fails
.catch(/* istanbul ignore next */ () => close(fileHandle).then(FALSE, FALSE))
}).catch(FALSE)
}

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

return BB.map(Object.keys(pkg.bin), bin => {
return Promise.all(Object.keys(pkg.bin).map(bin => {
var dest = path.resolve(binRoot, bin)
var src = path.resolve(folder, pkg.bin[bin])

/* istanbul ignore if - that unpossible */
if (src.indexOf(folder) !== 0) {
throw new Error('invalid bin entry for package ' +
pkg._id + '. key=' + bin + ', value=' + pkg.bin[bin])
return Promise.reject(new Error('invalid bin entry for package ' +
pkg._id + '. key=' + bin + ', value=' + pkg.bin[bin]))
}

return linkBin(src, dest, linkOpts).then(() => {
Expand Down Expand Up @@ -115,7 +123,7 @@ function linkBins (pkg, folder, parent, gtop, opts) {
if (err.code === 'ENOENT' && opts.ignoreScripts) return
throw err
})
})
}))
}

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

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

var stem = parseMan[1]
Expand All @@ -167,8 +175,8 @@ function linkMans (pkg, folder, parent, gtop, opts) {
var manSrc = path.resolve(folder, man)
/* istanbul ignore if - that unpossible */
if (manSrc.indexOf(folder) !== 0) {
throw new Error('invalid man entry for package ' +
pkg._id + '. man=' + manSrc)
return Promise.reject(new Error('invalid man entry for package ' +
pkg._id + '. man=' + manSrc))
}

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

return linkIfExists(manSrc, manDest, getLinkOpts(opts, gtop && folder))
})
}))
}
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
},
"homepage": "https://github.com/npm/bin-links#readme",
"dependencies": {
"bluebird": "^3.5.3",
"cmd-shim": "^3.0.0",
"gentle-fs": "^2.3.0",
"graceful-fs": "^4.1.15",
Expand Down
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const mkdirp = require('mkdirp')
const rimraf = require('rimraf')
const test = require('tap').test

const binLinks = require('../index.js')
const binLinks = require('../')

const log = {
clearProgress () {},
Expand Down
3 changes: 1 addition & 2 deletions test/link-bins.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const t = require('tap')
const BB = require('bluebird')
const binLinks = BB.promisify(require('../'))
const binLinks = require('../')

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

0 comments on commit ee939c0

Please sign in to comment.