Skip to content

Commit c5fe623

Browse files
committed
avoid reading large directories at all costs
1 parent b9bf9fa commit c5fe623

File tree

7 files changed

+67
-21
lines changed

7 files changed

+67
-21
lines changed

src/repositories/bladeComponents.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { inAppDirs } from "@src/support/fileWatcher";
12
import { runInLaravel, template } from "@src/support/php";
23
import { repository } from ".";
34

@@ -22,7 +23,10 @@ const load = () => {
2223

2324
export const getBladeComponents = repository<BladeComponents>(
2425
load,
25-
["{,**/}{view,views}/{*,**/*}", "app/View/Components/{,*,**/*}.php"],
26+
[
27+
inAppDirs("{,**/}{view,views}/{*,**/*}"),
28+
"app/View/Components/{,*,**/*}.php",
29+
],
2630
{
2731
components: {},
2832
prefixes: [],

src/repositories/controllers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { inAppDirs } from "@src/support/fileWatcher";
12
import * as fs from "fs";
23
import { repository } from ".";
34
import { projectPath } from "../support/project";
@@ -80,6 +81,6 @@ export const getControllers = repository<string[]>(
8081
resolve(load());
8182
});
8283
},
83-
"{,**/}{Controllers}{.php,/*.php,/**/*.php}",
84+
inAppDirs("{,**/}{Controllers}{.php,/*.php,/**/*.php}"),
8485
[],
8586
);

src/repositories/inertia.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { runInLaravel, template } from "@src/support/php";
22
import { projectPath, relativePath } from "@src/support/project";
3+
import { waitForValue } from "@src/support/util";
34
import * as fs from "fs";
45
import * as sysPath from "path";
56
import { repository } from ".";
@@ -88,20 +89,12 @@ export const getInertiaViews = repository<ViewItem>(
8889
);
8990
}),
9091
() =>
91-
new Promise((resolve) => {
92-
const checkForPagePaths = () => {
93-
if (inertiaPagePaths === null) {
94-
return setTimeout(checkForPagePaths, 100);
95-
}
96-
97-
if (inertiaPagePaths.length === 0) {
98-
resolve(null);
99-
} else {
100-
resolve(`{${inertiaPagePaths.join(",")}}/{*,**/*}`);
101-
}
102-
};
92+
waitForValue(() => inertiaPagePaths).then((pagePaths) => {
93+
if (pagePaths === null || pagePaths.length === 0) {
94+
return null;
95+
}
10396

104-
checkForPagePaths();
97+
return `{${pagePaths.join(",")}}/{*,**/*}`;
10598
}),
10699
{},
107100
["create", "delete"],

src/repositories/routes.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { inAppDirs } from "@src/support/fileWatcher";
12
import { repository } from ".";
23
import { runInLaravel, template } from "../support/php";
34

@@ -11,10 +12,12 @@ interface RouteItem {
1112
line: number | null;
1213
}
1314

15+
const routesPattern = "{[Rr]oute}{,s}{.php,/*.php,/**/*.php}";
16+
1417
export const getRoutes = repository<RouteItem[]>(
1518
() => {
1619
return runInLaravel<RouteItem[]>(template("routes"), "HTTP Routes");
1720
},
18-
"{,**/}{[Rr]oute}{,s}{.php,/*.php,/**/*.php}",
21+
[inAppDirs(`{,**/}${routesPattern}`), routesPattern],
1922
[],
2023
);

src/repositories/translations.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { runInLaravel, template } from "@src/support/php";
22
import { projectPath } from "@src/support/project";
3+
import { waitForValue } from "@src/support/util";
34
import { repository } from ".";
45

56
export interface TranslationItem {
@@ -28,8 +29,11 @@ interface TranslationGroupPhpResult {
2829
params: string[][];
2930
paths: string[];
3031
values: string[];
32+
to_watch: string[];
3133
}
3234

35+
let dirsToWatch: string[] | null = null;
36+
3337
const load = () => {
3438
return runInLaravel<TranslationGroupPhpResult>(
3539
template("translations"),
@@ -54,6 +58,8 @@ const load = () => {
5458
},
5559
);
5660

61+
dirsToWatch = res.to_watch;
62+
5763
return {
5864
default: res.default,
5965
translations: result,
@@ -63,7 +69,14 @@ const load = () => {
6369

6470
export const getTranslations = repository<TranslationGroupResult>(
6571
load,
66-
"{,**/}{lang,localization,localizations,trans,translation,translations}/{*,**/*}",
72+
() =>
73+
waitForValue(() => dirsToWatch).then((value) => {
74+
if (value === null || value.length === 0) {
75+
return null;
76+
}
77+
78+
return `{${value.join(",")}}/{*,**/*}`;
79+
}),
6780
{
6881
default: "",
6982
translations: {},

src/repositories/views.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { inAppDirs } from "@src/support/fileWatcher";
12
import { runInLaravel, template } from "@src/support/php";
23
import { repository } from ".";
34

@@ -27,7 +28,7 @@ const load = () => {
2728

2829
export const getViews = repository<ViewItem[]>(
2930
load,
30-
"{,**/}{view,views}/{*,**/*}",
31+
inAppDirs("{,**/}{view,views}/{*,**/*}"),
3132
[],
3233
["create", "delete"],
3334
);

src/support/fileWatcher.ts

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { readdirSync } from "fs";
12
import * as vscode from "vscode";
23
import { getWorkspaceFolders, hasWorkspace } from "./project";
34
import { leadingDebounce } from "./util";
@@ -36,11 +37,36 @@ export const loadAndWatch = (
3637
);
3738
}
3839
});
40+
} else {
41+
createFileWatcher(
42+
patterns,
43+
leadingDebounce(load, debounceTime),
44+
events,
45+
);
46+
}
47+
};
3948

40-
return;
49+
let appDirsRead = false;
50+
const appDirs: string[] = [];
51+
const ignoreDirs = ["node_modules", ".git", "vendor", "storage"];
52+
53+
export const inAppDirs = (pattern: string) => {
54+
if (!appDirsRead) {
55+
appDirsRead = true;
56+
readdirSync(getWorkspaceFolders()[0].uri.fsPath, {
57+
withFileTypes: true,
58+
}).forEach((file) => {
59+
if (file.isDirectory() && !ignoreDirs.includes(file.name)) {
60+
appDirs.push(file.name);
61+
}
62+
});
63+
}
64+
65+
if (appDirs.length === 0) {
66+
return pattern;
4167
}
4268

43-
createFileWatcher(patterns, leadingDebounce(load, debounceTime), events);
69+
return `{${appDirs.join(",")}}${pattern}`;
4470
};
4571

4672
const patternWatchers: Record<
@@ -74,8 +100,13 @@ export const createFileWatcher = (
74100
return patternWatchers[pattern].watcher;
75101
}
76102

103+
const relativePattern = new vscode.RelativePattern(
104+
getWorkspaceFolders()[0],
105+
pattern,
106+
);
107+
77108
const watcher = vscode.workspace.createFileSystemWatcher(
78-
new vscode.RelativePattern(getWorkspaceFolders()[0], pattern),
109+
relativePattern,
79110
!events.includes("create"),
80111
!events.includes("change"),
81112
!events.includes("delete"),

0 commit comments

Comments
 (0)