@kapeta/local-cluster-service 0.24.2 → 0.24.3

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 CHANGED
@@ -1,3 +1,10 @@
1
+ ## [0.24.3](https://github.com/kapetacom/local-cluster-service/compare/v0.24.2...v0.24.3) (2023-10-28)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * Handle renaming of local assets ([#92](https://github.com/kapetacom/local-cluster-service/issues/92)) ([2a8d278](https://github.com/kapetacom/local-cluster-service/commit/2a8d2785ae45f548333d6a177ed6b6096fa39c3c))
7
+
1
8
  ## [0.24.2](https://github.com/kapetacom/local-cluster-service/compare/v0.24.1...v0.24.2) (2023-10-27)
2
9
 
3
10
 
@@ -16,6 +16,7 @@ export declare class RepositoryWatcher extends EventEmitter {
16
16
  unwatch(): Promise<void>;
17
17
  private getAssetIdentity;
18
18
  private handleFileChange;
19
+ private getRepositoryPath;
19
20
  private checkForChange;
20
21
  private exists;
21
22
  private removeSymlinkTarget;
@@ -14,6 +14,7 @@ const lodash_1 = __importDefault(require("lodash"));
14
14
  const socketManager_1 = require("./socketManager");
15
15
  const cacheManager_1 = require("./cacheManager");
16
16
  const node_events_1 = require("node:events");
17
+ const assetManager_1 = require("./assetManager");
17
18
  const KAPETA_YML_RX = /^kapeta.ya?ml$/;
18
19
  class RepositoryWatcher extends node_events_1.EventEmitter {
19
20
  watcher;
@@ -134,11 +135,11 @@ class RepositoryWatcher extends node_events_1.EventEmitter {
134
135
  if (!path) {
135
136
  return;
136
137
  }
137
- const assetIdentity = await this.getAssetIdentity(path);
138
- if (!assetIdentity) {
138
+ if (this.disabled) {
139
139
  return;
140
140
  }
141
- if (this.disabled) {
141
+ const assetIdentity = await this.getAssetIdentity(path);
142
+ if (!assetIdentity) {
142
143
  return;
143
144
  }
144
145
  // If this is false it's because we're watching a symlink target
@@ -156,13 +157,31 @@ class RepositoryWatcher extends node_events_1.EventEmitter {
156
157
  await this.updateSymlinkTarget(path);
157
158
  }
158
159
  }
160
+ const repoPath = this.getRepositoryPath(assetIdentity);
161
+ if (eventName === 'change' &&
162
+ !withinRepo &&
163
+ assetIdentity.version === 'local' &&
164
+ node_path_1.default.basename(path) === 'kapeta.yml' &&
165
+ (await this.exists(path)) &&
166
+ !(await this.exists(repoPath))) {
167
+ // This happens when a local asset is renamed
168
+ const oldPath = lodash_1.default.findKey(this.symbolicLinks, (link) => link === path);
169
+ if (oldPath) {
170
+ await fs_extra_1.default.unlink(oldPath);
171
+ await assetManager_1.assetManager.importFile(path);
172
+ return;
173
+ }
174
+ }
159
175
  const sourceOfChange = this.sourceOfChange.get(path) ?? 'filesystem';
160
176
  await this.checkForChange(assetIdentity, sourceOfChange);
161
177
  // We consume the sourceOfChange when the file is changed
162
178
  this.sourceOfChange.delete(path);
163
179
  }
180
+ getRepositoryPath(assetIdentity) {
181
+ return node_path_1.default.join(this.baseDir, assetIdentity.handle, assetIdentity.name, assetIdentity.version);
182
+ }
164
183
  async checkForChange(assetIdentity, sourceOfChange) {
165
- const ymlPath = node_path_1.default.join(this.baseDir, assetIdentity.handle, assetIdentity.name, assetIdentity.version, 'kapeta.yml');
184
+ const ymlPath = node_path_1.default.join(this.getRepositoryPath(assetIdentity), 'kapeta.yml');
166
185
  const newDefinitions = local_cluster_config_1.default.getDefinitions();
167
186
  const newDefinition = newDefinitions.find((d) => d.ymlPath === ymlPath);
168
187
  let currentDefinition = this.allDefinitions.find((d) => d.ymlPath === ymlPath);
@@ -187,7 +206,6 @@ class RepositoryWatcher extends node_events_1.EventEmitter {
187
206
  }
188
207
  else {
189
208
  if (currentDefinition) {
190
- const ref = (0, nodejs_utils_1.parseKapetaUri)(`${currentDefinition.definition.metadata.name}:${currentDefinition.version}`).id;
191
209
  //Something was removed
192
210
  type = 'removed';
193
211
  }
@@ -23,7 +23,7 @@ declare class AssetManager {
23
23
  getBlockInstance(systemId: string, instanceId: string): Promise<BlockInstance>;
24
24
  getAsset(ref: string, noCache?: boolean, autoFetch?: boolean): Promise<EnrichedAsset | undefined>;
25
25
  createAsset(path: string, yaml: BlockDefinition, sourceOfChange?: SourceOfChange): Promise<EnrichedAsset[]>;
26
- updateAsset(ref: string, yaml: BlockDefinition, sourceOfChange?: SourceOfChange): Promise<void>;
26
+ updateAsset(ref: string, yaml: Definition, sourceOfChange?: SourceOfChange): Promise<void>;
27
27
  importFile(filePath: string): Promise<EnrichedAsset[]>;
28
28
  unregisterAsset(ref: string): Promise<void>;
29
29
  installAsset(ref: string, wait?: boolean): Promise<import("./taskManager").Task<void>[] | undefined>;
@@ -1,10 +1,10 @@
1
- import { BlockDefinition } from '@kapeta/schemas';
1
+ import { Definition } from '@kapeta/local-cluster-config';
2
2
  declare class CodeGeneratorManager {
3
3
  private ensureLanguageTargetInRegistry;
4
4
  reload(): Promise<void>;
5
5
  initialize(): Promise<void>;
6
- canGenerateCode(yamlContent: BlockDefinition): Promise<boolean>;
7
- generate(yamlFile: string, yamlContent: BlockDefinition): Promise<void>;
6
+ canGenerateCode(yamlContent: Definition): Promise<boolean>;
7
+ generate(yamlFile: string, yamlContent: Definition): Promise<void>;
8
8
  }
9
9
  export declare const codeGeneratorManager: CodeGeneratorManager;
10
10
  export {};
@@ -16,6 +16,7 @@ export declare class RepositoryWatcher extends EventEmitter {
16
16
  unwatch(): Promise<void>;
17
17
  private getAssetIdentity;
18
18
  private handleFileChange;
19
+ private getRepositoryPath;
19
20
  private checkForChange;
20
21
  private exists;
21
22
  private removeSymlinkTarget;
@@ -14,6 +14,7 @@ const lodash_1 = __importDefault(require("lodash"));
14
14
  const socketManager_1 = require("./socketManager");
15
15
  const cacheManager_1 = require("./cacheManager");
16
16
  const node_events_1 = require("node:events");
17
+ const assetManager_1 = require("./assetManager");
17
18
  const KAPETA_YML_RX = /^kapeta.ya?ml$/;
18
19
  class RepositoryWatcher extends node_events_1.EventEmitter {
19
20
  watcher;
@@ -134,11 +135,11 @@ class RepositoryWatcher extends node_events_1.EventEmitter {
134
135
  if (!path) {
135
136
  return;
136
137
  }
137
- const assetIdentity = await this.getAssetIdentity(path);
138
- if (!assetIdentity) {
138
+ if (this.disabled) {
139
139
  return;
140
140
  }
141
- if (this.disabled) {
141
+ const assetIdentity = await this.getAssetIdentity(path);
142
+ if (!assetIdentity) {
142
143
  return;
143
144
  }
144
145
  // If this is false it's because we're watching a symlink target
@@ -156,13 +157,31 @@ class RepositoryWatcher extends node_events_1.EventEmitter {
156
157
  await this.updateSymlinkTarget(path);
157
158
  }
158
159
  }
160
+ const repoPath = this.getRepositoryPath(assetIdentity);
161
+ if (eventName === 'change' &&
162
+ !withinRepo &&
163
+ assetIdentity.version === 'local' &&
164
+ node_path_1.default.basename(path) === 'kapeta.yml' &&
165
+ (await this.exists(path)) &&
166
+ !(await this.exists(repoPath))) {
167
+ // This happens when a local asset is renamed
168
+ const oldPath = lodash_1.default.findKey(this.symbolicLinks, (link) => link === path);
169
+ if (oldPath) {
170
+ await fs_extra_1.default.unlink(oldPath);
171
+ await assetManager_1.assetManager.importFile(path);
172
+ return;
173
+ }
174
+ }
159
175
  const sourceOfChange = this.sourceOfChange.get(path) ?? 'filesystem';
160
176
  await this.checkForChange(assetIdentity, sourceOfChange);
161
177
  // We consume the sourceOfChange when the file is changed
162
178
  this.sourceOfChange.delete(path);
163
179
  }
180
+ getRepositoryPath(assetIdentity) {
181
+ return node_path_1.default.join(this.baseDir, assetIdentity.handle, assetIdentity.name, assetIdentity.version);
182
+ }
164
183
  async checkForChange(assetIdentity, sourceOfChange) {
165
- const ymlPath = node_path_1.default.join(this.baseDir, assetIdentity.handle, assetIdentity.name, assetIdentity.version, 'kapeta.yml');
184
+ const ymlPath = node_path_1.default.join(this.getRepositoryPath(assetIdentity), 'kapeta.yml');
166
185
  const newDefinitions = local_cluster_config_1.default.getDefinitions();
167
186
  const newDefinition = newDefinitions.find((d) => d.ymlPath === ymlPath);
168
187
  let currentDefinition = this.allDefinitions.find((d) => d.ymlPath === ymlPath);
@@ -187,7 +206,6 @@ class RepositoryWatcher extends node_events_1.EventEmitter {
187
206
  }
188
207
  else {
189
208
  if (currentDefinition) {
190
- const ref = (0, nodejs_utils_1.parseKapetaUri)(`${currentDefinition.definition.metadata.name}:${currentDefinition.version}`).id;
191
209
  //Something was removed
192
210
  type = 'removed';
193
211
  }
@@ -23,7 +23,7 @@ declare class AssetManager {
23
23
  getBlockInstance(systemId: string, instanceId: string): Promise<BlockInstance>;
24
24
  getAsset(ref: string, noCache?: boolean, autoFetch?: boolean): Promise<EnrichedAsset | undefined>;
25
25
  createAsset(path: string, yaml: BlockDefinition, sourceOfChange?: SourceOfChange): Promise<EnrichedAsset[]>;
26
- updateAsset(ref: string, yaml: BlockDefinition, sourceOfChange?: SourceOfChange): Promise<void>;
26
+ updateAsset(ref: string, yaml: Definition, sourceOfChange?: SourceOfChange): Promise<void>;
27
27
  importFile(filePath: string): Promise<EnrichedAsset[]>;
28
28
  unregisterAsset(ref: string): Promise<void>;
29
29
  installAsset(ref: string, wait?: boolean): Promise<import("./taskManager").Task<void>[] | undefined>;
@@ -1,10 +1,10 @@
1
- import { BlockDefinition } from '@kapeta/schemas';
1
+ import { Definition } from '@kapeta/local-cluster-config';
2
2
  declare class CodeGeneratorManager {
3
3
  private ensureLanguageTargetInRegistry;
4
4
  reload(): Promise<void>;
5
5
  initialize(): Promise<void>;
6
- canGenerateCode(yamlContent: BlockDefinition): Promise<boolean>;
7
- generate(yamlFile: string, yamlContent: BlockDefinition): Promise<void>;
6
+ canGenerateCode(yamlContent: Definition): Promise<boolean>;
7
+ generate(yamlFile: string, yamlContent: Definition): Promise<void>;
8
8
  }
9
9
  export declare const codeGeneratorManager: CodeGeneratorManager;
10
10
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kapeta/local-cluster-service",
3
- "version": "0.24.2",
3
+ "version": "0.24.3",
4
4
  "description": "Manages configuration, ports and service discovery for locally running Kapeta systems",
5
5
  "type": "commonjs",
6
6
  "exports": {
@@ -9,6 +9,7 @@ import { socketManager } from './socketManager';
9
9
  import { SourceOfChange, WatchEventName } from './types';
10
10
  import { cacheManager } from './cacheManager';
11
11
  import { EventEmitter } from 'node:events';
12
+ import { assetManager } from './assetManager';
12
13
 
13
14
  interface AssetIdentity {
14
15
  handle: string;
@@ -143,12 +144,12 @@ export class RepositoryWatcher extends EventEmitter {
143
144
  return;
144
145
  }
145
146
 
146
- const assetIdentity = await this.getAssetIdentity(path);
147
- if (!assetIdentity) {
147
+ if (this.disabled) {
148
148
  return;
149
149
  }
150
150
 
151
- if (this.disabled) {
151
+ const assetIdentity = await this.getAssetIdentity(path);
152
+ if (!assetIdentity) {
152
153
  return;
153
154
  }
154
155
 
@@ -170,6 +171,25 @@ export class RepositoryWatcher extends EventEmitter {
170
171
  }
171
172
  }
172
173
 
174
+ const repoPath = this.getRepositoryPath(assetIdentity);
175
+
176
+ if (
177
+ eventName === 'change' &&
178
+ !withinRepo &&
179
+ assetIdentity.version === 'local' &&
180
+ Path.basename(path) === 'kapeta.yml' &&
181
+ (await this.exists(path)) &&
182
+ !(await this.exists(repoPath))
183
+ ) {
184
+ // This happens when a local asset is renamed
185
+ const oldPath = _.findKey(this.symbolicLinks, (link) => link === path);
186
+ if (oldPath) {
187
+ await FS.unlink(oldPath);
188
+ await assetManager.importFile(path);
189
+ return;
190
+ }
191
+ }
192
+
173
193
  const sourceOfChange = this.sourceOfChange.get(path) ?? 'filesystem';
174
194
  await this.checkForChange(assetIdentity, sourceOfChange);
175
195
 
@@ -177,18 +197,17 @@ export class RepositoryWatcher extends EventEmitter {
177
197
  this.sourceOfChange.delete(path);
178
198
  }
179
199
 
200
+ private getRepositoryPath(assetIdentity: AssetIdentity) {
201
+ return Path.join(this.baseDir, assetIdentity.handle, assetIdentity.name, assetIdentity.version);
202
+ }
203
+
180
204
  private async checkForChange(assetIdentity: AssetIdentity, sourceOfChange: SourceOfChange) {
181
- const ymlPath = Path.join(
182
- this.baseDir,
183
- assetIdentity.handle,
184
- assetIdentity.name,
185
- assetIdentity.version,
186
- 'kapeta.yml'
187
- );
205
+ const ymlPath = Path.join(this.getRepositoryPath(assetIdentity), 'kapeta.yml');
188
206
  const newDefinitions = ClusterConfiguration.getDefinitions();
189
207
  const newDefinition = newDefinitions.find((d) => d.ymlPath === ymlPath);
190
208
  let currentDefinition = this.allDefinitions.find((d) => d.ymlPath === ymlPath);
191
209
  const ymlExists = await this.exists(ymlPath);
210
+
192
211
  let type;
193
212
  if (ymlExists) {
194
213
  if (currentDefinition) {
@@ -206,9 +225,6 @@ export class RepositoryWatcher extends EventEmitter {
206
225
  }
207
226
  } else {
208
227
  if (currentDefinition) {
209
- const ref = parseKapetaUri(
210
- `${currentDefinition.definition.metadata.name}:${currentDefinition.version}`
211
- ).id;
212
228
  //Something was removed
213
229
  type = 'removed';
214
230
  } else {
@@ -171,7 +171,7 @@ class AssetManager {
171
171
  return asset;
172
172
  }
173
173
 
174
- async updateAsset(ref: string, yaml: BlockDefinition, sourceOfChange: SourceOfChange = 'filesystem') {
174
+ async updateAsset(ref: string, yaml: Definition, sourceOfChange: SourceOfChange = 'filesystem') {
175
175
  ref = normalizeKapetaUri(ref);
176
176
  const asset = await this.getAsset(ref, true, false);
177
177
  if (!asset) {
@@ -253,7 +253,7 @@ class AssetManager {
253
253
  return await repositoryManager.ensureAsset(uri.handle, uri.name, uri.version, wait);
254
254
  }
255
255
 
256
- private async maybeGenerateCode(ref: string, ymlPath: string, block: BlockDefinition) {
256
+ private async maybeGenerateCode(ref: string, ymlPath: string, block: Definition) {
257
257
  ref = normalizeKapetaUri(ref);
258
258
  if (await codeGeneratorManager.canGenerateCode(block)) {
259
259
  const assetTitle = block.metadata.title ? block.metadata.title : parseKapetaUri(block.metadata.name).name;
@@ -55,7 +55,7 @@ class CodeGeneratorManager {
55
55
  });
56
56
  }
57
57
 
58
- async canGenerateCode(yamlContent: BlockDefinition): Promise<boolean> {
58
+ async canGenerateCode(yamlContent: Definition): Promise<boolean> {
59
59
  if (!yamlContent.spec.target?.kind) {
60
60
  //Not all block types have targets
61
61
  return false;
@@ -68,7 +68,7 @@ class CodeGeneratorManager {
68
68
  return !!(yamlContent && yamlContent.kind && blockTypeKinds.indexOf(yamlContent.kind.toLowerCase()) > -1);
69
69
  }
70
70
 
71
- async generate(yamlFile: string, yamlContent: BlockDefinition) {
71
+ async generate(yamlFile: string, yamlContent: Definition) {
72
72
  if (!yamlContent.spec.target?.kind) {
73
73
  //Not all block types have targets
74
74
  return;
@@ -87,7 +87,7 @@ class CodeGeneratorManager {
87
87
  await this.ensureLanguageTargetInRegistry(targetAsset?.path, targetAsset?.version, targetAsset?.data);
88
88
  const baseDir = Path.dirname(yamlFile);
89
89
  console.log('Generating code for path: %s', baseDir);
90
- const codeGenerator = new BlockCodeGenerator(yamlContent);
90
+ const codeGenerator = new BlockCodeGenerator(yamlContent as BlockDefinition);
91
91
 
92
92
  const output = await codeGenerator.generate();
93
93
  const writer = new CodeWriter(baseDir, {});