1
1
// Documentation and interface for walk were adapted from Go
2
2
// https://golang.org/pkg/path/filepath/#Walk
3
3
// Copyright 2009 The Go Authors. All rights reserved. BSD license.
4
- const { readDir, readDirSync } = Deno ;
4
+ const { readDir, readDirSync, stat , statSync } = Deno ;
5
5
type FileInfo = Deno . FileInfo ;
6
6
import { unimplemented } from "../testing/asserts.ts" ;
7
7
import { join } from "./path/mod.ts" ;
8
8
9
9
export interface WalkOptions {
10
10
maxDepth ?: number ;
11
+ includeDirs ?: boolean ;
11
12
exts ?: string [ ] ;
12
13
match ?: RegExp [ ] ;
13
14
skip ?: RegExp [ ] ;
@@ -47,13 +48,14 @@ export interface WalkInfo {
47
48
info : FileInfo ;
48
49
}
49
50
50
- /** Walks the file tree rooted at root, calling walkFn for each file or
51
- * directory in the tree, including root . The files are walked in lexical
51
+ /** Walks the file tree rooted at root, yielding each file or directory in the
52
+ * tree filtered according to the given options . The files are walked in lexical
52
53
* order, which makes the output deterministic but means that for very large
53
54
* directories walk() can be inefficient.
54
55
*
55
56
* Options:
56
57
* - maxDepth?: number;
58
+ * - includeDirs?: boolean;
57
59
* - exts?: string[];
58
60
* - match?: RegExp[];
59
61
* - skip?: RegExp[];
@@ -70,6 +72,10 @@ export async function* walk(
70
72
options : WalkOptions = { }
71
73
) : AsyncIterableIterator < WalkInfo > {
72
74
options . maxDepth ! -= 1 ;
75
+ if ( options . includeDirs && include ( root , options ) ) {
76
+ const rootInfo = await stat ( root ) ;
77
+ yield { filename : root , info : rootInfo } ;
78
+ }
73
79
let ls : FileInfo [ ] = [ ] ;
74
80
try {
75
81
ls = await readDir ( root ) ;
@@ -108,6 +114,10 @@ export function* walkSync(
108
114
options : WalkOptions = { }
109
115
) : IterableIterator < WalkInfo > {
110
116
options . maxDepth ! -= 1 ;
117
+ if ( options . includeDirs && include ( root , options ) ) {
118
+ const rootInfo = statSync ( root ) ;
119
+ yield { filename : root , info : rootInfo } ;
120
+ }
111
121
let ls : FileInfo [ ] = [ ] ;
112
122
try {
113
123
ls = readDirSync ( root ) ;
0 commit comments