mirror of
https://github.com/denoland/std.git
synced 2024-11-22 04:59:05 +00:00
feat(fs/walk): include symlink option (#3464)
Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
This commit is contained in:
parent
b5c05eea16
commit
a9eda9b9e1
@ -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",
|
||||
]);
|
||||
});
|
||||
|
20
fs/walk.ts
20
fs/walk.ts
@ -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,
|
||||
|
@ -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(
|
||||
|
Loading…
Reference in New Issue
Block a user