forked from PowerShell/vscode-powershell
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.ts
142 lines (122 loc) · 4.21 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import fs = require('fs');
import os = require('os');
import path = require('path');
export let PowerShellLanguageId = 'powershell';
export function ensurePathExists(targetPath: string) {
// Ensure that the path exists
try {
fs.mkdirSync(targetPath);
}
catch (e) {
// If the exception isn't to indicate that the folder
// exists already, rethrow it.
if (e.code != 'EEXIST') {
throw e;
}
}
}
export function getUniqueSessionId() {
// We need to uniquely identify the current VS Code session
// using some string so that we get a reliable pipe server name
// for both the language and debug servers.
if (os.platform() == "linux") {
// Electron running on Linux uses an additional layer of
// separation between parent and child processes which
// prevents environment variables from being inherited
// easily. This causes VSCODE_PID to not be available
// (for now) so use a different variable to get a
// unique session.
return process.env.VSCODE_PID;
}
else {
// VSCODE_PID is available on Windows and OSX
return process.env.VSCODE_PID;
}
}
export function getPipePath(pipeName: string) {
if (os.platform() == "win32") {
return "\\\\.\\pipe\\" + pipeName;
}
else {
// On UNIX platforms the pipe will live under the temp path
// For details on how this path is computed, see the corefx
// source for System.IO.Pipes.PipeStream:
// https://github.com/dotnet/corefx/blob/d0dc5fc099946adc1035b34a8b1f6042eddb0c75/src/System.IO.Pipes/src/System/IO/Pipes/PipeStream.Unix.cs#L340
return path.resolve(
os.tmpdir(),
".dotnet", "corefx", "pipe",
pipeName);
}
}
export interface EditorServicesSessionDetails {
status: string;
reason: string;
detail: string;
powerShellVersion: string;
channel: string;
languageServicePort: number;
debugServicePort: number;
}
export interface ReadSessionFileCallback {
(details: EditorServicesSessionDetails): void;
}
export interface WaitForSessionFileCallback {
(details: EditorServicesSessionDetails, error: string): void;
}
let sessionsFolder = path.resolve(__dirname, "..", "sessions/");
let sessionFilePath = path.resolve(sessionsFolder, "PSES-VSCode-" + process.env.VSCODE_PID);
// Create the sessions path if it doesn't exist already
ensurePathExists(sessionsFolder);
export function getSessionFilePath() {
return sessionFilePath;
}
export function writeSessionFile(sessionDetails: EditorServicesSessionDetails) {
ensurePathExists(sessionsFolder);
var writeStream = fs.createWriteStream(sessionFilePath);
writeStream.write(JSON.stringify(sessionDetails));
writeStream.close();
}
export function waitForSessionFile(callback: WaitForSessionFileCallback) {
function innerTryFunc(remainingTries: number, delayMilliseconds: number) {
if (remainingTries == 0) {
callback(undefined, "Timed out waiting for session file to appear.");
}
else if(!checkIfFileExists(sessionFilePath)) {
// Wait a bit and try again
setTimeout(
function() { innerTryFunc(remainingTries - 1, delayMilliseconds); },
delayMilliseconds);
}
else {
// Session file was found, load and return it
callback(readSessionFile(), undefined);
}
}
// Try once per second for 60 seconds, one full minute
innerTryFunc(60, 1000);
}
export function readSessionFile(): EditorServicesSessionDetails {
let fileContents = fs.readFileSync(sessionFilePath, "utf-8");
return JSON.parse(fileContents)
}
export function deleteSessionFile() {
try {
fs.unlinkSync(sessionFilePath);
}
catch (e) {
// TODO: Be more specific about what we're catching
}
}
export function checkIfFileExists(filePath: string): boolean {
try {
fs.accessSync(filePath, fs.constants.R_OK)
return true;
}
catch (e) {
return false;
}
}
export function getTimestampString() {
var time = new Date();
return `[${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}]`
}