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-11-19 06:53:43 -05:00
|
|
|
/** Create WalkEntry for the `path` synchronously */
|
2020-09-23 14:09:09 -04:00
|
|
|
export function _createWalkEntrySync(path: string): WalkEntry {
|
2020-04-29 16:00:31 -04:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-11-19 06:53:43 -05:00
|
|
|
/** Create WalkEntry for the `path` asynchronously */
|
2020-09-23 14:09:09 -04:00
|
|
|
export async function _createWalkEntry(path: string): Promise<WalkEntry> {
|
2020-04-29 16:00:31 -04:00
|
|
|
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[],
|
2020-07-14 15:24:17 -04:00
|
|
|
skip?: RegExp[],
|
2019-11-14 22:22:33 -05:00
|
|
|
): 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-08-23 05:34:12 -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,
|
2020-07-14 15:24:17 -04: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-09-23 14:09:09 -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-11-30 09:34:36 -05:00
|
|
|
assert(entry.name != null);
|
|
|
|
let path = join(root, entry.name);
|
|
|
|
|
2020-04-29 16:00:31 -04:00
|
|
|
if (entry.isSymlink) {
|
2019-11-14 22:22:33 -05:00
|
|
|
if (followSymlinks) {
|
2020-11-30 09:34:36 -05:00
|
|
|
path = await Deno.realPath(path);
|
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
|
|
|
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,
|
2020-07-14 15:24:17 -04: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-09-23 14:09:09 -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-11-30 09:34:36 -05:00
|
|
|
assert(entry.name != null);
|
|
|
|
let path = join(root, entry.name);
|
|
|
|
|
2020-04-29 16:00:31 -04:00
|
|
|
if (entry.isSymlink) {
|
2019-11-14 22:22:33 -05:00
|
|
|
if (followSymlinks) {
|
2020-11-30 09:34:36 -05:00
|
|
|
path = Deno.realPathSync(path);
|
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
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|