2
2
3
3
const path = require ( 'path' )
4
4
const fs = require ( 'fs' )
5
+ const { promisify } = require ( 'util' )
5
6
const readPackage = require ( 'read-package-json' )
6
7
const builtins = require ( 'module' ) . builtinModules
7
8
const resolveModule = require ( 'resolve' )
@@ -11,34 +12,30 @@ const globby = require('globby')
11
12
const micromatch = require ( 'micromatch' )
12
13
const pkgUp = require ( 'pkg-up' )
13
14
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 )
25
17
18
+ async function resolveGlobbedPath ( entries , cwd ) {
26
19
if ( typeof entries === 'string' ) entries = [ entries ]
27
20
28
21
debug ( 'globby resolving' , entries )
29
22
30
- globby . sync ( entries , {
23
+ const resolvedEntries = await globby ( entries , {
31
24
cwd,
32
25
absolute : true ,
33
26
expandDirectories : false
34
- } ) . forEach ( entry => {
27
+ } )
28
+
29
+ const paths = Object . keys ( resolvedEntries . reduce ( ( result , entry ) => {
35
30
// Globby yields unix-style paths.
36
31
const normalized = path . resolve ( entry )
37
32
38
- if ( ! paths . includes ( normalized ) ) {
39
- paths . push ( normalized )
33
+ if ( ! result [ normalized ] ) {
34
+ result [ normalized ] = true
40
35
}
41
- } )
36
+
37
+ return result
38
+ } , { } ) )
42
39
43
40
debug ( 'globby resolved' , paths )
44
41
@@ -50,7 +47,7 @@ module.exports = function (opts, cb) {
50
47
let entries
51
48
52
49
const result = promisedReadPackage ( pkgPath )
53
- . catch ( err => {
50
+ . catch ( async ( err ) => {
54
51
if ( ! err ) {
55
52
return Promise . reject ( new Error ( 'Failed to read package.json, but received no error' ) )
56
53
} else if ( pkgPath . endsWith ( '/package.json' ) || pkgPath === 'package.json' ) {
@@ -61,7 +58,7 @@ module.exports = function (opts, cb) {
61
58
}
62
59
63
60
// 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 )
65
62
66
63
if ( ! entries [ 0 ] ) {
67
64
return Promise . reject ( new Error ( 'Failed to find package.json, could not find any matching files' ) )
@@ -187,7 +184,7 @@ function joinAndResolvePath (basePath, targetPath) {
187
184
return path . resolve ( path . join ( basePath , targetPath ) )
188
185
}
189
186
190
- function resolveDefaultEntriesPaths ( opts ) {
187
+ async function resolveDefaultEntriesPaths ( opts ) {
191
188
const pkgPath = opts . path
192
189
const pkgDir = path . dirname ( pkgPath )
193
190
const pkg = opts . package
@@ -197,7 +194,10 @@ function resolveDefaultEntriesPaths (opts) {
197
194
let paths = [ ]
198
195
199
196
// 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 ) { }
201
201
202
202
// Add the path of binaries
203
203
if ( pkg . bin ) {
@@ -213,22 +213,30 @@ function resolveDefaultEntriesPaths (opts) {
213
213
return paths
214
214
}
215
215
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
+
217
225
return [
218
- ...( ! opts . noDefaultEntries ? resolveDefaultEntriesPaths ( opts ) : [ ] ) ,
219
- ...( opts . entries ? resolveGlobbedPath ( opts . entries , path . dirname ( opts . path ) ) : [ ] )
226
+ ...defaultEntries ,
227
+ ...globbedPaths
220
228
]
221
229
}
222
230
223
- function parse ( opts ) {
231
+ async function parse ( opts ) {
224
232
const pkg = opts . package
225
233
const extensions = opts . extensions
226
234
227
235
const deps = { }
228
236
const seen = [ ]
229
237
const core = [ ]
230
238
231
- const paths = resolvePaths ( opts )
239
+ const paths = await resolvePaths ( opts )
232
240
233
241
debug ( 'entry paths' , paths )
234
242
0 commit comments