3
3
4
4
"use strict" ;
5
5
6
- import fs = require( "fs" ) ;
7
6
import os = require( "os" ) ;
8
7
import path = require( "path" ) ;
9
8
import vscode = require( "vscode" ) ;
10
9
11
10
export const PowerShellLanguageId = "powershell" ;
12
11
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
-
30
12
export function getPipePath ( pipeName : string ) {
31
13
if ( os . platform ( ) === "win32" ) {
32
14
return "\\\\.\\pipe\\" + pipeName ;
@@ -37,22 +19,28 @@ export function getPipePath(pipeName: string) {
37
19
}
38
20
}
39
21
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 > {
41
25
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.
45
34
return false ;
46
35
}
47
36
}
48
37
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 ) ;
56
44
}
57
45
58
46
export function getTimestampString ( ) {
0 commit comments