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-06-07 09:20:33 -04:00
|
|
|
import { assert } from "../_util/assert.ts";
|
2020-04-29 16:00:31 -04:00
|
|
|
import { basename, join, normalize } from "../path/mod.ts";
|
2019-02-15 11:20:59 -05:00
|
|
|
|
2020-04-29 16:00:31 -04:00
|
|
|
export function createWalkEntrySync(path: string): WalkEntry {
|
|
|
|
path = normalize(path);
|
|
|
|
const name = basename(path);
|
2020-06-12 15:23:38 -04:00
|
|
|
const info = Deno.statSync(path);
|
2020-04-29 16:00:31 -04:00
|
|
|
return {
|
|
|
|
path,
|
|
|
|
name,
|
|
|
|
isFile: info.isFile,
|
|
|
|
isDirectory: info.isDirectory,
|
|
|
|
isSymlink: info.isSymlink,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function createWalkEntry(path: string): Promise<WalkEntry> {
|
|
|
|
path = normalize(path);
|
|
|
|
const name = basename(path);
|
2020-06-12 15:23:38 -04:00
|
|
|
const info = await Deno.stat(path);
|
2020-04-29 16:00:31 -04:00
|
|
|
return {
|
|
|
|
path,
|
|
|
|
name,
|
|
|
|
isFile: info.isFile,
|
|
|
|
isDirectory: info.isDirectory,
|
|
|
|
isSymlink: info.isSymlink,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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(
|
2020-04-29 16:00:31 -04:00
|
|
|
path: string,
|
2019-11-14 22:22:33 -05:00
|
|
|
exts?: string[],
|
|
|
|
match?: RegExp[],
|
|
|
|
skip?: RegExp[]
|
|
|
|
): boolean {
|
2020-04-29 16:00:31 -04:00
|
|
|
if (exts && !exts.some((ext): boolean => path.endsWith(ext))) {
|
2019-03-12 01:51:51 -04:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-29 16:00:31 -04:00
|
|
|
if (match && !match.some((pattern): boolean => !!path.match(pattern))) {
|
2019-03-12 01:51:51 -04:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-29 16:00:31 -04:00
|
|
|
if (skip && skip.some((pattern): boolean => !!path.match(pattern))) {
|
2019-03-12 01:51:51 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-29 16:00:31 -04:00
|
|
|
export interface WalkEntry extends Deno.DirEntry {
|
|
|
|
path: string;
|
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
|
|
|
*
|
2020-05-01 12:37:32 -04:00
|
|
|
* for await (const entry of walk(".")) {
|
|
|
|
* console.log(entry.path);
|
|
|
|
* assert(entry.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,
|
2020-03-28 13:03:49 -04:00
|
|
|
skip = undefined,
|
2019-11-14 22:22:33 -05:00
|
|
|
}: WalkOptions = {}
|
2020-04-16 01:40:30 -04:00
|
|
|
): AsyncIterableIterator<WalkEntry> {
|
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)) {
|
2020-04-29 16:00:31 -04:00
|
|
|
yield await createWalkEntry(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-06-12 15:23:38 -04:00
|
|
|
for await (const entry of Deno.readDir(root)) {
|
2020-04-29 16:00:31 -04:00
|
|
|
if (entry.isSymlink) {
|
2019-11-14 22:22:33 -05:00
|
|
|
if (followSymlinks) {
|
2019-05-14 17:14:08 -04:00
|
|
|
// TODO(ry) Re-enable followSymlinks.
|
2020-06-07 09:20:33 -04:00
|
|
|
throw new Error("unimplemented");
|
2019-02-15 11:20:59 -05:00
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2019-05-14 17:14:08 -04:00
|
|
|
|
2020-04-29 16:00:31 -04:00
|
|
|
assert(entry.name != null);
|
|
|
|
const path = join(root, entry.name);
|
2019-05-14 17:14:08 -04:00
|
|
|
|
2020-04-29 16:00:31 -04:00
|
|
|
if (entry.isFile) {
|
|
|
|
if (includeFiles && include(path, exts, match, skip)) {
|
|
|
|
yield { path, ...entry };
|
2019-02-15 11:20:59 -05:00
|
|
|
}
|
|
|
|
} else {
|
2020-04-29 16:00:31 -04:00
|
|
|
yield* walk(path, {
|
2019-11-14 22:22:33 -05:00
|
|
|
maxDepth: maxDepth - 1,
|
|
|
|
includeFiles,
|
|
|
|
includeDirs,
|
|
|
|
followSymlinks,
|
|
|
|
exts,
|
|
|
|
match,
|
2020-03-28 13:03:49 -04:00
|
|
|
skip,
|
2019-11-14 22:22:33 -05:00
|
|
|
});
|
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,
|
2020-03-28 13:03:49 -04:00
|
|
|
skip = undefined,
|
2019-11-14 22:22:33 -05:00
|
|
|
}: WalkOptions = {}
|
2020-04-16 01:40:30 -04:00
|
|
|
): IterableIterator<WalkEntry> {
|
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)) {
|
2020-04-29 16:00:31 -04:00
|
|
|
yield createWalkEntrySync(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-06-12 15:23:38 -04:00
|
|
|
for (const entry of Deno.readDirSync(root)) {
|
2020-04-29 16:00:31 -04:00
|
|
|
if (entry.isSymlink) {
|
2019-11-14 22:22:33 -05:00
|
|
|
if (followSymlinks) {
|
2020-06-07 09:20:33 -04:00
|
|
|
throw new Error("unimplemented");
|
2019-02-15 11:20:59 -05:00
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2019-05-14 17:14:08 -04:00
|
|
|
|
2020-04-29 16:00:31 -04:00
|
|
|
assert(entry.name != null);
|
|
|
|
const path = join(root, entry.name);
|
2019-05-14 17:14:08 -04:00
|
|
|
|
2020-04-29 16:00:31 -04:00
|
|
|
if (entry.isFile) {
|
|
|
|
if (includeFiles && include(path, exts, match, skip)) {
|
|
|
|
yield { path, ...entry };
|
2019-02-15 11:20:59 -05:00
|
|
|
}
|
|
|
|
} else {
|
2020-04-29 16:00:31 -04:00
|
|
|
yield* walkSync(path, {
|
2019-11-14 22:22:33 -05:00
|
|
|
maxDepth: maxDepth - 1,
|
|
|
|
includeFiles,
|
|
|
|
includeDirs,
|
|
|
|
followSymlinks,
|
|
|
|
exts,
|
|
|
|
match,
|
2020-03-28 13:03:49 -04:00
|
|
|
skip,
|
2019-11-14 22:22:33 -05:00
|
|
|
});
|
2019-02-15 11:20:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|