summaryrefslogtreecommitdiff
path: root/tools/util.ts
blob: 5ff7da34456d664feb3b0e201f735b1a17a7ad4b (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
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { lstatSync, readDirSync } from "deno";

export interface FindOptions {
  skip?: string[];
  depth?: number;
}

/**
 * Finds files of the give extensions under the given paths recursively.
 * @param dirs directories
 * @param exts extensions
 * @param skip patterns to ignore
 * @param depth depth to find
 */
export function findFiles(
  dirs: string[],
  exts: string[],
  { skip = [], depth = 20 }: FindOptions = {}
) {
  return findFilesWalk(dirs, depth).filter(
    path =>
      exts.some(ext => path.endsWith(ext)) &&
      skip.every(pattern => !path.includes(pattern))
  );
}

function findFilesWalk(paths: string[], depth: number) {
  if (depth < 0) {
    return [];
  }

  const foundPaths = paths.map(path =>
    lstatSync(path).isDirectory()
      ? findFilesWalk(readDirSync(path).map(f => f.path), depth - 1)
      : path
  );

  return [].concat(...foundPaths);
}