Skip to content

Commit 26e4b80

Browse files
committed
Do initial scan synchronously
1 parent c6ca6d6 commit 26e4b80

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

src/utils/file-watcher.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,16 @@ export class CssWatcher {
2020
this.patterns = patterns;
2121
// Ignore hidden files by default
2222
this.ignorePatterns = ['**/.*', ...ignore];
23-
this.setupWatcher();
24-
}
2523

26-
private setupWatcher() {
27-
// Set up chokidar with appropriate options
2824
const cwd = process.cwd();
25+
this.setupWatcher(cwd);
26+
this.initialScan(cwd);
27+
}
28+
29+
private setupWatcher(cwd: string) {
2930
this.watcher = chokidar.watch(cwd, {
3031
persistent: true,
31-
ignoreInitial: false, // This ensures we get the initial scan
32+
ignoreInitial: true, // We need to do our initial scan synchronously
3233
ignored: (path, stats) => {
3334
return (
3435
micromatch.isMatch(path, this.ignorePatterns) ||
@@ -59,9 +60,9 @@ export class CssWatcher {
5960
});
6061
}
6162

62-
private async updateClassesForFile(filePath: string) {
63+
private updateClassesForFile(filePath: string) {
6364
try {
64-
const content = await fs.promises.readFile(filePath, 'utf8');
65+
const content = fs.readFileSync(filePath, 'utf8');
6566
const fileClasses = extractClassesFromCss(content);
6667
this.state.fileClasses.set(filePath, new Set(fileClasses));
6768
this.state.lastUpdate = Date.now();
@@ -71,6 +72,26 @@ export class CssWatcher {
7172
}
7273
}
7374

75+
private initialScan(cwd: string) {
76+
const dirs = [cwd];
77+
for (const dir of dirs) {
78+
const entries = fs.readdirSync(dir);
79+
for (const entry of entries) {
80+
const fullPath = `${dir}/${entry}`;
81+
if (micromatch.isMatch(fullPath, this.ignorePatterns)) {
82+
continue;
83+
}
84+
85+
const stats = fs.statSync(fullPath);
86+
if (stats.isDirectory()) {
87+
dirs.push(fullPath);
88+
} else if (stats.isFile() && !micromatch.isMatch(fullPath, this.ignorePatterns)) {
89+
this.updateClassesForFile(fullPath);
90+
}
91+
}
92+
}
93+
}
94+
7495
hasClass(className: string): boolean {
7596
for (const classes of this.state.fileClasses.values()) {
7697
if (classes.has(className)) {

0 commit comments

Comments
 (0)