Skip to content

Commit b3662b5

Browse files
committed
Use vscode.workspace.fs.stat to check file existence asynchronously
1 parent 491ca7c commit b3662b5

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

src/features/Examples.ts

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
import * as fs from "fs";
54
import path = require("path");
5+
import utils = require("../utils")
66
import vscode = require("vscode");
77

88
export class ExamplesFeature implements vscode.Disposable {
99
private command: vscode.Disposable;
10-
private examplesPath: string;
10+
private examplesPath: vscode.Uri;
1111

1212
constructor() {
13-
this.examplesPath = path.resolve(__dirname, "../examples");
13+
this.examplesPath = vscode.Uri.file(path.resolve(__dirname, "../examples"));
1414
this.command = vscode.commands.registerCommand("PowerShell.OpenExamplesFolder", () => {
15-
vscode.commands.executeCommand(
16-
"vscode.openFolder",
17-
vscode.Uri.file(this.examplesPath),
18-
true);
19-
15+
vscode.commands.executeCommand("vscode.openFolder", this.examplesPath, true);
2016
// Return existence of the path for testing. The `vscode.openFolder`
2117
// command should do this, but doesn't (yet).
22-
return fs.existsSync(this.examplesPath)
18+
return utils.fileExists(this.examplesPath);
2319
});
2420
}
2521

src/features/PesterTests.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT License.
33

44
import * as path from "path";
5-
import * as fs from "fs";
65
import vscode = require("vscode");
76
import { SessionManager } from "../session";
87
import Settings = require("../settings");
@@ -144,7 +143,7 @@ export class PesterTestsFeature implements vscode.Disposable {
144143
//
145144
// Ensure the necessary script exists (for testing). The debugger will
146145
// start regardless, but we also pass its success along.
147-
return fs.existsSync(this.invokePesterStubScriptPath)
146+
return utils.fileExists(this.invokePesterStubScriptPath)
148147
&& vscode.debug.startDebugging(vscode.workspace.workspaceFolders[0], launchConfig);
149148
}
150149
}

src/utils.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
import fs = require("fs");
77
import os = require("os");
88
import path = require("path");
9+
import vscode = require("vscode");
910

1011
export const PowerShellLanguageId = "powershell";
1112

12-
export function ensurePathExists(targetPath: string) {
13+
export function ensurePathExists(targetPath: string): void {
1314
// Ensure that the path exists
1415
try {
16+
// TODO: Use vscode.workspace.fs
1517
fs.mkdirSync(targetPath);
1618
} catch (e) {
1719
// If the exception isn't to indicate that the folder exists already, rethrow it.
@@ -21,6 +23,23 @@ export function ensurePathExists(targetPath: string) {
2123
}
2224
}
2325

26+
// Check that the file exists in an asynchronous manner that relies solely on the VS Code API, not Node's fs library.
27+
export async function fileExists(targetPath: string | vscode.Uri): Promise<boolean> {
28+
try {
29+
await vscode.workspace.fs.stat(
30+
targetPath instanceof vscode.Uri
31+
? targetPath
32+
: vscode.Uri.file(targetPath));
33+
return true;
34+
} catch (e) {
35+
if (e instanceof vscode.FileSystemError.FileNotFound) {
36+
return false;
37+
}
38+
throw e;
39+
}
40+
41+
}
42+
2443
export function getPipePath(pipeName: string) {
2544
if (os.platform() === "win32") {
2645
return "\\\\.\\pipe\\" + pipeName;

0 commit comments

Comments
 (0)