-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathcreateComponentHelpers.ts
More file actions
170 lines (159 loc) · 6.04 KB
/
createComponentHelpers.ts
File metadata and controls
170 lines (159 loc) · 6.04 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
/*-----------------------------------------------------------------------------------------------
* 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 * as YAML from 'yaml';
import { Registry } from '../../odo/componentType';
import OpenShiftItem from '../../openshift/openshiftItem';
import { ComponentTypesView } from '../../registriesView';
import { Devfile, DevfileRegistry } from '../common/devfile';
/**
* Represents if something if valid, and if not, why
*/
type ValidationResult = {
/**
* True if the project folder is valid and false otherwise
*/
valid: boolean;
/**
* A message explaining why the project folder is invalid,
* or providing further information in the case that the project folder is valid
*/
message: string;
};
/**
* Returns a ValidationResult indicating whether the project folder is valid.
*
* @param folder the parent folder
* @param componentName the name of the component, which will be used for the child folder
* @returns a ValidationResult indicating whether the project folder is valid.
*/
export async function isValidProjectFolder(
folder: string,
componentName: string,
): Promise<ValidationResult> {
let projectFolderExists = false;
try {
const stats = await fs.stat(folder);
projectFolderExists = stats.isDirectory();
} catch (_) {
// do nothing
}
let projectFolderWritable = false;
if (projectFolderExists) {
try {
await fs.access(folder, fs.constants.W_OK);
projectFolderWritable = true;
} catch (_) {
// do nothing
}
}
const childFolder = path.join(folder, componentName);
let childFolderExists = false;
if (projectFolderExists && projectFolderWritable) {
try {
await fs.access(childFolder);
childFolderExists = true;
} catch (_) {
// do nothing
}
}
let validationMessage: string = undefined;
if (!projectFolderExists) {
validationMessage = `Project folder ${folder} doesn't exist`;
} else if (!projectFolderWritable) {
validationMessage = `Project folder ${folder} is not writable`;
} else if (childFolderExists) {
validationMessage = `There is already a folder ${componentName} in ${folder}`;
} else {
validationMessage = `Project will be created in ${childFolder}`;
}
return {
valid: projectFolderExists && projectFolderWritable && !childFolderExists,
message: validationMessage,
};
}
/**
* Returns the validation message if the component name is invalid, and undefined otherwise.
*
* @param name the component name to validate
* @returns the validation message if the component name is invalid, and undefined otherwise
*/
export function validateComponentName(name: string): string {
let validationMessage = OpenShiftItem.emptyName('Please enter a component name.', name);
if (!validationMessage) {
validationMessage = OpenShiftItem.validateMatches(
`Not a valid component name.
Please use lower case alphanumeric characters or '-', start with an alphabetic character, and end with an alphanumeric character`,
name,
);
}
if (!validationMessage) {
validationMessage = OpenShiftItem.lengthName(
'Component name should be between 2-63 characters',
name,
0,
);
}
return validationMessage;
}
/**
* Returns a list of the devfile registries with their devfiles attached.
*
* @returns a list of the devfile registries with their devfiles attached
*/
export function getDevfileRegistries(): DevfileRegistry[] {
const registries = ComponentTypesView.instance.getListOfRegistries();
if (!registries || registries.length === 0) {
throw new Error('No Devfile registries available. Default registry is missing');
}
const devfileRegistries = registries.map((registry: Registry) => {
return {
devfiles: [],
name: registry.name,
url: registry.url,
} as DevfileRegistry;
});
const components = ComponentTypesView.instance.getCompDescriptions();
for (const component of components) {
const devfileRegistry = devfileRegistries.find(
(devfileRegistry) => devfileRegistry.url === component.registry.url.toString(),
);
devfileRegistry.devfiles.push({
description: component.description,
registryName: devfileRegistry.name,
logoUrl: component.devfileData.devfile.metadata.icon,
name: component.displayName,
id: component.name,
starterProjects: component.devfileData.devfile.starterProjects,
tags: component.tags,
yaml: YAML.stringify(component.devfileData.devfile),
supportsDebug:
Boolean(
component.devfileData.devfile.commands?.find(
(command) => command.exec?.group?.kind === 'debug',
),
) ||
Boolean(
component.devfileData.devfile.commands?.find(
(command) => command.composite?.group?.kind === 'debug',
),
),
supportsDeploy:
Boolean(
component.devfileData.devfile.commands?.find(
(command) => command.exec?.group?.kind === 'deploy',
),
) ||
Boolean(
component.devfileData.devfile.commands?.find(
(command) => command.composite?.group?.kind === 'deploy',
),
),
} as Devfile);
}
devfileRegistries.sort((a, b) => (a.name < b.name ? -1 : 1));
return devfileRegistries;
}