Skip to content

Commit

Permalink
Add posix: boolean option to return / paths
Browse files Browse the repository at this point in the history
Fix: #520
  • Loading branch information
isaacs committed Apr 14, 2023
1 parent d6e8645 commit a1cba4d
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 16 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,13 @@ share the previously loaded cache.

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

- `posix` Set to true to use `/` as the path separator in
returned results. On posix systems, this has no effect. On
Windows systems, this will return `/` delimited path results,
and absolute paths will be returned in their full resolved UNC
path form, eg insted of `'C:\\foo\\bar'`, it will return
`//?/C:/foo/bar`.

- `platform` Defaults to value of `process.platform` if
available, or `'linux'` if not. Setting `platform:'win32'` on
non-Windows systems may cause strange behavior.
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"fs.realpath": "^1.0.0",
"minimatch": "^9.0.0",
"minipass": "^5.0.0",
"path-scurry": "^1.6.4"
"path-scurry": "^1.7.0"
},
"devDependencies": {
"@types/node": "^18.11.18",
Expand Down
13 changes: 13 additions & 0 deletions src/glob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,24 @@ export interface GlobOptions {
* matching operations slower and *extremely* noisy.
*/
debug?: boolean

/**
* Return `/` delimited paths, even on Windows.
*
* On posix systems, this has no effect. But, on Windows, it means that
* paths will be `/` delimited, and absolute paths will be their full
* resolved UNC forms, eg instead of `'C:\\foo\\bar'`, it would return
* `'//?/C:/foo/bar'`
*/
posix?: boolean
}

export type GlobOptionsWithFileTypesTrue = GlobOptions & {
withFileTypes: true
// string options not relevant if returning Path objects.
absolute?: undefined
mark?: undefined
posix?: undefined
}

export type GlobOptionsWithFileTypesFalse = GlobOptions & {
Expand Down
8 changes: 5 additions & 3 deletions src/walker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface GlobWalkerOpts {
noext?: boolean
noglobstar?: boolean
platform?: NodeJS.Platform
posix?: boolean
realpath?: boolean
root?: string
stat?: boolean
Expand Down Expand Up @@ -109,7 +110,7 @@ export abstract class GlobUtil<O extends GlobWalkerOpts = GlobWalkerOpts> {
this.patterns = patterns
this.path = path
this.opts = opts
this.#sep = opts.platform === 'win32' ? '\\' : '/'
this.#sep = !opts.posix && opts.platform === 'win32' ? '\\' : '/'
if (opts.ignore) {
this.#ignore = makeIgnore(opts.ignore, opts)
}
Expand Down Expand Up @@ -207,9 +208,10 @@ export abstract class GlobUtil<O extends GlobWalkerOpts = GlobWalkerOpts> {
if (this.opts.withFileTypes) {
this.matchEmit(e)
} else if (abs) {
this.matchEmit(e.fullpath() + mark)
const abs = this.opts.posix ? e.fullpathPosix() : e.fullpath()
this.matchEmit(abs + mark)
} else {
const rel = e.relative()
const rel = this.opts.posix ? e.relativePosix() : e.relative()
const pre =
this.opts.dotRelative && !rel.startsWith('..' + this.#sep)
? '.' + this.#sep
Expand Down
13 changes: 8 additions & 5 deletions test/mark.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { sep } from 'path'
import t from 'tap'
import { glob } from '../'
process.chdir(__dirname + '/fixtures')

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

Expand Down Expand Up @@ -146,11 +146,14 @@ for (const mark of [true, false]) {
for (const slash of [true, false]) {
t.test('cwd mark:' + mark + ' slash:' + slash, async t => {
const pattern = cwd + (slash ? '/' : '')
const results = await glob(pattern, { mark })
const results = await glob(pattern, { mark, posix: true })
t.equal(results.length, 1)
const res = results[0].replace(/\\/g, '/')
const syncResults = glob.globSync(pattern, { mark: mark })
const syncRes = syncResults[0].replace(/\\/g, '/')
const res = results[0]
const syncResults = glob.globSync(pattern, {
mark: mark,
posix: true,
})
const syncRes = syncResults[0]
if (mark) {
t.equal(res, cwd + '/')
} else {
Expand Down
4 changes: 1 addition & 3 deletions test/windows-paths-fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ t.test('treat backslash as escape', t => {
for (const [pattern, expect] of cases) {
t.test(pattern, async t => {
t.strictSame(
glob
.globSync(pattern, { cwd: dir })
.map(s => s.replace(/\\/g, '/')),
glob.globSync(pattern, { cwd: dir, posix: true }),
expect,
'sync'
)
Expand Down

1 comment on commit a1cba4d

@isaacs
Copy link
Owner Author

@isaacs isaacs commented on a1cba4d Apr 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commit message should reference #521, not #520

Please sign in to comment.