diff options
author | Yoshiya Hinosawa <stibium121@gmail.com> | 2019-01-18 05:09:44 +0900 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2019-01-17 15:09:44 -0500 |
commit | f19622e7681b7753788137706e535f72c3ebb38e (patch) | |
tree | 0cd5261230f895359ca479d2b2234a56f137e924 /tools/util.ts | |
parent | befc6b2e7650af09f81562cd306a6f5d3f46dc21 (diff) |
Rewrite tools/format.py in deno (#1528)
Note: findFiles and findFilesWalk are borrowed from the previous
attempt of @pseudo-su (#1434)
Diffstat (limited to 'tools/util.ts')
-rw-r--r-- | tools/util.ts | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/tools/util.ts b/tools/util.ts new file mode 100644 index 000000000..5ff7da344 --- /dev/null +++ b/tools/util.ts @@ -0,0 +1,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); +} |