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.
|
|
|
|
const { readDir, readDirSync } = Deno;
|
2019-03-07 19:25:16 -05:00
|
|
|
type FileInfo = Deno.FileInfo;
|
2019-05-14 17:14:08 -04:00
|
|
|
import { unimplemented } from "../testing/asserts.ts";
|
|
|
|
import { join } from "./path/mod.ts";
|
2019-02-15 11:20:59 -05:00
|
|
|
|
|
|
|
export interface WalkOptions {
|
|
|
|
maxDepth?: number;
|
|
|
|
exts?: string[];
|
|
|
|
match?: RegExp[];
|
|
|
|
skip?: RegExp[];
|
2019-03-12 01:51:51 -04:00
|
|
|
onError?: (err: Error) => void;
|
|
|
|
followSymlinks?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-04-24 07:41:23 -04:00
|
|
|
return patterns.some(
|
|
|
|
(pattern): boolean => {
|
|
|
|
let 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-05-14 17:14:08 -04:00
|
|
|
/** Walks the file tree rooted at root, calling walkFn for each file or
|
|
|
|
* directory in the tree, including root. 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;
|
|
|
|
* - exts?: string[];
|
|
|
|
* - match?: RegExp[];
|
|
|
|
* - skip?: RegExp[];
|
|
|
|
* - onError?: (err: Error) => void;
|
|
|
|
* - followSymlinks?: boolean;
|
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-05-30 08:59:30 -04:00
|
|
|
options.maxDepth! -= 1;
|
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-05-14 17:14:08 -04:00
|
|
|
for (let info of ls) {
|
|
|
|
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()) {
|
|
|
|
if (include(filename, options)) {
|
|
|
|
yield { filename, info };
|
2019-02-15 11:20:59 -05:00
|
|
|
}
|
|
|
|
} else {
|
2019-05-30 08:59:30 -04:00
|
|
|
if (!(options.maxDepth! < 0)) {
|
2019-05-14 17:14:08 -04:00
|
|
|
yield* walk(filename, options);
|
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-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
|
|
|
): IterableIterator<WalkInfo> {
|
2019-05-30 08:59:30 -04:00
|
|
|
options.maxDepth! -= 1;
|
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-05-14 17:14:08 -04:00
|
|
|
for (let info of ls) {
|
|
|
|
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()) {
|
|
|
|
if (include(filename, options)) {
|
|
|
|
yield { filename, info };
|
2019-02-15 11:20:59 -05:00
|
|
|
}
|
|
|
|
} else {
|
2019-05-30 08:59:30 -04:00
|
|
|
if (!(options.maxDepth! < 0)) {
|
2019-05-14 17:14:08 -04:00
|
|
|
yield* walkSync(filename, options);
|
2019-02-15 11:20:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|