summaryrefslogtreecommitdiff
path: root/tools/util.ts
blob: c055959fef7b464a22bfd58d2e2cd8487a362e75 (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
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { platform, lstatSync, readDirSync } from "deno";
import { join } from "../js/deps/https/deno.land/x/std/fs/path/mod.ts";

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);
}

export const executableSuffix = platform.os === "win" ? ".exe" : "";

/** Returns true if the path exists. */
export function existsSync(path: string): boolean {
  try {
    lstatSync(path);
  } catch (e) {
    return false;
  }
  return true;
}

/**
 * Looks up the available deno path with the priority
 * of release -> debug -> global
 */
export function lookupDenoPath(): string {
  const denoExe = "deno" + executableSuffix;
  const releaseExe = join("target", "release", denoExe);
  const debugExe = join("target", "debug", denoExe);

  if (existsSync(releaseExe)) {
    return releaseExe;
  } else if (existsSync(debugExe)) {
    return debugExe;
  }

  return denoExe;
}