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
138 lines (128 loc) · 5.15 KB
/
utils.ts
File metadata and controls
138 lines (128 loc) · 5.15 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
/*-----------------------------------------------------------------------------------------------
* 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 validator from 'validator';
import { extensions, Uri, WebviewPanel, WebviewView } from 'vscode';
import * as NameValidator from '../../openshift/nameValidator';
import { ExtensionID } from '../../util/constants';
import { gitUrlParse } from '../../util/gitParse';
import { validateURLProps } from '../common/propertyTypes';
export type Message = {
action: string;
data: any;
};
export async function loadWebviewHtml(webviewName: string, webviewPanel: WebviewPanel | WebviewView, additionalInjections?: Map<string, string>): Promise<string> {
const extensionPath = extensions.getExtension(ExtensionID).extensionPath;
const reactAppRootOnDisk = path.join(extensionPath, 'out', webviewName, 'app');
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 validateURL(event: Message | { command: string; data: object }, isRequired = true): validateURLProps {
if (isRequired && typeof event.data === 'string' && (event.data).trim().length === 0) {
return {
url: event.data,
error: true,
helpText: 'Please enter a URL.'
} as validateURLProps
} else if (!validator.isURL(event.data)) {
return {
url: event.data,
error: true,
helpText: 'Entered URL is invalid'
} as validateURLProps
}
return {
url: event.data,
error: false,
helpText: !isRequired ? '' : 'URL is valid'
} as validateURLProps
}
export function validateGitURL(event: Message): validateURLProps {
if (typeof event.data === 'string' && (event.data).trim().length === 0) {
return {
url: event.data,
error: true,
helpText: 'Please enter a Git URL.'
} as validateURLProps
}
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 validateURLProps
}
return {
url: event.data,
error: true,
helpText: 'URL is missing organization or repo name.'
} as validateURLProps
} catch (e) {
return {
url: event.data,
error: true,
helpText: 'Invalid Git URL.'
} as validateURLProps
}
}
export function validateName(value: string): string | null {
let validationMessage = NameValidator.emptyName('Required', JSON.parse(value) as unknown as string);
if (!validationMessage) {
validationMessage = NameValidator.validateMatches(
'Only lower case alphabets and numeric characters or \'-\', start and ends with only alphabets',
JSON.parse(value) as unknown as string
);
}
if (!validationMessage) { validationMessage = NameValidator.lengthName('Should be between 2-63 characters', value, 0); }
return validationMessage;
}
export function validatePath(value: string): string | null {
return NameValidator.validatePath(
'Given path is not valid',
JSON.parse(value) as unknown as string
);
}