Skip to content

Commit 130c135

Browse files
aduh95targos
authored andcommitted
fs: add support for URL for fs.glob's cwd option
PR-URL: #58182 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: LiviaMedeiros <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Jason Zhang <[email protected]>
1 parent 1774612 commit 130c135

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

doc/api/fs.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,9 @@ behavior is similar to `cp dir1/ dir2/`.
10771077
<!-- YAML
10781078
added: v22.0.0
10791079
changes:
1080+
- version: REPLACEME
1081+
pr-url: https://github.com/nodejs/node/pull/58182
1082+
description: Add support for `URL` instances for `cwd` option.
10801083
- version: v24.0.0
10811084
pr-url: https://github.com/nodejs/node/pull/57513
10821085
description: Marking the API stable.
@@ -1092,7 +1095,7 @@ changes:
10921095
10931096
* `pattern` {string|string\[]}
10941097
* `options` {Object}
1095-
* `cwd` {string} current working directory. **Default:** `process.cwd()`
1098+
* `cwd` {string|URL} current working directory. **Default:** `process.cwd()`
10961099
* `exclude` {Function|string\[]} Function to filter out files/directories or a
10971100
list of glob patterns to be excluded. If a function is provided, return
10981101
`true` to exclude the item, `false` to include it. **Default:** `undefined`.
@@ -3132,6 +3135,9 @@ descriptor. See [`fs.utimes()`][].
31323135
<!-- YAML
31333136
added: v22.0.0
31343137
changes:
3138+
- version: REPLACEME
3139+
pr-url: https://github.com/nodejs/node/pull/58182
3140+
description: Add support for `URL` instances for `cwd` option.
31353141
- version: v24.0.0
31363142
pr-url: https://github.com/nodejs/node/pull/57513
31373143
description: Marking the API stable.
@@ -3148,7 +3154,7 @@ changes:
31483154
* `pattern` {string|string\[]}
31493155
31503156
* `options` {Object}
3151-
* `cwd` {string} current working directory. **Default:** `process.cwd()`
3157+
* `cwd` {string|URL} current working directory. **Default:** `process.cwd()`
31523158
* `exclude` {Function|string\[]} Function to filter out files/directories or a
31533159
list of glob patterns to be excluded. If a function is provided, return
31543160
`true` to exclude the item, `false` to include it. **Default:** `undefined`.
@@ -5685,6 +5691,9 @@ Synchronous version of [`fs.futimes()`][]. Returns `undefined`.
56855691
<!-- YAML
56865692
added: v22.0.0
56875693
changes:
5694+
- version: REPLACEME
5695+
pr-url: https://github.com/nodejs/node/pull/58182
5696+
description: Add support for `URL` instances for `cwd` option.
56885697
- version: v24.0.0
56895698
pr-url: https://github.com/nodejs/node/pull/57513
56905699
description: Marking the API stable.
@@ -5700,7 +5709,7 @@ changes:
57005709
57015710
* `pattern` {string|string\[]}
57025711
* `options` {Object}
5703-
* `cwd` {string} current working directory. **Default:** `process.cwd()`
5712+
* `cwd` {string|URL} current working directory. **Default:** `process.cwd()`
57045713
* `exclude` {Function|string\[]} Function to filter out files/directories or a
57055714
list of glob patterns to be excluded. If a function is provided, return
57065715
`true` to exclude the item, `false` to include it. **Default:** `undefined`.

lib/internal/fs/glob.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const {
3838
hideStackFrames,
3939
} = require('internal/errors');
4040
const assert = require('internal/assert');
41+
const { toPathIfFileURL } = require('internal/url');
4142

4243
let minimatch;
4344
function lazyMinimatch() {
@@ -268,7 +269,7 @@ class Glob {
268269
constructor(pattern, options = kEmptyObject) {
269270
validateObject(options, 'options');
270271
const { exclude, cwd, withFileTypes } = options;
271-
this.#root = cwd ?? '.';
272+
this.#root = toPathIfFileURL(cwd) ?? '.';
272273
this.#withFileTypes = !!withFileTypes;
273274
if (exclude != null) {
274275
validateStringArrayOrFunction(exclude, 'options.exclude');

test/parallel/test-fs-glob.mjs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { resolve, dirname, sep, relative, join, isAbsolute } from 'node:path';
44
import { mkdir, writeFile, symlink, glob as asyncGlob } from 'node:fs/promises';
55
import { glob, globSync, Dirent } from 'node:fs';
66
import { test, describe } from 'node:test';
7+
import { pathToFileURL } from 'node:url';
78
import { promisify } from 'node:util';
89
import assert from 'node:assert';
910

@@ -338,6 +339,39 @@ describe('fsPromises glob', function() {
338339
}
339340
});
340341

342+
describe('glob - with file: URL as cwd', function() {
343+
const promisified = promisify(glob);
344+
for (const [pattern, expected] of Object.entries(patterns)) {
345+
test(pattern, async () => {
346+
const actual = (await promisified(pattern, { cwd: pathToFileURL(fixtureDir) })).sort();
347+
const normalized = expected.filter(Boolean).map((item) => item.replaceAll('/', sep)).sort();
348+
assert.deepStrictEqual(actual, normalized);
349+
});
350+
}
351+
});
352+
353+
describe('globSync - with file: URL as cwd', function() {
354+
for (const [pattern, expected] of Object.entries(patterns)) {
355+
test(pattern, () => {
356+
const actual = globSync(pattern, { cwd: pathToFileURL(fixtureDir) }).sort();
357+
const normalized = expected.filter(Boolean).map((item) => item.replaceAll('/', sep)).sort();
358+
assert.deepStrictEqual(actual, normalized);
359+
});
360+
}
361+
});
362+
363+
describe('fsPromises.glob - with file: URL as cwd', function() {
364+
for (const [pattern, expected] of Object.entries(patterns)) {
365+
test(pattern, async () => {
366+
const actual = [];
367+
for await (const item of asyncGlob(pattern, { cwd: pathToFileURL(fixtureDir) })) actual.push(item);
368+
actual.sort();
369+
const normalized = expected.filter(Boolean).map((item) => item.replaceAll('/', sep)).sort();
370+
assert.deepStrictEqual(actual, normalized);
371+
});
372+
}
373+
});
374+
341375
const normalizeDirent = (dirent) => relative(fixtureDir, join(dirent.parentPath, dirent.name));
342376
// The call to `join()` with only one argument is important, as
343377
// it ensures that the proper path seperators are applied.

0 commit comments

Comments
 (0)