-
Notifications
You must be signed in to change notification settings - Fork 343
Expand file tree
/
Copy pathutils.ts
More file actions
259 lines (240 loc) · 9.28 KB
/
Copy pathutils.ts
File metadata and controls
259 lines (240 loc) · 9.28 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/********************************************************************************
* Copyright (c) 2019 TypeFox and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
********************************************************************************/
import { ErrorResponse } from './server-request';
export function addQuery(url: string, queries: { key: string, value: string | number | undefined }[]): string {
const nonEmpty = queries.filter(q => q.value !== undefined);
if (nonEmpty.length === 0) {
return url;
}
return url + '?' + nonEmpty.map<string>(q => q.key + '=' + encodeURIComponent(q.value!)).join('&');
}
export function createAbsoluteURL(arr: string[], queries?: { key: string, value: string | number | undefined }[]): string {
const url = arr.length === 0 ? '' : arr.reduce((prev, curr) => prev + '/' + curr);
if (queries && queries.length > 0) {
return addQuery(url, queries);
} else {
return url;
}
}
export function createRoute(arr: string[], queries?: { key: string, value: string | number }[]): string {
const url = createAbsoluteURL(arr, queries);
return url.startsWith('/') ? url : '/' + url;
}
export function debounce(task: () => void, token: { timeout?: number }, delay: number = 150): void {
clearTimeout(token.timeout);
token.timeout = window.setTimeout(task, delay);
}
export function toLocalTime(timestamp?: string): string | undefined {
if (!timestamp) {
return undefined;
}
const date = new Date(timestamp);
const options: Intl.DateTimeFormatOptions = {
year: 'numeric',
month: 'numeric',
day: 'numeric',
hour: 'numeric',
minute: 'numeric'
};
return new Intl.DateTimeFormat(undefined, options).format(date);
}
const msPerSecond = 1000;
const msPerMinute = msPerSecond * 60;
const msPerHour = msPerMinute * 60;
const msPerDay = msPerHour * 24;
const msPerMonth = msPerDay * 30.4;
const msPerYear = msPerDay * 365;
export function toRelativeTime(timestamp?: string, isFutureTime: boolean = false): string | undefined {
if (!timestamp) {
return undefined;
}
const date = new Date(timestamp);
const elapsed = Date.now() - date.getTime();
if (isFutureTime) {
const remaining = -elapsed;
if (remaining < 0) {
return "now";
} else if (remaining < msPerMinute) {
const value = Math.round(remaining / msPerSecond);
return `in ${value} second${value !== 1 ? 's' : ''}`;
} else if (remaining < msPerHour) {
const value = Math.round(remaining / msPerMinute);
return `in ${value} minute${value !== 1 ? 's' : ''}`;
} else if (remaining < msPerDay) {
const value = Math.round(remaining / msPerHour);
return `in ${value} hour${value !== 1 ? 's' : ''}`;
} else if (remaining < msPerMonth) {
const value = Math.round(remaining / msPerDay);
return `in ${value} day${value !== 1 ? 's' : ''}`;
} else if (remaining < msPerYear) {
const value = Math.round(remaining / msPerMonth);
return `in ${value} month${value !== 1 ? 's' : ''}`;
} else {
const value = Math.round(remaining / msPerYear);
return `in ${value} year${value !== 1 ? 's' : ''}`;
}
} else {
if (elapsed < msPerMinute) {
return 'now';
} else if (elapsed < msPerHour) {
const value = Math.round(elapsed / msPerMinute);
return `${value} minute${value !== 1 ? 's' : ''} ago`;
} else if (elapsed < msPerDay) {
const value = Math.round(elapsed / msPerHour);
return `${value} hour${value !== 1 ? 's' : ''} ago`;
} else if (elapsed < msPerMonth) {
const value = Math.round(elapsed / msPerDay);
return `${value} day${value !== 1 ? 's' : ''} ago`;
} else if (elapsed < msPerYear) {
const value = Math.round(elapsed / msPerMonth);
return `${value} month${value !== 1 ? 's' : ''} ago`;
} else {
const value = Math.round(elapsed / msPerYear);
return `${value} year${value !== 1 ? 's' : ''} ago`;
}
}
}
export function handleError(err?: Error | Partial<ErrorResponse>): string {
if (err) {
console.error(err);
if (err instanceof Error) {
if (err.message === 'Failed to fetch'
|| err.message === 'Unexpected token < in JSON at position 0') {
return 'Something went wrong while fetching data. Please contact the site administrators.';
}
return `An unexpected error occurred: ${err.message}`;
} else if (err.error && err.message) {
return `${err.error} (${err.message})`;
} else if (err.error) {
return err.error;
} else if (err.message) {
return err.message;
}
}
return 'An unexpected error occurred.';
}
export interface Cookie {
key: string;
value: string;
path?: string;
domain?: string;
maxAge?: number;
expires?: string;
secure?: boolean;
samesite?: 'lax' | 'strict' | 'none';
}
export function setCookie(cookie: Cookie): void {
let cookieString = `${cookie.key}=${encodeURIComponent(cookie.value)}`;
if (cookie.path) {
cookieString += `; path=${cookie.path}`;
}
if (cookie.domain) {
cookieString += `; domain=${cookie.domain}`;
}
if (cookie.maxAge) {
cookieString += `; max-age=${cookie.maxAge}`;
}
if (cookie.expires) {
cookieString += `; expires=${cookie.expires}`;
}
if (cookie.secure) {
cookieString += '; secure';
}
if (cookie.samesite) {
cookieString += `; samesite=${cookie.samesite}`;
}
document.cookie = cookieString;
}
export function getCookieValueByKey(key: string): string | undefined {
const value = `; ${document.cookie}`;
const parts = value.split(`; ${key}=`);
if (parts.length === 2) {
const value = parts[1].split(';')[0];
return decodeURIComponent(value);
}
return undefined;
}
namespace TargetPlatform {
export const WIN32_IA32 = 'win32-ia32';
export const WIN32_X64 = 'win32-x64';
export const WIN32_ARM64 = 'win32-arm64';
export const LINUX_X64 = 'linux-x64';
export const LINUX_ARM64 = 'linux-arm64';
export const LINUX_ARMHF = 'linux-armhf';
export const LINUX_LOONG64 = 'linux-loong64';
export const LINUX_PPC64 = 'linux-ppc64le';
export const LINUX_RISCV64 = 'linux-riscv64';
export const LINUX_S390X = 'linux-s390x';
export const ALPINE_X64 = 'alpine-x64';
export const ALPINE_ARM64 = 'alpine-arm64';
export const DARWIN_X64 = 'darwin-x64';
export const DARWIN_ARM64 = 'darwin-arm64';
export const WEB = 'web';
export const UNIVERSAL = 'universal';
}
export function getTargetPlatforms(): string[] {
return [
TargetPlatform.WIN32_IA32,
TargetPlatform.WIN32_X64,
TargetPlatform.WIN32_ARM64,
TargetPlatform.LINUX_X64,
TargetPlatform.LINUX_ARM64,
TargetPlatform.LINUX_ARMHF,
TargetPlatform.LINUX_LOONG64,
TargetPlatform.LINUX_PPC64,
TargetPlatform.LINUX_RISCV64,
TargetPlatform.LINUX_S390X,
TargetPlatform.ALPINE_X64,
TargetPlatform.ALPINE_ARM64,
TargetPlatform.DARWIN_X64,
TargetPlatform.DARWIN_ARM64,
TargetPlatform.WEB,
TargetPlatform.UNIVERSAL
];
}
export function getTargetPlatformDisplayName(targetPlatform: string): string {
const targetPlatformDisplayNames = new Map([
[TargetPlatform.UNIVERSAL, 'Universal'],
[TargetPlatform.WIN32_IA32, 'Windows x86'],
[TargetPlatform.WIN32_X64, 'Windows x64'],
[TargetPlatform.WIN32_ARM64, 'Windows ARM'],
[TargetPlatform.LINUX_X64, 'Linux x64'],
[TargetPlatform.LINUX_ARM64, 'Linux ARM64'],
[TargetPlatform.LINUX_ARMHF, 'Linux ARMhf'],
[TargetPlatform.LINUX_LOONG64, 'Linux LoongArch 64 bit'],
[TargetPlatform.LINUX_PPC64, 'Linux PowerPC 64 bit (little endian)'],
[TargetPlatform.LINUX_RISCV64, 'Linux RISC-V 64 bit'],
[TargetPlatform.LINUX_S390X, 'Linux s390x'],
[TargetPlatform.ALPINE_X64, 'Alpine Linux 64 bit'],
[TargetPlatform.ALPINE_ARM64, 'Alpine Linux ARM64'],
[TargetPlatform.DARWIN_X64, 'macOS Intel'],
[TargetPlatform.DARWIN_ARM64, 'macOS Apple Silicon'],
[TargetPlatform.WEB, 'Web']
]);
return targetPlatformDisplayNames.get(targetPlatform) ?? '';
}
export function getEngineDisplayName(engine: string): string {
const engineDisplayNames = new Map([
['vscode', 'VS Code'],
['node', 'Node.js'],
['npm', 'npm'],
['pnpm', 'pnpm'],
['yarn', 'Yarn'],
['vsce', 'vsce'],
['sqlops', 'SQL Operation Studio'],
['azdata', 'Azure Data Studio'],
['theiaPlugin', 'Theia Plugin'],
['opensumi', 'OpenSumi'],
['nadako.vshaxe', 'nadako.vshaxe'],
['hbenl.vscode-test-explorer', 'hbenl.vscode-test-explorer'],
['positron', 'Positron']
]);
return engineDisplayNames.get(engine) ?? '';
}