forked from PowerShell/vscode-powershell
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.ts
130 lines (110 loc) · 3.8 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
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 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 sessionFilePathPrefix = path.resolve(sessionsFolder, "PSES-VSCode-" + process.env.VSCODE_PID);
// Create the sessions path if it doesn't exist already
ensurePathExists(sessionsFolder);
export function getSessionFilePath(uniqueId: number) {
return `${sessionFilePathPrefix}-${uniqueId}`;
}
export function getDebugSessionFilePath() {
return `${sessionFilePathPrefix}-Debug`;
}
export function writeSessionFile(sessionFilePath: string, sessionDetails: EditorServicesSessionDetails) {
ensurePathExists(sessionsFolder);
var writeStream = fs.createWriteStream(sessionFilePath);
writeStream.write(JSON.stringify(sessionDetails));
writeStream.close();
}
export function waitForSessionFile(sessionFilePath: string, 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(sessionFilePath), undefined);
}
}
// Try once per second for 60 seconds, one full minute
innerTryFunc(60, 1000);
}
export function readSessionFile(sessionFilePath: string): EditorServicesSessionDetails {
let fileContents = fs.readFileSync(sessionFilePath, "utf-8");
return JSON.parse(fileContents)
}
export function deleteSessionFile(sessionFilePath: string) {
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()}]`
}
export function isWindowsOS(): boolean {
return os.platform() == "win32";
}