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

Commit

Permalink
Make parse() + path resolving async
Browse files Browse the repository at this point in the history
  • Loading branch information
voxpelli committed Jul 28, 2019
1 parent 0320bb7 commit 8eaba08
Showing 1 changed file with 33 additions and 25 deletions.
58 changes: 33 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const path = require('path')
const fs = require('fs')
const { promisify } = require('util')
const readPackage = require('read-package-json')
const builtins = require('module').builtinModules
const resolveModule = require('resolve')
Expand All @@ -11,34 +12,30 @@ const globby = require('globby')
const micromatch = require('micromatch')
const pkgUp = require('pkg-up')

const promisedReadPackage = function (pkgPath) {
return new Promise((resolve, reject) => {
readPackage(pkgPath, (err, pkg) => {
if (err) return reject(err)
resolve(pkg)
})
})
}

const resolveGlobbedPath = function (entries, cwd) {
const paths = []
const promisedFsAccess = promisify(fs.access)
const promisedReadPackage = promisify(readPackage)

async function resolveGlobbedPath (entries, cwd) {
if (typeof entries === 'string') entries = [entries]

debug('globby resolving', entries)

globby.sync(entries, {
const resolvedEntries = await globby(entries, {
cwd,
absolute: true,
expandDirectories: false
}).forEach(entry => {
})

const paths = Object.keys(resolvedEntries.reduce((result, entry) => {
// Globby yields unix-style paths.
const normalized = path.resolve(entry)

if (!paths.includes(normalized)) {
paths.push(normalized)
if (!result[normalized]) {
result[normalized] = true
}
})

return result
}, {}))

debug('globby resolved', paths)

Expand All @@ -50,7 +47,7 @@ module.exports = function (opts, cb) {
let entries

const result = promisedReadPackage(pkgPath)
.catch(err => {
.catch(async (err) => {
if (!err) {
return Promise.reject(new Error('Failed to read package.json, but received no error'))
} else if (pkgPath.endsWith('/package.json') || pkgPath === 'package.json') {
Expand All @@ -61,7 +58,7 @@ module.exports = function (opts, cb) {
}

// We've likely been given entries rather than a package.json or module path, try resolving that instead
entries = resolveGlobbedPath(pkgPath)
entries = await resolveGlobbedPath(pkgPath)

if (!entries[0]) {
return Promise.reject(new Error('Failed to find package.json, could not find any matching files'))
Expand Down Expand Up @@ -187,7 +184,7 @@ function joinAndResolvePath (basePath, targetPath) {
return path.resolve(path.join(basePath, targetPath))
}

function resolveDefaultEntriesPaths (opts) {
async function resolveDefaultEntriesPaths (opts) {
const pkgPath = opts.path
const pkgDir = path.dirname(pkgPath)
const pkg = opts.package
Expand All @@ -197,7 +194,10 @@ function resolveDefaultEntriesPaths (opts) {
let paths = []

// Add the path of the main file
if (fs.existsSync(mainPath)) paths.push(mainPath)
try {
await promisedFsAccess(mainPath)
paths.push(mainPath)
} catch (err) {}

// Add the path of binaries
if (pkg.bin) {
Expand All @@ -213,22 +213,30 @@ function resolveDefaultEntriesPaths (opts) {
return paths
}

function resolvePaths (opts) {
async function resolvePaths (opts) {
const [
defaultEntries,
globbedPaths
] = await Promise.all([
!opts.noDefaultEntries ? await resolveDefaultEntriesPaths(opts) : [],
opts.entries ? await resolveGlobbedPath(opts.entries, path.dirname(opts.path)) : []
])

return [
...(!opts.noDefaultEntries ? resolveDefaultEntriesPaths(opts) : []),
...(opts.entries ? resolveGlobbedPath(opts.entries, path.dirname(opts.path)) : [])
...defaultEntries,
...globbedPaths
]
}

function parse (opts) {
async function parse (opts) {
const pkg = opts.package
const extensions = opts.extensions

const deps = {}
const seen = []
const core = []

const paths = resolvePaths(opts)
const paths = await resolvePaths(opts)

debug('entry paths', paths)

Expand Down

0 comments on commit 8eaba08

Please sign in to comment.