Skip to content

Commit de8d0ab

Browse files
nayeemrmnry
authored andcommitted
Add includeDirs to WalkOptions (#601)
1 parent 06958a4 commit de8d0ab

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

fs/walk.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
// Documentation and interface for walk were adapted from Go
22
// https://golang.org/pkg/path/filepath/#Walk
33
// Copyright 2009 The Go Authors. All rights reserved. BSD license.
4-
const { readDir, readDirSync } = Deno;
4+
const { readDir, readDirSync, stat, statSync } = Deno;
55
type FileInfo = Deno.FileInfo;
66
import { unimplemented } from "../testing/asserts.ts";
77
import { join } from "./path/mod.ts";
88

99
export interface WalkOptions {
1010
maxDepth?: number;
11+
includeDirs?: boolean;
1112
exts?: string[];
1213
match?: RegExp[];
1314
skip?: RegExp[];
@@ -47,13 +48,14 @@ export interface WalkInfo {
4748
info: FileInfo;
4849
}
4950

50-
/** Walks the file tree rooted at root, calling walkFn for each file or
51-
* directory in the tree, including root. The files are walked in lexical
51+
/** Walks the file tree rooted at root, yielding each file or directory in the
52+
* tree filtered according to the given options. The files are walked in lexical
5253
* order, which makes the output deterministic but means that for very large
5354
* directories walk() can be inefficient.
5455
*
5556
* Options:
5657
* - maxDepth?: number;
58+
* - includeDirs?: boolean;
5759
* - exts?: string[];
5860
* - match?: RegExp[];
5961
* - skip?: RegExp[];
@@ -70,6 +72,10 @@ export async function* walk(
7072
options: WalkOptions = {}
7173
): AsyncIterableIterator<WalkInfo> {
7274
options.maxDepth! -= 1;
75+
if (options.includeDirs && include(root, options)) {
76+
const rootInfo = await stat(root);
77+
yield { filename: root, info: rootInfo };
78+
}
7379
let ls: FileInfo[] = [];
7480
try {
7581
ls = await readDir(root);
@@ -108,6 +114,10 @@ export function* walkSync(
108114
options: WalkOptions = {}
109115
): IterableIterator<WalkInfo> {
110116
options.maxDepth! -= 1;
117+
if (options.includeDirs && include(root, options)) {
118+
const rootInfo = statSync(root);
119+
yield { filename: root, info: rootInfo };
120+
}
111121
let ls: FileInfo[] = [];
112122
try {
113123
ls = readDirSync(root);

fs/walk_test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,20 @@ testWalk(
118118
}
119119
);
120120

121+
testWalk(
122+
async (d: string): Promise<void> => {
123+
await touch(d + "/a");
124+
await mkdir(d + "/b");
125+
await touch(d + "/b/c");
126+
},
127+
async function includeDirs(): Promise<void> {
128+
assertReady(2);
129+
const arr = await walkArray(".", { includeDirs: true });
130+
assertEquals(arr.length, 4);
131+
assertEquals(arr, [".", "a", "b", "b/c"]);
132+
}
133+
);
134+
121135
testWalk(
122136
async (d: string): Promise<void> => {
123137
await touch(d + "/x.ts");

0 commit comments

Comments
 (0)