-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
194 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import * as vscode from "vscode"; | ||
import * as path from "path"; | ||
import * as os from "os"; | ||
|
||
const hasCode = <T>(error: T): error is T & { code: string } => { | ||
return typeof (error as { code?: unknown }).code === "string"; | ||
}; | ||
|
||
const isFileNotFoundError = (err: unknown): boolean => { | ||
if (err instanceof vscode.FileSystemError) { | ||
return err.code === vscode.FileSystemError.FileNotFound().code; | ||
} else if (hasCode(err)) { | ||
return err.code === "ENOENT"; | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
export const fileExists = async ( | ||
file: string | vscode.Uri | ||
): Promise<boolean> => { | ||
const uri = typeof file === "string" ? vscode.Uri.file(file) : file; | ||
|
||
return vscode.workspace.fs.stat(uri).then( | ||
() => true, | ||
(err) => !isFileNotFoundError(err) | ||
); | ||
}; | ||
|
||
export const getHomeDirectory = (): string => { | ||
if (process.env.HOME !== undefined) { | ||
return process.env.HOME; | ||
} | ||
if (process.env.USERPROFILE !== undefined) { | ||
return process.env.USERPROFILE; | ||
} | ||
if (process.env.HOMEPATH !== undefined) { | ||
const homeDrive: string = process.env.HOMEDRIVE || "C:"; | ||
|
||
return path.join(homeDrive, process.env.HOMEPATH); | ||
} | ||
|
||
return os.homedir(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters