summaryrefslogtreecommitdiff
path: root/std/fs/walk.ts
blob: 1655b63ab2284a90d4a2fe43750c12e9c474bc5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// 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.
import { unimplemented } from "../testing/asserts.ts";
import { join } from "../path/mod.ts";
const { readDir, readDirSync, stat, statSync } = Deno;
type FileInfo = Deno.FileInfo;

export interface WalkOptions {
  maxDepth?: number;
  includeFiles?: boolean;
  includeDirs?: boolean;
  followSymlinks?: boolean;
  exts?: string[];
  match?: RegExp[];
  skip?: RegExp[];
}

function include(
  filename: string,
  exts?: string[],
  match?: RegExp[],
  skip?: RegExp[]
): boolean {
  if (exts && !exts.some((ext): boolean => filename.endsWith(ext))) {
    return false;
  }
  if (match && !match.some((pattern): boolean => !!filename.match(pattern))) {
    return false;
  }
  if (skip && skip.some((pattern): boolean => !!filename.match(pattern))) {
    return false;
  }
  return true;
}

export interface WalkInfo {
  filename: string;
  info: FileInfo;
}

/** 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 = Infinity;
 * - includeFiles?: boolean = true;
 * - includeDirs?: boolean = true;
 * - followSymlinks?: boolean = false;
 * - exts?: string[];
 * - match?: RegExp[];
 * - skip?: RegExp[];
 *
 *      for await (const { filename, info } of walk(".")) {
 *        console.log(filename);
 *        assert(info.isFile());
 *      };
 */
export async function* walk(
  root: string,
  {
    maxDepth = Infinity,
    includeFiles = true,
    includeDirs = true,
    followSymlinks = false,
    exts = null,
    match = null,
    skip = null
  }: WalkOptions = {}
): AsyncIterableIterator<WalkInfo> {
  if (maxDepth < 0) {
    return;
  }
  if (includeDirs && include(root, exts, match, skip)) {
    yield { filename: root, info: await stat(root) };
  }
  if (maxDepth < 1 || !include(root, null, null, skip)) {
    return;
  }
  const ls: FileInfo[] = await readDir(root);
  for (const info of ls) {
    if (info.isSymlink()) {
      if (followSymlinks) {
        // TODO(ry) Re-enable followSymlinks.
        unimplemented();
      } else {
        continue;
      }
    }

    const filename = join(root, info.name!);

    if (info.isFile()) {
      if (includeFiles && include(filename, exts, match, skip)) {
        yield { filename, info };
      }
    } else {
      yield* walk(filename, {
        maxDepth: maxDepth - 1,
        includeFiles,
        includeDirs,
        followSymlinks,
        exts,
        match,
        skip
      });
    }
  }
}

/** Same as walk() but uses synchronous ops */
export function* walkSync(
  root: string,
  {
    maxDepth = Infinity,
    includeFiles = true,
    includeDirs = true,
    followSymlinks = false,
    exts = null,
    match = null,
    skip = null
  }: WalkOptions = {}
): IterableIterator<WalkInfo> {
  if (maxDepth < 0) {
    return;
  }
  if (includeDirs && include(root, exts, match, skip)) {
    yield { filename: root, info: statSync(root) };
  }
  if (maxDepth < 1 || !include(root, null, null, skip)) {
    return;
  }
  const ls: FileInfo[] = readDirSync(root);
  for (const info of ls) {
    if (info.isSymlink()) {
      if (followSymlinks) {
        unimplemented();
      } else {
        continue;
      }
    }

    const filename = join(root, info.name!);

    if (info.isFile()) {
      if (includeFiles && include(filename, exts, match, skip)) {
        yield { filename, info };
      }
    } else {
      yield* walkSync(filename, {
        maxDepth: maxDepth - 1,
        includeFiles,
        includeDirs,
        followSymlinks,
        exts,
        match,
        skip
      });
    }
  }
}