Skip to content
This repository was archived by the owner on Mar 13, 2024. It is now read-only.

Commit 80866f8

Browse files
committedJul 28, 2019
BREAKING: Drop callback parameter from main export
+ refactor main export to be fully async and use async/await
1 parent 8eaba08 commit 80866f8

File tree

1 file changed

+27
-33
lines changed

1 file changed

+27
-33
lines changed
 

‎index.js

+27-33
Original file line numberDiff line numberDiff line change
@@ -42,50 +42,44 @@ async function resolveGlobbedPath (entries, cwd) {
4242
return paths
4343
}
4444

45-
module.exports = function (opts, cb) {
46-
let pkgPath = opts.path
47-
let entries
48-
49-
const result = promisedReadPackage(pkgPath)
50-
.catch(async (err) => {
51-
if (!err) {
52-
return Promise.reject(new Error('Failed to read package.json, but received no error'))
53-
} else if (pkgPath.endsWith('/package.json') || pkgPath === 'package.json') {
54-
return Promise.reject(new Error('Failed to read package.json: ' + err.message))
55-
} else if (err.code === 'EISDIR') {
56-
pkgPath = path.join(pkgPath, 'package.json')
57-
return promisedReadPackage(pkgPath)
58-
}
45+
module.exports = async function (opts) {
46+
let targetPath = opts.path
47+
let pkgPath = targetPath
48+
let entries = []
49+
let pkg
50+
51+
try {
52+
pkg = await promisedReadPackage(targetPath)
53+
} catch (err) {
54+
if (targetPath.endsWith('/package.json') || targetPath === 'package.json') {
55+
throw new Error('Failed to read package.json: ' + err.message)
56+
}
5957

58+
if (err && err.code === 'EISDIR') {
59+
pkgPath = path.join(targetPath, 'package.json')
60+
} else {
6061
// We've likely been given entries rather than a package.json or module path, try resolving that instead
6162
entries = await resolveGlobbedPath(pkgPath)
6263

6364
if (!entries[0]) {
64-
return Promise.reject(new Error('Failed to find package.json, could not find any matching files'))
65+
throw new Error('Failed to find package.json, could not find any matching files')
6566
}
6667

6768
opts.noDefaultEntries = true
68-
pkgPath = pkgUp.sync({ cwd: path.dirname(entries[0]) })
69+
pkgPath = await pkgUp({ cwd: path.dirname(entries[0]) })
70+
}
6971

70-
return promisedReadPackage(pkgPath)
71-
})
72-
.then(pkg => parse({
73-
path: pkgPath,
74-
package: pkg,
75-
entries: (entries || []).concat(opts.entries),
76-
noDefaultEntries: opts.noDefaultEntries,
77-
builtins: opts.builtins,
78-
extensions: getExtensions(opts.extensions, opts.detective)
79-
}))
80-
81-
if (cb) {
82-
result
83-
.then(value => { cb(null, value) })
84-
.catch(err => { cb(err) })
85-
return
72+
pkg = await promisedReadPackage(pkgPath)
8673
}
8774

88-
return result
75+
return parse({
76+
path: pkgPath,
77+
package: pkg,
78+
entries: entries.concat(opts.entries),
79+
noDefaultEntries: opts.noDefaultEntries,
80+
builtins: opts.builtins,
81+
extensions: getExtensions(opts.extensions, opts.detective)
82+
})
8983
}
9084

9185
module.exports.missing = function (pkg, deps, options) {

0 commit comments

Comments
 (0)
This repository has been archived.