aboutsummaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/find-npm-prefix/find-prefix.js
diff options
context:
space:
mode:
Diffstat (limited to 'deps/npm/node_modules/find-npm-prefix/find-prefix.js')
-rw-r--r--deps/npm/node_modules/find-npm-prefix/find-prefix.js50
1 files changed, 24 insertions, 26 deletions
diff --git a/deps/npm/node_modules/find-npm-prefix/find-prefix.js b/deps/npm/node_modules/find-npm-prefix/find-prefix.js
index d5e271322b..089572ec11 100644
--- a/deps/npm/node_modules/find-npm-prefix/find-prefix.js
+++ b/deps/npm/node_modules/find-npm-prefix/find-prefix.js
@@ -5,9 +5,11 @@ module.exports = findPrefix
const fs = require('fs')
const path = require('path')
+const Bluebird = require('bluebird')
+const readdir = Bluebird.promisify(fs.readdir)
function findPrefix (dir) {
- return new Promise((resolve, reject) => {
+ return Bluebird.try(() => {
dir = path.resolve(dir)
// this is a weird special case where an infinite recurse of
@@ -18,11 +20,9 @@ function findPrefix (dir) {
dir = path.dirname(dir)
walkedUp = true
}
- if (walkedUp) {
- resolve(dir)
- } else {
- resolve(findPrefix_(dir))
- }
+ if (walkedUp) return dir
+
+ return findPrefix_(dir)
})
}
@@ -32,25 +32,23 @@ function findPrefix_ (dir, original) {
const parent = path.dirname(dir)
// this is a platform independent way of checking if we're in the root
// directory
- if (parent === dir) return Promise.resolve(original)
-
- return new Promise((resolve, reject) => {
- fs.readdir(dir, (err, files) => {
- if (err) {
- // an error right away is a bad sign.
- // unless the prefix was simply a non
- // existent directory.
- if (err && dir === original && err.code !== 'ENOENT') {
- reject(err)
- } else {
- resolve(original)
- }
- } else if (files.indexOf('node_modules') !== -1 ||
- files.indexOf('package.json') !== -1) {
- resolve(dir)
- } else {
- resolve(findPrefix_(parent, original))
- }
- })
+ if (parent === dir) return original
+
+ return readdir(dir).then(files => {
+ if (files.indexOf('node_modules') !== -1 ||
+ files.indexOf('package.json') !== -1) {
+ return dir
+ }
+
+ return findPrefix_(parent, original)
+ }, er => {
+ // an error right away is a bad sign.
+ // unless the prefix was simply a non
+ // existent directory.
+ if (er && dir === original && er.code !== 'ENOENT') {
+ throw er
+ } else {
+ return original
+ }
})
}