forked from redhat-developer/vscode-openshift-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
110 lines (101 loc) · 4.17 KB
/
utils.ts
File metadata and controls
110 lines (101 loc) · 4.17 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
/*-----------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/
import * as fs from 'fs/promises';
import * as path from 'path';
import { Uri, WebviewPanel, extensions } from 'vscode';
import OpenShiftItem from '../../openshift/openshiftItem';
import { ExtensionID } from '../../util/constants';
import { gitUrlParse } from '../../util/gitParse';
import { validateGitURLProps } from '../common/propertyTypes';
export type Message = {
action: string;
data: any;
};
export async function loadWebviewHtml(webviewName: string, webviewPanel: WebviewPanel, additionalInjections?: Map<string, string>): Promise<string> {
const extensionPath = extensions.getExtension(ExtensionID).extensionPath;
const reactAppRootOnDisk = path.join(extensionPath, 'out', webviewName);
const reactJavascriptUri = webviewPanel.webview.asWebviewUri(
Uri.file(path.join(reactAppRootOnDisk, 'index.js'))
);
const reactStylesheetUri = webviewPanel.webview.asWebviewUri(
Uri.file(path.join(reactAppRootOnDisk, 'index.css'))
);
const htmlString: Buffer = await fs.readFile(path.join(reactAppRootOnDisk, 'index.html'));
const meta = `<meta http-equiv="Content-Security-Policy"
content="connect-src *;
default-src 'none';
img-src ${webviewPanel.webview.cspSource} https: 'self' data:;
script-src 'unsafe-eval' 'unsafe-inline' vscode-resource:;
style-src 'self' vscode-resource: 'unsafe-inline';">`;
const htmlWithDefaultInjections = `${htmlString}`
.replace('%PLATFORM%', process.platform)
.replace('%SCRIPT%', `${reactJavascriptUri}`)
.replace('%BASE_URL%', `${reactJavascriptUri}`)
.replace('%STYLE%', `${reactStylesheetUri}`)
.replace('<!-- meta http-equiv="Content-Security-Policy" -->', meta);
if (!additionalInjections) {
return htmlWithDefaultInjections;
}
let htmlWithAdditionalInjections = htmlWithDefaultInjections;
for (const [key, value] of additionalInjections.entries()) {
htmlWithAdditionalInjections = htmlWithAdditionalInjections.replace(key, value);
}
return htmlWithAdditionalInjections;
}
function isGitURL(host: string): boolean {
return [
'github.com',
'bitbucket.org',
'gitlab.com',
'git.sr.ht',
'codeberg.org',
'gitea.com',
].includes(host);
}
export function validateGitURL(event: Message): validateGitURLProps {
if (typeof event.data === 'string' && (event.data).trim().length === 0) {
return {
url: event.data,
error: true,
helpText: 'Please enter a Git URL.'
} as validateGitURLProps
}
try {
const parse = gitUrlParse(event.data);
const isGitRepo = isGitURL(parse.host);
if (!isGitRepo) {
throw 'Invalid Git URL';
}
if (parse.organization !== '' && parse.name !== '') {
return {
url: event.data,
error: false,
helpText: 'The git repo URL is valid.'
} as validateGitURLProps
}
return {
url: event.data,
error: true,
helpText: 'URL is missing organization or repo name.'
} as validateGitURLProps
} catch (e) {
return {
url: event.data,
error: true,
helpText: 'Invalid Git URL.'
} as validateGitURLProps
}
}
export function validateName(value: string): string | null {
let validationMessage = OpenShiftItem.emptyName('Required', value.trim());
if (!validationMessage) {
validationMessage = OpenShiftItem.validateMatches(
'Only lower case alphabets and numeric characters or \'-\', start and ends with only alphabets',
value,
);
}
if (!validationMessage) { validationMessage = OpenShiftItem.lengthName('Should be between 2-63 characters', value, 0); }
return validationMessage;
}