1
1
'use strict'
2
2
3
- const Fs = require ( 'fs' )
3
+ const fs = require ( 'fs-extra' )
4
+ const glob = require ( 'it-glob' )
4
5
const Path = require ( 'path' )
5
- const glob = require ( 'glob' )
6
- const pushable = require ( 'it-pushable' )
7
6
const errCode = require ( 'err-code' )
8
7
9
8
/**
@@ -16,9 +15,9 @@ const errCode = require('err-code')
16
15
* @param {Boolean } [options.hidden] Include .dot files in matched paths
17
16
* @param {Array<String> } [options.ignore] Glob paths to ignore
18
17
* @param {Boolean } [options.followSymlinks] follow symlinks
19
- * @returns {AsyncIterable }
18
+ * @returns {Iterable } source iterable
20
19
*/
21
- module . exports = ( ... args ) => ( async function * ( ) {
20
+ module . exports = async function * globSource ( ... args ) {
22
21
const options = typeof args [ args . length - 1 ] === 'string' ? { } : args . pop ( )
23
22
const paths = args
24
23
@@ -32,63 +31,49 @@ module.exports = (...args) => (async function * () {
32
31
}
33
32
34
33
// Check the input paths comply with options.recursive and convert to glob sources
35
- const results = await Promise . all ( paths . map ( pathAndType ) )
36
- const globSources = results . map ( r => toGlobSource ( r , globSourceOptions ) )
37
-
38
- for ( const globSource of globSources ) {
39
- for await ( const { path, contentPath } of globSource ) {
40
- yield { path, content : Fs . createReadStream ( contentPath ) }
41
- }
34
+ for ( const path of paths ) {
35
+ const stat = await fs . stat ( path )
36
+ const prefix = Path . dirname ( path )
37
+ yield * toGlobSource ( { path, type : stat . isDirectory ( ) ? 'dir' : 'file' , prefix } , globSourceOptions ) )
42
38
}
43
- } ) ( )
44
-
45
- function toGlobSource ( { path, type } , options ) {
46
- return ( async function * ( ) {
47
- options = options || { }
39
+ }
48
40
49
- const baseName = Path . basename ( path )
41
+ async function * toGlobSource ( { path, type, prefix } , options ) {
42
+ options = options || { }
50
43
51
- if ( type === 'file' ) {
52
- yield { path : baseName , contentPath : path }
53
- return
54
- }
44
+ const baseName = Path . basename ( path )
55
45
56
- if ( type === 'dir' && ! options . recursive ) {
57
- throw errCode (
58
- new Error ( `' ${ path } ' is a directory and recursive option not set` ) ,
59
- 'ERR_DIR_NON_RECURSIVE' ,
60
- { path }
46
+ if ( type === 'file' ) {
47
+ yield {
48
+ path : baseName . replace ( prefix , '' ) ,
49
+ content : fs . createReadStream (
50
+ Path . isAbsolute ( path ) ? path : Path . join ( process . cwd ( ) , path )
61
51
)
62
52
}
53
+ return
54
+ }
63
55
64
- const globOptions = Object . assign ( { } , options . glob , {
65
- cwd : path ,
66
- nodir : true ,
67
- realpath : false ,
68
- absolute : false
69
- } )
70
-
71
- // TODO: want to use pull-glob but it doesn't have the features...
72
- const pusher = pushable ( )
56
+ if ( type === 'dir' && ! options . recursive ) {
57
+ throw errCode (
58
+ new Error ( `'${ path } ' is a directory and recursive option not set` ) ,
59
+ 'ERR_DIR_NON_RECURSIVE' ,
60
+ { path }
61
+ )
62
+ }
73
63
74
- glob ( '**/*' , globOptions )
75
- . on ( 'match' , m => pusher . push ( m ) )
76
- . on ( 'end' , ( ) => pusher . end ( ) )
77
- . on ( 'abort' , ( ) => pusher . end ( ) )
78
- . on ( 'error' , err => pusher . end ( err ) )
64
+ const globOptions = Object . assign ( { } , options . glob , {
65
+ cwd : path ,
66
+ nodir : true ,
67
+ realpath : false ,
68
+ absolute : false
69
+ } )
79
70
80
- for await ( const p of pusher ) {
81
- yield {
82
- path : `${ baseName } /${ toPosix ( p ) } ` ,
83
- contentPath : Path . join ( path , p )
84
- }
71
+ for await ( const p of glob ( path , '**/*' , globOptions ) ) {
72
+ yield {
73
+ path : toPosix ( p . replace ( prefix , '' ) ) ,
74
+ content : fs . createReadStream ( p )
85
75
}
86
- } ) ( )
87
- }
88
-
89
- async function pathAndType ( path ) {
90
- const stat = await Fs . promises . stat ( path )
91
- return { path, type : stat . isDirectory ( ) ? 'dir' : 'file' }
76
+ }
92
77
}
93
78
94
79
const toPosix = path => path . replace ( / \\ / g, '/' )
0 commit comments