Skip to content

Commit

Permalink
feat(fs/walk): include symlink option (#3464)
Browse files Browse the repository at this point in the history
Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
  • Loading branch information
MrKleeblatt and kt3k authored Jul 27, 2023
1 parent b5c05ee commit a9eda9b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
3 changes: 3 additions & 0 deletions fs/expand_glob_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Deno.test("expandGlobWildcard", async function () {
"abc",
"abcdef",
"abcdefghi",
"link",
"subdir",
]);
});
Expand All @@ -70,6 +71,7 @@ Deno.test("expandGlobParent", async function () {
"abc",
"abcdef",
"abcdefghi",
"link",
"subdir",
]);
});
Expand Down Expand Up @@ -118,6 +120,7 @@ Deno.test("expandGlobGlobstarFalseWithGlob", async function () {
"abc",
"abcdef",
"abcdefghi",
"link",
"subdir",
]);
});
Expand Down
20 changes: 18 additions & 2 deletions fs/walk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ export interface WalkOptions {
includeFiles?: boolean;
/** @default {true} */
includeDirs?: boolean;
/** @default {true} */
includeSymlinks?: boolean;
/** @default {false} */
followSymlinks?: boolean;
exts?: string[];
Expand Down Expand Up @@ -84,6 +86,7 @@ export async function* walk(
maxDepth = Infinity,
includeFiles = true,
includeDirs = true,
includeSymlinks = true,
followSymlinks = false,
exts = undefined,
match = undefined,
Expand All @@ -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
Expand All @@ -121,6 +129,7 @@ export async function* walk(
maxDepth: maxDepth - 1,
includeFiles,
includeDirs,
includeSymlinks,
followSymlinks,
exts,
match,
Expand All @@ -142,6 +151,7 @@ export function* walkSync(
maxDepth = Infinity,
includeFiles = true,
includeDirs = true,
includeSymlinks = true,
followSymlinks = false,
exts = undefined,
match = undefined,
Expand Down Expand Up @@ -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
Expand All @@ -184,6 +199,7 @@ export function* walkSync(
maxDepth: maxDepth - 1,
includeFiles,
includeDirs,
includeSymlinks,
followSymlinks,
exts,
match,
Expand Down
6 changes: 6 additions & 0 deletions fs/walk_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit a9eda9b

Please sign in to comment.