Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

created branch for async fs work #505

Merged
merged 5 commits into from
Mar 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)));
});
}