Skip to content

Commit 332747d

Browse files
committed
fmt
1 parent 7b885c0 commit 332747d

1 file changed

Lines changed: 45 additions & 2 deletions

File tree

packages/react-doctor/src/utils/discover-project.ts

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,65 @@ const FRAMEWORK_DISPLAY_NAMES: Record<Framework, string> = {
6969
export const formatFrameworkName = (framework: Framework): string =>
7070
FRAMEWORK_DISPLAY_NAMES[framework];
7171

72-
const countSourceFiles = (rootDirectory: string): number => {
72+
const IGNORED_DIRECTORIES = new Set([
73+
"node_modules",
74+
".git",
75+
".next",
76+
".turbo",
77+
"dist",
78+
"build",
79+
"coverage",
80+
".cache",
81+
".output",
82+
".nuxt",
83+
".expo",
84+
]);
85+
86+
const countSourceFilesViaFilesystem = (rootDirectory: string): number => {
87+
let count = 0;
88+
const stack = [rootDirectory];
89+
90+
while (stack.length > 0) {
91+
const currentDirectory = stack.pop()!;
92+
const entries = fs.readdirSync(currentDirectory, { withFileTypes: true });
93+
94+
for (const entry of entries) {
95+
if (entry.isDirectory()) {
96+
if (!entry.name.startsWith(".") || entry.name === ".git") {
97+
if (!IGNORED_DIRECTORIES.has(entry.name)) {
98+
stack.push(path.join(currentDirectory, entry.name));
99+
}
100+
}
101+
continue;
102+
}
103+
if (entry.isFile() && SOURCE_FILE_PATTERN.test(entry.name)) {
104+
count++;
105+
}
106+
}
107+
}
108+
109+
return count;
110+
};
111+
112+
const countSourceFilesViaGit = (rootDirectory: string): number | null => {
73113
const result = spawnSync("git", ["ls-files", "--cached", "--others", "--exclude-standard"], {
74114
cwd: rootDirectory,
75115
encoding: "utf-8",
76116
maxBuffer: GIT_LS_FILES_MAX_BUFFER_BYTES,
77117
});
78118

79119
if (result.error || result.status !== 0) {
80-
return 0;
120+
return null;
81121
}
82122

83123
return result.stdout
84124
.split("\n")
85125
.filter((filePath) => filePath.length > 0 && SOURCE_FILE_PATTERN.test(filePath)).length;
86126
};
87127

128+
const countSourceFiles = (rootDirectory: string): number =>
129+
countSourceFilesViaGit(rootDirectory) ?? countSourceFilesViaFilesystem(rootDirectory);
130+
88131
const collectAllDependencies = (packageJson: PackageJson): Record<string, string> => ({
89132
...packageJson.peerDependencies,
90133
...packageJson.dependencies,

0 commit comments

Comments
 (0)