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.
|
|
|
|
import { unimplemented } from "../testing/asserts.ts";
|
2019-10-16 14:39:33 -04:00
|
|
|
import { join } from "../path/mod.ts";
|
2019-10-02 13:59:27 -04:00
|
|
|
const { readDir, readDirSync, stat, statSync } = Deno;
|
|
|
|
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
|
|
|
onError?: (err: Error) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
function patternTest(patterns: RegExp[], path: string): boolean {
|
|
|
|
// Forced to reset last index on regex while iterating for have
|
|
|
|
// consistent results.
|
|
|
|
// See: https://stackoverflow.com/a/1520853
|
2019-11-13 13:42:34 -05:00
|
|
|
return patterns.some((pattern): boolean => {
|
|
|
|
const r = pattern.test(path);
|
|
|
|
pattern.lastIndex = 0;
|
|
|
|
return r;
|
|
|
|
});
|
2019-03-12 01:51:51 -04:00
|
|
|
}
|
|
|
|
|
2019-05-14 17:14:08 -04:00
|
|
|
function include(filename: string, options: WalkOptions): boolean {
|
2019-04-24 07:41:23 -04:00
|
|
|
if (
|
|
|
|
options.exts &&
|
2019-05-14 17:14:08 -04:00
|
|
|
!options.exts.some((ext): boolean => filename.endsWith(ext))
|
2019-04-24 07:41:23 -04:00
|
|
|
) {
|
2019-03-12 01:51:51 -04:00
|
|
|
return false;
|
|
|
|
}
|
2019-05-14 17:14:08 -04:00
|
|
|
if (options.match && !patternTest(options.match, filename)) {
|
2019-03-12 01:51:51 -04:00
|
|
|
return false;
|
|
|
|
}
|
2019-05-14 17:14:08 -04:00
|
|
|
if (options.skip && patternTest(options.skip, filename)) {
|
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[];
|
|
|
|
* - onError?: (err: Error) => void;
|
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-02-15 11:20:59 -05:00
|
|
|
options: WalkOptions = {}
|
2019-05-14 17:14:08 -04:00
|
|
|
): AsyncIterableIterator<WalkInfo> {
|
2019-10-02 13:59:27 -04:00
|
|
|
const maxDepth = options.maxDepth != undefined ? options.maxDepth! : Infinity;
|
|
|
|
if (maxDepth < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (options.includeDirs != false && include(root, options)) {
|
|
|
|
let rootInfo: FileInfo;
|
|
|
|
try {
|
|
|
|
rootInfo = await stat(root);
|
|
|
|
} catch (err) {
|
|
|
|
if (options.onError) {
|
|
|
|
options.onError(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
yield { filename: root, info: rootInfo! };
|
|
|
|
}
|
|
|
|
if (maxDepth < 1 || patternTest(options.skip || [], root)) {
|
|
|
|
return;
|
2019-09-18 11:37:37 -04:00
|
|
|
}
|
2019-02-15 11:20:59 -05:00
|
|
|
let ls: FileInfo[] = [];
|
|
|
|
try {
|
2019-05-14 17:14:08 -04:00
|
|
|
ls = await readDir(root);
|
2019-02-15 11:20:59 -05:00
|
|
|
} catch (err) {
|
|
|
|
if (options.onError) {
|
|
|
|
options.onError(err);
|
|
|
|
}
|
|
|
|
}
|
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-02-15 11:20:59 -05:00
|
|
|
if (options.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
|
|
|
|
2019-05-30 08:59:30 -04:00
|
|
|
const filename = join(root, info.name!);
|
2019-05-14 17:14:08 -04:00
|
|
|
|
|
|
|
if (info.isFile()) {
|
2019-10-02 13:59:27 -04:00
|
|
|
if (options.includeFiles != false && include(filename, options)) {
|
2019-05-14 17:14:08 -04:00
|
|
|
yield { filename, info };
|
2019-02-15 11:20:59 -05:00
|
|
|
}
|
|
|
|
} else {
|
2019-10-02 13:59:27 -04:00
|
|
|
yield* walk(filename, { ...options, maxDepth: maxDepth - 1 });
|
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-02-15 11:20:59 -05:00
|
|
|
options: WalkOptions = {}
|
2019-05-14 17:14:08 -04:00
|
|
|
): IterableIterator<WalkInfo> {
|
2019-10-02 13:59:27 -04:00
|
|
|
const maxDepth = options.maxDepth != undefined ? options.maxDepth! : Infinity;
|
|
|
|
if (maxDepth < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (options.includeDirs != false && include(root, options)) {
|
|
|
|
let rootInfo: FileInfo;
|
|
|
|
try {
|
|
|
|
rootInfo = statSync(root);
|
|
|
|
} catch (err) {
|
|
|
|
if (options.onError) {
|
|
|
|
options.onError(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
yield { filename: root, info: rootInfo! };
|
|
|
|
}
|
|
|
|
if (maxDepth < 1 || patternTest(options.skip || [], root)) {
|
|
|
|
return;
|
2019-09-18 11:37:37 -04:00
|
|
|
}
|
2019-02-15 11:20:59 -05:00
|
|
|
let ls: FileInfo[] = [];
|
|
|
|
try {
|
2019-05-14 17:14:08 -04:00
|
|
|
ls = readDirSync(root);
|
2019-02-15 11:20:59 -05:00
|
|
|
} catch (err) {
|
|
|
|
if (options.onError) {
|
|
|
|
options.onError(err);
|
|
|
|
}
|
|
|
|
}
|
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-02-15 11:20:59 -05:00
|
|
|
if (options.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
|
|
|
|
2019-05-30 08:59:30 -04:00
|
|
|
const filename = join(root, info.name!);
|
2019-05-14 17:14:08 -04:00
|
|
|
|
|
|
|
if (info.isFile()) {
|
2019-10-02 13:59:27 -04:00
|
|
|
if (options.includeFiles != false && include(filename, options)) {
|
2019-05-14 17:14:08 -04:00
|
|
|
yield { filename, info };
|
2019-02-15 11:20:59 -05:00
|
|
|
}
|
|
|
|
} else {
|
2019-10-02 13:59:27 -04:00
|
|
|
yield* walkSync(filename, { ...options, maxDepth: maxDepth - 1 });
|
2019-02-15 11:20:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|