@kapeta/local-cluster-service 0.5.9 → 0.5.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/package.json +1 -1
- package/src/assetManager.js +29 -30
- package/src/utils/pathTemplateParser.js +42 -23
package/CHANGELOG.md
CHANGED
@@ -1,3 +1,17 @@
|
|
1
|
+
## [0.5.11](https://github.com/kapetacom/local-cluster-service/compare/v0.5.10...v0.5.11) (2023-06-20)
|
2
|
+
|
3
|
+
|
4
|
+
### Bug Fixes
|
5
|
+
|
6
|
+
* Improve path template parser. ([#36](https://github.com/kapetacom/local-cluster-service/issues/36)) ([b655da1](https://github.com/kapetacom/local-cluster-service/commit/b655da1e97740ff2b4fe0f2bcd7f75b04003753e))
|
7
|
+
|
8
|
+
## [0.5.10](https://github.com/kapetacom/local-cluster-service/compare/v0.5.9...v0.5.10) (2023-06-19)
|
9
|
+
|
10
|
+
|
11
|
+
### Bug Fixes
|
12
|
+
|
13
|
+
* delete asset via ref ([6b5a968](https://github.com/kapetacom/local-cluster-service/commit/6b5a968a029753baea3a651a0538468086cda665))
|
14
|
+
|
1
15
|
## [0.5.9](https://github.com/kapetacom/local-cluster-service/compare/v0.5.8...v0.5.9) (2023-06-18)
|
2
16
|
|
3
17
|
|
package/package.json
CHANGED
package/src/assetManager.js
CHANGED
@@ -1,14 +1,14 @@
|
|
1
|
-
const Path = require(
|
1
|
+
const Path = require('node:path');
|
2
2
|
const FS = require('node:fs');
|
3
3
|
const FSExtra = require('fs-extra');
|
4
4
|
const YAML = require('yaml');
|
5
5
|
const ClusterConfiguration = require('@kapeta/local-cluster-config').default;
|
6
|
-
const {Actions} = require('@kapeta/nodejs-registry-utils');
|
6
|
+
const { Actions } = require('@kapeta/nodejs-registry-utils');
|
7
7
|
const codeGeneratorManager = require('./codeGeneratorManager');
|
8
8
|
const progressListener = require('./progressListener');
|
9
|
-
const {parseKapetaUri} = require(
|
10
|
-
const repositoryManager = require(
|
11
|
-
const NodeCache = require(
|
9
|
+
const { parseKapetaUri } = require('@kapeta/nodejs-utils');
|
10
|
+
const repositoryManager = require('./repositoryManager');
|
11
|
+
const NodeCache = require('node-cache');
|
12
12
|
|
13
13
|
function enrichAsset(asset) {
|
14
14
|
return {
|
@@ -19,8 +19,8 @@ function enrichAsset(asset) {
|
|
19
19
|
kind: asset.definition.kind,
|
20
20
|
data: asset.definition,
|
21
21
|
path: asset.path,
|
22
|
-
ymlPath: asset.ymlPath
|
23
|
-
}
|
22
|
+
ymlPath: asset.ymlPath,
|
23
|
+
};
|
24
24
|
}
|
25
25
|
|
26
26
|
function compareRefs(a, b) {
|
@@ -30,29 +30,21 @@ function compareRefs(a, b) {
|
|
30
30
|
return aProtocol === bProtocol && aId === bId;
|
31
31
|
}
|
32
32
|
function parseRef(ref) {
|
33
|
-
let out = ref.split(/:\/\//,2);
|
33
|
+
let out = ref.split(/:\/\//, 2);
|
34
34
|
|
35
35
|
if (out.length === 1) {
|
36
|
-
return [
|
37
|
-
'kapeta',
|
38
|
-
ref.toLowerCase()
|
39
|
-
]
|
36
|
+
return ['kapeta', ref.toLowerCase()];
|
40
37
|
}
|
41
|
-
return [
|
42
|
-
out[0].toLowerCase(),
|
43
|
-
out[1].toLowerCase()
|
44
|
-
];
|
38
|
+
return [out[0].toLowerCase(), out[1].toLowerCase()];
|
45
39
|
}
|
46
40
|
|
47
41
|
class AssetManager {
|
48
|
-
|
49
42
|
constructor() {
|
50
43
|
this.cache = new NodeCache({
|
51
44
|
stdTTL: 60 * 60, // 1 hour
|
52
|
-
})
|
45
|
+
});
|
53
46
|
}
|
54
47
|
|
55
|
-
|
56
48
|
/**
|
57
49
|
*
|
58
50
|
* @param {string[]} [assetKinds]
|
@@ -62,10 +54,10 @@ class AssetManager {
|
|
62
54
|
if (!assetKinds) {
|
63
55
|
const blockTypeProviders = ClusterConfiguration.getDefinitions([
|
64
56
|
'core/block-type',
|
65
|
-
'core/block-type-operator'
|
57
|
+
'core/block-type-operator',
|
66
58
|
]);
|
67
|
-
assetKinds = blockTypeProviders.map(p => {
|
68
|
-
return `${p.definition.metadata.name}:${p.version}
|
59
|
+
assetKinds = blockTypeProviders.map((p) => {
|
60
|
+
return `${p.definition.metadata.name}:${p.version}`;
|
69
61
|
});
|
70
62
|
assetKinds.push('core/plan');
|
71
63
|
}
|
@@ -80,7 +72,6 @@ class AssetManager {
|
|
80
72
|
}
|
81
73
|
|
82
74
|
async getPlan(ref, noCache = false) {
|
83
|
-
|
84
75
|
const asset = await this.getAsset(ref, noCache);
|
85
76
|
|
86
77
|
if ('core/plan' !== asset.kind) {
|
@@ -100,7 +91,7 @@ class AssetManager {
|
|
100
91
|
|
101
92
|
let asset = ClusterConfiguration.getDefinitions()
|
102
93
|
.map(enrichAsset)
|
103
|
-
.find(a => parseKapetaUri(a.ref).equals(uri));
|
94
|
+
.find((a) => parseKapetaUri(a.ref).equals(uri));
|
104
95
|
|
105
96
|
if (!asset) {
|
106
97
|
throw new Error('Asset not found: ' + ref);
|
@@ -149,7 +140,10 @@ class AssetManager {
|
|
149
140
|
if (codeGeneratorManager.canGenerateCode(yaml)) {
|
150
141
|
await codeGeneratorManager.generate(asset.ymlPath, yaml);
|
151
142
|
} else {
|
152
|
-
console.log(
|
143
|
+
console.log(
|
144
|
+
'Could not generate code for %s',
|
145
|
+
yaml.kind ? yaml.kind : 'unknown yaml'
|
146
|
+
);
|
153
147
|
}
|
154
148
|
}
|
155
149
|
|
@@ -162,15 +156,20 @@ class AssetManager {
|
|
162
156
|
throw new Error('File not found: ' + filePath);
|
163
157
|
}
|
164
158
|
|
165
|
-
const assetInfos = YAML.parseAllDocuments(
|
166
|
-
.
|
159
|
+
const assetInfos = YAML.parseAllDocuments(
|
160
|
+
FS.readFileSync(filePath).toString()
|
161
|
+
).map((doc) => doc.toJSON());
|
167
162
|
|
168
163
|
await Actions.link(progressListener, Path.dirname(filePath));
|
169
164
|
|
170
165
|
const version = 'local';
|
171
|
-
const refs = assetInfos.map(
|
166
|
+
const refs = assetInfos.map(
|
167
|
+
(assetInfo) => `kapeta://${assetInfo.metadata.name}:${version}`
|
168
|
+
);
|
172
169
|
this.cache.flushAll();
|
173
|
-
return this.getAssets().filter(a =>
|
170
|
+
return this.getAssets().filter((a) =>
|
171
|
+
refs.some((ref) => compareRefs(ref, a.ref))
|
172
|
+
);
|
174
173
|
}
|
175
174
|
|
176
175
|
async unregisterAsset(ref) {
|
@@ -179,7 +178,7 @@ class AssetManager {
|
|
179
178
|
throw new Error('Asset does not exists: ' + ref);
|
180
179
|
}
|
181
180
|
this.cache.flushAll();
|
182
|
-
await Actions.uninstall(progressListener, asset.
|
181
|
+
await Actions.uninstall(progressListener, [asset.ref]);
|
183
182
|
}
|
184
183
|
}
|
185
184
|
|
@@ -2,6 +2,17 @@
|
|
2
2
|
const TYPE_VARIABLE = 'variable';
|
3
3
|
const TYPE_PATH = 'path';
|
4
4
|
|
5
|
+
/**
|
6
|
+
* A path template is a string that can be used to match a path and extract variables from it.
|
7
|
+
*
|
8
|
+
* E.g. /foo/{bar}/baz
|
9
|
+
*
|
10
|
+
* Would match /foo/123/baz and extract bar=123
|
11
|
+
*
|
12
|
+
* You can also specify a regex for the variable:
|
13
|
+
* /foo/{bar:[0-9]+}/baz
|
14
|
+
*
|
15
|
+
*/
|
5
16
|
class PathTemplate {
|
6
17
|
constructor(pathTemplate) {
|
7
18
|
if (!pathTemplate.startsWith('/')) {
|
@@ -9,33 +20,41 @@ class PathTemplate {
|
|
9
20
|
}
|
10
21
|
this._path = pathTemplate;
|
11
22
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
23
|
+
const variableRegex = /{([^}]+)}/g;
|
24
|
+
let match, offset = 0;
|
25
|
+
this._parts = [];
|
26
|
+
while((match = variableRegex.exec(pathTemplate)) !== null) {
|
27
|
+
if (match.index > offset) {
|
28
|
+
this._parts.push({
|
29
|
+
type: TYPE_PATH,
|
30
|
+
value: pathTemplate.substring(offset, match.index)
|
31
|
+
});
|
32
|
+
}
|
18
33
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
regex = /^[^\/]+/
|
23
|
-
}
|
34
|
+
let regex;
|
35
|
+
let value = match[1];
|
36
|
+
[value, regex] = value.split(/:/, 2);
|
24
37
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
};
|
38
|
+
if (regex) {
|
39
|
+
regex = new RegExp('^' + regex);
|
40
|
+
} else {
|
41
|
+
regex = /^[^\/]+/
|
30
42
|
}
|
31
43
|
|
32
|
-
|
33
|
-
type:
|
34
|
-
value
|
35
|
-
|
36
|
-
|
37
|
-
|
44
|
+
this._parts.push({
|
45
|
+
type: TYPE_VARIABLE,
|
46
|
+
value,
|
47
|
+
regex
|
48
|
+
});
|
49
|
+
offset = match.index + match[0].length;
|
50
|
+
}
|
38
51
|
|
52
|
+
if (offset < pathTemplate.length) {
|
53
|
+
this._parts.push({
|
54
|
+
type: TYPE_PATH,
|
55
|
+
value: pathTemplate.substring(offset)
|
56
|
+
});
|
57
|
+
}
|
39
58
|
}
|
40
59
|
|
41
60
|
get path() {
|
@@ -61,7 +80,7 @@ class PathTemplate {
|
|
61
80
|
return null;
|
62
81
|
}
|
63
82
|
|
64
|
-
path = path.
|
83
|
+
path = path.substring(part.value.length);
|
65
84
|
break;
|
66
85
|
case TYPE_VARIABLE:
|
67
86
|
if (!part.regex.test(path)) {
|