Skip to content

Commit 3128b34

Browse files
committed
add support for custom fs override option
Fix: #280
1 parent 31b7e78 commit 3128b34

4 files changed

Lines changed: 51 additions & 1 deletion

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,10 @@ share the previously loaded cache.
366366
- `signal` An AbortSignal which will cancel the Glob walk when
367367
triggered.
368368

369+
- `fs` An override object to pass in custom filesystem methods.
370+
See [PathScurry docs](http://npm.im/path-scurry) for what can
371+
be overridden.
372+
369373
- `scurry` A [PathScurry](http://npm.im/path-scurry) object used
370374
to traverse the file system. If the `nocase` option is set
371375
explicitly, then any provided `scurry` object must match this

changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# cganhe glo
2+
3+
## 9.2
4+
5+
- Support using a custom fs object, which is passed to PathScurry
6+
17
## 9.1
28

39
- Bring back the `root` option, albeit with slightly different

src/glob.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Minimatch, MinimatchOptions } from 'minimatch'
22
import Minipass from 'minipass'
33
import {
4+
FSOption,
45
Path,
56
PathScurry,
67
PathScurryDarwin,
@@ -240,6 +241,12 @@ export interface GlobOptions {
240241
* Conflicts with {@link absolute}
241242
*/
242243
withFileTypes?: boolean
244+
245+
/**
246+
* An fs implementation to override some or all of the defaults. See
247+
* http://npm.im/path-scurry for details about what can be overridden.
248+
*/
249+
fs?: FSOption
243250
}
244251

245252
export type GlobOptionsWithFileTypesTrue = GlobOptions & {
@@ -389,7 +396,10 @@ export class Glob<Opts extends GlobOptions> implements GlobOptions {
389396
: opts.platform
390397
? PathScurryPosix
391398
: PathScurry
392-
this.scurry = new Scurry(this.cwd, { nocase: opts.nocase })
399+
this.scurry = new Scurry(this.cwd, {
400+
nocase: opts.nocase,
401+
fs: opts.fs,
402+
})
393403
}
394404
this.nocase = this.scurry.nocase
395405

test/custom-fs.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import t from 'tap'
2+
import { globSync } from '../'
3+
4+
// just a rudimentary test, since PathScurry tests it more anyway
5+
import { readdirSync } from 'fs'
6+
let readdirCalled = 0
7+
const myReaddirSync = (path: string, options: { withFileTypes: true }) => {
8+
readdirCalled++
9+
return readdirSync(path, options)
10+
}
11+
12+
const cwd = t.testdir({
13+
a: '',
14+
b: '',
15+
c: {},
16+
})
17+
18+
t.same(
19+
new Set(['a', 'b', 'c', '']),
20+
new Set(
21+
globSync('**', {
22+
fs: {
23+
readdirSync: myReaddirSync,
24+
},
25+
cwd,
26+
})
27+
)
28+
)
29+
30+
t.equal(readdirCalled, 2)

0 commit comments

Comments
 (0)