@cyberismo/data-handler 0.0.15 → 0.0.16

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 (173) 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 +19 -3
  5. package/dist/command-handler.js.map +1 -1
  6. package/dist/command-manager.d.ts +24 -1
  7. package/dist/command-manager.js +27 -3
  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/import.d.ts +9 -1
  16. package/dist/commands/import.js +15 -7
  17. package/dist/commands/import.js.map +1 -1
  18. package/dist/commands/move.js +0 -1
  19. package/dist/commands/move.js.map +1 -1
  20. package/dist/commands/remove.d.ts +8 -1
  21. package/dist/commands/remove.js +8 -4
  22. package/dist/commands/remove.js.map +1 -1
  23. package/dist/commands/rename.d.ts +4 -9
  24. package/dist/commands/rename.js +32 -101
  25. package/dist/commands/rename.js.map +1 -1
  26. package/dist/commands/show.d.ts +16 -10
  27. package/dist/commands/show.js +71 -55
  28. package/dist/commands/show.js.map +1 -1
  29. package/dist/commands/transition.d.ts +9 -2
  30. package/dist/commands/transition.js +25 -17
  31. package/dist/commands/transition.js.map +1 -1
  32. package/dist/commands/update.d.ts +16 -12
  33. package/dist/commands/update.js +19 -17
  34. package/dist/commands/update.js.map +1 -1
  35. package/dist/commands/validate.d.ts +17 -9
  36. package/dist/commands/validate.js +96 -35
  37. package/dist/commands/validate.js.map +1 -1
  38. package/dist/containers/project/calculation-engine.d.ts +7 -4
  39. package/dist/containers/project/calculation-engine.js +61 -66
  40. package/dist/containers/project/calculation-engine.js.map +1 -1
  41. package/dist/containers/project/project-paths.d.ts +5 -4
  42. package/dist/containers/project/project-paths.js +16 -12
  43. package/dist/containers/project/project-paths.js.map +1 -1
  44. package/dist/containers/project/resource-cache.d.ts +169 -0
  45. package/dist/containers/project/resource-cache.js +507 -0
  46. package/dist/containers/project/resource-cache.js.map +1 -0
  47. package/dist/containers/project/resource-handler.d.ts +129 -0
  48. package/dist/containers/project/resource-handler.js +206 -0
  49. package/dist/containers/project/resource-handler.js.map +1 -0
  50. package/dist/containers/project.d.ts +38 -153
  51. package/dist/containers/project.js +129 -405
  52. package/dist/containers/project.js.map +1 -1
  53. package/dist/containers/template.d.ts +8 -2
  54. package/dist/containers/template.js +20 -15
  55. package/dist/containers/template.js.map +1 -1
  56. package/dist/interfaces/folder-content-interfaces.d.ts +5 -3
  57. package/dist/interfaces/folder-content-interfaces.js +3 -3
  58. package/dist/interfaces/folder-content-interfaces.js.map +1 -1
  59. package/dist/interfaces/project-interfaces.d.ts +2 -4
  60. package/dist/interfaces/project-interfaces.js.map +1 -1
  61. package/dist/interfaces/resource-interfaces.d.ts +14 -1
  62. package/dist/interfaces/resource-interfaces.js.map +1 -1
  63. package/dist/macros/graph/index.js +12 -26
  64. package/dist/macros/graph/index.js.map +1 -1
  65. package/dist/macros/index.d.ts +1 -1
  66. package/dist/macros/index.js +2 -2
  67. package/dist/macros/index.js.map +1 -1
  68. package/dist/macros/report/index.js +3 -6
  69. package/dist/macros/report/index.js.map +1 -1
  70. package/dist/module-manager.d.ts +16 -3
  71. package/dist/module-manager.js +51 -19
  72. package/dist/module-manager.js.map +1 -1
  73. package/dist/project-settings.d.ts +16 -3
  74. package/dist/project-settings.js +79 -14
  75. package/dist/project-settings.js.map +1 -1
  76. package/dist/resources/calculation-resource.d.ts +4 -3
  77. package/dist/resources/calculation-resource.js +11 -5
  78. package/dist/resources/calculation-resource.js.map +1 -1
  79. package/dist/resources/card-type-resource.d.ts +6 -1
  80. package/dist/resources/card-type-resource.js +34 -23
  81. package/dist/resources/card-type-resource.js.map +1 -1
  82. package/dist/resources/create-defaults.d.ts +3 -2
  83. package/dist/resources/create-defaults.js +3 -2
  84. package/dist/resources/create-defaults.js.map +1 -1
  85. package/dist/resources/field-type-resource.d.ts +4 -1
  86. package/dist/resources/field-type-resource.js +22 -23
  87. package/dist/resources/field-type-resource.js.map +1 -1
  88. package/dist/resources/file-resource.d.ts +5 -9
  89. package/dist/resources/file-resource.js +6 -11
  90. package/dist/resources/file-resource.js.map +1 -1
  91. package/dist/resources/folder-resource.d.ts +29 -32
  92. package/dist/resources/folder-resource.js +59 -78
  93. package/dist/resources/folder-resource.js.map +1 -1
  94. package/dist/resources/graph-model-resource.d.ts +4 -1
  95. package/dist/resources/graph-model-resource.js +11 -4
  96. package/dist/resources/graph-model-resource.js.map +1 -1
  97. package/dist/resources/graph-view-resource.d.ts +5 -2
  98. package/dist/resources/graph-view-resource.js +7 -3
  99. package/dist/resources/graph-view-resource.js.map +1 -1
  100. package/dist/resources/link-type-resource.d.ts +5 -2
  101. package/dist/resources/link-type-resource.js +5 -2
  102. package/dist/resources/link-type-resource.js.map +1 -1
  103. package/dist/resources/report-resource.d.ts +6 -7
  104. package/dist/resources/report-resource.js +14 -23
  105. package/dist/resources/report-resource.js.map +1 -1
  106. package/dist/resources/resource-object.d.ts +93 -8
  107. package/dist/resources/resource-object.js +162 -110
  108. package/dist/resources/resource-object.js.map +1 -1
  109. package/dist/resources/template-resource.d.ts +7 -3
  110. package/dist/resources/template-resource.js +10 -3
  111. package/dist/resources/template-resource.js.map +1 -1
  112. package/dist/resources/workflow-resource.d.ts +5 -2
  113. package/dist/resources/workflow-resource.js +18 -22
  114. package/dist/resources/workflow-resource.js.map +1 -1
  115. package/dist/utils/card-utils.d.ts +2 -2
  116. package/dist/utils/card-utils.js +1 -1
  117. package/dist/utils/clingo-fact-builder.d.ts +25 -14
  118. package/dist/utils/clingo-fact-builder.js +27 -5
  119. package/dist/utils/clingo-fact-builder.js.map +1 -1
  120. package/dist/utils/clingo-facts.js +3 -4
  121. package/dist/utils/clingo-facts.js.map +1 -1
  122. package/dist/utils/resource-utils.d.ts +1 -0
  123. package/dist/utils/resource-utils.js +2 -1
  124. package/dist/utils/resource-utils.js.map +1 -1
  125. package/package.json +8 -8
  126. package/src/card-metadata-updater.ts +6 -2
  127. package/src/command-handler.ts +24 -5
  128. package/src/command-manager.ts +29 -17
  129. package/src/commands/create.ts +43 -78
  130. package/src/commands/export.ts +63 -52
  131. package/src/commands/import.ts +24 -14
  132. package/src/commands/move.ts +0 -1
  133. package/src/commands/remove.ts +11 -7
  134. package/src/commands/rename.ts +43 -149
  135. package/src/commands/show.ts +113 -78
  136. package/src/commands/transition.ts +26 -28
  137. package/src/commands/update.ts +25 -22
  138. package/src/commands/validate.ts +108 -67
  139. package/src/containers/project/calculation-engine.ts +61 -93
  140. package/src/containers/project/project-paths.ts +21 -13
  141. package/src/containers/project/resource-cache.ts +648 -0
  142. package/src/containers/project/resource-handler.ts +265 -0
  143. package/src/containers/project.ts +178 -522
  144. package/src/containers/template.ts +24 -19
  145. package/src/interfaces/folder-content-interfaces.ts +7 -6
  146. package/src/interfaces/project-interfaces.ts +7 -6
  147. package/src/interfaces/resource-interfaces.ts +18 -3
  148. package/src/macros/graph/index.ts +26 -47
  149. package/src/macros/index.ts +2 -2
  150. package/src/macros/report/index.ts +3 -9
  151. package/src/module-manager.ts +74 -17
  152. package/src/project-settings.ts +83 -14
  153. package/src/resources/calculation-resource.ts +18 -18
  154. package/src/resources/card-type-resource.ts +50 -50
  155. package/src/resources/create-defaults.ts +3 -2
  156. package/src/resources/field-type-resource.ts +41 -55
  157. package/src/resources/file-resource.ts +10 -36
  158. package/src/resources/folder-resource.ts +69 -120
  159. package/src/resources/graph-model-resource.ts +20 -22
  160. package/src/resources/graph-view-resource.ts +15 -17
  161. package/src/resources/link-type-resource.ts +10 -13
  162. package/src/resources/report-resource.ts +21 -43
  163. package/src/resources/resource-object.ts +194 -152
  164. package/src/resources/template-resource.ts +17 -16
  165. package/src/resources/workflow-resource.ts +25 -44
  166. package/src/utils/card-utils.ts +2 -2
  167. package/src/utils/clingo-fact-builder.ts +28 -16
  168. package/src/utils/clingo-facts.ts +3 -4
  169. package/src/utils/resource-utils.ts +2 -1
  170. package/dist/containers/project/resource-collector.d.ts +0 -110
  171. package/dist/containers/project/resource-collector.js +0 -344
  172. package/dist/containers/project/resource-collector.js.map +0 -1
  173. package/src/containers/project/resource-collector.ts +0 -404
@@ -0,0 +1,169 @@
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
+ import { CalculationResource } from '../../resources/calculation-resource.js';
15
+ import { CardTypeResource } from '../../resources/card-type-resource.js';
16
+ import { FieldTypeResource } from '../../resources/field-type-resource.js';
17
+ import { GraphModelResource } from '../../resources/graph-model-resource.js';
18
+ import { GraphViewResource } from '../../resources/graph-view-resource.js';
19
+ import { LinkTypeResource } from '../../resources/link-type-resource.js';
20
+ import { ReportResource } from '../../resources/report-resource.js';
21
+ import { TemplateResource } from '../../resources/template-resource.js';
22
+ import { WorkflowResource } from '../../resources/workflow-resource.js';
23
+ import type { Project } from '../project.js';
24
+ import type { ResourceFolderType } from '../../interfaces/project-interfaces.js';
25
+ import type { ResourceName } from '../../utils/resource-utils.js';
26
+ export interface Resource {
27
+ name: string;
28
+ path: string;
29
+ }
30
+ export type ResourceMap = {
31
+ calculations: CalculationResource;
32
+ cardTypes: CardTypeResource;
33
+ fieldTypes: FieldTypeResource;
34
+ graphViews: GraphViewResource;
35
+ graphModels: GraphModelResource;
36
+ linkTypes: LinkTypeResource;
37
+ reports: ReportResource;
38
+ templates: TemplateResource;
39
+ workflows: WorkflowResource;
40
+ };
41
+ export type ExtractResourceType<T extends string> = T extends `${string}/${infer R}/${string}` ? R : never;
42
+ export type SafeExtract<T extends string> = ExtractResourceType<T> extends keyof ResourceMap ? ExtractResourceType<T> : never;
43
+ export declare enum ResourcesFrom {
44
+ all = "all",
45
+ importedOnly = "imported",
46
+ localOnly = "local"
47
+ }
48
+ /**
49
+ * ResourceCache handles all resource collecting, caching, and management.
50
+ * Uses a two-layered approach:
51
+ * 1. lightweight registry for collecting items that exist on disk
52
+ * 2. more complex instance cache that contains full instance data of a resource.
53
+ *
54
+ * Resource populates the first layer automatically when created.
55
+ * When new instance of a resource is created by an access function (e.g. resourceByType() or resourceByName()),
56
+ * instance of resource is saved to cache.
57
+ *
58
+ */
59
+ export declare class ResourceCache {
60
+ private resourceRegistry;
61
+ private instanceCache;
62
+ private project;
63
+ private constructor();
64
+ private initialize;
65
+ private buildResourceName;
66
+ private createResourceObject;
67
+ private collectAllResources;
68
+ private collectLocalResources;
69
+ private collectModuleResources;
70
+ private collectResourceContentFiles;
71
+ private collectResourcesOfType;
72
+ private deleteKey;
73
+ private hasSetContentFiles;
74
+ private static get logger();
75
+ private normalizeResourceName;
76
+ /**
77
+ * Add a resource instance to cache. If using
78
+ * @param name Name of the resource to update
79
+ * @param instance New data for the resource.
80
+ */
81
+ addResource(name: string | ResourceName, instance: unknown): void;
82
+ /**
83
+ * Refresh local resources in the cache.
84
+ */
85
+ changed(): void;
86
+ /**
87
+ * Refresh module resources in the cache.
88
+ * @param moduleName Name of the module. If given, will only update this modules resources.
89
+ */
90
+ changedModules(moduleName?: string): void;
91
+ /**
92
+ * Change resource name in cache, but keep instance information.
93
+ * Cache has to create cache key for new and move the existing instance to it.
94
+ * @param oldName Old name of the resource
95
+ * @param newName New name of the resource
96
+ */
97
+ changeResourceName(oldName: string, newName: string): void;
98
+ /**
99
+ * Creates and initializes a ResourceCache.
100
+ * This performs filesystem I/O to collect all resources.
101
+ * @param project Project to use
102
+ * @returns Initialized ResourceCache
103
+ */
104
+ static create(project: Project): ResourceCache;
105
+ /**
106
+ * Handle file system changes
107
+ * This is used by the Watcher in the Project class.
108
+ * @param fileName A changed file in the file system.
109
+ */
110
+ handleFileSystemChange(fileName: string): void;
111
+ /**
112
+ * Invalidate a resource instance.
113
+ * This forces reload on next access.
114
+ * @param name Name of the resource to invalidate.
115
+ */
116
+ invalidateResource(name: string | ResourceName): void;
117
+ /**
118
+ * Get module names.
119
+ * @returns Module names.
120
+ */
121
+ moduleNames(): string[];
122
+ /**
123
+ * Get certain types of resources from a specific module.
124
+ * @param type Type of resource to fetch
125
+ * @param moduleName Name of the module
126
+ * @returns resources names from a specific module.
127
+ */
128
+ moduleResourceNames(type: ResourceFolderType, moduleName: string): string[];
129
+ /**
130
+ * Invalidate all resources of a specific module.
131
+ * @param moduleName Name of the module.
132
+ */
133
+ removeModule(moduleName: string): void;
134
+ /**
135
+ * Remove a resource from cache. This includes both registry and instance cache.
136
+ * @param name Resource to remove.
137
+ */
138
+ removeResource(name: string | ResourceName): void;
139
+ /**
140
+ * Get resource with explicit type parameter
141
+ * @param name Name of the resource
142
+ * @param type Type of the resource
143
+ * @template T Resource type
144
+ * @throws If resource creation fails.
145
+ * @returns Typed resource that matches name and type.
146
+ */
147
+ resourceByType<T extends keyof ResourceMap>(name: string, type: T): ResourceMap[T];
148
+ /**
149
+ * Get resource by ResourceName object
150
+ * @param name Resource name.
151
+ * @throws If resource creation fails.
152
+ * @returns Typed resource that matches the name.
153
+ */
154
+ resourceByName<T extends keyof ResourceMap>(name: ResourceName): ResourceMap[T];
155
+ /**
156
+ * Check if a resource exists.
157
+ * @param name Resource name to check.
158
+ * @returns true, if resource is in the cache; false otherwise.
159
+ */
160
+ has(name: string | ResourceName): boolean;
161
+ /**
162
+ * Get resources with full metadata for a specific type
163
+ * @param type Type of resources to get.
164
+ * @param from Where to return resources from (all, local, imported modules)
165
+ * @template T Resource type
166
+ * @returns Array of resources with metadata.
167
+ */
168
+ resources<T extends keyof ResourceMap>(type: T, from?: ResourcesFrom): Array<ResourceMap[T]>;
169
+ }
@@ -0,0 +1,507 @@
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
+ import { dirname, extname, join } from 'node:path';
15
+ import { readdirSync, readFileSync } from 'node:fs';
16
+ import { getChildLogger } from '../../utils/log-utils.js';
17
+ import { pathToResourceName, resourceName, resourceNameToString, } from '../../utils/resource-utils.js';
18
+ import { stripExtension } from '../../utils/file-utils.js';
19
+ import { VALID_FOLDER_RESOURCE_FILES } from '../../utils/constants.js';
20
+ import { CalculationResource } from '../../resources/calculation-resource.js';
21
+ import { CardTypeResource } from '../../resources/card-type-resource.js';
22
+ import { FieldTypeResource } from '../../resources/field-type-resource.js';
23
+ import { GraphModelResource } from '../../resources/graph-model-resource.js';
24
+ import { GraphViewResource } from '../../resources/graph-view-resource.js';
25
+ import { LinkTypeResource } from '../../resources/link-type-resource.js';
26
+ import { ReportResource } from '../../resources/report-resource.js';
27
+ import { TemplateResource } from '../../resources/template-resource.js';
28
+ import { WorkflowResource } from '../../resources/workflow-resource.js';
29
+ // Defines where resources are collected from.
30
+ export var ResourcesFrom;
31
+ (function (ResourcesFrom) {
32
+ ResourcesFrom["all"] = "all";
33
+ ResourcesFrom["importedOnly"] = "imported";
34
+ ResourcesFrom["localOnly"] = "local";
35
+ })(ResourcesFrom || (ResourcesFrom = {}));
36
+ // Allowed files in resource instance data.
37
+ const allowedExtensions = ['.lp', '.json'];
38
+ // Resource types that have internal folders with content files
39
+ const FOLDER_RESOURCE_TYPES = [
40
+ 'calculations',
41
+ 'graphModels',
42
+ 'graphViews',
43
+ 'reports',
44
+ 'templates',
45
+ ];
46
+ /**
47
+ * ResourceCache handles all resource collecting, caching, and management.
48
+ * Uses a two-layered approach:
49
+ * 1. lightweight registry for collecting items that exist on disk
50
+ * 2. more complex instance cache that contains full instance data of a resource.
51
+ *
52
+ * Resource populates the first layer automatically when created.
53
+ * When new instance of a resource is created by an access function (e.g. resourceByType() or resourceByName()),
54
+ * instance of resource is saved to cache.
55
+ *
56
+ */
57
+ export class ResourceCache {
58
+ resourceRegistry = new Map();
59
+ instanceCache = new Map();
60
+ project;
61
+ // Private constructor - use ResourceCache.create() instead.
62
+ constructor(project) {
63
+ this.project = project;
64
+ }
65
+ // Initialize the cache by collecting all resources.
66
+ initialize() {
67
+ this.collectAllResources();
68
+ }
69
+ // Build a full resource name from partial name and type.
70
+ buildResourceName(name, type) {
71
+ if (type && name && name.split('/').length === 1) {
72
+ name = `${this.project.projectPrefix}/${type}/${name}`;
73
+ }
74
+ return resourceName(name);
75
+ }
76
+ // Create a resource object instance
77
+ createResourceObject(resourceName) {
78
+ const key = resourceNameToString(resourceName);
79
+ const metadata = this.resourceRegistry.get(key);
80
+ let resource;
81
+ if (resourceName.type === 'calculations') {
82
+ resource = new CalculationResource(this.project, resourceName);
83
+ }
84
+ else if (resourceName.type === 'cardTypes') {
85
+ resource = new CardTypeResource(this.project, resourceName);
86
+ }
87
+ else if (resourceName.type === 'fieldTypes') {
88
+ resource = new FieldTypeResource(this.project, resourceName);
89
+ }
90
+ else if (resourceName.type === 'graphModels') {
91
+ resource = new GraphModelResource(this.project, resourceName);
92
+ }
93
+ else if (resourceName.type === 'graphViews') {
94
+ resource = new GraphViewResource(this.project, resourceName);
95
+ }
96
+ else if (resourceName.type === 'linkTypes') {
97
+ resource = new LinkTypeResource(this.project, resourceName);
98
+ }
99
+ else if (resourceName.type === 'reports') {
100
+ resource = new ReportResource(this.project, resourceName);
101
+ }
102
+ else if (resourceName.type === 'templates') {
103
+ resource = new TemplateResource(this.project, resourceName);
104
+ }
105
+ else if (resourceName.type === 'workflows') {
106
+ resource = new WorkflowResource(this.project, resourceName);
107
+ }
108
+ else {
109
+ throw new Error(`Unsupported resource type '${resourceName.type}'`);
110
+ }
111
+ // Populate content files into folder resources
112
+ if (metadata?.contentFiles && this.hasSetContentFiles(resource)) {
113
+ resource.setContentFiles(metadata.contentFiles);
114
+ }
115
+ return resource;
116
+ }
117
+ // Collects all resources; both local and modules.
118
+ collectAllResources() {
119
+ this.collectLocalResources();
120
+ this.collectModuleResources();
121
+ }
122
+ // Collect all local resources from the filesystem
123
+ collectLocalResources() {
124
+ const resourceTypes = [
125
+ 'calculations',
126
+ 'cardTypes',
127
+ 'fieldTypes',
128
+ 'graphModels',
129
+ 'graphViews',
130
+ 'linkTypes',
131
+ 'reports',
132
+ 'templates',
133
+ 'workflows',
134
+ ];
135
+ for (const type of resourceTypes) {
136
+ this.collectResourcesOfType(type, 'local');
137
+ }
138
+ }
139
+ // Collect all module resources from the filesystem
140
+ // Only collects modules that are registered in the project configuration
141
+ // todo: For future:
142
+ // Should it also try to collect what is in .local/modules and then log for disparities?
143
+ collectModuleResources() {
144
+ try {
145
+ const registeredModules = this.project.configuration.modules.map((m) => m.name);
146
+ if (registeredModules.length === 0) {
147
+ return;
148
+ }
149
+ const resourceTypes = [
150
+ 'calculations',
151
+ 'cardTypes',
152
+ 'fieldTypes',
153
+ 'graphModels',
154
+ 'graphViews',
155
+ 'linkTypes',
156
+ 'reports',
157
+ 'templates',
158
+ 'workflows',
159
+ ];
160
+ for (const moduleName of registeredModules) {
161
+ for (const type of resourceTypes) {
162
+ this.collectResourcesOfType(type, 'module', moduleName);
163
+ }
164
+ }
165
+ }
166
+ catch {
167
+ ResourceCache.logger.warn(`.cards/modules folder is missing`);
168
+ }
169
+ }
170
+ // Collects one folder resource's internal folder content.
171
+ collectResourceContentFiles(type, entry) {
172
+ const identifier = stripExtension(entry.name);
173
+ let contentFiles = undefined;
174
+ // Set content files for folder resources
175
+ if (FOLDER_RESOURCE_TYPES.includes(type)) {
176
+ const internalFolder = join(entry.parentPath, identifier);
177
+ try {
178
+ const contentEntries = readdirSync(internalFolder, {
179
+ withFileTypes: true,
180
+ });
181
+ const files = new Map();
182
+ for (const contentEntry of contentEntries) {
183
+ if (contentEntry.isFile() &&
184
+ VALID_FOLDER_RESOURCE_FILES.includes(contentEntry.name)) {
185
+ try {
186
+ const filePath = join(internalFolder, contentEntry.name);
187
+ const content = readFileSync(filePath, 'utf8');
188
+ files.set(contentEntry.name, content);
189
+ }
190
+ catch {
191
+ ResourceCache.logger.warn(`Failed to read content file '${contentEntry.name}' for resource '${name}'`);
192
+ }
193
+ }
194
+ }
195
+ contentFiles = files.size > 0 ? files : undefined;
196
+ }
197
+ catch {
198
+ // Internal folder doesn't exist - this is okay
199
+ }
200
+ }
201
+ return contentFiles;
202
+ }
203
+ // Collect resources of a specific type
204
+ collectResourcesOfType(type, source, moduleName) {
205
+ const resourceFolder = source === 'local'
206
+ ? this.project.paths.resourcePath(type)
207
+ : join(this.project.paths.modulesFolder, moduleName, type);
208
+ try {
209
+ const entries = readdirSync(resourceFolder, { withFileTypes: true });
210
+ for (const entry of entries) {
211
+ if (entry.isFile() && allowedExtensions.includes(extname(entry.name))) {
212
+ const name = source === 'local'
213
+ ? `${this.project.projectPrefix}/${type}/${stripExtension(entry.name)}`
214
+ : `${moduleName}/${type}/${stripExtension(entry.name)}`;
215
+ this.resourceRegistry.set(name, {
216
+ name: name,
217
+ type: type,
218
+ path: entry.parentPath,
219
+ source: source,
220
+ moduleName: source === 'module' ? moduleName : undefined,
221
+ contentFiles: this.collectResourceContentFiles(type, entry),
222
+ });
223
+ }
224
+ }
225
+ }
226
+ catch {
227
+ ResourceCache.logger.warn(`Resource folder '${resourceFolder}' is missing`);
228
+ }
229
+ }
230
+ // Removes a key from cache layers.
231
+ deleteKey(key) {
232
+ this.resourceRegistry.delete(key);
233
+ this.instanceCache.delete(key);
234
+ }
235
+ // Type guard to check if resource has setContentFiles method
236
+ hasSetContentFiles(resource) {
237
+ return (typeof resource.setContentFiles ===
238
+ 'function');
239
+ }
240
+ // Returns instance of logger.
241
+ static get logger() {
242
+ return getChildLogger({
243
+ module: 'resourceCache',
244
+ });
245
+ }
246
+ // Normalize resource name (string or ResourceName) to a consistent string format.
247
+ normalizeResourceName(name) {
248
+ if (typeof name === 'string') {
249
+ const resName = resourceName(name);
250
+ return resourceNameToString(resName);
251
+ }
252
+ return resourceNameToString(name);
253
+ }
254
+ /**
255
+ * Add a resource instance to cache. If using
256
+ * @param name Name of the resource to update
257
+ * @param instance New data for the resource.
258
+ */
259
+ addResource(name, instance) {
260
+ const key = this.normalizeResourceName(name);
261
+ if (!this.resourceRegistry.has(key)) {
262
+ const resName = typeof name === 'string' ? resourceName(name) : name;
263
+ const isModule = resName.prefix !== this.project.projectPrefix;
264
+ const resourcePath = isModule
265
+ ? this.project.paths.moduleResourcePath(resName.prefix, resName.type)
266
+ : this.project.paths.resourcePath(resName.type);
267
+ this.resourceRegistry.set(key, {
268
+ name: key,
269
+ type: resName.type,
270
+ path: resourcePath,
271
+ source: isModule ? 'module' : 'local',
272
+ moduleName: isModule ? resName.prefix : undefined,
273
+ contentFiles: undefined, // resources will set this as-needed
274
+ });
275
+ this.instanceCache.set(key, instance);
276
+ }
277
+ }
278
+ /**
279
+ * Refresh local resources in the cache.
280
+ */
281
+ changed() {
282
+ for (const [key, metadata] of this.resourceRegistry) {
283
+ if (metadata.source === 'local') {
284
+ this.resourceRegistry.delete(key);
285
+ }
286
+ }
287
+ this.collectLocalResources();
288
+ }
289
+ /**
290
+ * Refresh module resources in the cache.
291
+ * @param moduleName Name of the module. If given, will only update this modules resources.
292
+ */
293
+ changedModules(moduleName) {
294
+ for (const [key, metadata] of this.resourceRegistry) {
295
+ if (metadata.source === 'module' &&
296
+ (metadata.moduleName === moduleName || !moduleName)) {
297
+ this.resourceRegistry.delete(key);
298
+ }
299
+ }
300
+ this.collectModuleResources();
301
+ }
302
+ /**
303
+ * Change resource name in cache, but keep instance information.
304
+ * Cache has to create cache key for new and move the existing instance to it.
305
+ * @param oldName Old name of the resource
306
+ * @param newName New name of the resource
307
+ */
308
+ changeResourceName(oldName, newName) {
309
+ const oldKey = this.normalizeResourceName(oldName);
310
+ const newKey = this.normalizeResourceName(newName);
311
+ // Move instance from old key to new key if it exists
312
+ if (this.instanceCache.has(oldKey)) {
313
+ const resource = this.instanceCache.get(oldKey);
314
+ this.instanceCache.delete(oldKey);
315
+ this.instanceCache.set(newKey, resource);
316
+ }
317
+ // Update registry
318
+ const metadata = this.resourceRegistry.get(oldKey);
319
+ if (metadata) {
320
+ this.resourceRegistry.delete(oldKey);
321
+ this.resourceRegistry.set(newKey, {
322
+ ...metadata,
323
+ name: newKey,
324
+ });
325
+ }
326
+ }
327
+ /**
328
+ * Creates and initializes a ResourceCache.
329
+ * This performs filesystem I/O to collect all resources.
330
+ * @param project Project to use
331
+ * @returns Initialized ResourceCache
332
+ */
333
+ static create(project) {
334
+ const cache = new ResourceCache(project);
335
+ cache.initialize();
336
+ return cache;
337
+ }
338
+ /**
339
+ * Handle file system changes
340
+ * This is used by the Watcher in the Project class.
341
+ * @param fileName A changed file in the file system.
342
+ */
343
+ handleFileSystemChange(fileName) {
344
+ try {
345
+ const resource = pathToResourceName(this.project, fileName);
346
+ if (!resource) {
347
+ return;
348
+ }
349
+ const name = resourceNameToString(resource);
350
+ // Update registry with new path
351
+ const isModule = resource.prefix !== this.project.projectPrefix;
352
+ this.resourceRegistry.set(name, {
353
+ name: name,
354
+ type: resource.type,
355
+ path: dirname(fileName),
356
+ source: isModule ? 'module' : 'local',
357
+ moduleName: isModule ? resource.prefix : undefined,
358
+ contentFiles: undefined,
359
+ });
360
+ // Invalidate cached instance
361
+ this.invalidateResource(name);
362
+ }
363
+ catch {
364
+ ResourceCache.logger.warn(`Not a resource file: ${fileName}`);
365
+ }
366
+ }
367
+ /**
368
+ * Invalidate a resource instance.
369
+ * This forces reload on next access.
370
+ * @param name Name of the resource to invalidate.
371
+ */
372
+ invalidateResource(name) {
373
+ const key = this.normalizeResourceName(name);
374
+ // Remove from instance cache, but keep in registry
375
+ this.instanceCache.delete(key);
376
+ }
377
+ /**
378
+ * Get module names.
379
+ * @returns Module names.
380
+ */
381
+ moduleNames() {
382
+ const names = new Set();
383
+ for (const [, metadata] of this.resourceRegistry) {
384
+ if (metadata.source === 'module' && metadata.moduleName) {
385
+ names.add(metadata.moduleName);
386
+ }
387
+ }
388
+ return Array.from(names);
389
+ }
390
+ /**
391
+ * Get certain types of resources from a specific module.
392
+ * @param type Type of resource to fetch
393
+ * @param moduleName Name of the module
394
+ * @returns resources names from a specific module.
395
+ */
396
+ moduleResourceNames(type, moduleName) {
397
+ const names = [];
398
+ for (const [key, metadata] of this.resourceRegistry) {
399
+ if (metadata.type === type &&
400
+ metadata.source === 'module' &&
401
+ metadata.moduleName === moduleName) {
402
+ names.push(key);
403
+ }
404
+ }
405
+ return names;
406
+ }
407
+ /**
408
+ * Invalidate all resources of a specific module.
409
+ * @param moduleName Name of the module.
410
+ */
411
+ removeModule(moduleName) {
412
+ for (const [key, metadata] of this.resourceRegistry) {
413
+ if (metadata &&
414
+ metadata.source === 'module' &&
415
+ metadata.moduleName === moduleName) {
416
+ this.removeResource(key);
417
+ }
418
+ }
419
+ }
420
+ /**
421
+ * Remove a resource from cache. This includes both registry and instance cache.
422
+ * @param name Resource to remove.
423
+ */
424
+ removeResource(name) {
425
+ const key = this.normalizeResourceName(name);
426
+ if (!this.resourceRegistry.get(key)) {
427
+ return;
428
+ }
429
+ this.deleteKey(key);
430
+ }
431
+ /**
432
+ * Get resource with explicit type parameter
433
+ * @param name Name of the resource
434
+ * @param type Type of the resource
435
+ * @template T Resource type
436
+ * @throws If resource creation fails.
437
+ * @returns Typed resource that matches name and type.
438
+ */
439
+ resourceByType(name, type) {
440
+ const builtName = this.buildResourceName(name, type);
441
+ const key = resourceNameToString(builtName);
442
+ if (this.instanceCache.has(key)) {
443
+ return this.instanceCache.get(key);
444
+ }
445
+ const resource = this.createResourceObject(builtName);
446
+ if (!resource) {
447
+ throw new Error(`Failed to create resource '${key}'`);
448
+ }
449
+ if (this.resourceRegistry.has(key)) {
450
+ this.instanceCache.set(key, resource);
451
+ }
452
+ return resource;
453
+ }
454
+ /**
455
+ * Get resource by ResourceName object
456
+ * @param name Resource name.
457
+ * @throws If resource creation fails.
458
+ * @returns Typed resource that matches the name.
459
+ */
460
+ resourceByName(name) {
461
+ const key = resourceNameToString(name);
462
+ if (this.instanceCache.has(key)) {
463
+ return this.instanceCache.get(key);
464
+ }
465
+ const resource = this.createResourceObject(name);
466
+ if (!resource) {
467
+ throw new Error(`Failed to create resource '${key}'`);
468
+ }
469
+ if (this.resourceRegistry.has(key)) {
470
+ this.instanceCache.set(key, resource);
471
+ }
472
+ return resource;
473
+ }
474
+ /**
475
+ * Check if a resource exists.
476
+ * @param name Resource name to check.
477
+ * @returns true, if resource is in the cache; false otherwise.
478
+ */
479
+ has(name) {
480
+ const key = this.normalizeResourceName(name);
481
+ return this.resourceRegistry.has(key);
482
+ }
483
+ /**
484
+ * Get resources with full metadata for a specific type
485
+ * @param type Type of resources to get.
486
+ * @param from Where to return resources from (all, local, imported modules)
487
+ * @template T Resource type
488
+ * @returns Array of resources with metadata.
489
+ */
490
+ resources(type, from = ResourcesFrom.all) {
491
+ const resources = [];
492
+ for (const [key, metadata] of this.resourceRegistry) {
493
+ if (metadata.type !== type)
494
+ continue;
495
+ if (from === ResourcesFrom.localOnly && metadata.source !== 'local')
496
+ continue;
497
+ if (from === ResourcesFrom.importedOnly && metadata.source !== 'module')
498
+ continue;
499
+ // Get or create the actual resource instance
500
+ const resName = resourceName(key);
501
+ const resource = this.resourceByName(resName);
502
+ resources.push(resource);
503
+ }
504
+ return resources;
505
+ }
506
+ }
507
+ //# sourceMappingURL=resource-cache.js.map