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

+15-11
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

+13-9
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

+5-2
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

+1-1
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

+1-1
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

+9
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

+4
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

+4
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

+4
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

+2-11
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);

src/fs/rmdir.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { rmdir as fsRmdir } from "original-fs";
2+
import { promisify } from "util";
3+
4+
export const rmdir = promisify(fsRmdir);

src/fs/stat.ts

+3-12
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
1-
import { stat as fsStat, Stats } from "original-fs";
1+
import { stat as fsStat } from "original-fs";
2+
import { promisify } from "util";
23

3-
export function stat(filePath: string): Promise<Stats> {
4-
return new Promise((resolve, reject) => {
5-
fsStat(filePath, (err, stats) => {
6-
if (err) {
7-
reject(err);
8-
}
9-
10-
resolve(stats);
11-
});
12-
});
13-
}
4+
export const stat = promisify(fsStat);

src/fs/unlink.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { unlink as fsUnlink } from "original-fs";
2+
import { promisify } from "util";
3+
4+
export const unlink = promisify(fsUnlink);

src/fs/write_file.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { writeFile as fsWriteFile } from "original-fs";
2+
import { promisify } from "util";
3+
4+
export const writeFile = promisify(fsWriteFile);

src/historyView/common.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { createHash } from "crypto";
2-
import * as fs from "original-fs";
32
import * as path from "path";
43
import {
54
commands,
@@ -10,6 +9,7 @@ import {
109
window
1110
} from "vscode";
1211
import { ISvnLogEntry, ISvnLogEntryPath } from "../common/types";
12+
import { exists, lstat } from "../fs";
1313
import { configuration } from "../helpers/configuration";
1414
import { IRemoteRepository } from "../remoteRepository";
1515
import { SvnRI } from "../svnRI";
@@ -128,7 +128,10 @@ export function insertBaseMarker(
128128
return undefined;
129129
}
130130

131-
export function checkIfFile(e: SvnRI, local: boolean): boolean | undefined {
131+
export async function checkIfFile(
132+
e: SvnRI,
133+
local: boolean
134+
): Promise<boolean | undefined> {
132135
if (e.localFullPath === undefined) {
133136
if (local) {
134137
window.showErrorMessage("No working copy for this path");
@@ -137,7 +140,7 @@ export function checkIfFile(e: SvnRI, local: boolean): boolean | undefined {
137140
}
138141
let stat;
139142
try {
140-
stat = fs.lstatSync(e.localFullPath.fsPath);
143+
stat = await lstat(e.localFullPath.fsPath);
141144
} catch {
142145
window.showWarningMessage(
143146
"Not available from this working copy: " + e.localFullPath
@@ -238,7 +241,7 @@ async function downloadFile(
238241
const nm = repo.getPathNormalizer();
239242
const ri = nm.parse(arg.toString(true));
240243
const localPath = ri.localFullPath;
241-
if (localPath === undefined || !fs.existsSync(localPath.path)) {
244+
if (localPath === undefined || !(await exists(localPath.path))) {
242245
const errorMsg =
243246
"BASE revision doesn't exist for " +
244247
(localPath ? localPath.path : "remote path");

src/historyView/repoLogProvider.ts

+6-6
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,
@@ -17,6 +16,7 @@ import {
1716
ISvnLogEntry,
1817
ISvnLogEntryPath
1918
} from "../common/types";
19+
import { exists } from "../fs";
2020
import { Model } from "../model";
2121
import { IRemoteRepository } from "../remoteRepository";
2222
import { Repository } from "../repository";
@@ -203,7 +203,7 @@ export class RepoLogProvider
203203
public addRepolikeGui() {
204204
const box = window.createInputBox();
205205
box.prompt = "Enter SVN URL or local path";
206-
box.onDidAccept(() => {
206+
box.onDidAccept(async () => {
207207
let repoLike = box.value;
208208
if (
209209
!path.isAbsolute(repoLike) &&
@@ -213,7 +213,7 @@ export class RepoLogProvider
213213
) {
214214
for (const wsf of workspace.workspaceFolders) {
215215
const joined = path.join(wsf.uri.fsPath, repoLike);
216-
if (fs.existsSync(joined)) {
216+
if (await exists(joined)) {
217217
repoLike = joined;
218218
break;
219219
}
@@ -232,11 +232,11 @@ export class RepoLogProvider
232232
box.show();
233233
}
234234

235-
public openFileRemoteCmd(element: ILogTreeItem) {
235+
public async openFileRemoteCmd(element: ILogTreeItem) {
236236
const commit = element.data as ISvnLogEntryPath;
237237
const item = this.getCached(element);
238238
const ri = item.repo.getPathNormalizer().parse(commit._);
239-
if (checkIfFile(ri, false) === false) {
239+
if ((await checkIfFile(ri, false)) === false) {
240240
return;
241241
}
242242
const parent = (element.parent as ILogTreeItem).data as ISvnLogEntry;
@@ -257,7 +257,7 @@ export class RepoLogProvider
257257
const commit = element.data as ISvnLogEntryPath;
258258
const item = this.getCached(element);
259259
const ri = item.repo.getPathNormalizer().parse(commit._);
260-
if (checkIfFile(ri, false) === false) {
260+
if ((await checkIfFile(ri, false)) === false) {
261261
return;
262262
}
263263
const parent = (element.parent as ILogTreeItem).data as ISvnLogEntry;

src/model.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ import {
1818
Status
1919
} from "./common/types";
2020
import { debounce } from "./decorators";
21-
import { exists } from "./fs/exists";
22-
import { readdir } from "./fs/readdir";
23-
import { stat } from "./fs/stat";
21+
import { exists, readdir, stat } from "./fs";
2422
import { configuration } from "./helpers/configuration";
2523
import { RemoteRepository } from "./remoteRepository";
2624
import { Repository } from "./repository";

src/svnRepository.ts

+3-3
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 * as tmp from "tmp";
43
import { Uri, workspace } from "vscode";
@@ -12,6 +11,7 @@ import {
1211
Status
1312
} from "./common/types";
1413
import { sequentialize } from "./decorators";
14+
import { exists, writeFile } from "./fs";
1515
import { getBranchName } from "./helpers/branch";
1616
import { configuration } from "./helpers/configuration";
1717
import { parseInfoXml } from "./infoParser";
@@ -209,7 +209,7 @@ export class Repository {
209209

210210
const args = ["commit", ...files];
211211

212-
if (fs.existsSync(path.join(this.workspaceRoot, message))) {
212+
if (await exists(path.join(this.workspaceRoot, message))) {
213213
args.push("--force-log");
214214
}
215215

@@ -228,7 +228,7 @@ export class Repository {
228228
prefix: "svn-commit-message-"
229229
});
230230

231-
fs.writeFileSync(tmpFile.name, message, "UTF-8");
231+
await writeFile(tmpFile.name, message, "UTF-8");
232232

233233
args.push("-F", tmpFile.name);
234234
args.push("--encoding", "UTF-8");

src/tempFiles.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
// use import { promises as fs } from "original-fs"; when nodejs will be updated
2-
import * as fs from "original-fs";
31
import * as os from "os";
42
import * as path from "path";
5-
import * as util from "util";
63
import { Uri } from "vscode";
7-
8-
const writeFile = util.promisify(fs.writeFile);
4+
import { exists, mkdir, writeFile } from "./fs";
95

106
export const tempdir = path.join(os.tmpdir(), "vscode-svn");
117

@@ -14,8 +10,8 @@ export async function dumpSvnFile(
1410
revision: string,
1511
payload: string
1612
): Promise<Uri> {
17-
if (!fs.existsSync(tempdir)) {
18-
await fs.mkdirSync(tempdir);
13+
if (!await exists(tempdir)) {
14+
await mkdir(tempdir);
1915
}
2016
const fname = `r${revision}_${path.basename(snvUri.fsPath)}`;
2117
const fpath = path.join(tempdir, fname);

0 commit comments

Comments
 (0)