Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add includeDirs to WalkOptions #601

Merged
merged 2 commits into from
Sep 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions fs/walk.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// 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;
const { readDir, readDirSync, stat, statSync } = Deno;
type FileInfo = Deno.FileInfo;
import { unimplemented } from "../testing/asserts.ts";
import { join } from "./path/mod.ts";

export interface WalkOptions {
maxDepth?: number;
includeDirs?: boolean;
exts?: string[];
match?: RegExp[];
skip?: RegExp[];
Expand Down Expand Up @@ -47,13 +48,14 @@ export interface WalkInfo {
info: FileInfo;
}

/** 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
/** 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
* order, which makes the output deterministic but means that for very large
* directories walk() can be inefficient.
*
* Options:
* - maxDepth?: number;
* - includeDirs?: boolean;
* - exts?: string[];
* - match?: RegExp[];
* - skip?: RegExp[];
Expand All @@ -70,6 +72,10 @@ export async function* walk(
options: WalkOptions = {}
): AsyncIterableIterator<WalkInfo> {
options.maxDepth! -= 1;
if (options.includeDirs && include(root, options)) {
const rootInfo = await stat(root);
yield { filename: root, info: rootInfo };
}
let ls: FileInfo[] = [];
try {
ls = await readDir(root);
Expand Down Expand Up @@ -108,6 +114,10 @@ export function* walkSync(
options: WalkOptions = {}
): IterableIterator<WalkInfo> {
options.maxDepth! -= 1;
if (options.includeDirs && include(root, options)) {
const rootInfo = statSync(root);
yield { filename: root, info: rootInfo };
}
let ls: FileInfo[] = [];
try {
ls = readDirSync(root);
Expand Down
14 changes: 14 additions & 0 deletions fs/walk_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
async (d: string): Promise<void> => {
await touch(d + "/x.ts");
Expand Down