2019-05-14 17:14:08 -04:00
|
|
|
// 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.
|
2020-02-07 02:23:38 -05:00
|
|
|
import { unimplemented, assert } from "../testing/asserts.ts";
|
2019-10-16 14:39:33 -04:00
|
|
|
import { join } from "../path/mod.ts";
|
2020-03-06 08:34:02 -05:00
|
|
|
const { readdir, readdirSync, stat, statSync } = Deno;
|
2019-10-02 13:59:27 -04:00
|
|
|
type FileInfo = Deno.FileInfo;
|
2019-02-15 11:20:59 -05:00
|
|
|
|
|
|
|
export interface WalkOptions {
|
|
|
|
maxDepth?: number;
|
2019-10-02 13:59:27 -04:00
|
|
|
includeFiles?: boolean;
|
2019-09-18 11:37:37 -04:00
|
|
|
includeDirs?: boolean;
|
2019-10-02 13:59:27 -04:00
|
|
|
followSymlinks?: boolean;
|
2019-02-15 11:20:59 -05:00
|
|
|
exts?: string[];
|
|
|
|
match?: RegExp[];
|
|
|
|
skip?: RegExp[];
|
2019-03-12 01:51:51 -04:00
|
|
|
}
|
|
|
|
|
2019-11-14 22:22:33 -05:00
|
|
|
function include(
|
|
|
|
filename: string,
|
|
|
|
exts?: string[],
|
|
|
|
match?: RegExp[],
|
|
|
|
skip?: RegExp[]
|
|
|
|
): boolean {
|
|
|
|
if (exts && !exts.some((ext): boolean => filename.endsWith(ext))) {
|
2019-03-12 01:51:51 -04:00
|
|
|
return false;
|
|
|
|
}
|
2019-11-14 22:22:33 -05:00
|
|
|
if (match && !match.some((pattern): boolean => !!filename.match(pattern))) {
|
2019-03-12 01:51:51 -04:00
|
|
|
return false;
|
|
|
|
}
|
2019-11-14 22:22:33 -05:00
|
|
|
if (skip && skip.some((pattern): boolean => !!filename.match(pattern))) {
|
2019-03-12 01:51:51 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-14 17:14:08 -04:00
|
|
|
export interface WalkInfo {
|
|
|
|
filename: string;
|
|
|
|
info: FileInfo;
|
2019-03-12 01:51:51 -04:00
|
|
|
}
|
|
|
|
|
2019-09-18 11:37:37 -04:00
|
|
|
/** 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
|
2019-05-14 17:14:08 -04:00
|
|
|
* order, which makes the output deterministic but means that for very large
|
|
|
|
* directories walk() can be inefficient.
|
|
|
|
*
|
|
|
|
* Options:
|
2019-10-02 13:59:27 -04:00
|
|
|
* - maxDepth?: number = Infinity;
|
|
|
|
* - includeFiles?: boolean = true;
|
|
|
|
* - includeDirs?: boolean = true;
|
|
|
|
* - followSymlinks?: boolean = false;
|
2019-05-14 17:14:08 -04:00
|
|
|
* - exts?: string[];
|
|
|
|
* - match?: RegExp[];
|
|
|
|
* - skip?: RegExp[];
|
2019-02-15 11:20:59 -05:00
|
|
|
*
|
2019-05-14 17:14:08 -04:00
|
|
|
* for await (const { filename, info } of walk(".")) {
|
|
|
|
* console.log(filename);
|
|
|
|
* assert(info.isFile());
|
2019-02-15 11:20:59 -05:00
|
|
|
* };
|
|
|
|
*/
|
|
|
|
export async function* walk(
|
2019-05-14 17:14:08 -04:00
|
|
|
root: string,
|
2019-11-14 22:22:33 -05:00
|
|
|
{
|
|
|
|
maxDepth = Infinity,
|
|
|
|
includeFiles = true,
|
|
|
|
includeDirs = true,
|
|
|
|
followSymlinks = false,
|
2020-02-19 15:36:18 -05:00
|
|
|
exts = undefined,
|
|
|
|
match = undefined,
|
|
|
|
skip = undefined
|
2019-11-14 22:22:33 -05:00
|
|
|
}: WalkOptions = {}
|
2019-05-14 17:14:08 -04:00
|
|
|
): AsyncIterableIterator<WalkInfo> {
|
2019-10-02 13:59:27 -04:00
|
|
|
if (maxDepth < 0) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-14 22:22:33 -05:00
|
|
|
if (includeDirs && include(root, exts, match, skip)) {
|
|
|
|
yield { filename: root, info: await stat(root) };
|
2019-10-02 13:59:27 -04:00
|
|
|
}
|
2020-02-19 15:36:18 -05:00
|
|
|
if (maxDepth < 1 || !include(root, undefined, undefined, skip)) {
|
2019-10-02 13:59:27 -04:00
|
|
|
return;
|
2019-09-18 11:37:37 -04:00
|
|
|
}
|
2020-03-06 08:34:02 -05:00
|
|
|
const ls: FileInfo[] = await readdir(root);
|
2019-10-05 12:02:34 -04:00
|
|
|
for (const info of ls) {
|
2019-05-14 17:14:08 -04:00
|
|
|
if (info.isSymlink()) {
|
2019-11-14 22:22:33 -05:00
|
|
|
if (followSymlinks) {
|
2019-05-14 17:14:08 -04:00
|
|
|
// TODO(ry) Re-enable followSymlinks.
|
|
|
|
unimplemented();
|
2019-02-15 11:20:59 -05:00
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2019-05-14 17:14:08 -04:00
|
|
|
|
2020-02-07 02:23:38 -05:00
|
|
|
assert(info.name != null);
|
|
|
|
const filename = join(root, info.name);
|
2019-05-14 17:14:08 -04:00
|
|
|
|
|
|
|
if (info.isFile()) {
|
2019-11-14 22:22:33 -05:00
|
|
|
if (includeFiles && include(filename, exts, match, skip)) {
|
2019-05-14 17:14:08 -04:00
|
|
|
yield { filename, info };
|
2019-02-15 11:20:59 -05:00
|
|
|
}
|
|
|
|
} else {
|
2019-11-14 22:22:33 -05:00
|
|
|
yield* walk(filename, {
|
|
|
|
maxDepth: maxDepth - 1,
|
|
|
|
includeFiles,
|
|
|
|
includeDirs,
|
|
|
|
followSymlinks,
|
|
|
|
exts,
|
|
|
|
match,
|
|
|
|
skip
|
|
|
|
});
|
2019-02-15 11:20:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-14 17:14:08 -04:00
|
|
|
/** Same as walk() but uses synchronous ops */
|
2019-02-15 11:20:59 -05:00
|
|
|
export function* walkSync(
|
2019-10-02 13:59:27 -04:00
|
|
|
root: string,
|
2019-11-14 22:22:33 -05:00
|
|
|
{
|
|
|
|
maxDepth = Infinity,
|
|
|
|
includeFiles = true,
|
|
|
|
includeDirs = true,
|
|
|
|
followSymlinks = false,
|
2020-02-19 15:36:18 -05:00
|
|
|
exts = undefined,
|
|
|
|
match = undefined,
|
|
|
|
skip = undefined
|
2019-11-14 22:22:33 -05:00
|
|
|
}: WalkOptions = {}
|
2019-05-14 17:14:08 -04:00
|
|
|
): IterableIterator<WalkInfo> {
|
2019-10-02 13:59:27 -04:00
|
|
|
if (maxDepth < 0) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-14 22:22:33 -05:00
|
|
|
if (includeDirs && include(root, exts, match, skip)) {
|
|
|
|
yield { filename: root, info: statSync(root) };
|
2019-10-02 13:59:27 -04:00
|
|
|
}
|
2020-02-19 15:36:18 -05:00
|
|
|
if (maxDepth < 1 || !include(root, undefined, undefined, skip)) {
|
2019-10-02 13:59:27 -04:00
|
|
|
return;
|
2019-09-18 11:37:37 -04:00
|
|
|
}
|
2020-03-06 08:34:02 -05:00
|
|
|
const ls: FileInfo[] = readdirSync(root);
|
2019-10-05 12:02:34 -04:00
|
|
|
for (const info of ls) {
|
2019-05-14 17:14:08 -04:00
|
|
|
if (info.isSymlink()) {
|
2019-11-14 22:22:33 -05:00
|
|
|
if (followSymlinks) {
|
2019-05-14 17:14:08 -04:00
|
|
|
unimplemented();
|
2019-02-15 11:20:59 -05:00
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2019-05-14 17:14:08 -04:00
|
|
|
|
2020-02-07 02:23:38 -05:00
|
|
|
assert(info.name != null);
|
|
|
|
const filename = join(root, info.name);
|
2019-05-14 17:14:08 -04:00
|
|
|
|
|
|
|
if (info.isFile()) {
|
2019-11-14 22:22:33 -05:00
|
|
|
if (includeFiles && include(filename, exts, match, skip)) {
|
2019-05-14 17:14:08 -04:00
|
|
|
yield { filename, info };
|
2019-02-15 11:20:59 -05:00
|
|
|
}
|
|
|
|
} else {
|
2019-11-14 22:22:33 -05:00
|
|
|
yield* walkSync(filename, {
|
|
|
|
maxDepth: maxDepth - 1,
|
|
|
|
includeFiles,
|
|
|
|
includeDirs,
|
|
|
|
followSymlinks,
|
|
|
|
exts,
|
|
|
|
match,
|
|
|
|
skip
|
|
|
|
});
|
2019-02-15 11:20:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|