@cyberismo/data-handler 0.0.15 → 0.0.17

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.
Files changed (191) hide show
  1. package/dist/card-metadata-updater.js +7 -1
  2. package/dist/card-metadata-updater.js.map +1 -1
  3. package/dist/command-handler.d.ts +4 -0
  4. package/dist/command-handler.js +22 -8
  5. package/dist/command-handler.js.map +1 -1
  6. package/dist/command-manager.d.ts +24 -1
  7. package/dist/command-manager.js +31 -7
  8. package/dist/command-manager.js.map +1 -1
  9. package/dist/commands/create.d.ts +1 -1
  10. package/dist/commands/create.js +34 -36
  11. package/dist/commands/create.js.map +1 -1
  12. package/dist/commands/export.d.ts +11 -2
  13. package/dist/commands/export.js +54 -41
  14. package/dist/commands/export.js.map +1 -1
  15. package/dist/commands/fetch.d.ts +8 -0
  16. package/dist/commands/fetch.js +101 -23
  17. package/dist/commands/fetch.js.map +1 -1
  18. package/dist/commands/import.d.ts +14 -3
  19. package/dist/commands/import.js +27 -10
  20. package/dist/commands/import.js.map +1 -1
  21. package/dist/commands/move.js +0 -1
  22. package/dist/commands/move.js.map +1 -1
  23. package/dist/commands/remove.d.ts +11 -2
  24. package/dist/commands/remove.js +15 -5
  25. package/dist/commands/remove.js.map +1 -1
  26. package/dist/commands/rename.d.ts +4 -9
  27. package/dist/commands/rename.js +37 -101
  28. package/dist/commands/rename.js.map +1 -1
  29. package/dist/commands/show.d.ts +20 -12
  30. package/dist/commands/show.js +79 -57
  31. package/dist/commands/show.js.map +1 -1
  32. package/dist/commands/transition.d.ts +9 -2
  33. package/dist/commands/transition.js +25 -17
  34. package/dist/commands/transition.js.map +1 -1
  35. package/dist/commands/update.d.ts +16 -12
  36. package/dist/commands/update.js +19 -17
  37. package/dist/commands/update.js.map +1 -1
  38. package/dist/commands/validate.d.ts +17 -9
  39. package/dist/commands/validate.js +94 -35
  40. package/dist/commands/validate.js.map +1 -1
  41. package/dist/containers/card-container.d.ts +7 -5
  42. package/dist/containers/card-container.js +30 -5
  43. package/dist/containers/card-container.js.map +1 -1
  44. package/dist/containers/project/calculation-engine.d.ts +7 -4
  45. package/dist/containers/project/calculation-engine.js +61 -66
  46. package/dist/containers/project/calculation-engine.js.map +1 -1
  47. package/dist/containers/project/project-paths.d.ts +7 -4
  48. package/dist/containers/project/project-paths.js +22 -12
  49. package/dist/containers/project/project-paths.js.map +1 -1
  50. package/dist/containers/project/resource-cache.d.ts +169 -0
  51. package/dist/containers/project/resource-cache.js +509 -0
  52. package/dist/containers/project/resource-cache.js.map +1 -0
  53. package/dist/containers/project/resource-handler.d.ts +129 -0
  54. package/dist/containers/project/resource-handler.js +206 -0
  55. package/dist/containers/project/resource-handler.js.map +1 -0
  56. package/dist/containers/project.d.ts +46 -152
  57. package/dist/containers/project.js +179 -409
  58. package/dist/containers/project.js.map +1 -1
  59. package/dist/containers/template.d.ts +8 -2
  60. package/dist/containers/template.js +24 -19
  61. package/dist/containers/template.js.map +1 -1
  62. package/dist/interfaces/command-options.d.ts +3 -1
  63. package/dist/interfaces/folder-content-interfaces.d.ts +5 -3
  64. package/dist/interfaces/folder-content-interfaces.js +3 -3
  65. package/dist/interfaces/folder-content-interfaces.js.map +1 -1
  66. package/dist/interfaces/project-interfaces.d.ts +7 -9
  67. package/dist/interfaces/project-interfaces.js.map +1 -1
  68. package/dist/interfaces/resource-interfaces.d.ts +14 -1
  69. package/dist/interfaces/resource-interfaces.js.map +1 -1
  70. package/dist/macros/graph/index.js +12 -26
  71. package/dist/macros/graph/index.js.map +1 -1
  72. package/dist/macros/index.d.ts +1 -1
  73. package/dist/macros/index.js +2 -2
  74. package/dist/macros/index.js.map +1 -1
  75. package/dist/macros/report/index.js +3 -6
  76. package/dist/macros/report/index.js.map +1 -1
  77. package/dist/module-manager.d.ts +16 -3
  78. package/dist/module-manager.js +51 -19
  79. package/dist/module-manager.js.map +1 -1
  80. package/dist/project-settings.d.ts +21 -3
  81. package/dist/project-settings.js +91 -14
  82. package/dist/project-settings.js.map +1 -1
  83. package/dist/resources/calculation-resource.d.ts +4 -3
  84. package/dist/resources/calculation-resource.js +11 -5
  85. package/dist/resources/calculation-resource.js.map +1 -1
  86. package/dist/resources/card-type-resource.d.ts +6 -1
  87. package/dist/resources/card-type-resource.js +34 -23
  88. package/dist/resources/card-type-resource.js.map +1 -1
  89. package/dist/resources/create-defaults.d.ts +3 -2
  90. package/dist/resources/create-defaults.js +3 -2
  91. package/dist/resources/create-defaults.js.map +1 -1
  92. package/dist/resources/field-type-resource.d.ts +4 -1
  93. package/dist/resources/field-type-resource.js +22 -23
  94. package/dist/resources/field-type-resource.js.map +1 -1
  95. package/dist/resources/file-resource.d.ts +5 -9
  96. package/dist/resources/file-resource.js +6 -11
  97. package/dist/resources/file-resource.js.map +1 -1
  98. package/dist/resources/folder-resource.d.ts +29 -32
  99. package/dist/resources/folder-resource.js +59 -78
  100. package/dist/resources/folder-resource.js.map +1 -1
  101. package/dist/resources/graph-model-resource.d.ts +4 -1
  102. package/dist/resources/graph-model-resource.js +11 -4
  103. package/dist/resources/graph-model-resource.js.map +1 -1
  104. package/dist/resources/graph-view-resource.d.ts +5 -2
  105. package/dist/resources/graph-view-resource.js +7 -3
  106. package/dist/resources/graph-view-resource.js.map +1 -1
  107. package/dist/resources/link-type-resource.d.ts +5 -2
  108. package/dist/resources/link-type-resource.js +5 -2
  109. package/dist/resources/link-type-resource.js.map +1 -1
  110. package/dist/resources/report-resource.d.ts +6 -7
  111. package/dist/resources/report-resource.js +14 -23
  112. package/dist/resources/report-resource.js.map +1 -1
  113. package/dist/resources/resource-object.d.ts +94 -8
  114. package/dist/resources/resource-object.js +212 -109
  115. package/dist/resources/resource-object.js.map +1 -1
  116. package/dist/resources/template-resource.d.ts +7 -3
  117. package/dist/resources/template-resource.js +10 -3
  118. package/dist/resources/template-resource.js.map +1 -1
  119. package/dist/resources/workflow-resource.d.ts +5 -2
  120. package/dist/resources/workflow-resource.js +18 -22
  121. package/dist/resources/workflow-resource.js.map +1 -1
  122. package/dist/utils/card-utils.d.ts +2 -2
  123. package/dist/utils/card-utils.js +1 -1
  124. package/dist/utils/clingo-fact-builder.d.ts +25 -14
  125. package/dist/utils/clingo-fact-builder.js +27 -5
  126. package/dist/utils/clingo-fact-builder.js.map +1 -1
  127. package/dist/utils/clingo-facts.js +3 -4
  128. package/dist/utils/clingo-facts.js.map +1 -1
  129. package/dist/utils/configuration-logger.d.ts +91 -0
  130. package/dist/utils/configuration-logger.js +151 -0
  131. package/dist/utils/configuration-logger.js.map +1 -0
  132. package/dist/utils/constants.d.ts +1 -1
  133. package/dist/utils/constants.js +5 -3
  134. package/dist/utils/constants.js.map +1 -1
  135. package/dist/utils/resource-utils.d.ts +1 -0
  136. package/dist/utils/resource-utils.js +2 -1
  137. package/dist/utils/resource-utils.js.map +1 -1
  138. package/package.json +9 -9
  139. package/src/card-metadata-updater.ts +6 -2
  140. package/src/command-handler.ts +39 -12
  141. package/src/command-manager.ts +33 -21
  142. package/src/commands/create.ts +43 -78
  143. package/src/commands/export.ts +63 -52
  144. package/src/commands/fetch.ts +143 -34
  145. package/src/commands/import.ts +37 -15
  146. package/src/commands/move.ts +0 -1
  147. package/src/commands/remove.ts +20 -7
  148. package/src/commands/rename.ts +58 -149
  149. package/src/commands/show.ts +123 -80
  150. package/src/commands/transition.ts +26 -28
  151. package/src/commands/update.ts +25 -22
  152. package/src/commands/validate.ts +104 -67
  153. package/src/containers/card-container.ts +37 -5
  154. package/src/containers/project/calculation-engine.ts +61 -93
  155. package/src/containers/project/project-paths.ts +29 -13
  156. package/src/containers/project/resource-cache.ts +651 -0
  157. package/src/containers/project/resource-handler.ts +265 -0
  158. package/src/containers/project.ts +250 -527
  159. package/src/containers/template.ts +28 -23
  160. package/src/interfaces/command-options.ts +3 -1
  161. package/src/interfaces/folder-content-interfaces.ts +7 -6
  162. package/src/interfaces/project-interfaces.ts +12 -11
  163. package/src/interfaces/resource-interfaces.ts +18 -3
  164. package/src/macros/graph/index.ts +26 -47
  165. package/src/macros/index.ts +2 -2
  166. package/src/macros/report/index.ts +3 -9
  167. package/src/module-manager.ts +74 -17
  168. package/src/project-settings.ts +96 -14
  169. package/src/resources/calculation-resource.ts +18 -18
  170. package/src/resources/card-type-resource.ts +50 -50
  171. package/src/resources/create-defaults.ts +3 -2
  172. package/src/resources/field-type-resource.ts +41 -55
  173. package/src/resources/file-resource.ts +10 -36
  174. package/src/resources/folder-resource.ts +69 -120
  175. package/src/resources/graph-model-resource.ts +20 -22
  176. package/src/resources/graph-view-resource.ts +15 -17
  177. package/src/resources/link-type-resource.ts +10 -13
  178. package/src/resources/report-resource.ts +21 -43
  179. package/src/resources/resource-object.ts +263 -149
  180. package/src/resources/template-resource.ts +17 -16
  181. package/src/resources/workflow-resource.ts +25 -44
  182. package/src/utils/card-utils.ts +2 -2
  183. package/src/utils/clingo-fact-builder.ts +28 -16
  184. package/src/utils/clingo-facts.ts +3 -4
  185. package/src/utils/configuration-logger.ts +206 -0
  186. package/src/utils/constants.ts +5 -3
  187. package/src/utils/resource-utils.ts +2 -1
  188. package/dist/containers/project/resource-collector.d.ts +0 -110
  189. package/dist/containers/project/resource-collector.js +0 -344
  190. package/dist/containers/project/resource-collector.js.map +0 -1
  191. package/src/containers/project/resource-collector.ts +0 -404
@@ -0,0 +1,651 @@
1
+ /**
2
+ Cyberismo
3
+ Copyright © Cyberismo Ltd and contributors 2025
4
+
5
+ This program is free software: you can redistribute it and/or modify it under
6
+ the terms of the GNU Affero General Public License version 3 as published by
7
+ the Free Software Foundation. This program is distributed in the hope that it
8
+ will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
9
+ of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
+ See the GNU Affero General Public License for more details.
11
+ You should have received a copy of the GNU Affero General Public
12
+ License along with this program. If not, see <https://www.gnu.org/licenses/>.
13
+ */
14
+
15
+ import { dirname, extname, join } from 'node:path';
16
+ import { type Dirent, readdirSync, readFileSync } from 'node:fs';
17
+
18
+ import { getChildLogger } from '../../utils/log-utils.js';
19
+ import {
20
+ pathToResourceName,
21
+ resourceName,
22
+ resourceNameToString,
23
+ } from '../../utils/resource-utils.js';
24
+ import { stripExtension } from '../../utils/file-utils.js';
25
+ import { VALID_FOLDER_RESOURCE_FILES } from '../../utils/constants.js';
26
+
27
+ import { CalculationResource } from '../../resources/calculation-resource.js';
28
+ import { CardTypeResource } from '../../resources/card-type-resource.js';
29
+ import { FieldTypeResource } from '../../resources/field-type-resource.js';
30
+ import { GraphModelResource } from '../../resources/graph-model-resource.js';
31
+ import { GraphViewResource } from '../../resources/graph-view-resource.js';
32
+ import { LinkTypeResource } from '../../resources/link-type-resource.js';
33
+ import { ReportResource } from '../../resources/report-resource.js';
34
+ import { TemplateResource } from '../../resources/template-resource.js';
35
+ import { WorkflowResource } from '../../resources/workflow-resource.js';
36
+
37
+ import type { Project } from '../project.js';
38
+ import type { ResourceFolderType } from '../../interfaces/project-interfaces.js';
39
+ import type { ResourceName } from '../../utils/resource-utils.js';
40
+
41
+ // Project resource, such as workflow, template or card type as file system object.
42
+ // @todo: Once template constructor has been fixed, no need to export this.
43
+ export interface Resource {
44
+ name: string;
45
+ path: string;
46
+ }
47
+
48
+ // Resource type mappings
49
+ export type ResourceMap = {
50
+ calculations: CalculationResource;
51
+ cardTypes: CardTypeResource;
52
+ fieldTypes: FieldTypeResource;
53
+ graphViews: GraphViewResource;
54
+ graphModels: GraphModelResource;
55
+ linkTypes: LinkTypeResource;
56
+ reports: ReportResource;
57
+ templates: TemplateResource;
58
+ workflows: WorkflowResource;
59
+ };
60
+
61
+ // Helper for SafeExtract.
62
+ export type ExtractResourceType<T extends string> =
63
+ T extends `${string}/${infer R}/${string}` ? R : never;
64
+
65
+ // If type is correct, this always infers type correctly.
66
+ export type SafeExtract<T extends string> =
67
+ ExtractResourceType<T> extends keyof ResourceMap
68
+ ? ExtractResourceType<T>
69
+ : never;
70
+
71
+ // Defines where resources are collected from.
72
+ export enum ResourcesFrom {
73
+ all = 'all',
74
+ importedOnly = 'imported',
75
+ localOnly = 'local',
76
+ }
77
+
78
+ // Resource as stored in the instance cache.
79
+ interface ResourceMetadata {
80
+ name: string;
81
+ type: ResourceFolderType;
82
+ path: string;
83
+ source: 'local' | 'module';
84
+ moduleName?: string;
85
+ contentFiles?: Map<string, string>;
86
+ }
87
+
88
+ // Allowed files in resource instance data.
89
+ const allowedExtensions = ['.lp', '.json'];
90
+
91
+ // Resource types that have internal folders with content files
92
+ const FOLDER_RESOURCE_TYPES: ResourceFolderType[] = [
93
+ 'calculations',
94
+ 'graphModels',
95
+ 'graphViews',
96
+ 'reports',
97
+ 'templates',
98
+ ];
99
+
100
+ /**
101
+ * ResourceCache handles all resource collecting, caching, and management.
102
+ * Uses a two-layered approach:
103
+ * 1. lightweight registry for collecting items that exist on disk
104
+ * 2. more complex instance cache that contains full instance data of a resource.
105
+ *
106
+ * Resource populates the first layer automatically when created.
107
+ * When new instance of a resource is created by an access function (e.g. resourceByType() or resourceByName()),
108
+ * instance of resource is saved to cache.
109
+ *
110
+ */
111
+ export class ResourceCache {
112
+ private resourceRegistry = new Map<string, ResourceMetadata>();
113
+ private instanceCache = new Map<string, unknown>();
114
+
115
+ private project: Project;
116
+
117
+ // Private constructor - use ResourceCache.create() instead.
118
+ private constructor(project: Project) {
119
+ this.project = project;
120
+ }
121
+
122
+ // Initialize the cache by collecting all resources.
123
+ private initialize(): void {
124
+ this.collectAllResources();
125
+ }
126
+
127
+ // Build a full resource name from partial name and type.
128
+ private buildResourceName(name: string, type?: string): ResourceName {
129
+ if (type && name && name.split('/').length === 1) {
130
+ name = `${this.project.projectPrefix}/${type}/${name}`;
131
+ }
132
+ return resourceName(name);
133
+ }
134
+
135
+ // Create a resource object instance
136
+ private createResourceObject(resourceName: ResourceName): unknown {
137
+ const key = resourceNameToString(resourceName);
138
+ const metadata = this.resourceRegistry.get(key);
139
+ let resource: unknown;
140
+
141
+ if (resourceName.type === 'calculations') {
142
+ resource = new CalculationResource(this.project, resourceName);
143
+ } else if (resourceName.type === 'cardTypes') {
144
+ resource = new CardTypeResource(this.project, resourceName);
145
+ } else if (resourceName.type === 'fieldTypes') {
146
+ resource = new FieldTypeResource(this.project, resourceName);
147
+ } else if (resourceName.type === 'graphModels') {
148
+ resource = new GraphModelResource(this.project, resourceName);
149
+ } else if (resourceName.type === 'graphViews') {
150
+ resource = new GraphViewResource(this.project, resourceName);
151
+ } else if (resourceName.type === 'linkTypes') {
152
+ resource = new LinkTypeResource(this.project, resourceName);
153
+ } else if (resourceName.type === 'reports') {
154
+ resource = new ReportResource(this.project, resourceName);
155
+ } else if (resourceName.type === 'templates') {
156
+ resource = new TemplateResource(this.project, resourceName);
157
+ } else if (resourceName.type === 'workflows') {
158
+ resource = new WorkflowResource(this.project, resourceName);
159
+ } else {
160
+ throw new Error(`Unsupported resource type '${resourceName.type}'`);
161
+ }
162
+
163
+ // Populate content files into folder resources
164
+ if (metadata?.contentFiles && this.hasSetContentFiles(resource)) {
165
+ resource.setContentFiles(metadata.contentFiles);
166
+ }
167
+
168
+ return resource;
169
+ }
170
+
171
+ // Collects all resources; both local and modules.
172
+ private collectAllResources() {
173
+ this.collectLocalResources();
174
+ this.collectModuleResources();
175
+ }
176
+
177
+ // Collect all local resources from the filesystem
178
+ private collectLocalResources() {
179
+ const resourceTypes: ResourceFolderType[] = [
180
+ 'calculations',
181
+ 'cardTypes',
182
+ 'fieldTypes',
183
+ 'graphModels',
184
+ 'graphViews',
185
+ 'linkTypes',
186
+ 'reports',
187
+ 'templates',
188
+ 'workflows',
189
+ ];
190
+
191
+ for (const type of resourceTypes) {
192
+ this.collectResourcesOfType(type, 'local');
193
+ }
194
+ }
195
+
196
+ // Collect all module resources from the filesystem
197
+ private collectModuleResources() {
198
+ try {
199
+ const moduleEntries = readdirSync(this.project.paths.modulesFolder, {
200
+ withFileTypes: true,
201
+ });
202
+ const moduleNames = moduleEntries
203
+ .filter((entry) => entry.isDirectory())
204
+ .map((entry) => entry.name);
205
+
206
+ if (moduleNames.length === 0) {
207
+ return;
208
+ }
209
+
210
+ const resourceTypes: ResourceFolderType[] = [
211
+ 'calculations',
212
+ 'cardTypes',
213
+ 'fieldTypes',
214
+ 'graphModels',
215
+ 'graphViews',
216
+ 'linkTypes',
217
+ 'reports',
218
+ 'templates',
219
+ 'workflows',
220
+ ];
221
+
222
+ for (const moduleName of moduleNames) {
223
+ for (const type of resourceTypes) {
224
+ this.collectResourcesOfType(type, 'module', moduleName);
225
+ }
226
+ }
227
+ } catch {
228
+ ResourceCache.logger.debug(
229
+ `.cards/modules folder is missing or inaccessible`,
230
+ );
231
+ }
232
+ }
233
+
234
+ // Collects one folder resource's internal folder content.
235
+ private collectResourceContentFiles(type: ResourceFolderType, entry: Dirent) {
236
+ const identifier = stripExtension(entry.name);
237
+ let contentFiles: Map<string, string> | undefined = undefined;
238
+
239
+ // Set content files for folder resources
240
+ if (FOLDER_RESOURCE_TYPES.includes(type)) {
241
+ const internalFolder = join(entry.parentPath, identifier);
242
+ try {
243
+ const contentEntries = readdirSync(internalFolder, {
244
+ withFileTypes: true,
245
+ });
246
+ const files = new Map<string, string>();
247
+
248
+ for (const contentEntry of contentEntries) {
249
+ if (
250
+ contentEntry.isFile() &&
251
+ VALID_FOLDER_RESOURCE_FILES.includes(contentEntry.name)
252
+ ) {
253
+ try {
254
+ const filePath = join(internalFolder, contentEntry.name);
255
+ const content = readFileSync(filePath, 'utf8');
256
+ files.set(contentEntry.name, content);
257
+ } catch {
258
+ ResourceCache.logger.warn(
259
+ `Failed to read content file '${contentEntry.name}' for resource '${name}'`,
260
+ );
261
+ }
262
+ }
263
+ }
264
+
265
+ contentFiles = files.size > 0 ? files : undefined;
266
+ } catch {
267
+ // Internal folder doesn't exist - this is okay
268
+ }
269
+ }
270
+ return contentFiles;
271
+ }
272
+
273
+ // Collect resources of a specific type
274
+ private collectResourcesOfType(
275
+ type: ResourceFolderType,
276
+ source: 'local' | 'module',
277
+ moduleName?: string,
278
+ ) {
279
+ const resourceFolder =
280
+ source === 'local'
281
+ ? this.project.paths.resourcePath(type)
282
+ : join(this.project.paths.modulesFolder, moduleName!, type);
283
+
284
+ try {
285
+ const entries = readdirSync(resourceFolder, { withFileTypes: true });
286
+
287
+ for (const entry of entries) {
288
+ if (entry.isFile() && allowedExtensions.includes(extname(entry.name))) {
289
+ const name =
290
+ source === 'local'
291
+ ? `${this.project.projectPrefix}/${type}/${stripExtension(entry.name)}`
292
+ : `${moduleName}/${type}/${stripExtension(entry.name)}`;
293
+
294
+ this.resourceRegistry.set(name, {
295
+ name: name,
296
+ type: type,
297
+ path: entry.parentPath,
298
+ source: source,
299
+ moduleName: source === 'module' ? moduleName : undefined,
300
+ contentFiles: this.collectResourceContentFiles(type, entry),
301
+ });
302
+ }
303
+ }
304
+ } catch {
305
+ ResourceCache.logger.warn(
306
+ `Resource folder '${resourceFolder}' is missing`,
307
+ );
308
+ }
309
+ }
310
+
311
+ // Removes a key from cache layers.
312
+ private deleteKey(key: string) {
313
+ this.resourceRegistry.delete(key);
314
+ this.instanceCache.delete(key);
315
+ }
316
+
317
+ // Type guard to check if resource has setContentFiles method
318
+ private hasSetContentFiles(
319
+ resource: unknown,
320
+ ): resource is { setContentFiles: (files: Map<string, string>) => void } {
321
+ return (
322
+ typeof (resource as { setContentFiles?: unknown }).setContentFiles ===
323
+ 'function'
324
+ );
325
+ }
326
+
327
+ // Returns instance of logger.
328
+ private static get logger() {
329
+ return getChildLogger({
330
+ module: 'resourceCache',
331
+ });
332
+ }
333
+
334
+ // Normalize resource name (string or ResourceName) to a consistent string format.
335
+ private normalizeResourceName(name: string | ResourceName): string {
336
+ if (typeof name === 'string') {
337
+ const resName = resourceName(name);
338
+ return resourceNameToString(resName);
339
+ }
340
+ return resourceNameToString(name);
341
+ }
342
+
343
+ /**
344
+ * Add a resource instance to cache. If using
345
+ * @param name Name of the resource to update
346
+ * @param instance New data for the resource.
347
+ */
348
+ public addResource(name: string | ResourceName, instance: unknown) {
349
+ const key = this.normalizeResourceName(name);
350
+ if (!this.resourceRegistry.has(key)) {
351
+ const resName = typeof name === 'string' ? resourceName(name) : name;
352
+ const isModule = resName.prefix !== this.project.projectPrefix;
353
+ const resourcePath = isModule
354
+ ? this.project.paths.moduleResourcePath(
355
+ resName.prefix,
356
+ resName.type as ResourceFolderType,
357
+ )
358
+ : this.project.paths.resourcePath(resName.type as ResourceFolderType);
359
+
360
+ this.resourceRegistry.set(key, {
361
+ name: key,
362
+ type: resName.type as ResourceFolderType,
363
+ path: resourcePath,
364
+ source: isModule ? 'module' : 'local',
365
+ moduleName: isModule ? resName.prefix : undefined,
366
+ contentFiles: undefined, // resources will set this as-needed
367
+ });
368
+
369
+ this.instanceCache.set(key, instance);
370
+ }
371
+ }
372
+
373
+ /**
374
+ * Refresh local resources in the cache.
375
+ */
376
+ public changed() {
377
+ for (const [key, metadata] of this.resourceRegistry) {
378
+ if (metadata.source === 'local') {
379
+ this.resourceRegistry.delete(key);
380
+ }
381
+ }
382
+ this.collectLocalResources();
383
+ }
384
+
385
+ /**
386
+ * Refresh module resources in the cache.
387
+ * @param moduleName Name of the module. If given, will only update this modules resources.
388
+ */
389
+ public changedModules(moduleName?: string) {
390
+ for (const [key, metadata] of this.resourceRegistry) {
391
+ if (
392
+ metadata.source === 'module' &&
393
+ (metadata.moduleName === moduleName || !moduleName)
394
+ ) {
395
+ this.resourceRegistry.delete(key);
396
+ }
397
+ }
398
+ this.collectModuleResources();
399
+ }
400
+
401
+ /**
402
+ * Change resource name in cache, but keep instance information.
403
+ * Cache has to create cache key for new and move the existing instance to it.
404
+ * @param oldName Old name of the resource
405
+ * @param newName New name of the resource
406
+ */
407
+ public changeResourceName(oldName: string, newName: string) {
408
+ const oldKey = this.normalizeResourceName(oldName);
409
+ const newKey = this.normalizeResourceName(newName);
410
+
411
+ // Move instance from old key to new key if it exists
412
+ if (this.instanceCache.has(oldKey)) {
413
+ const resource = this.instanceCache.get(oldKey);
414
+ this.instanceCache.delete(oldKey);
415
+ this.instanceCache.set(newKey, resource);
416
+ }
417
+
418
+ // Update registry
419
+ const metadata = this.resourceRegistry.get(oldKey);
420
+ if (metadata) {
421
+ this.resourceRegistry.delete(oldKey);
422
+ this.resourceRegistry.set(newKey, {
423
+ ...metadata,
424
+ name: newKey,
425
+ });
426
+ }
427
+ }
428
+
429
+ /**
430
+ * Creates and initializes a ResourceCache.
431
+ * This performs filesystem I/O to collect all resources.
432
+ * @param project Project to use
433
+ * @returns Initialized ResourceCache
434
+ */
435
+ public static create(project: Project): ResourceCache {
436
+ const cache = new ResourceCache(project);
437
+ cache.initialize();
438
+ return cache;
439
+ }
440
+
441
+ /**
442
+ * Handle file system changes
443
+ * This is used by the Watcher in the Project class.
444
+ * @param fileName A changed file in the file system.
445
+ */
446
+ public handleFileSystemChange(fileName: string) {
447
+ try {
448
+ const resource = pathToResourceName(this.project, fileName);
449
+ if (!resource) {
450
+ return;
451
+ }
452
+
453
+ const name = resourceNameToString(resource);
454
+
455
+ // Update registry with new path
456
+ const isModule = resource.prefix !== this.project.projectPrefix;
457
+ this.resourceRegistry.set(name, {
458
+ name: name,
459
+ type: resource.type as ResourceFolderType,
460
+ path: dirname(fileName),
461
+ source: isModule ? 'module' : 'local',
462
+ moduleName: isModule ? resource.prefix : undefined,
463
+ contentFiles: undefined,
464
+ });
465
+
466
+ // Invalidate cached instance
467
+ this.invalidateResource(name);
468
+ } catch {
469
+ ResourceCache.logger.warn(`Not a resource file: ${fileName}`);
470
+ }
471
+ }
472
+
473
+ /**
474
+ * Invalidate a resource instance.
475
+ * This forces reload on next access.
476
+ * @param name Name of the resource to invalidate.
477
+ */
478
+ public invalidateResource(name: string | ResourceName) {
479
+ const key = this.normalizeResourceName(name);
480
+
481
+ // Remove from instance cache, but keep in registry
482
+ this.instanceCache.delete(key);
483
+ }
484
+
485
+ /**
486
+ * Get module names.
487
+ * @returns Module names.
488
+ */
489
+ public moduleNames(): string[] {
490
+ const names = new Set<string>();
491
+
492
+ for (const [, metadata] of this.resourceRegistry) {
493
+ if (metadata.source === 'module' && metadata.moduleName) {
494
+ names.add(metadata.moduleName);
495
+ }
496
+ }
497
+
498
+ return Array.from(names);
499
+ }
500
+
501
+ /**
502
+ * Get certain types of resources from a specific module.
503
+ * @param type Type of resource to fetch
504
+ * @param moduleName Name of the module
505
+ * @returns resources names from a specific module.
506
+ */
507
+ public moduleResourceNames(
508
+ type: ResourceFolderType,
509
+ moduleName: string,
510
+ ): string[] {
511
+ const names: string[] = [];
512
+
513
+ for (const [key, metadata] of this.resourceRegistry) {
514
+ if (
515
+ metadata.type === type &&
516
+ metadata.source === 'module' &&
517
+ metadata.moduleName === moduleName
518
+ ) {
519
+ names.push(key);
520
+ }
521
+ }
522
+
523
+ return names;
524
+ }
525
+
526
+ /**
527
+ * Invalidate all resources of a specific module.
528
+ * @param moduleName Name of the module.
529
+ */
530
+ public removeModule(moduleName: string) {
531
+ for (const [key, metadata] of this.resourceRegistry) {
532
+ if (
533
+ metadata &&
534
+ metadata.source === 'module' &&
535
+ metadata.moduleName === moduleName
536
+ ) {
537
+ this.removeResource(key);
538
+ }
539
+ }
540
+ }
541
+
542
+ /**
543
+ * Remove a resource from cache. This includes both registry and instance cache.
544
+ * @param name Resource to remove.
545
+ */
546
+ public removeResource(name: string | ResourceName) {
547
+ const key = this.normalizeResourceName(name);
548
+ if (!this.resourceRegistry.get(key)) {
549
+ return;
550
+ }
551
+ this.deleteKey(key);
552
+ }
553
+
554
+ /**
555
+ * Get resource with explicit type parameter
556
+ * @param name Name of the resource
557
+ * @param type Type of the resource
558
+ * @template T Resource type
559
+ * @throws If resource creation fails.
560
+ * @returns Typed resource that matches name and type.
561
+ */
562
+ public resourceByType<T extends keyof ResourceMap>(
563
+ name: string,
564
+ type: T,
565
+ ): ResourceMap[T] {
566
+ const builtName = this.buildResourceName(name, type);
567
+ const key = resourceNameToString(builtName);
568
+
569
+ if (this.instanceCache.has(key)) {
570
+ return this.instanceCache.get(key) as ResourceMap[T];
571
+ }
572
+
573
+ const resource = this.createResourceObject(builtName);
574
+ if (!resource) {
575
+ throw new Error(`Failed to create resource '${key}'`);
576
+ }
577
+
578
+ if (this.resourceRegistry.has(key)) {
579
+ this.instanceCache.set(key, resource);
580
+ }
581
+
582
+ return resource as ResourceMap[T];
583
+ }
584
+
585
+ /**
586
+ * Get resource by ResourceName object
587
+ * @param name Resource name.
588
+ * @throws If resource creation fails.
589
+ * @returns Typed resource that matches the name.
590
+ */
591
+ public resourceByName<T extends keyof ResourceMap>(
592
+ name: ResourceName,
593
+ ): ResourceMap[T] {
594
+ const key = resourceNameToString(name);
595
+
596
+ if (this.instanceCache.has(key)) {
597
+ return this.instanceCache.get(key) as ResourceMap[T];
598
+ }
599
+
600
+ const resource = this.createResourceObject(name);
601
+ if (!resource) {
602
+ throw new Error(`Failed to create resource '${key}'`);
603
+ }
604
+
605
+ if (this.resourceRegistry.has(key)) {
606
+ this.instanceCache.set(key, resource);
607
+ }
608
+
609
+ return resource as ResourceMap[T];
610
+ }
611
+
612
+ /**
613
+ * Check if a resource exists.
614
+ * @param name Resource name to check.
615
+ * @returns true, if resource is in the cache; false otherwise.
616
+ */
617
+ public has(name: string | ResourceName): boolean {
618
+ const key = this.normalizeResourceName(name);
619
+ return this.resourceRegistry.has(key);
620
+ }
621
+
622
+ /**
623
+ * Get resources with full metadata for a specific type
624
+ * @param type Type of resources to get.
625
+ * @param from Where to return resources from (all, local, imported modules)
626
+ * @template T Resource type
627
+ * @returns Array of resources with metadata.
628
+ */
629
+ public resources<T extends keyof ResourceMap>(
630
+ type: T,
631
+ from: ResourcesFrom = ResourcesFrom.all,
632
+ ): Array<ResourceMap[T]> {
633
+ const resources: ResourceMap[T][] = [];
634
+
635
+ for (const [key, metadata] of this.resourceRegistry) {
636
+ if (metadata.type !== type) continue;
637
+
638
+ if (from === ResourcesFrom.localOnly && metadata.source !== 'local')
639
+ continue;
640
+ if (from === ResourcesFrom.importedOnly && metadata.source !== 'module')
641
+ continue;
642
+
643
+ // Get or create the actual resource instance
644
+ const resName = resourceName(key);
645
+ const resource = this.resourceByName<T>(resName);
646
+ resources.push(resource);
647
+ }
648
+
649
+ return resources;
650
+ }
651
+ }