33
44"use strict" ;
55
6- import fs = require( "fs" ) ;
76import os = require( "os" ) ;
87import path = require( "path" ) ;
98import vscode = require( "vscode" ) ;
109
1110export 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-
3012export 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
5846export function getTimestampString ( ) {
0 commit comments