1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-01 09:24:20 -04:00
denoland-deno/tools/util.ts
Yoshiya Hinosawa f19622e768 Rewrite tools/format.py in deno (#1528)
Note: findFiles and findFilesWalk are borrowed from the previous
attempt of @pseudo-su (#1434)
2019-01-17 15:09:44 -05:00

40 lines
962 B
TypeScript

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