-
Notifications
You must be signed in to change notification settings - Fork 511
/
Copy pathutils.ts
117 lines (100 loc) · 4.36 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import os = require("os");
import path = require("path");
import vscode = require("vscode");
import { satisfies } from "semver";
export const PowerShellLanguageId = "powershell";
// Path to the shell integration script in the VS Code installation
// See https://github.com/microsoft/vscode/pull/227244
const shellIntegrationMoved = satisfies(vscode.version, ">=1.94", { includePrerelease: true });
export const ShellIntegrationScript = path.join(vscode.env.appRoot, "out", "vs", "workbench", "contrib", "terminal",
shellIntegrationMoved ? "common" : "browser",
shellIntegrationMoved ? "scripts" : "media",
"shellIntegration.ps1");
export function escapeSingleQuotes(p: string): string {
return p.replace(new RegExp("'", "g"), "''");
}
export function stripQuotePair(p: string | undefined): string | undefined {
if (p === undefined) {
return p;
}
// Remove matching surrounding quotes from p (without regex)
if (p.startsWith("'") && p.endsWith("'")
|| p.startsWith("\"") && p.endsWith("\"")) {
return p.slice(1, -1);
}
return p;
}
export function getPipePath(pipeName: string): string {
if (os.platform() === "win32") {
return "\\\\.\\pipe\\" + pipeName;
} else {
// Windows uses NamedPipes where non-Windows platforms use Unix Domain Sockets.
// This requires connecting to the pipe file in different locations on Windows vs non-Windows.
return path.join(os.tmpdir(), `CoreFxPipe_${pipeName}`);
}
}
// Check that the file or directory exists in an asynchronous manner that relies
// solely on the VS Code API, not Node's fs library, ignoring symlinks.
async function checkIfFileOrDirectoryExists(targetPath: string | vscode.Uri, type: vscode.FileType): Promise<boolean> {
if (targetPath === "") {
return false;
}
try {
const stat: vscode.FileStat = await vscode.workspace.fs.stat(
targetPath instanceof vscode.Uri
? targetPath
: vscode.Uri.file(targetPath));
return (stat.type & type) !== 0;
} catch {
return false;
}
}
export async function checkIfFileExists(filePath: string | vscode.Uri): Promise<boolean> {
return await checkIfFileOrDirectoryExists(filePath, vscode.FileType.File);
}
export async function checkIfDirectoryExists(directoryPath: string | vscode.Uri): Promise<boolean> {
return await checkIfFileOrDirectoryExists(directoryPath, vscode.FileType.Directory);
}
export async function readDirectory(directoryPath: string | vscode.Uri): Promise<string[]> {
const items = await vscode.workspace.fs.readDirectory(
directoryPath instanceof vscode.Uri
? directoryPath
: vscode.Uri.file(directoryPath));
return items.map(([name, _type]) => name);
}
export function getTimestampString(): string {
const time = new Date();
return `[${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}]`;
}
export function sleep(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms));
}
/**
* Invokes the specified action when a PowerShell setting changes
* @param setting a string representation of the setting you wish to evaluate
* @param action the action to take when the setting changes
* @param scope the scope in which the vscode setting should be evaluated. Defaults to
* @param workspace
* @param onDidChangeConfiguration
* @example onPowerShellSettingChange("settingName", (newValue) => console.log(newValue));
* @returns a Disposable object that can be used to stop listening for changes
*/
// Because we actually do use the constraint in the callback
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters
export function onPowerShellSettingChange<T>(
setting: string,
listener: (newValue: T | undefined) => void,
section: "powershell",
scope?: vscode.ConfigurationScope,
): vscode.Disposable {
return vscode.workspace.onDidChangeConfiguration(e => {
if (!e.affectsConfiguration(section, scope)) { return; }
const value = vscode.workspace.getConfiguration(section, scope).get<T>(setting);
listener(value);
});
}
export const isMacOS: boolean = process.platform === "darwin";
export const isWindows: boolean = process.platform === "win32";
export const isLinux: boolean = !isMacOS && !isWindows;