-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource-config.ts
More file actions
152 lines (122 loc) · 4.21 KB
/
resource-config.ts
File metadata and controls
152 lines (122 loc) · 4.21 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
import { ResourceJson, ResourceOs, ResourceConfig as SchemaResourceConfig } from '@codifycli/schemas';
import { deepEqual } from '../utils/index.js';
import { ConfigBlock, ConfigType } from './config.js';
import { ResourceInfo } from './resource-info.js';
/** Resource JSON supported format
* {
* "type": "plugin_name_resource_1",
* "name?": "optional-name"
* "parameter1": {
* "plguin1": "^10.6.2", // From registry
* "https://www.github.com/project": "^10.3.2", // url
* }
* "parameter2": "string",
* "parameter3": 1,
* }
*
* We won't be able to validate the parameters until we get the resource definitions from the plugins
*/
const REFERENCE_REGEX = /\${(?<reference>[\w.]+)}/g
export class ResourceConfig implements ConfigBlock {
readonly configClass = ConfigType.RESOURCE;
raw: SchemaResourceConfig;
type: string;
name?: string;
dependsOn: string[];
os?: ResourceOs[];
sourceMapKey?: string;
// Calculated
dependencyIds: string[] = []; // id of other nodes
parameters: Record<string, unknown>;
resourceInfo?: ResourceInfo;
constructor(config: SchemaResourceConfig, sourceMapKey?: string) {
const { dependsOn, name, type, os, ...parameters } = config;
this.raw = config;
this.type = type;
this.name = name;
this.os = os;
this.parameters = parameters ?? {};
this.dependsOn = dependsOn ?? []
this.sourceMapKey = sourceMapKey;
}
static fromJson(json: ResourceJson): ResourceConfig {
return new ResourceConfig({
...json.core,
...json.parameters,
})
}
get id() {
return this.name ? `${this.type}.${this.name}` : this.type;
}
core(excludeName?: boolean): SchemaResourceConfig {
return {
type: this.type,
...(excludeName || !this.name ? {} : { name: this.name }),
...(this.dependsOn.length > 0 ? { dependsOn: this.dependsOn } : {}),
...(this.os && this.os?.length > 0 ? { os: this.os } : {})
};
}
toJson(): ResourceJson {
return {
core: this.core(),
parameters: this.parameters ?? {},
}
}
isSame(type: string, name?: string): boolean {
const externalId = name ? `${type}.${name}` : type;
return externalId === this.id;
}
isDeepEqual(other?: ResourceConfig | null): boolean {
if (!other) {
return false;
}
return deepEqual(other.parameters, this.parameters)
&& deepEqual({ type: this.type, name: this.name }, { type: other.type, name: other.name });
}
setName(name: string) {
this.name = name;
this.raw.name = name;
}
setParameter(name: string, value: unknown) {
this.parameters[name] = value;
this.raw[name] = value;
}
addDependenciesFromDependsOn(getMatchingResourceIds: (idOrType: string) => string[]) {
for (const idOrType of this.dependsOn) {
const matchingIds = getMatchingResourceIds(idOrType);
if (matchingIds.length === 0) {
throw new Error(`Reference ${idOrType} is not a valid resource`);
}
this.dependencyIds.push(...matchingIds);
}
}
addDependenciesBasedOnParameters(resourceExists: (id: string) => boolean) {
// TODO: Only string dependencies are supported currently
const parametersWithDependencies = Object.entries(this.parameters)
.filter(([, v]) => typeof v === 'string')
.filter(([, v]) => REFERENCE_REGEX.test(v as string));
for (const [, value] of parametersWithDependencies) {
const matchResult = [...(value as string).matchAll(REFERENCE_REGEX)];
if (!matchResult) {
throw new Error('Internal Error: expect dependency match result to not be null');
}
const ids = matchResult.map(([, capturedStr]) => capturedStr)
// Validate that each id exists
for (const id of ids) {
if (!resourceExists(id)) {
throw new Error(`Reference ${id} is not a valid resource`)
}
}
this.dependencyIds.push(...ids);
}
}
addDependencies(dependencies: string[]) {
this.dependencyIds.push(...dependencies);
}
attachResourceInfo(resourceInfo: ResourceInfo) {
if (resourceInfo.type !== this.type) {
throw new Error(`Attempting to attach resource info (${resourceInfo.type}) on an un-related resource (${this.type})`)
}
this.resourceInfo = resourceInfo;
}
}