Skip to content

Commit

Permalink
add support for custom fs override option
Browse files Browse the repository at this point in the history
Fix: #280
  • Loading branch information
isaacs committed Mar 1, 2023
1 parent 31b7e78 commit 3128b34
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@ share the previously loaded cache.
- `signal` An AbortSignal which will cancel the Glob walk when
triggered.

- `fs` An override object to pass in custom filesystem methods.
See [PathScurry docs](http://npm.im/path-scurry) for what can
be overridden.

- `scurry` A [PathScurry](http://npm.im/path-scurry) object used
to traverse the file system. If the `nocase` option is set
explicitly, then any provided `scurry` object must match this
Expand Down
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# cganhe glo

## 9.2

- Support using a custom fs object, which is passed to PathScurry

## 9.1

- Bring back the `root` option, albeit with slightly different
Expand Down
12 changes: 11 additions & 1 deletion src/glob.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Minimatch, MinimatchOptions } from 'minimatch'
import Minipass from 'minipass'
import {
FSOption,
Path,
PathScurry,
PathScurryDarwin,
Expand Down Expand Up @@ -240,6 +241,12 @@ export interface GlobOptions {
* Conflicts with {@link absolute}
*/
withFileTypes?: boolean

/**
* An fs implementation to override some or all of the defaults. See
* http://npm.im/path-scurry for details about what can be overridden.
*/
fs?: FSOption
}

export type GlobOptionsWithFileTypesTrue = GlobOptions & {
Expand Down Expand Up @@ -389,7 +396,10 @@ export class Glob<Opts extends GlobOptions> implements GlobOptions {
: opts.platform
? PathScurryPosix
: PathScurry
this.scurry = new Scurry(this.cwd, { nocase: opts.nocase })
this.scurry = new Scurry(this.cwd, {
nocase: opts.nocase,
fs: opts.fs,
})
}
this.nocase = this.scurry.nocase

Expand Down
30 changes: 30 additions & 0 deletions test/custom-fs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import t from 'tap'
import { globSync } from '../'

// just a rudimentary test, since PathScurry tests it more anyway
import { readdirSync } from 'fs'
let readdirCalled = 0
const myReaddirSync = (path: string, options: { withFileTypes: true }) => {
readdirCalled++
return readdirSync(path, options)
}

const cwd = t.testdir({
a: '',
b: '',
c: {},
})

t.same(
new Set(['a', 'b', 'c', '']),
new Set(
globSync('**', {
fs: {
readdirSync: myReaddirSync,
},
cwd,
})
)
)

t.equal(readdirCalled, 2)

0 comments on commit 3128b34

Please sign in to comment.