forked from thi-ng/umbrella
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.ts
31 lines (28 loc) · 794 Bytes
/
io.ts
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
import { readdirSync, readFileSync, statSync } from "fs";
export const readJSON = (path: string) => JSON.parse(<any>readFileSync(path));
export const readText = (path: string) => readFileSync(path).toString();
/**
* Recursively reads given directory and returns file names matching
* given extension.
*
* @param dir
* @param ext
* @param maxDepth
* @param depth
*/
export function* files(
dir: string,
ext: string,
maxDepth = Infinity,
depth = 0
): IterableIterator<string> {
if (depth >= maxDepth) return;
for (let f of readdirSync(dir)) {
const curr = dir + "/" + f;
if (f.endsWith(ext)) {
yield curr;
} else if (statSync(curr).isDirectory()) {
yield* files(curr, ext, maxDepth, depth + 1);
}
}
}