From ee939c0c42b42262591e4abab98f12a63ac206b2 Mon Sep 17 00:00:00 2001 From: isaacs Date: Sun, 5 Jan 2020 15:25:19 -0800 Subject: [PATCH] chore: Use native Promises instead of Bluebird BREAKING CHANGE: Promises are now Promises, rather than blue birds. Fix #13 --- README.md | 8 +++--- index.js | 66 ++++++++++++++++++++++++++--------------------- package-lock.json | 3 ++- package.json | 1 - test/index.js | 2 +- test/link-bins.js | 3 +-- 6 files changed, 46 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index 558d5c7..ef83388 100644 --- a/README.md +++ b/README.md @@ -38,10 +38,12 @@ jump in if you'd like to, or even ask us questions if something isn't clear. ### API -#### `> binLinks(pkg, folder, global, opts, cb)` +#### `> 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) -``` \ No newline at end of file +binLinks(pkg, folder, global, opts).then(() => console.log('bins linked!')) +``` diff --git a/index.js b/index.js index 79c2cb5..7352188 100644 --- a/index.js +++ b/index.js @@ -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) @@ -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) { @@ -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(() => { @@ -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) { @@ -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] @@ -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) @@ -179,5 +187,5 @@ function linkMans (pkg, folder, parent, gtop, opts) { opts.clobberLinkGently = true return linkIfExists(manSrc, manDest, getLinkOpts(opts, gtop && folder)) - }) + })) } diff --git a/package-lock.json b/package-lock.json index a4f435a..da8cbea 100644 --- a/package-lock.json +++ b/package-lock.json @@ -401,7 +401,8 @@ "bluebird": { "version": "3.5.3", "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.5.3.tgz", - "integrity": "sha512-/qKPUQlaW1OyR51WeCPBvRnAlnZFUJkCSG5HzGnuIqhgyJtF+T94lFnn33eiazjRm2LAHVy2guNnaq48X9SJuw==" + "integrity": "sha512-/qKPUQlaW1OyR51WeCPBvRnAlnZFUJkCSG5HzGnuIqhgyJtF+T94lFnn33eiazjRm2LAHVy2guNnaq48X9SJuw==", + "dev": true }, "brace-expansion": { "version": "1.1.8", diff --git a/package.json b/package.json index 06b24a0..152f7a2 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/index.js b/test/index.js index 59dadad..fe25c23 100644 --- a/test/index.js +++ b/test/index.js @@ -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 () {}, diff --git a/test/link-bins.js b/test/link-bins.js index 3e5a7de..9ff6172 100644 --- a/test/link-bins.js +++ b/test/link-bins.js @@ -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.