1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-29 02:29:06 -05:00

Add includeDirs to WalkOptions (denoland/deno_std#601)

Original: de8d0ab4a1
This commit is contained in:
Nayeem Rahman 2019-09-18 16:37:37 +01:00 committed by Ryan Dahl
parent 878a9a29e7
commit fd44fe34d4
2 changed files with 27 additions and 3 deletions

View file

@ -1,13 +1,14 @@
// Documentation and interface for walk were adapted from Go // Documentation and interface for walk were adapted from Go
// https://golang.org/pkg/path/filepath/#Walk // https://golang.org/pkg/path/filepath/#Walk
// Copyright 2009 The Go Authors. All rights reserved. BSD license. // Copyright 2009 The Go Authors. All rights reserved. BSD license.
const { readDir, readDirSync } = Deno; const { readDir, readDirSync, stat, statSync } = Deno;
type FileInfo = Deno.FileInfo; type FileInfo = Deno.FileInfo;
import { unimplemented } from "../testing/asserts.ts"; import { unimplemented } from "../testing/asserts.ts";
import { join } from "./path/mod.ts"; import { join } from "./path/mod.ts";
export interface WalkOptions { export interface WalkOptions {
maxDepth?: number; maxDepth?: number;
includeDirs?: boolean;
exts?: string[]; exts?: string[];
match?: RegExp[]; match?: RegExp[];
skip?: RegExp[]; skip?: RegExp[];
@ -47,13 +48,14 @@ export interface WalkInfo {
info: FileInfo; info: FileInfo;
} }
/** Walks the file tree rooted at root, calling walkFn for each file or /** Walks the file tree rooted at root, yielding each file or directory in the
* directory in the tree, including root. The files are walked in lexical * tree filtered according to the given options. The files are walked in lexical
* order, which makes the output deterministic but means that for very large * order, which makes the output deterministic but means that for very large
* directories walk() can be inefficient. * directories walk() can be inefficient.
* *
* Options: * Options:
* - maxDepth?: number; * - maxDepth?: number;
* - includeDirs?: boolean;
* - exts?: string[]; * - exts?: string[];
* - match?: RegExp[]; * - match?: RegExp[];
* - skip?: RegExp[]; * - skip?: RegExp[];
@ -70,6 +72,10 @@ export async function* walk(
options: WalkOptions = {} options: WalkOptions = {}
): AsyncIterableIterator<WalkInfo> { ): AsyncIterableIterator<WalkInfo> {
options.maxDepth! -= 1; options.maxDepth! -= 1;
if (options.includeDirs && include(root, options)) {
const rootInfo = await stat(root);
yield { filename: root, info: rootInfo };
}
let ls: FileInfo[] = []; let ls: FileInfo[] = [];
try { try {
ls = await readDir(root); ls = await readDir(root);
@ -108,6 +114,10 @@ export function* walkSync(
options: WalkOptions = {} options: WalkOptions = {}
): IterableIterator<WalkInfo> { ): IterableIterator<WalkInfo> {
options.maxDepth! -= 1; options.maxDepth! -= 1;
if (options.includeDirs && include(root, options)) {
const rootInfo = statSync(root);
yield { filename: root, info: rootInfo };
}
let ls: FileInfo[] = []; let ls: FileInfo[] = [];
try { try {
ls = readDirSync(root); ls = readDirSync(root);

View file

@ -118,6 +118,20 @@ testWalk(
} }
); );
testWalk(
async (d: string): Promise<void> => {
await touch(d + "/a");
await mkdir(d + "/b");
await touch(d + "/b/c");
},
async function includeDirs(): Promise<void> {
assertReady(2);
const arr = await walkArray(".", { includeDirs: true });
assertEquals(arr.length, 4);
assertEquals(arr, [".", "a", "b", "b/c"]);
}
);
testWalk( testWalk(
async (d: string): Promise<void> => { async (d: string): Promise<void> => {
await touch(d + "/x.ts"); await touch(d + "/x.ts");