@@ -7,131 +7,59 @@ const Path = require('path')
7
7
const errCode = require ( 'err-code' )
8
8
9
9
/**
10
- * Create an async iterator that yields paths that match requested file paths.
10
+ * Create an async iterator that yields paths that match requested glob pattern
11
11
*
12
- * @param {Iterable<string> | AsyncIterable<string> | string } paths - File system path(s) to glob from
12
+ * @param {string } cwd - The directory to start matching the pattern in
13
+ * @param {string } pattern - Glob pattern to match
13
14
* @param {Object } [options] - Optional options
14
- * @param {boolean } [options.recursive] - Recursively glob all paths in directories
15
15
* @param {boolean } [options.hidden] - Include .dot files in matched paths
16
- * @param {Array<string> } [options.ignore] - Glob paths to ignore
17
16
* @param {boolean } [options.followSymlinks] - follow symlinks
18
17
* @param {boolean } [options.preserveMode] - preserve mode
19
18
* @param {boolean } [options.preserveMtime] - preserve mtime
20
19
* @param {number } [options.mode] - mode to use - if preserveMode is true this will be ignored
21
20
* @param {import('ipfs-unixfs').MtimeLike } [options.mtime] - mtime to use - if preserveMtime is true this will be ignored
22
21
* @yields {Object} File objects in the form `{ path: String, content: AsyncIterator<Buffer> }`
23
22
*/
24
- module . exports = async function * globSource ( paths , options ) {
23
+ module . exports = async function * globSource ( cwd , pattern , options ) {
25
24
options = options || { }
26
25
27
- if ( typeof paths === 'string' ) {
28
- paths = [ paths ]
26
+ if ( typeof pattern !== 'string' ) {
27
+ throw errCode (
28
+ new Error ( 'Pattern must be a string' ) ,
29
+ 'ERR_INVALID_PATH' ,
30
+ { pattern }
31
+ )
29
32
}
30
33
31
- const globSourceOptions = {
32
- recursive : options . recursive ,
33
- glob : {
34
- dot : Boolean ( options . hidden ) ,
35
- ignore : Array . isArray ( options . ignore ) ? options . ignore : [ ] ,
36
- follow : options . followSymlinks != null ? options . followSymlinks : true
37
- }
34
+ if ( ! Path . isAbsolute ( cwd ) ) {
35
+ cwd = Path . resolve ( process . cwd ( ) , cwd )
38
36
}
39
37
40
- // Check the input paths comply with options.recursive and convert to glob sources
41
- for await ( const path of paths ) {
42
- if ( typeof path !== 'string' ) {
43
- throw errCode (
44
- new Error ( 'Path must be a string' ) ,
45
- 'ERR_INVALID_PATH' ,
46
- { path }
47
- )
48
- }
38
+ const globOptions = Object . assign ( { } , {
39
+ nodir : false ,
40
+ realpath : false ,
41
+ absolute : true ,
42
+ dot : Boolean ( options . hidden ) ,
43
+ follow : options . followSymlinks != null ? options . followSymlinks : true
44
+ } )
49
45
50
- const absolutePath = Path . resolve ( process . cwd ( ) , path )
51
- const stat = await fsp . stat ( absolutePath )
52
- const prefix = Path . dirname ( absolutePath )
46
+ for await ( const p of glob ( cwd , pattern , globOptions ) ) {
47
+ const stat = await fsp . stat ( p )
53
48
54
49
let mode = options . mode
55
50
56
51
if ( options . preserveMode ) {
57
- // @ts -ignore
58
52
mode = stat . mode
59
53
}
60
54
61
55
let mtime = options . mtime
62
56
63
57
if ( options . preserveMtime ) {
64
- // @ts -ignore
65
58
mtime = stat . mtime
66
59
}
67
60
68
- if ( stat . isDirectory ( ) ) {
69
- yield {
70
- path : `/${ Path . basename ( path ) } ` ,
71
- mode,
72
- mtime
73
- }
74
- }
75
-
76
- yield * toGlobSource ( {
77
- path,
78
- type : stat . isDirectory ( ) ? 'dir' : 'file' ,
79
- prefix,
80
- mode,
81
- mtime,
82
- preserveMode : options . preserveMode ,
83
- preserveMtime : options . preserveMtime
84
- } , globSourceOptions )
85
- }
86
- }
87
-
88
- // @ts -ignore
89
- async function * toGlobSource ( { path, type, prefix, mode, mtime, preserveMode, preserveMtime } , options ) {
90
- options = options || { }
91
-
92
- const baseName = Path . basename ( path )
93
-
94
- if ( type === 'file' ) {
95
- yield {
96
- path : `/${ baseName . replace ( prefix , '' ) } ` ,
97
- content : fs . createReadStream ( Path . isAbsolute ( path ) ? path : Path . join ( process . cwd ( ) , path ) ) ,
98
- mode,
99
- mtime
100
- }
101
-
102
- return
103
- }
104
-
105
- if ( type === 'dir' && ! options . recursive ) {
106
- throw errCode (
107
- new Error ( `'${ path } ' is a directory and recursive option not set` ) ,
108
- 'ERR_DIR_NON_RECURSIVE' ,
109
- { path }
110
- )
111
- }
112
-
113
- const globOptions = Object . assign ( { } , options . glob , {
114
- cwd : path ,
115
- nodir : false ,
116
- realpath : false ,
117
- absolute : true
118
- } )
119
-
120
- for await ( const p of glob ( path , '**/*' , globOptions ) ) {
121
- const stat = await fsp . stat ( p )
122
-
123
- if ( preserveMode || preserveMtime ) {
124
- if ( preserveMode ) {
125
- mode = stat . mode
126
- }
127
-
128
- if ( preserveMtime ) {
129
- mtime = stat . mtime
130
- }
131
- }
132
-
133
61
yield {
134
- path : toPosix ( p . replace ( prefix , '' ) ) ,
62
+ path : toPosix ( p . replace ( cwd , '' ) ) ,
135
63
content : stat . isFile ( ) ? fs . createReadStream ( p ) : undefined ,
136
64
mode,
137
65
mtime
0 commit comments