Skip to content

Commit

Permalink
fix: Removed sync fs calls from model (#505)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnstonCode committed Mar 16, 2019
1 parent 0b686e2 commit 516dc22
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 7 deletions.
6 changes: 4 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": ["--extensionDevelopmentPath=${workspaceRoot}"],
"args": [
"--disable-extensions",
"--extensionDevelopmentPath=${workspaceRoot}"
],
"stopOnEntry": false,
"sourceMaps": true,
"outFiles": ["${workspaceRoot}/out/**/*.js"],
"preLaunchTask": "build"
},
{
"name": "Launch Tests",
Expand Down
38 changes: 33 additions & 5 deletions src/model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as fs from "fs";
import { Stats } from "fs";
import * as path from "path";
import {
commands,
Expand Down Expand Up @@ -31,6 +31,7 @@ import {
isDescendant,
normalizePath
} from "./util";
import { exists, readDir, stat } from "./util/async_fs";
import { matchAll } from "./util/globMatch";

export class Model implements IDisposable {
Expand Down Expand Up @@ -269,15 +270,27 @@ export class Model implements IDisposable {
return;
}

let isSvnFolder = fs.existsSync(path + "/.svn");
let isSvnFolder = false;

try {
isSvnFolder = await exists(path + "/.svn");
} catch (error) {
// error
}

// If open only a subpath.
if (!isSvnFolder && level === 0) {
const pathParts = path.split(/[\\/]/);
while (pathParts.length > 0) {
pathParts.pop();
const topPath = pathParts.join("/") + "/.svn";
isSvnFolder = fs.existsSync(topPath);

try {
isSvnFolder = await exists(topPath);
} catch (error) {
// error
}

if (isSvnFolder) {
break;
}
Expand Down Expand Up @@ -320,11 +333,26 @@ export class Model implements IDisposable {

const newLevel = level + 1;
if (newLevel <= this.maxDepth) {
for (const file of fs.readdirSync(path)) {
let files: string[] | Buffer[] = [];

try {
files = await readDir(path);
} catch (error) {
return;
}

for (const file of files) {
const dir = path + "/" + file;
let stats: Stats;

try {
stats = await stat(dir);
} catch (error) {
continue;
}

if (
fs.statSync(dir).isDirectory() &&
stats.isDirectory() &&
!matchAll(dir, this.ignoreList, { dot: true })
) {
await this.tryOpenRepository(dir, newLevel);
Expand Down
31 changes: 31 additions & 0 deletions src/util/async_fs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as fs from "fs";

export function readDir(path: string): Promise<string[]> {
return new Promise((resolve, reject) => {
fs.readdir(path, (err, files) => {
if (err) {
reject(err);
}

resolve(files);
});
});
}

export function stat(path: string): Promise<fs.Stats> {
return new Promise((resolve, reject) => {
fs.stat(path, (err, stats) => {
if (err) {
reject(err);
}

resolve(stats);
});
});
}

export function exists(path: string): Promise<boolean> {
return new Promise((resolve, reject) => {
fs.access(path, err => (err ? reject(err) : resolve(true)));
});
}

0 comments on commit 516dc22

Please sign in to comment.