Skip to content

Commit ad7177e

Browse files
committed
Clean up using utils.checkIfFileOrDirectoryExists()
We have file/directory existence logic in too many places.
1 parent 1ae818d commit ad7177e

File tree

3 files changed

+19
-31
lines changed

3 files changed

+19
-31
lines changed

src/features/Examples.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class ExamplesFeature implements vscode.Disposable {
1515
vscode.commands.executeCommand("vscode.openFolder", this.examplesPath, true);
1616
// Return existence of the path for testing. The `vscode.openFolder`
1717
// command should do this, but doesn't (yet).
18-
return utils.fileExists(this.examplesPath);
18+
return utils.checkIfFileExists(this.examplesPath);
1919
});
2020
}
2121

src/features/PesterTests.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export class PesterTestsFeature implements vscode.Disposable {
134134
//
135135
// Ensure the necessary script exists (for testing). The debugger will
136136
// start regardless, but we also pass its success along.
137-
return utils.fileExists(this.invokePesterStubScriptPath)
137+
return utils.checkIfFileExists(this.invokePesterStubScriptPath)
138138
&& vscode.debug.startDebugging(vscode.workspace.workspaceFolders?.[0], launchConfig);
139139
}
140140
}

src/utils.ts

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,12 @@
33

44
"use strict";
55

6-
import fs = require("fs");
76
import os = require("os");
87
import path = require("path");
98
import vscode = require("vscode");
109

1110
export const PowerShellLanguageId = "powershell";
1211

13-
// Check that the file exists in an asynchronous manner that relies solely on the VS Code API, not Node's fs library.
14-
export async function fileExists(targetPath: string | vscode.Uri): Promise<boolean> {
15-
try {
16-
await vscode.workspace.fs.stat(
17-
targetPath instanceof vscode.Uri
18-
? targetPath
19-
: vscode.Uri.file(targetPath));
20-
return true;
21-
} catch (e) {
22-
if (e instanceof vscode.FileSystemError.FileNotFound) {
23-
return false;
24-
}
25-
throw e;
26-
}
27-
28-
}
29-
3012
export function getPipePath(pipeName: string) {
3113
if (os.platform() === "win32") {
3214
return "\\\\.\\pipe\\" + pipeName;
@@ -37,22 +19,28 @@ export function getPipePath(pipeName: string) {
3719
}
3820
}
3921

40-
export async function checkIfFileExists(filePath: vscode.Uri): Promise<boolean> {
22+
// Check that the file or directory exists in an asynchronous manner that relies
23+
// solely on the VS Code API, not Node's fs library, ignoring symlinks.
24+
async function checkIfFileOrDirectoryExists(targetPath: string | vscode.Uri, type: vscode.FileType): Promise<boolean> {
4125
try {
42-
const stat: vscode.FileStat = await vscode.workspace.fs.stat(filePath);
43-
return stat.type === vscode.FileType.File;
44-
} catch (e) {
26+
const stat: vscode.FileStat = await vscode.workspace.fs.stat(
27+
targetPath instanceof vscode.Uri
28+
? targetPath
29+
: vscode.Uri.file(targetPath));
30+
// tslint:disable-next-line:no-bitwise
31+
return (stat.type & type) !== 0;
32+
} catch {
33+
// TODO: Maybe throw if it's not a FileNotFound exception.
4534
return false;
4635
}
4736
}
4837

49-
export async function checkIfDirectoryExists(directoryPath: string): Promise<boolean> {
50-
try {
51-
const stat: vscode.FileStat = await vscode.workspace.fs.stat(vscode.Uri.file(directoryPath));
52-
return stat.type === vscode.FileType.Directory;
53-
} catch (e) {
54-
return false;
55-
}
38+
export async function checkIfFileExists(filePath: string | vscode.Uri): Promise<boolean> {
39+
return await checkIfFileOrDirectoryExists(filePath, vscode.FileType.File);
40+
}
41+
42+
export async function checkIfDirectoryExists(directoryPath: string | vscode.Uri): Promise<boolean> {
43+
return await checkIfFileOrDirectoryExists(directoryPath, vscode.FileType.Directory);
5644
}
5745

5846
export function getTimestampString() {

0 commit comments

Comments
 (0)