-
Notifications
You must be signed in to change notification settings - Fork 576
/
walk.ts
219 lines (206 loc) · 5.38 KB
/
walk.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Documentation and interface for walk were adapted from Go
// https://golang.org/pkg/path/filepath/#Walk
// Copyright 2009 The Go Authors. All rights reserved. BSD license.
import { assert } from "../_util/assert.ts";
import { basename, join, normalize } from "../path/mod.ts";
/** Create WalkEntry for the `path` synchronously */
export function _createWalkEntrySync(path: string): WalkEntry {
path = normalize(path);
const name = basename(path);
const info = Deno.statSync(path);
return {
path,
name,
isFile: info.isFile,
isDirectory: info.isDirectory,
isSymlink: info.isSymlink,
};
}
/** Create WalkEntry for the `path` asynchronously */
export async function _createWalkEntry(path: string): Promise<WalkEntry> {
path = normalize(path);
const name = basename(path);
const info = await Deno.stat(path);
return {
path,
name,
isFile: info.isFile,
isDirectory: info.isDirectory,
isSymlink: info.isSymlink,
};
}
export interface WalkOptions {
maxDepth?: number;
includeFiles?: boolean;
includeDirs?: boolean;
followSymlinks?: boolean;
exts?: string[];
match?: RegExp[];
skip?: RegExp[];
}
function include(
path: string,
exts?: string[],
match?: RegExp[],
skip?: RegExp[],
): boolean {
if (exts && !exts.some((ext): boolean => path.endsWith(ext))) {
return false;
}
if (match && !match.some((pattern): boolean => !!path.match(pattern))) {
return false;
}
if (skip && skip.some((pattern): boolean => !!path.match(pattern))) {
return false;
}
return true;
}
function wrapErrorWithRootPath(err: unknown, root: string) {
if (err instanceof Error && "root" in err) return err;
const e = new Error() as Error & { root: string };
e.root = root;
e.message = err instanceof Error
? `${err.message} for path "${root}"`
: `[non-error thrown] for path "${root}"`;
e.stack = err instanceof Error ? err.stack : undefined;
e.cause = err instanceof Error ? err.cause : undefined;
return e;
}
export interface WalkEntry extends Deno.DirEntry {
path: string;
}
/** Walks the file tree rooted at root, yielding each file or directory in the
* tree filtered according to the given options. The files are walked in lexical
* order, which makes the output deterministic but means that for very large
* directories walk() can be inefficient.
*
* Options:
* - maxDepth?: number = Infinity;
* - includeFiles?: boolean = true;
* - includeDirs?: boolean = true;
* - followSymlinks?: boolean = false;
* - exts?: string[];
* - match?: RegExp[];
* - skip?: RegExp[];
*
* ```ts
* import { walk } from "./walk.ts";
* import { assert } from "../testing/asserts.ts";
*
* for await (const entry of walk(".")) {
* console.log(entry.path);
* assert(entry.isFile);
* }
* ```
*/
export async function* walk(
root: string,
{
maxDepth = Infinity,
includeFiles = true,
includeDirs = true,
followSymlinks = false,
exts = undefined,
match = undefined,
skip = undefined,
}: WalkOptions = {},
): AsyncIterableIterator<WalkEntry> {
if (maxDepth < 0) {
return;
}
if (includeDirs && include(root, exts, match, skip)) {
yield await _createWalkEntry(root);
}
if (maxDepth < 1 || !include(root, undefined, undefined, skip)) {
return;
}
try {
for await (const entry of Deno.readDir(root)) {
assert(entry.name != null);
let path = join(root, entry.name);
let isFile = entry.isFile;
if (entry.isSymlink) {
if (followSymlinks) {
path = await Deno.realPath(path);
isFile = await Deno.lstat(path).then((s) => s.isFile);
} else {
continue;
}
}
if (isFile) {
if (includeFiles && include(path, exts, match, skip)) {
yield { path, ...entry };
}
} else {
yield* walk(path, {
maxDepth: maxDepth - 1,
includeFiles,
includeDirs,
followSymlinks,
exts,
match,
skip,
});
}
}
} catch (err) {
throw wrapErrorWithRootPath(err, normalize(root));
}
}
/** Same as walk() but uses synchronous ops */
export function* walkSync(
root: string,
{
maxDepth = Infinity,
includeFiles = true,
includeDirs = true,
followSymlinks = false,
exts = undefined,
match = undefined,
skip = undefined,
}: WalkOptions = {},
): IterableIterator<WalkEntry> {
if (maxDepth < 0) {
return;
}
if (includeDirs && include(root, exts, match, skip)) {
yield _createWalkEntrySync(root);
}
if (maxDepth < 1 || !include(root, undefined, undefined, skip)) {
return;
}
let entries;
try {
entries = Deno.readDirSync(root);
} catch (err) {
throw wrapErrorWithRootPath(err, normalize(root));
}
for (const entry of entries) {
assert(entry.name != null);
let path = join(root, entry.name);
let isFile = entry.isFile;
if (entry.isSymlink) {
if (followSymlinks) {
path = Deno.realPathSync(path);
isFile = Deno.lstatSync(path).isFile;
} else {
continue;
}
}
if (isFile) {
if (includeFiles && include(path, exts, match, skip)) {
yield { path, ...entry };
}
} else {
yield* walkSync(path, {
maxDepth: maxDepth - 1,
includeFiles,
includeDirs,
followSymlinks,
exts,
match,
skip,
});
}
}
}