Skip to content

Commit 516dc22

Browse files
authored
fix: Removed sync fs calls from model (#505)
1 parent 0b686e2 commit 516dc22

File tree

3 files changed

+68
-7
lines changed

3 files changed

+68
-7
lines changed

.vscode/launch.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
"type": "extensionHost",
88
"request": "launch",
99
"runtimeExecutable": "${execPath}",
10-
"args": ["--extensionDevelopmentPath=${workspaceRoot}"],
10+
"args": [
11+
"--disable-extensions",
12+
"--extensionDevelopmentPath=${workspaceRoot}"
13+
],
1114
"stopOnEntry": false,
1215
"sourceMaps": true,
1316
"outFiles": ["${workspaceRoot}/out/**/*.js"],
14-
"preLaunchTask": "build"
1517
},
1618
{
1719
"name": "Launch Tests",

src/model.ts

+33-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as fs from "fs";
1+
import { Stats } from "fs";
22
import * as path from "path";
33
import {
44
commands,
@@ -31,6 +31,7 @@ import {
3131
isDescendant,
3232
normalizePath
3333
} from "./util";
34+
import { exists, readDir, stat } from "./util/async_fs";
3435
import { matchAll } from "./util/globMatch";
3536

3637
export class Model implements IDisposable {
@@ -269,15 +270,27 @@ export class Model implements IDisposable {
269270
return;
270271
}
271272

272-
let isSvnFolder = fs.existsSync(path + "/.svn");
273+
let isSvnFolder = false;
274+
275+
try {
276+
isSvnFolder = await exists(path + "/.svn");
277+
} catch (error) {
278+
// error
279+
}
273280

274281
// If open only a subpath.
275282
if (!isSvnFolder && level === 0) {
276283
const pathParts = path.split(/[\\/]/);
277284
while (pathParts.length > 0) {
278285
pathParts.pop();
279286
const topPath = pathParts.join("/") + "/.svn";
280-
isSvnFolder = fs.existsSync(topPath);
287+
288+
try {
289+
isSvnFolder = await exists(topPath);
290+
} catch (error) {
291+
// error
292+
}
293+
281294
if (isSvnFolder) {
282295
break;
283296
}
@@ -320,11 +333,26 @@ export class Model implements IDisposable {
320333

321334
const newLevel = level + 1;
322335
if (newLevel <= this.maxDepth) {
323-
for (const file of fs.readdirSync(path)) {
336+
let files: string[] | Buffer[] = [];
337+
338+
try {
339+
files = await readDir(path);
340+
} catch (error) {
341+
return;
342+
}
343+
344+
for (const file of files) {
324345
const dir = path + "/" + file;
346+
let stats: Stats;
347+
348+
try {
349+
stats = await stat(dir);
350+
} catch (error) {
351+
continue;
352+
}
325353

326354
if (
327-
fs.statSync(dir).isDirectory() &&
355+
stats.isDirectory() &&
328356
!matchAll(dir, this.ignoreList, { dot: true })
329357
) {
330358
await this.tryOpenRepository(dir, newLevel);

src/util/async_fs.ts

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import * as fs from "fs";
2+
3+
export function readDir(path: string): Promise<string[]> {
4+
return new Promise((resolve, reject) => {
5+
fs.readdir(path, (err, files) => {
6+
if (err) {
7+
reject(err);
8+
}
9+
10+
resolve(files);
11+
});
12+
});
13+
}
14+
15+
export function stat(path: string): Promise<fs.Stats> {
16+
return new Promise((resolve, reject) => {
17+
fs.stat(path, (err, stats) => {
18+
if (err) {
19+
reject(err);
20+
}
21+
22+
resolve(stats);
23+
});
24+
});
25+
}
26+
27+
export function exists(path: string): Promise<boolean> {
28+
return new Promise((resolve, reject) => {
29+
fs.access(path, err => (err ? reject(err) : resolve(true)));
30+
});
31+
}

0 commit comments

Comments
 (0)