forked from JoshuaKGoldberg/TypeStat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollectFileNames.ts
More file actions
52 lines (45 loc) · 1.44 KB
/
collectFileNames.ts
File metadata and controls
52 lines (45 loc) · 1.44 KB
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
import { glob } from "node:fs/promises";
import path from "node:path";
export interface CollectFileNamesResult {
error?: string;
fileNames: readonly string[];
}
export const collectFileNames = async (
cwd: string,
include: readonly string[] | undefined,
): Promise<CollectFileNamesResult | undefined> => {
if (!include?.length) {
return undefined;
}
const fileNames = await collectFileNamesFromGlobs(cwd, include);
const implicitNodeModulesInclude = implicitNodeModulesIncluded(
include,
fileNames,
);
if (implicitNodeModulesInclude) {
return {
error: `At least one path including node_modules was included implicitly: '${implicitNodeModulesInclude}'. Either adjust TypeStat's included files to not include node_modules (recommended) or explicitly include node_modules/ (not recommended).`,
fileNames: [],
};
}
return { fileNames };
};
const collectFileNamesFromGlobs = async (
cwd: string,
include: readonly string[],
): Promise<readonly string[]> => {
const fileNames: string[] = [];
for await (const entry of glob(include, { cwd, withFileTypes: true })) {
fileNames.push(path.join(entry.parentPath, entry.name));
}
return fileNames;
};
const implicitNodeModulesIncluded = (
fileGlobs: readonly string[],
fileNames: readonly string[],
): string | undefined => {
if (fileGlobs.some((glob) => glob.includes("node_modules"))) {
return undefined;
}
return fileNames.find((fileName) => fileName.includes("node_modules"));
};