feat(fs/walk): include symlink option (#3464)

Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
This commit is contained in:
MrKleeblatt 2023-07-27 15:07:44 +02:00 committed by GitHub
parent b5c05eea16
commit a9eda9b9e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 2 deletions

View File

@ -51,6 +51,7 @@ Deno.test("expandGlobWildcard", async function () {
"abc",
"abcdef",
"abcdefghi",
"link",
"subdir",
]);
});
@ -70,6 +71,7 @@ Deno.test("expandGlobParent", async function () {
"abc",
"abcdef",
"abcdefghi",
"link",
"subdir",
]);
});
@ -118,6 +120,7 @@ Deno.test("expandGlobGlobstarFalseWithGlob", async function () {
"abc",
"abcdef",
"abcdefghi",
"link",
"subdir",
]);
});

View File

@ -55,6 +55,8 @@ export interface WalkOptions {
includeFiles?: boolean;
/** @default {true} */
includeDirs?: boolean;
/** @default {true} */
includeSymlinks?: boolean;
/** @default {false} */
followSymlinks?: boolean;
exts?: string[];
@ -84,6 +86,7 @@ export async function* walk(
maxDepth = Infinity,
includeFiles = true,
includeDirs = true,
includeSymlinks = true,
followSymlinks = false,
exts = undefined,
match = undefined,
@ -108,7 +111,12 @@ export async function* walk(
let { isSymlink, isDirectory } = entry;
if (isSymlink) {
if (!followSymlinks) continue;
if (!followSymlinks) {
if (includeSymlinks && include(path, exts, match, skip)) {
yield { path, ...entry };
}
continue;
}
path = await Deno.realPath(path);
// Caveat emptor: don't assume |path| is not a symlink. realpath()
// resolves symlinks but another process can replace the file system
@ -121,6 +129,7 @@ export async function* walk(
maxDepth: maxDepth - 1,
includeFiles,
includeDirs,
includeSymlinks,
followSymlinks,
exts,
match,
@ -142,6 +151,7 @@ export function* walkSync(
maxDepth = Infinity,
includeFiles = true,
includeDirs = true,
includeSymlinks = true,
followSymlinks = false,
exts = undefined,
match = undefined,
@ -171,7 +181,12 @@ export function* walkSync(
let { isSymlink, isDirectory } = entry;
if (isSymlink) {
if (!followSymlinks) continue;
if (!followSymlinks) {
if (includeSymlinks && include(path, exts, match, skip)) {
yield { path, ...entry };
}
continue;
}
path = Deno.realPathSync(path);
// Caveat emptor: don't assume |path| is not a symlink. realpath()
// resolves symlinks but another process can replace the file system
@ -184,6 +199,7 @@ export function* walkSync(
maxDepth: maxDepth - 1,
includeFiles,
includeDirs,
includeSymlinks,
followSymlinks,
exts,
match,

View File

@ -78,6 +78,12 @@ Deno.test("[fs/walk] symlink", async () =>
followSymlinks: true,
}));
Deno.test("[fs/walk] symlink without followSymlink", async () => {
await assertWalkPaths("symlink", [".", "x", "y"], {
followSymlinks: false,
});
});
Deno.test("[fs/walk] non-existent root", async () => {
const root = resolve(testdataDir, "non_existent");
await assertRejects(