forked from AminoffZ/github-repo-size
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoints.ts
46 lines (42 loc) · 1.18 KB
/
entrypoints.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { readdir } from 'fs/promises';
import { extname, join } from 'path';
const sourceDir = './src/scripts';
/**
* Recursively get all .ts and .js entrypoints from the directory
*
* @param dir Directory path to scan
* @returns {Promise<string[]>} The entrypoints
*/
async function getFiles(options?: {
root?: string;
deep?: boolean;
}): Promise<string[]> {
const dir = options?.root ?? sourceDir;
const deep = options?.deep ?? false;
const dirents = await readdir(dir, { withFileTypes: true });
const files = await Promise.all(
dirents.map((dirent) => {
const res = join(dir, dirent.name);
if (dirent.isDirectory() && deep) {
return getFiles({ ...options, root: res });
} else {
return Promise.resolve(res);
}
})
);
// Flatten the array and filter only .ts and .js files
return Array.prototype
.concat(...files)
.filter((file) => ['.ts', '.js'].includes(extname(file)));
}
/**
* Get all entrypoints from the src directory
*
* @returns {Promise<string[]>} The entrypoints
*/
export default async function entryPoints(options?: {
root?: string;
deep?: boolean;
}): Promise<string[]> {
return await getFiles(options);
}