Skip to content

Commit a1cba4d

Browse files
committed
Add posix: boolean option to return / paths
Fix: #520
1 parent d6e8645 commit a1cba4d

7 files changed

Lines changed: 39 additions & 16 deletions

File tree

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,13 @@ share the previously loaded cache.
430430

431431
`absolute` may not be used along with `withFileTypes`.
432432

433+
- `posix` Set to true to use `/` as the path separator in
434+
returned results. On posix systems, this has no effect. On
435+
Windows systems, this will return `/` delimited path results,
436+
and absolute paths will be returned in their full resolved UNC
437+
path form, eg insted of `'C:\\foo\\bar'`, it will return
438+
`//?/C:/foo/bar`.
439+
433440
- `platform` Defaults to value of `process.platform` if
434441
available, or `'linux'` if not. Setting `platform:'win32'` on
435442
non-Windows systems may cause strange behavior.

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
"fs.realpath": "^1.0.0",
6363
"minimatch": "^9.0.0",
6464
"minipass": "^5.0.0",
65-
"path-scurry": "^1.6.4"
65+
"path-scurry": "^1.7.0"
6666
},
6767
"devDependencies": {
6868
"@types/node": "^18.11.18",

src/glob.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,24 @@ export interface GlobOptions {
282282
* matching operations slower and *extremely* noisy.
283283
*/
284284
debug?: boolean
285+
286+
/**
287+
* Return `/` delimited paths, even on Windows.
288+
*
289+
* On posix systems, this has no effect. But, on Windows, it means that
290+
* paths will be `/` delimited, and absolute paths will be their full
291+
* resolved UNC forms, eg instead of `'C:\\foo\\bar'`, it would return
292+
* `'//?/C:/foo/bar'`
293+
*/
294+
posix?: boolean
285295
}
286296

287297
export type GlobOptionsWithFileTypesTrue = GlobOptions & {
288298
withFileTypes: true
299+
// string options not relevant if returning Path objects.
289300
absolute?: undefined
301+
mark?: undefined
302+
posix?: undefined
290303
}
291304

292305
export type GlobOptionsWithFileTypesFalse = GlobOptions & {

src/walker.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export interface GlobWalkerOpts {
3535
noext?: boolean
3636
noglobstar?: boolean
3737
platform?: NodeJS.Platform
38+
posix?: boolean
3839
realpath?: boolean
3940
root?: string
4041
stat?: boolean
@@ -109,7 +110,7 @@ export abstract class GlobUtil<O extends GlobWalkerOpts = GlobWalkerOpts> {
109110
this.patterns = patterns
110111
this.path = path
111112
this.opts = opts
112-
this.#sep = opts.platform === 'win32' ? '\\' : '/'
113+
this.#sep = !opts.posix && opts.platform === 'win32' ? '\\' : '/'
113114
if (opts.ignore) {
114115
this.#ignore = makeIgnore(opts.ignore, opts)
115116
}
@@ -207,9 +208,10 @@ export abstract class GlobUtil<O extends GlobWalkerOpts = GlobWalkerOpts> {
207208
if (this.opts.withFileTypes) {
208209
this.matchEmit(e)
209210
} else if (abs) {
210-
this.matchEmit(e.fullpath() + mark)
211+
const abs = this.opts.posix ? e.fullpathPosix() : e.fullpath()
212+
this.matchEmit(abs + mark)
211213
} else {
212-
const rel = e.relative()
214+
const rel = this.opts.posix ? e.relativePosix() : e.relative()
213215
const pre =
214216
this.opts.dotRelative && !rel.startsWith('..' + this.#sep)
215217
? '.' + this.#sep

test/mark.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import { sep } from 'path'
12
import t from 'tap'
23
import { glob } from '../'
34
process.chdir(__dirname + '/fixtures')
45

56
const alphasort = (a: string, b: string) => a.localeCompare(b, 'en')
6-
import { sep } from 'path'
77
const j = (a: string[]) =>
88
a.map(s => s.split('/').join(sep)).sort(alphasort)
99

@@ -146,11 +146,14 @@ for (const mark of [true, false]) {
146146
for (const slash of [true, false]) {
147147
t.test('cwd mark:' + mark + ' slash:' + slash, async t => {
148148
const pattern = cwd + (slash ? '/' : '')
149-
const results = await glob(pattern, { mark })
149+
const results = await glob(pattern, { mark, posix: true })
150150
t.equal(results.length, 1)
151-
const res = results[0].replace(/\\/g, '/')
152-
const syncResults = glob.globSync(pattern, { mark: mark })
153-
const syncRes = syncResults[0].replace(/\\/g, '/')
151+
const res = results[0]
152+
const syncResults = glob.globSync(pattern, {
153+
mark: mark,
154+
posix: true,
155+
})
156+
const syncRes = syncResults[0]
154157
if (mark) {
155158
t.equal(res, cwd + '/')
156159
} else {

test/windows-paths-fs.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ t.test('treat backslash as escape', t => {
3131
for (const [pattern, expect] of cases) {
3232
t.test(pattern, async t => {
3333
t.strictSame(
34-
glob
35-
.globSync(pattern, { cwd: dir })
36-
.map(s => s.replace(/\\/g, '/')),
34+
glob.globSync(pattern, { cwd: dir, posix: true }),
3735
expect,
3836
'sync'
3937
)

0 commit comments

Comments
 (0)