22
33const path = require ( 'path' )
44const fs = require ( 'fs' )
5+ const { promisify } = require ( 'util' )
56const readPackage = require ( 'read-package-json' )
67const builtins = require ( 'module' ) . builtinModules
78const resolveModule = require ( 'resolve' )
@@ -11,34 +12,30 @@ const globby = require('globby')
1112const micromatch = require ( 'micromatch' )
1213const pkgUp = require ( 'pkg-up' )
1314
14- const promisedReadPackage = function ( pkgPath ) {
15- return new Promise ( ( resolve , reject ) => {
16- readPackage ( pkgPath , ( err , pkg ) => {
17- if ( err ) return reject ( err )
18- resolve ( pkg )
19- } )
20- } )
21- }
22-
23- const resolveGlobbedPath = function ( entries , cwd ) {
24- const paths = [ ]
15+ const promisedFsAccess = promisify ( fs . access )
16+ const promisedReadPackage = promisify ( readPackage )
2517
18+ async function resolveGlobbedPath ( entries , cwd ) {
2619 if ( typeof entries === 'string' ) entries = [ entries ]
2720
2821 debug ( 'globby resolving' , entries )
2922
30- globby . sync ( entries , {
23+ const resolvedEntries = await globby ( entries , {
3124 cwd,
3225 absolute : true ,
3326 expandDirectories : false
34- } ) . forEach ( entry => {
27+ } )
28+
29+ const paths = Object . keys ( resolvedEntries . reduce ( ( result , entry ) => {
3530 // Globby yields unix-style paths.
3631 const normalized = path . resolve ( entry )
3732
38- if ( ! paths . includes ( normalized ) ) {
39- paths . push ( normalized )
33+ if ( ! result [ normalized ] ) {
34+ result [ normalized ] = true
4035 }
41- } )
36+
37+ return result
38+ } , { } ) )
4239
4340 debug ( 'globby resolved' , paths )
4441
@@ -50,7 +47,7 @@ module.exports = function (opts, cb) {
5047 let entries
5148
5249 const result = promisedReadPackage ( pkgPath )
53- . catch ( err => {
50+ . catch ( async ( err ) => {
5451 if ( ! err ) {
5552 return Promise . reject ( new Error ( 'Failed to read package.json, but received no error' ) )
5653 } else if ( pkgPath . endsWith ( '/package.json' ) || pkgPath === 'package.json' ) {
@@ -61,7 +58,7 @@ module.exports = function (opts, cb) {
6158 }
6259
6360 // We've likely been given entries rather than a package.json or module path, try resolving that instead
64- entries = resolveGlobbedPath ( pkgPath )
61+ entries = await resolveGlobbedPath ( pkgPath )
6562
6663 if ( ! entries [ 0 ] ) {
6764 return Promise . reject ( new Error ( 'Failed to find package.json, could not find any matching files' ) )
@@ -187,7 +184,7 @@ function joinAndResolvePath (basePath, targetPath) {
187184 return path . resolve ( path . join ( basePath , targetPath ) )
188185}
189186
190- function resolveDefaultEntriesPaths ( opts ) {
187+ async function resolveDefaultEntriesPaths ( opts ) {
191188 const pkgPath = opts . path
192189 const pkgDir = path . dirname ( pkgPath )
193190 const pkg = opts . package
@@ -197,7 +194,10 @@ function resolveDefaultEntriesPaths (opts) {
197194 let paths = [ ]
198195
199196 // Add the path of the main file
200- if ( fs . existsSync ( mainPath ) ) paths . push ( mainPath )
197+ try {
198+ await promisedFsAccess ( mainPath )
199+ paths . push ( mainPath )
200+ } catch ( err ) { }
201201
202202 // Add the path of binaries
203203 if ( pkg . bin ) {
@@ -213,22 +213,30 @@ function resolveDefaultEntriesPaths (opts) {
213213 return paths
214214}
215215
216- function resolvePaths ( opts ) {
216+ async function resolvePaths ( opts ) {
217+ const [
218+ defaultEntries ,
219+ globbedPaths
220+ ] = await Promise . all ( [
221+ ! opts . noDefaultEntries ? await resolveDefaultEntriesPaths ( opts ) : [ ] ,
222+ opts . entries ? await resolveGlobbedPath ( opts . entries , path . dirname ( opts . path ) ) : [ ]
223+ ] )
224+
217225 return [
218- ...( ! opts . noDefaultEntries ? resolveDefaultEntriesPaths ( opts ) : [ ] ) ,
219- ...( opts . entries ? resolveGlobbedPath ( opts . entries , path . dirname ( opts . path ) ) : [ ] )
226+ ...defaultEntries ,
227+ ...globbedPaths
220228 ]
221229}
222230
223- function parse ( opts ) {
231+ async function parse ( opts ) {
224232 const pkg = opts . package
225233 const extensions = opts . extensions
226234
227235 const deps = { }
228236 const seen = [ ]
229237 const core = [ ]
230238
231- const paths = resolvePaths ( opts )
239+ const paths = await resolvePaths ( opts )
232240
233241 debug ( 'entry paths' , paths )
234242
0 commit comments