GeezerVison is showing github.com — fs: add support for `URL` for `fs.glob`'s `cwd` option · nodejs/node@130c135
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/fs.md +12-3Lines changed: 12 additions & 3 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -1077,6 +1077,9 @@ behavior is similar to `cp dir1/ dir2/`. | 1077 | 1077 | <!-- YAML | 1078 | 1078 | added: v22.0.0 | 1079 | 1079 | changes: | | 1080 | + - version: REPLACEME | | 1081 | + pr-url: https://github.com/nodejs/node/pull/58182 | | 1082 | + description: Add support for `URL` instances for `cwd` option. | 1080 | 1083 | - version: v24.0.0 | 1081 | 1084 | pr-url: https://github.com/nodejs/node/pull/57513 | 1082 | 1085 | description: Marking the API stable. | @@ -1092,7 +1095,7 @@ changes: | 1092 | 1095 |
| 1093 | 1096 | * `pattern` {string|string\[]} | 1094 | 1097 | * `options` {Object} | 1095 | | - * `cwd` {string} current working directory. **Default:** `process.cwd()` | | 1098 | + * `cwd` {string|URL} current working directory. **Default:** `process.cwd()` | 1096 | 1099 | * `exclude` {Function|string\[]} Function to filter out files/directories or a | 1097 | 1100 | list of glob patterns to be excluded. If a function is provided, return | 1098 | 1101 | `true` to exclude the item, `false` to include it. **Default:** `undefined`. | @@ -3132,6 +3135,9 @@ descriptor. See [`fs.utimes()`][]. | 3132 | 3135 | <!-- YAML | 3133 | 3136 | added: v22.0.0 | 3134 | 3137 | changes: | | 3138 | + - version: REPLACEME | | 3139 | + pr-url: https://github.com/nodejs/node/pull/58182 | | 3140 | + description: Add support for `URL` instances for `cwd` option. | 3135 | 3141 | - version: v24.0.0 | 3136 | 3142 | pr-url: https://github.com/nodejs/node/pull/57513 | 3137 | 3143 | description: Marking the API stable. | @@ -3148,7 +3154,7 @@ changes: | 3148 | 3154 | * `pattern` {string|string\[]} | 3149 | 3155 |
| 3150 | 3156 | * `options` {Object} | 3151 | | - * `cwd` {string} current working directory. **Default:** `process.cwd()` | | 3157 | + * `cwd` {string|URL} current working directory. **Default:** `process.cwd()` | 3152 | 3158 | * `exclude` {Function|string\[]} Function to filter out files/directories or a | 3153 | 3159 | list of glob patterns to be excluded. If a function is provided, return | 3154 | 3160 | `true` to exclude the item, `false` to include it. **Default:** `undefined`. | @@ -5685,6 +5691,9 @@ Synchronous version of [`fs.futimes()`][]. Returns `undefined`. | 5685 | 5691 | <!-- YAML | 5686 | 5692 | added: v22.0.0 | 5687 | 5693 | changes: | | 5694 | + - version: REPLACEME | | 5695 | + pr-url: https://github.com/nodejs/node/pull/58182 | | 5696 | + description: Add support for `URL` instances for `cwd` option. | 5688 | 5697 | - version: v24.0.0 | 5689 | 5698 | pr-url: https://github.com/nodejs/node/pull/57513 | 5690 | 5699 | description: Marking the API stable. | @@ -5700,7 +5709,7 @@ changes: | 5700 | 5709 |
| 5701 | 5710 | * `pattern` {string|string\[]} | 5702 | 5711 | * `options` {Object} | 5703 | | - * `cwd` {string} current working directory. **Default:** `process.cwd()` | | 5712 | + * `cwd` {string|URL} current working directory. **Default:** `process.cwd()` | 5704 | 5713 | * `exclude` {Function|string\[]} Function to filter out files/directories or a | 5705 | 5714 | list of glob patterns to be excluded. If a function is provided, return | 5706 | 5715 | `true` to exclude the item, `false` to include it. **Default:** `undefined`. | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/fs/glob.js +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -38,6 +38,7 @@ const { | 38 | 38 | hideStackFrames, | 39 | 39 | } = require('internal/errors'); | 40 | 40 | const assert = require('internal/assert'); | | 41 | +const { toPathIfFileURL } = require('internal/url'); | 41 | 42 |
| 42 | 43 | let minimatch; | 43 | 44 | function lazyMinimatch() { | @@ -268,7 +269,7 @@ class Glob { | 268 | 269 | constructor(pattern, options = kEmptyObject) { | 269 | 270 | validateObject(options, 'options'); | 270 | 271 | const { exclude, cwd, withFileTypes } = options; | 271 | | - this.#root = cwd ?? '.'; | | 272 | + this.#root = toPathIfFileURL(cwd) ?? '.'; | 272 | 273 | this.#withFileTypes = !!withFileTypes; | 273 | 274 | if (exclude != null) { | 274 | 275 | validateStringArrayOrFunction(exclude, 'options.exclude'); | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-fs-glob.mjs +34Lines changed: 34 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -4,6 +4,7 @@ import { resolve, dirname, sep, relative, join, isAbsolute } from 'node:path'; | 4 | 4 | import { mkdir, writeFile, symlink, glob as asyncGlob } from 'node:fs/promises'; | 5 | 5 | import { glob, globSync, Dirent } from 'node:fs'; | 6 | 6 | import { test, describe } from 'node:test'; | | 7 | +import { pathToFileURL } from 'node:url'; | 7 | 8 | import { promisify } from 'node:util'; | 8 | 9 | import assert from 'node:assert'; | 9 | 10 |
| @@ -338,6 +339,39 @@ describe('fsPromises glob', function() { | 338 | 339 | } | 339 | 340 | }); | 340 | 341 |
| | 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 | + | 341 | 375 | const normalizeDirent = (dirent) => relative(fixtureDir, join(dirent.parentPath, dirent.name)); | 342 | 376 | // The call to `join()` with only one argument is important, as | 343 | 377 | // it ensures that the proper path seperators are applied. | |
|