Skip to content

Commit

Permalink
perf: All fs is done async (#540)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnstonCode committed Mar 29, 2019
1 parent 778dcb5 commit b26602f
Show file tree
Hide file tree
Showing 21 changed files with 133 additions and 78 deletions.
26 changes: 15 additions & 11 deletions src/commands/command.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as fs from "original-fs";
import * as path from "path";
import {
commands,
Expand All @@ -16,6 +15,7 @@ import {
WorkspaceEdit
} from "vscode";
import { ICommandOptions, Status, SvnUriAction } from "../common/types";
import { exists, readFile, stat, unlink } from "../fs";
import { inputIgnoreList } from "../ignoreitems";
import { applyLineChanges } from "../lineChanges";
import { Model } from "../model";
Expand Down Expand Up @@ -194,7 +194,7 @@ export abstract class Command implements Disposable {
preserveFocus?: boolean,
preserveSelection?: boolean
): Promise<void> {
let left = this.getLeftResource(resource, against);
let left = await this.getLeftResource(resource, against);
let right = this.getRightResource(resource, against);
const title = this.getTitle(resource, against);

Expand All @@ -209,8 +209,8 @@ export abstract class Command implements Disposable {
}

if (
fs.existsSync(right.fsPath) &&
fs.statSync(right.fsPath).isDirectory()
(await exists(right.fsPath)) &&
(await stat(right.fsPath)).isDirectory()
) {
return;
}
Expand Down Expand Up @@ -244,10 +244,10 @@ export abstract class Command implements Disposable {
);
}

protected getLeftResource(
protected async getLeftResource(
resource: Resource,
against: string = ""
): Uri | undefined {
): Promise<Uri | undefined> {
if (resource.remote) {
if (resource.type !== Status.DELETED) {
return toSvnUri(resource.resourceUri, SvnUriAction.SHOW, {
Expand All @@ -266,11 +266,11 @@ export abstract class Command implements Disposable {
// Show file if has conflicts marks
if (
resource.type === Status.CONFLICTED &&
fs.existsSync(resource.resourceUri.fsPath)
(await exists(resource.resourceUri.fsPath))
) {
const text = fs.readFileSync(resource.resourceUri.fsPath, {
const text = (await readFile(resource.resourceUri.fsPath, {
encoding: "utf8"
});
})) as string;

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

if (fs.existsSync(tempFile)) {
fs.unlinkSync(tempFile);
if (await exists(tempFile)) {
try {
await unlink(tempFile);
} catch (err) {
// TODO(cjohnston)//log error
}
}

const uri = Uri.file(tempFile).with({
Expand Down
22 changes: 13 additions & 9 deletions src/commands/deleteUnversioned.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as fs from "original-fs";
import { SourceControlResourceState, window } from "vscode";
import { exists, lstat, unlink } from "../fs";
import { deleteDirectory } from "../util";
import { Command } from "./command";

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

if (!fs.existsSync(fsPath)) {
continue;
}
try {
if (!(await exists(fsPath))) {
continue;
}

const stat = fs.lstatSync(fsPath);
const stat = await lstat(fsPath);

if (stat.isDirectory()) {
deleteDirectory(fsPath);
} else {
fs.unlinkSync(fsPath);
if (stat.isDirectory()) {
deleteDirectory(fsPath);
} else {
await unlink(fsPath);
}
} catch (err) {
// TODO(cjohnston) Show meaningful error to user
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/commands/openFile.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as fs from "original-fs";
import {
SourceControlResourceState,
TextDocumentShowOptions,
Expand All @@ -7,6 +6,7 @@ import {
window,
workspace
} from "vscode";
import { exists, stat } from "../fs";
import { Resource } from "../resource";
import IncomingChangeNode from "../treeView/nodes/incomingChangeNode";
import { fromSvnUri } from "../uri";
Expand Down Expand Up @@ -65,7 +65,10 @@ export class OpenFile extends Command {
const preview = uris.length === 1 ? true : false;
const activeTextEditor = window.activeTextEditor;
for (const uri of uris) {
if (fs.existsSync(uri.fsPath) && fs.statSync(uri.fsPath).isDirectory()) {
if (
(await exists(uri.fsPath)) &&
(await stat(uri.fsPath)).isDirectory()
) {
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion src/commands/openHeadFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class OpenHeadFile extends Command {
return;
}

const HEAD = this.getLeftResource(resource, "HEAD");
const HEAD = await this.getLeftResource(resource, "HEAD");

const basename = path.basename(resource.resourceUri.fsPath);
if (!HEAD) {
Expand Down
2 changes: 1 addition & 1 deletion src/fs/exists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import { access } from "original-fs";

export function exists(path: string): Promise<boolean> {
return new Promise((resolve, reject) => {
access(path, err => (err ? reject(err) : resolve(true)));
access(path, err => (err ? resolve(false) : resolve(true)));
});
}
9 changes: 9 additions & 0 deletions src/fs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export { exists } from "./exists";
export { lstat } from "./lstat";
export { mkdir } from "./mkdir";
export { readFile } from "./read_file";
export { readdir } from "./readdir";
export { rmdir } from "./rmdir";
export { stat } from "./stat";
export { unlink } from "./unlink";
export { writeFile } from "./write_file";
4 changes: 4 additions & 0 deletions src/fs/lstat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { lstat as fsLstat } from "original-fs";
import { promisify } from "util";

export const lstat = promisify(fsLstat);
4 changes: 4 additions & 0 deletions src/fs/mkdir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { mkdir as fsMkdir } from "original-fs";
import { promisify } from "util";

export const mkdir = promisify(fsMkdir);
4 changes: 4 additions & 0 deletions src/fs/read_file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { readFile as fsReadFile } from "original-fs";
import { promisify } from "util";

export const readFile = promisify(fsReadFile);
13 changes: 2 additions & 11 deletions src/fs/readdir.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
import { readdir as fsReaddir } from "original-fs";
import { promisify } from "util";

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

resolve(files);
});
});
}
export const readdir = promisify(fsReaddir);
4 changes: 4 additions & 0 deletions src/fs/rmdir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { rmdir as fsRmdir } from "original-fs";
import { promisify } from "util";

export const rmdir = promisify(fsRmdir);
15 changes: 3 additions & 12 deletions src/fs/stat.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
import { stat as fsStat, Stats } from "original-fs";
import { stat as fsStat } from "original-fs";
import { promisify } from "util";

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

resolve(stats);
});
});
}
export const stat = promisify(fsStat);
4 changes: 4 additions & 0 deletions src/fs/unlink.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { unlink as fsUnlink } from "original-fs";
import { promisify } from "util";

export const unlink = promisify(fsUnlink);
4 changes: 4 additions & 0 deletions src/fs/write_file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { writeFile as fsWriteFile } from "original-fs";
import { promisify } from "util";

export const writeFile = promisify(fsWriteFile);
11 changes: 7 additions & 4 deletions src/historyView/common.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { createHash } from "crypto";
import * as fs from "original-fs";
import * as path from "path";
import {
commands,
Expand All @@ -10,6 +9,7 @@ import {
window
} from "vscode";
import { ISvnLogEntry, ISvnLogEntryPath } from "../common/types";
import { exists, lstat } from "../fs";
import { configuration } from "../helpers/configuration";
import { IRemoteRepository } from "../remoteRepository";
import { SvnRI } from "../svnRI";
Expand Down Expand Up @@ -128,7 +128,10 @@ export function insertBaseMarker(
return undefined;
}

export function checkIfFile(e: SvnRI, local: boolean): boolean | undefined {
export async function checkIfFile(
e: SvnRI,
local: boolean
): Promise<boolean | undefined> {
if (e.localFullPath === undefined) {
if (local) {
window.showErrorMessage("No working copy for this path");
Expand All @@ -137,7 +140,7 @@ export function checkIfFile(e: SvnRI, local: boolean): boolean | undefined {
}
let stat;
try {
stat = fs.lstatSync(e.localFullPath.fsPath);
stat = await lstat(e.localFullPath.fsPath);
} catch {
window.showWarningMessage(
"Not available from this working copy: " + e.localFullPath
Expand Down Expand Up @@ -238,7 +241,7 @@ async function downloadFile(
const nm = repo.getPathNormalizer();
const ri = nm.parse(arg.toString(true));
const localPath = ri.localFullPath;
if (localPath === undefined || !fs.existsSync(localPath.path)) {
if (localPath === undefined || !(await exists(localPath.path))) {
const errorMsg =
"BASE revision doesn't exist for " +
(localPath ? localPath.path : "remote path");
Expand Down
12 changes: 6 additions & 6 deletions src/historyView/repoLogProvider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as fs from "original-fs";
import * as path from "path";
import {
commands,
Expand All @@ -17,6 +16,7 @@ import {
ISvnLogEntry,
ISvnLogEntryPath
} from "../common/types";
import { exists } from "../fs";
import { Model } from "../model";
import { IRemoteRepository } from "../remoteRepository";
import { Repository } from "../repository";
Expand Down Expand Up @@ -203,7 +203,7 @@ export class RepoLogProvider
public addRepolikeGui() {
const box = window.createInputBox();
box.prompt = "Enter SVN URL or local path";
box.onDidAccept(() => {
box.onDidAccept(async () => {
let repoLike = box.value;
if (
!path.isAbsolute(repoLike) &&
Expand All @@ -213,7 +213,7 @@ export class RepoLogProvider
) {
for (const wsf of workspace.workspaceFolders) {
const joined = path.join(wsf.uri.fsPath, repoLike);
if (fs.existsSync(joined)) {
if (await exists(joined)) {
repoLike = joined;
break;
}
Expand All @@ -232,11 +232,11 @@ export class RepoLogProvider
box.show();
}

public openFileRemoteCmd(element: ILogTreeItem) {
public async openFileRemoteCmd(element: ILogTreeItem) {
const commit = element.data as ISvnLogEntryPath;
const item = this.getCached(element);
const ri = item.repo.getPathNormalizer().parse(commit._);
if (checkIfFile(ri, false) === false) {
if ((await checkIfFile(ri, false)) === false) {
return;
}
const parent = (element.parent as ILogTreeItem).data as ISvnLogEntry;
Expand All @@ -257,7 +257,7 @@ export class RepoLogProvider
const commit = element.data as ISvnLogEntryPath;
const item = this.getCached(element);
const ri = item.repo.getPathNormalizer().parse(commit._);
if (checkIfFile(ri, false) === false) {
if ((await checkIfFile(ri, false)) === false) {
return;
}
const parent = (element.parent as ILogTreeItem).data as ISvnLogEntry;
Expand Down
4 changes: 1 addition & 3 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ import {
Status
} from "./common/types";
import { debounce } from "./decorators";
import { exists } from "./fs/exists";
import { readdir } from "./fs/readdir";
import { stat } from "./fs/stat";
import { exists, readdir, stat } from "./fs";
import { configuration } from "./helpers/configuration";
import { RemoteRepository } from "./remoteRepository";
import { Repository } from "./repository";
Expand Down
6 changes: 3 additions & 3 deletions src/svnRepository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as fs from "original-fs";
import * as path from "path";
import * as tmp from "tmp";
import { Uri, workspace } from "vscode";
Expand All @@ -12,6 +11,7 @@ import {
Status
} from "./common/types";
import { sequentialize } from "./decorators";
import { exists, writeFile } from "./fs";
import { getBranchName } from "./helpers/branch";
import { configuration } from "./helpers/configuration";
import { parseInfoXml } from "./infoParser";
Expand Down Expand Up @@ -209,7 +209,7 @@ export class Repository {

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

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

Expand All @@ -228,7 +228,7 @@ export class Repository {
prefix: "svn-commit-message-"
});

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

args.push("-F", tmpFile.name);
args.push("--encoding", "UTF-8");
Expand Down
10 changes: 3 additions & 7 deletions src/tempFiles.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
// use import { promises as fs } from "original-fs"; when nodejs will be updated
import * as fs from "original-fs";
import * as os from "os";
import * as path from "path";
import * as util from "util";
import { Uri } from "vscode";

const writeFile = util.promisify(fs.writeFile);
import { exists, mkdir, writeFile } from "./fs";

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

Expand All @@ -14,8 +10,8 @@ export async function dumpSvnFile(
revision: string,
payload: string
): Promise<Uri> {
if (!fs.existsSync(tempdir)) {
await fs.mkdirSync(tempdir);
if (!await exists(tempdir)) {
await mkdir(tempdir);
}
const fname = `r${revision}_${path.basename(snvUri.fsPath)}`;
const fpath = path.join(tempdir, fname);
Expand Down
Loading

0 comments on commit b26602f

Please sign in to comment.