@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
@@ -15,71 +15,55 @@
15
15
  import { basename, dirname, join, normalize } from 'node:path';
16
16
  import { mkdir, readdir, readFile, rename, rm } from 'node:fs/promises';
17
17
 
18
- import type { Card, Operation, ResourceName } from './file-resource.js';
19
- import type { ResourceBaseMetadata } from '../interfaces/resource-interfaces.js';
20
- import {
21
- isContentKey,
22
- type UpdateKey,
23
- } from '../interfaces/resource-interfaces.js';
24
- import type { ResourceFolderType } from '../interfaces/project-interfaces.js';
25
-
26
- import {
27
- DefaultContent,
28
- FileResource,
29
- Project,
30
- resourceName,
31
- resourceNameToString,
32
- sortCards,
33
- } from './file-resource.js';
34
- import type { FolderResourceContent } from '../interfaces/folder-content-interfaces.js';
18
+ import { isContentKey } from '../interfaces/resource-interfaces.js';
35
19
  import {
36
20
  filename,
37
- propertyName,
21
+ contentPropertyName,
38
22
  } from '../interfaces/folder-content-interfaces.js';
39
- import { formatJson, readJsonFile } from '../utils/json.js';
23
+ import { formatJson } from '../utils/json.js';
40
24
  import { VALID_FOLDER_RESOURCE_FILES } from '../utils/constants.js';
41
25
  import { writeFileSafe } from '../utils/file-utils.js';
42
- import type { ShowReturnType } from './resource-object.js';
43
26
  import { ResourceObject } from './resource-object.js';
27
+ import { resourceName } from '../utils/resource-utils.js';
44
28
 
45
- export {
46
- type Card,
47
- DefaultContent,
48
- FileResource,
49
- type Operation,
50
- Project,
51
- ResourceName,
52
- resourceNameToString,
53
- sortCards,
54
- };
29
+ import type { UpdateKey } from '../interfaces/resource-interfaces.js';
30
+ import type { FolderResourceContent } from '../interfaces/folder-content-interfaces.js';
31
+ import type { Operation } from './resource-object.js';
32
+ import type { Project } from '../containers/project.js';
33
+ import type { ResourceFolderType } from '../interfaces/project-interfaces.js';
34
+ import type { ResourceBaseMetadata } from '../interfaces/resource-interfaces.js';
35
+ import type { ResourceName } from '../utils/resource-utils.js';
36
+ import type { ShowReturnType } from './resource-object.js';
55
37
 
56
38
  /**
57
- * Folder type resource class. These are resources that have their own folders for content.
39
+ * Folder type resource class.
40
+ * These are resources that have their own folders for content.
58
41
  */
59
42
  export abstract class FolderResource<
60
43
  T extends ResourceBaseMetadata,
61
44
  U extends FolderResourceContent,
62
45
  > extends ResourceObject<T, U> {
63
46
  protected internalFolder: string = '';
47
+ private resourceContent: U = {} as U;
64
48
 
65
- // Cache for content files to avoid repeated filesystem operations. Content is stored as string.
66
- private contentFilesCache = new Map<string, string>();
67
-
49
+ /**
50
+ * Constructs a FolderResource object.
51
+ * @param project Project in which this resource exists.
52
+ * @param name Name for the resource.
53
+ * @param type Type for this resource.
54
+ */
68
55
  constructor(project: Project, name: ResourceName, type: ResourceFolderType) {
69
56
  super(project, name, type);
70
57
  this.initialize();
71
58
  }
72
59
 
73
- // Clears the content files cache.
74
- private clearContentCache() {
75
- this.contentFilesCache.clear();
76
- }
77
-
78
60
  /**
79
61
  * Creates a new folder type object. Base class writes the object to disk automatically.
80
62
  * @param newContent Content for the type.
81
63
  */
82
64
  protected async create(newContent?: T) {
65
+ // Validate resource identifier before creating on disk
66
+ this.validateResourceIdentifier();
83
67
  await super.create(newContent);
84
68
  await mkdir(this.internalFolder, { recursive: true });
85
69
  }
@@ -103,65 +87,36 @@ export abstract class FolderResource<
103
87
  protected abstract onNameChange?(previousName: string): Promise<void>;
104
88
 
105
89
  /**
106
- * Renames resource metadata file and renames memory resident object 'name'.
107
- * @param newName New name for the resource.
90
+ * Set content files. Should not be called by others than resource cache.
108
91
  */
109
- protected async rename(newName: ResourceName) {
110
- return super.rename(newName);
111
- }
92
+ public setContentFiles(contentFiles: Map<string, string>) {
93
+ const content = {} as Record<string, unknown>;
112
94
 
113
- /**
114
- * Shows the content of a file in the resource.
115
- * @param fileName Name of the file to show.
116
- * @param json Content is JSON file.
117
- * @returns the content of the file.
118
- */
119
- protected async showFile(
120
- fileName: string,
121
- json: boolean = false,
122
- ): Promise<string> {
123
- // Always first check cache...
124
- if (this.contentFilesCache.has(fileName)) {
125
- const cached = this.contentFilesCache.get(fileName)!;
126
- return json ? JSON.parse(cached) : cached;
95
+ for (const [fileName, fileContent] of contentFiles.entries()) {
96
+ const key = contentPropertyName(fileName);
97
+ if (key) {
98
+ const isJson = key === 'schema';
99
+ content[key] = isJson ? JSON.parse(fileContent) : fileContent;
100
+ }
127
101
  }
128
102
 
129
- // ...cache miss, read from filesystem
130
- const filePath = join(this.internalFolder, fileName);
131
- const content = json
132
- ? await readJsonFile(filePath)
133
- : await readFile(filePath, 'utf8');
134
-
135
- // Update cache
136
- const contentStr =
137
- typeof content === 'string' ? content : formatJson(content);
138
- this.contentFilesCache.set(fileName, contentStr);
139
-
140
- return json ? content : contentStr;
103
+ this.resourceContent = content as U;
141
104
  }
142
105
 
143
106
  /**
144
- * Shows all file names in the resource.
145
- * @returns all file names in the resource.
107
+ * Load all content files from the internal folder and set them.
146
108
  */
147
- protected async showFileNames(): Promise<string[]> {
148
- // Always first check cache...
149
- if (this.contentFilesCache.size > 0) {
150
- return Array.from(this.contentFilesCache.keys());
151
- }
152
-
153
- // ...cache miss, read from filesystem and populate cache
154
- const files = await readdir(this.internalFolder);
155
- const validFiles = files.filter((file) =>
156
- VALID_FOLDER_RESOURCE_FILES.includes(file),
157
- );
158
-
159
- // Update cache by reading all files. Each method call updates specific cache item.
160
- for (const fileName of validFiles) {
161
- await this.showFile(fileName);
109
+ protected async loadContentFiles() {
110
+ const contentFiles = new Map<string, string>();
111
+ const files = await readdir(this.internalFolder, { withFileTypes: true });
112
+ for (const file of files) {
113
+ if (file.isFile() && VALID_FOLDER_RESOURCE_FILES.includes(file.name)) {
114
+ const filePath = join(this.internalFolder, file.name);
115
+ const content = await readFile(filePath, 'utf-8');
116
+ contentFiles.set(file.name, content);
117
+ }
162
118
  }
163
-
164
- return validFiles;
119
+ this.setContentFiles(contentFiles);
165
120
  }
166
121
 
167
122
  /**
@@ -169,7 +124,7 @@ export abstract class FolderResource<
169
124
  * @param fileName The name of the file to update.
170
125
  * @param changedContent The new content for the file.
171
126
  */
172
- protected async updateFile(fileName: string, changedContent: string) {
127
+ public async updateFile(fileName: string, changedContent: string) {
173
128
  const filePath = join(this.internalFolder, fileName);
174
129
 
175
130
  // Do not allow updating file in other directories
@@ -190,8 +145,14 @@ export abstract class FolderResource<
190
145
 
191
146
  await writeFileSafe(filePath, changedContent, { flag: 'w' });
192
147
 
193
- // Update cache with new content
194
- this.contentFilesCache.set(fileName, changedContent);
148
+ // Update this resource's content
149
+ const key = contentPropertyName(fileName);
150
+ if (key) {
151
+ const isJson = key === 'schema';
152
+ (this.resourceContent as Record<string, unknown>)[key] = isJson
153
+ ? JSON.parse(changedContent)
154
+ : changedContent;
155
+ }
195
156
  }
196
157
 
197
158
  /**
@@ -214,32 +175,33 @@ export abstract class FolderResource<
214
175
  * Gets content of all files to properties.
215
176
  * @returns object with property names as keys and file contents as values.
216
177
  */
217
- public async contentData(): Promise<U> {
218
- const fileNames = await this.showFileNames();
219
- const content = {} as Record<string, unknown>;
220
-
221
- for (const fileName of fileNames) {
222
- const name = propertyName(fileName);
223
- if (name) {
224
- const JSONFile = name === 'schema';
225
- content[name] = await this.showFile(fileName, JSONFile);
226
- }
227
- }
228
-
178
+ public contentData(): U {
229
179
  // TODO: Instead of casting, validate that content matches U
230
180
  // This requires a runtime schema for U to be defined(via an abstract variable)
231
181
 
232
- return content as U;
182
+ return this.resourceContent;
233
183
  }
234
184
 
235
185
  /**
236
186
  * Deletes file and content folder from disk and clears out the memory resident object.
237
- * @throws if resource is a module resource or does not exist or is used by other resources.
238
187
  */
239
188
  public async delete() {
240
189
  await super.delete();
241
190
  await rm(this.internalFolder, { recursive: true, force: true });
242
- this.clearContentCache();
191
+ }
192
+
193
+ /**
194
+ * Shows metadata of the resource and content of the resource.
195
+ * @template T Resource type
196
+ * @template U Resource content
197
+ * @returns resource type's metadata and content.
198
+ */
199
+ public show(): ShowReturnType<T, U> {
200
+ this.assertResourceExists();
201
+ return {
202
+ ...this.content,
203
+ content: this.contentData(),
204
+ };
243
205
  }
244
206
 
245
207
  /**
@@ -286,17 +248,4 @@ export abstract class FolderResource<
286
248
  await this.onNameChange?.(existingName);
287
249
  }
288
250
  }
289
-
290
- /**
291
- * Shows metadata of the resource and content of the resource.
292
- * @returns resource type's metadata and content.
293
- * @throws if resource does not exist.
294
- */
295
- public async show(): Promise<ShowReturnType<T, U>> {
296
- this.assertResourceExists();
297
- return {
298
- ...this.content,
299
- content: await this.contentData(),
300
- };
301
- }
302
251
  }
@@ -14,24 +14,22 @@
14
14
 
15
15
  import { join } from 'node:path';
16
16
 
17
- import type {
18
- Card,
19
- Operation,
20
- Project,
21
- ResourceName,
22
- } from './folder-resource.js';
23
- import {
24
- DefaultContent,
25
- FolderResource,
26
- resourceNameToString,
27
- sortCards,
28
- } from './folder-resource.js';
17
+ import { DefaultContent } from './create-defaults.js';
18
+ import { FolderResource } from './folder-resource.js';
19
+ import { resourceNameToString } from '../utils/resource-utils.js';
20
+ import { sortCards } from '../utils/card-utils.js';
21
+ import { writeFileSafe } from '../utils/file-utils.js';
22
+ import { CONTENT_FILES } from '../interfaces/folder-content-interfaces.js';
23
+
24
+ import type { Card } from '../interfaces/project-interfaces.js';
29
25
  import type {
30
26
  GraphModelMetadata,
31
27
  UpdateKey,
32
28
  } from '../interfaces/resource-interfaces.js';
33
29
  import type { GraphModelContent } from '../interfaces/folder-content-interfaces.js';
34
- import { writeFileSafe } from '../utils/file-utils.js';
30
+ import type { Operation } from './resource-object.js';
31
+ import type { Project } from '../containers/project.js';
32
+ import type { ResourceName } from '../utils/resource-utils.js';
35
33
 
36
34
  /**
37
35
  * Graph model resource class.
@@ -54,7 +52,7 @@ export class GraphModelResource extends FolderResource<
54
52
  protected async onNameChange(existingName: string): Promise<void> {
55
53
  await Promise.all([
56
54
  super.updateHandleBars(existingName, this.content.name, [
57
- join(this.internalFolder, 'model.lp'),
55
+ join(this.internalFolder, CONTENT_FILES.model),
58
56
  ]),
59
57
  super.updateCalculations(existingName, this.content.name),
60
58
  ]);
@@ -79,14 +77,14 @@ export class GraphModelResource extends FolderResource<
79
77
  await super.create(newContent);
80
78
 
81
79
  // Create the internal folder in 'create', instead of 'write'.
82
- const calculationsFile = join(this.internalFolder, 'model.lp');
83
- await writeFileSafe(
84
- calculationsFile,
85
- `% add your calculations here for '${this.resourceName.identifier}'`,
86
- {
87
- flag: 'wx',
88
- },
89
- );
80
+ const modelContent = `% add your calculations here for '${this.resourceName.identifier}'`;
81
+ const modelFile = CONTENT_FILES.model;
82
+ const calculationsFile = join(this.internalFolder, modelFile);
83
+ await writeFileSafe(calculationsFile, modelContent, {
84
+ flag: 'wx',
85
+ });
86
+
87
+ await this.loadContentFiles();
90
88
  }
91
89
 
92
90
  /**
@@ -12,29 +12,25 @@
12
12
  License along with this program. If not, see <https://www.gnu.org/licenses/>.
13
13
  */
14
14
 
15
- import { readdir } from 'node:fs/promises';
16
15
  import { extname, join } from 'node:path';
16
+ import { readdir } from 'node:fs/promises';
17
17
 
18
- import type {
19
- Card,
20
- Operation,
21
- Project,
22
- ResourceName,
23
- } from './folder-resource.js';
24
- import {
25
- DefaultContent,
26
- FolderResource,
27
- resourceNameToString,
28
- sortCards,
29
- } from './folder-resource.js';
18
+ import { copyDir } from '../utils/file-utils.js';
19
+ import { DefaultContent } from './create-defaults.js';
20
+ import { FolderResource } from './folder-resource.js';
21
+ import { getStaticDirectoryPath } from '@cyberismo/assets';
22
+ import { resourceNameToString } from '../utils/resource-utils.js';
23
+ import { sortCards } from '../utils/card-utils.js';
24
+
25
+ import type { Card } from '../interfaces/project-interfaces.js';
26
+ import type { GraphViewContent } from '../interfaces/folder-content-interfaces.js';
30
27
  import type {
31
28
  GraphViewMetadata,
32
29
  UpdateKey,
33
30
  } from '../interfaces/resource-interfaces.js';
34
- import type { GraphViewContent } from '../interfaces/folder-content-interfaces.js';
35
-
36
- import { getStaticDirectoryPath } from '@cyberismo/assets';
37
- import { copyDir } from '../utils/file-utils.js';
31
+ import type { Operation } from './resource-object.js';
32
+ import type { Project } from '../containers/project.js';
33
+ import type { ResourceName } from '../utils/resource-utils.js';
38
34
 
39
35
  /**
40
36
  * Graph view resource class.
@@ -83,6 +79,8 @@ export class GraphViewResource extends FolderResource<
83
79
  join(await getStaticDirectoryPath(), 'defaultGraphView'),
84
80
  this.internalFolder,
85
81
  );
82
+
83
+ await this.loadContentFiles();
86
84
  }
87
85
 
88
86
  /**
@@ -10,19 +10,16 @@
10
10
  License along with this program. If not, see <https://www.gnu.org/licenses/>.
11
11
  */
12
12
 
13
- import type {
14
- Card,
15
- Operation,
16
- Project,
17
- ResourceName,
18
- } from './file-resource.js';
19
- import {
20
- DefaultContent,
21
- FileResource,
22
- resourceNameToString,
23
- sortCards,
24
- } from './folder-resource.js';
13
+ import { DefaultContent } from './create-defaults.js';
14
+ import { FileResource } from './file-resource.js';
15
+ import { resourceNameToString } from '../utils/resource-utils.js';
16
+ import { sortCards } from '../utils/card-utils.js';
17
+
18
+ import type { Card } from '../interfaces/project-interfaces.js';
25
19
  import type { LinkType, UpdateKey } from '../interfaces/resource-interfaces.js';
20
+ import type { Operation } from './resource-object.js';
21
+ import type { Project } from '../containers/project.js';
22
+ import type { ResourceName } from '../utils/resource-utils.js';
26
23
 
27
24
  /**
28
25
  * Link Type resource class.
@@ -38,7 +35,7 @@ export class LinkTypeResource extends FileResource<LinkType> {
38
35
  // When resource name changes.
39
36
  private async handleNameChange(existingName: string) {
40
37
  const current = this.content;
41
- const prefixes = await this.project.projectPrefixes();
38
+ const prefixes = this.project.projectPrefixes();
42
39
  if (current.sourceCardTypes) {
43
40
  current.sourceCardTypes = current.sourceCardTypes.map((item) =>
44
41
  this.updatePrefixInResourceName(item, prefixes),
@@ -12,38 +12,29 @@
12
12
  License along with this program. If not, see <https://www.gnu.org/licenses/>.
13
13
  */
14
14
 
15
- import { readdir } from 'node:fs/promises';
16
- import { readFileSync } from 'node:fs';
17
15
  import { extname, join } from 'node:path';
16
+ import { readdir } from 'node:fs/promises';
18
17
 
19
18
  import { copyDir } from '../utils/file-utils.js';
20
- import {
21
- DefaultContent,
22
- FolderResource,
23
- resourceNameToString,
24
- sortCards,
25
- } from './folder-resource.js';
19
+ import { DefaultContent } from './create-defaults.js';
20
+ import { FolderResource } from './folder-resource.js';
26
21
  import { getStaticDirectoryPath } from '@cyberismo/assets';
22
+ import { resourceNameToString } from '../utils/resource-utils.js';
23
+ import { sortCards } from '../utils/card-utils.js';
27
24
  import { Validate } from '../commands/validate.js';
28
25
 
26
+ import type { Card } from '../interfaces/project-interfaces.js';
27
+ import type { Operation } from './resource-object.js';
28
+ import type { Project } from '../containers/project.js';
29
+ import type { ReportContent } from '../interfaces/folder-content-interfaces.js';
29
30
  import type {
30
- Card,
31
- Operation,
32
- Project,
33
- ResourceName,
34
- } from './folder-resource.js';
35
- import {
36
- type ReportMetadata,
37
- type UpdateKey,
31
+ ReportMetadata,
32
+ UpdateKey,
38
33
  } from '../interfaces/resource-interfaces.js';
39
- import type { ReportContent } from '../interfaces/folder-content-interfaces.js';
40
- import type { Schema } from 'jsonschema';
34
+ import type { ResourceName } from '../utils/resource-utils.js';
41
35
 
42
- const REPORT_SCHEMA_FILE = 'parameterSchema.json';
43
36
  const PARAMETER_SCHEMA_ID = 'jsonSchema';
44
37
 
45
- const staticDirectoryPath = await getStaticDirectoryPath();
46
-
47
38
  /**
48
39
  * Report resource class.
49
40
  */
@@ -51,32 +42,18 @@ export class ReportResource extends FolderResource<
51
42
  ReportMetadata,
52
43
  ReportContent
53
44
  > {
54
- private reportSchema: Schema;
55
45
  constructor(project: Project, name: ResourceName) {
56
46
  super(project, name, 'reports');
57
47
 
58
48
  this.contentSchemaId = 'reportSchema';
59
49
  this.contentSchema = super.contentSchemaContent(this.contentSchemaId);
60
-
61
- const schemaPath = join(this.internalFolder, REPORT_SCHEMA_FILE);
62
- this.reportSchema = this.readSchemaFile(schemaPath);
63
50
  }
64
51
 
65
52
  // Path to content folder.
66
53
  // @todo: create the files' content dynamically.
67
- private defaultReportLocation: string = join(
68
- staticDirectoryPath,
69
- 'defaultReport',
70
- );
71
-
72
- // Try to read schema file content
73
- private readSchemaFile(path: string) {
74
- try {
75
- const schema = readFileSync(path);
76
- return JSON.parse(schema.toString());
77
- } catch {
78
- return undefined;
79
- }
54
+ private async getDefaultReportLocation(): Promise<string> {
55
+ const staticDirectoryPath = await getStaticDirectoryPath();
56
+ return join(staticDirectoryPath, 'defaultReport');
80
57
  }
81
58
 
82
59
  /**
@@ -98,8 +75,6 @@ export class ReportResource extends FolderResource<
98
75
 
99
76
  /**
100
77
  * Sets new metadata into the report object.
101
- * @param newContent metadata content for the report.
102
- * @throws if 'newContent' is not valid.
103
78
  */
104
79
  public async createReport() {
105
80
  const defaultContent = DefaultContent.report(
@@ -109,7 +84,9 @@ export class ReportResource extends FolderResource<
109
84
  await super.create(defaultContent);
110
85
 
111
86
  // Copy report default structure to destination.
112
- await copyDir(this.defaultReportLocation, this.internalFolder);
87
+ const defaultReportLocation = await this.getDefaultReportLocation();
88
+ await copyDir(defaultReportLocation, this.internalFolder);
89
+ await this.loadContentFiles();
113
90
  }
114
91
 
115
92
  /**
@@ -180,9 +157,10 @@ export class ReportResource extends FolderResource<
180
157
  * @note If content is not provided, base class validation will use resource's current content.
181
158
  */
182
159
  public async validate(content?: object) {
183
- if (this.reportSchema) {
160
+ const resourceContent = this.contentData();
161
+ if (resourceContent.schema) {
184
162
  const errors = Validate.getInstance().validateJson(
185
- this.reportSchema,
163
+ resourceContent.schema,
186
164
  PARAMETER_SCHEMA_ID,
187
165
  );
188
166
  if (errors.length > 0) {