forked from PowerShell/vscode-powershell
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.ts
121 lines (100 loc) · 3.83 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
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
"use strict";
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 {
// 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}`);
}
}
export interface IEditorServicesSessionDetails {
status: string;
reason: string;
detail: string;
powerShellVersion: string;
channel: string;
languageServicePort: number;
debugServicePort: number;
languageServicePipeName: string;
debugServicePipeName: string;
}
export type IReadSessionFileCallback = (details: IEditorServicesSessionDetails) => void;
export type IWaitForSessionFileCallback = (details: IEditorServicesSessionDetails, error: string) => void;
const sessionsFolder = path.resolve(__dirname, "..", "..", "sessions/");
const 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: IEditorServicesSessionDetails) {
ensurePathExists(sessionsFolder);
const writeStream = fs.createWriteStream(sessionFilePath);
writeStream.write(JSON.stringify(sessionDetails));
writeStream.close();
}
export function waitForSessionFile(sessionFilePath: string, callback: IWaitForSessionFileCallback) {
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(
() => { innerTryFunc(remainingTries - 1, delayMilliseconds); },
delayMilliseconds);
} else {
// Session file was found, load and return it
callback(readSessionFile(sessionFilePath), undefined);
}
}
// Try once every 2 seconds, 60 times - making two full minutes
innerTryFunc(60, 2000);
}
export function readSessionFile(sessionFilePath: string): IEditorServicesSessionDetails {
const 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() {
const time = new Date();
return `[${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}]`;
}
export function isWindowsOS(): boolean {
return os.platform() === "win32";
}