Skip to content

Commit b26602f

Browse files
authored
perf: All fs is done async (#540)
1 parent 778dcb5 commit b26602f

21 files changed

+133
-78
lines changed

src/commands/command.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as fs from "original-fs";
21
import * as path from "path";
32
import {
43
commands,
@@ -16,6 +15,7 @@ import {
1615
WorkspaceEdit
1716
} from "vscode";
1817
import { ICommandOptions, Status, SvnUriAction } from "../common/types";
18+
import { exists, readFile, stat, unlink } from "../fs";
1919
import { inputIgnoreList } from "../ignoreitems";
2020
import { applyLineChanges } from "../lineChanges";
2121
import { Model } from "../model";
@@ -194,7 +194,7 @@ export abstract class Command implements Disposable {
194194
preserveFocus?: boolean,
195195
preserveSelection?: boolean
196196
): Promise<void> {
197-
let left = this.getLeftResource(resource, against);
197+
let left = await this.getLeftResource(resource, against);
198198
let right = this.getRightResource(resource, against);
199199
const title = this.getTitle(resource, against);
200200

@@ -209,8 +209,8 @@ export abstract class Command implements Disposable {
209209
}
210210

211211
if (
212-
fs.existsSync(right.fsPath) &&
213-
fs.statSync(right.fsPath).isDirectory()
212+
(await exists(right.fsPath)) &&
213+
(await stat(right.fsPath)).isDirectory()
214214
) {
215215
return;
216216
}
@@ -244,10 +244,10 @@ export abstract class Command implements Disposable {
244244
);
245245
}
246246

247-
protected getLeftResource(
247+
protected async getLeftResource(
248248
resource: Resource,
249249
against: string = ""
250-
): Uri | undefined {
250+
): Promise<Uri | undefined> {
251251
if (resource.remote) {
252252
if (resource.type !== Status.DELETED) {
253253
return toSvnUri(resource.resourceUri, SvnUriAction.SHOW, {
@@ -266,11 +266,11 @@ export abstract class Command implements Disposable {
266266
// Show file if has conflicts marks
267267
if (
268268
resource.type === Status.CONFLICTED &&
269-
fs.existsSync(resource.resourceUri.fsPath)
269+
(await exists(resource.resourceUri.fsPath))
270270
) {
271-
const text = fs.readFileSync(resource.resourceUri.fsPath, {
271+
const text = (await readFile(resource.resourceUri.fsPath, {
272272
encoding: "utf8"
273-
});
273+
})) as string;
274274

275275
// Check for lines begin with "<<<<<<", "=======", ">>>>>>>"
276276
if (/^<{7}[^]+^={7}[^]+^>{7}/m.test(text)) {
@@ -394,8 +394,12 @@ export abstract class Command implements Disposable {
394394
try {
395395
const tempFile = path.join(repository.root, ".svn", "tmp", "svn.patch");
396396

397-
if (fs.existsSync(tempFile)) {
398-
fs.unlinkSync(tempFile);
397+
if (await exists(tempFile)) {
398+
try {
399+
await unlink(tempFile);
400+
} catch (err) {
401+
// TODO(cjohnston)//log error
402+
}
399403
}
400404

401405
const uri = Uri.file(tempFile).with({

src/commands/deleteUnversioned.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as fs from "original-fs";
21
import { SourceControlResourceState, window } from "vscode";
2+
import { exists, lstat, unlink } from "../fs";
33
import { deleteDirectory } from "../util";
44
import { Command } from "./command";
55

@@ -24,16 +24,20 @@ export class DeleteUnversioned extends Command {
2424
for (const uri of uris) {
2525
const fsPath = uri.fsPath;
2626

27-
if (!fs.existsSync(fsPath)) {
28-
continue;
29-
}
27+
try {
28+
if (!(await exists(fsPath))) {
29+
continue;
30+
}
3031

31-
const stat = fs.lstatSync(fsPath);
32+
const stat = await lstat(fsPath);
3233

33-
if (stat.isDirectory()) {
34-
deleteDirectory(fsPath);
35-
} else {
36-
fs.unlinkSync(fsPath);
34+
if (stat.isDirectory()) {
35+
deleteDirectory(fsPath);
36+
} else {
37+
await unlink(fsPath);
38+
}
39+
} catch (err) {
40+
// TODO(cjohnston) Show meaningful error to user
3741
}
3842
}
3943
}

src/commands/openFile.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as fs from "original-fs";
21
import {
32
SourceControlResourceState,
43
TextDocumentShowOptions,
@@ -7,6 +6,7 @@ import {
76
window,
87
workspace
98
} from "vscode";
9+
import { exists, stat } from "../fs";
1010
import { Resource } from "../resource";
1111
import IncomingChangeNode from "../treeView/nodes/incomingChangeNode";
1212
import { fromSvnUri } from "../uri";
@@ -65,7 +65,10 @@ export class OpenFile extends Command {
6565
const preview = uris.length === 1 ? true : false;
6666
const activeTextEditor = window.activeTextEditor;
6767
for (const uri of uris) {
68-
if (fs.existsSync(uri.fsPath) && fs.statSync(uri.fsPath).isDirectory()) {
68+
if (
69+
(await exists(uri.fsPath)) &&
70+
(await stat(uri.fsPath)).isDirectory()
71+
) {
6972
continue;
7073
}
7174

src/commands/openHeadFile.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class OpenHeadFile extends Command {
2626
return;
2727
}
2828

29-
const HEAD = this.getLeftResource(resource, "HEAD");
29+
const HEAD = await this.getLeftResource(resource, "HEAD");
3030

3131
const basename = path.basename(resource.resourceUri.fsPath);
3232
if (!HEAD) {

src/fs/exists.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ import { access } from "original-fs";
22

33
export function exists(path: string): Promise<boolean> {
44
return new Promise((resolve, reject) => {
5-
access(path, err => (err ? reject(err) : resolve(true)));
5+
access(path, err => (err ? resolve(false) : resolve(true)));
66
});
77
}

src/fs/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export { exists } from "./exists";
2+
export { lstat } from "./lstat";
3+
export { mkdir } from "./mkdir";
4+
export { readFile } from "./read_file";
5+
export { readdir } from "./readdir";
6+
export { rmdir } from "./rmdir";
7+
export { stat } from "./stat";
8+
export { unlink } from "./unlink";
9+
export { writeFile } from "./write_file";

src/fs/lstat.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { lstat as fsLstat } from "original-fs";
2+
import { promisify } from "util";
3+
4+
export const lstat = promisify(fsLstat);

src/fs/mkdir.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { mkdir as fsMkdir } from "original-fs";
2+
import { promisify } from "util";
3+
4+
export const mkdir = promisify(fsMkdir);

src/fs/read_file.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { readFile as fsReadFile } from "original-fs";
2+
import { promisify } from "util";
3+
4+
export const readFile = promisify(fsReadFile);

src/fs/readdir.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
11
import { readdir as fsReaddir } from "original-fs";
2+
import { promisify } from "util";
23

3-
export function readdir(path: string): Promise<string[]> {
4-
return new Promise((resolve, reject) => {
5-
fsReaddir(path, (err, files) => {
6-
if (err) {
7-
reject(err);
8-
}
9-
10-
resolve(files);
11-
});
12-
});
13-
}
4+
export const readdir = promisify(fsReaddir);

0 commit comments

Comments
 (0)