@cyberismo/data-handler 0.0.14 → 0.0.15

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 (240) hide show
  1. package/dist/card-metadata-updater.js +1 -3
  2. package/dist/card-metadata-updater.js.map +1 -1
  3. package/dist/command-handler.js +10 -16
  4. package/dist/command-handler.js.map +1 -1
  5. package/dist/command-manager.d.ts +1 -1
  6. package/dist/command-manager.js +4 -3
  7. package/dist/command-manager.js.map +1 -1
  8. package/dist/commands/create.js +13 -59
  9. package/dist/commands/create.js.map +1 -1
  10. package/dist/commands/edit.d.ts +1 -15
  11. package/dist/commands/edit.js +15 -89
  12. package/dist/commands/edit.js.map +1 -1
  13. package/dist/commands/export.js +4 -17
  14. package/dist/commands/export.js.map +1 -1
  15. package/dist/commands/import.js +3 -5
  16. package/dist/commands/import.js.map +1 -1
  17. package/dist/commands/move.d.ts +1 -2
  18. package/dist/commands/move.js +108 -146
  19. package/dist/commands/move.js.map +1 -1
  20. package/dist/commands/remove.js +9 -44
  21. package/dist/commands/remove.js.map +1 -1
  22. package/dist/commands/rename.js +2 -7
  23. package/dist/commands/rename.js.map +1 -1
  24. package/dist/commands/show.d.ts +7 -25
  25. package/dist/commands/show.js +38 -102
  26. package/dist/commands/show.js.map +1 -1
  27. package/dist/commands/transition.js +27 -30
  28. package/dist/commands/transition.js.map +1 -1
  29. package/dist/commands/update.d.ts +5 -3
  30. package/dist/commands/update.js +19 -5
  31. package/dist/commands/update.js.map +1 -1
  32. package/dist/commands/validate.d.ts +3 -3
  33. package/dist/commands/validate.js +19 -26
  34. package/dist/commands/validate.js.map +1 -1
  35. package/dist/containers/card-container.d.ts +87 -24
  36. package/dist/containers/card-container.js +183 -279
  37. package/dist/containers/card-container.js.map +1 -1
  38. package/dist/containers/project/calculation-engine.d.ts +6 -0
  39. package/dist/containers/project/calculation-engine.js +19 -12
  40. package/dist/containers/project/calculation-engine.js.map +1 -1
  41. package/dist/containers/project/card-cache.d.ts +146 -0
  42. package/dist/containers/project/card-cache.js +411 -0
  43. package/dist/containers/project/card-cache.js.map +1 -0
  44. package/dist/containers/project/resource-collector.d.ts +24 -1
  45. package/dist/containers/project/resource-collector.js +8 -1
  46. package/dist/containers/project/resource-collector.js.map +1 -1
  47. package/dist/containers/project.d.ts +117 -83
  48. package/dist/containers/project.js +418 -252
  49. package/dist/containers/project.js.map +1 -1
  50. package/dist/containers/template.d.ts +15 -31
  51. package/dist/containers/template.js +97 -104
  52. package/dist/containers/template.js.map +1 -1
  53. package/dist/index.d.ts +1 -0
  54. package/dist/index.js +1 -0
  55. package/dist/index.js.map +1 -1
  56. package/dist/interfaces/folder-content-interfaces.d.ts +2 -1
  57. package/dist/interfaces/folder-content-interfaces.js.map +1 -1
  58. package/dist/interfaces/macros.d.ts +1 -0
  59. package/dist/interfaces/macros.js +1 -1
  60. package/dist/interfaces/macros.js.map +1 -1
  61. package/dist/interfaces/project-interfaces.d.ts +5 -1
  62. package/dist/interfaces/project-interfaces.js.map +1 -1
  63. package/dist/interfaces/resource-interfaces.d.ts +11 -21
  64. package/dist/interfaces/resource-interfaces.js +3 -0
  65. package/dist/interfaces/resource-interfaces.js.map +1 -1
  66. package/dist/macros/common.d.ts +10 -10
  67. package/dist/macros/createCards/index.d.ts +0 -13
  68. package/dist/macros/createCards/index.js.map +1 -1
  69. package/dist/macros/createCards/types.d.ts +44 -0
  70. package/dist/macros/createCards/types.js +15 -0
  71. package/dist/macros/createCards/types.js.map +1 -0
  72. package/dist/macros/graph/index.d.ts +2 -6
  73. package/dist/macros/graph/index.js +2 -2
  74. package/dist/macros/graph/index.js.map +1 -1
  75. package/dist/macros/graph/types.d.ts +23 -0
  76. package/dist/macros/graph/types.js +15 -0
  77. package/dist/macros/graph/types.js.map +1 -0
  78. package/dist/macros/image/index.d.ts +8 -16
  79. package/dist/macros/image/index.js +36 -33
  80. package/dist/macros/image/index.js.map +1 -1
  81. package/dist/macros/image/types.d.ts +38 -0
  82. package/dist/macros/image/types.js +15 -0
  83. package/dist/macros/image/types.js.map +1 -0
  84. package/dist/macros/include/index.d.ts +1 -6
  85. package/dist/macros/include/index.js +4 -7
  86. package/dist/macros/include/index.js.map +1 -1
  87. package/dist/macros/include/types.d.ts +31 -0
  88. package/dist/macros/include/types.js +15 -0
  89. package/dist/macros/include/types.js.map +1 -0
  90. package/dist/macros/percentage/index.d.ts +0 -6
  91. package/dist/macros/percentage/index.js.map +1 -1
  92. package/dist/macros/percentage/types.d.ts +31 -0
  93. package/dist/macros/percentage/types.js +15 -0
  94. package/dist/macros/percentage/types.js.map +1 -0
  95. package/dist/macros/report/index.d.ts +0 -3
  96. package/dist/macros/report/index.js.map +1 -1
  97. package/dist/macros/report/types.d.ts +19 -0
  98. package/dist/macros/report/types.js +15 -0
  99. package/dist/macros/report/types.js.map +1 -0
  100. package/dist/macros/scoreCard/index.d.ts +0 -6
  101. package/dist/macros/scoreCard/index.js.map +1 -1
  102. package/dist/macros/scoreCard/types.d.ts +31 -0
  103. package/dist/macros/scoreCard/types.js +15 -0
  104. package/dist/macros/scoreCard/types.js.map +1 -0
  105. package/dist/macros/types.d.ts +25 -0
  106. package/dist/macros/types.js +2 -0
  107. package/dist/macros/types.js.map +1 -0
  108. package/dist/macros/vega/index.d.ts +0 -4
  109. package/dist/macros/vega/index.js.map +1 -1
  110. package/dist/macros/vega/types.d.ts +20 -0
  111. package/dist/macros/vega/types.js +2 -0
  112. package/dist/macros/vega/types.js.map +1 -0
  113. package/dist/macros/vegalite/index.d.ts +0 -4
  114. package/dist/macros/vegalite/index.js.map +1 -1
  115. package/dist/macros/vegalite/types.d.ts +20 -0
  116. package/dist/macros/vegalite/types.js +15 -0
  117. package/dist/macros/vegalite/types.js.map +1 -0
  118. package/dist/macros/xref/index.d.ts +0 -3
  119. package/dist/macros/xref/index.js +5 -14
  120. package/dist/macros/xref/index.js.map +1 -1
  121. package/dist/macros/xref/types.d.ts +19 -0
  122. package/dist/macros/xref/types.js +15 -0
  123. package/dist/macros/xref/types.js.map +1 -0
  124. package/dist/module-manager.js +4 -4
  125. package/dist/module-manager.js.map +1 -1
  126. package/dist/project-settings.js.map +1 -1
  127. package/dist/resources/calculation-resource.d.ts +4 -32
  128. package/dist/resources/calculation-resource.js +0 -55
  129. package/dist/resources/calculation-resource.js.map +1 -1
  130. package/dist/resources/card-type-resource.d.ts +4 -21
  131. package/dist/resources/card-type-resource.js +13 -44
  132. package/dist/resources/card-type-resource.js.map +1 -1
  133. package/dist/resources/field-type-resource.d.ts +4 -21
  134. package/dist/resources/field-type-resource.js +14 -38
  135. package/dist/resources/field-type-resource.js.map +1 -1
  136. package/dist/resources/file-resource.d.ts +12 -29
  137. package/dist/resources/file-resource.js +19 -293
  138. package/dist/resources/file-resource.js.map +1 -1
  139. package/dist/resources/folder-resource.d.ts +31 -50
  140. package/dist/resources/folder-resource.js +68 -96
  141. package/dist/resources/folder-resource.js.map +1 -1
  142. package/dist/resources/graph-model-resource.d.ts +5 -33
  143. package/dist/resources/graph-model-resource.js +8 -61
  144. package/dist/resources/graph-model-resource.js.map +1 -1
  145. package/dist/resources/graph-view-resource.d.ts +5 -28
  146. package/dist/resources/graph-view-resource.js +6 -45
  147. package/dist/resources/graph-view-resource.js.map +1 -1
  148. package/dist/resources/link-type-resource.d.ts +4 -21
  149. package/dist/resources/link-type-resource.js +6 -31
  150. package/dist/resources/link-type-resource.js.map +1 -1
  151. package/dist/resources/report-resource.d.ts +5 -17
  152. package/dist/resources/report-resource.js +6 -44
  153. package/dist/resources/report-resource.js.map +1 -1
  154. package/dist/resources/resource-object.d.ts +58 -23
  155. package/dist/resources/resource-object.js +293 -24
  156. package/dist/resources/resource-object.js.map +1 -1
  157. package/dist/resources/template-resource.d.ts +4 -15
  158. package/dist/resources/template-resource.js +10 -25
  159. package/dist/resources/template-resource.js.map +1 -1
  160. package/dist/resources/workflow-resource.d.ts +4 -23
  161. package/dist/resources/workflow-resource.js +12 -38
  162. package/dist/resources/workflow-resource.js.map +1 -1
  163. package/dist/utils/card-utils.d.ts +69 -19
  164. package/dist/utils/card-utils.js +179 -30
  165. package/dist/utils/card-utils.js.map +1 -1
  166. package/dist/utils/clingo-facts.js +11 -3
  167. package/dist/utils/clingo-facts.js.map +1 -1
  168. package/dist/utils/clingo-parser.js +1 -1
  169. package/dist/utils/clingo-parser.js.map +1 -1
  170. package/dist/utils/constants.d.ts +2 -0
  171. package/dist/utils/constants.js +4 -0
  172. package/dist/utils/constants.js.map +1 -1
  173. package/dist/utils/csv.js +1 -1
  174. package/dist/utils/csv.js.map +1 -1
  175. package/package.json +5 -5
  176. package/src/card-metadata-updater.ts +3 -5
  177. package/src/command-handler.ts +11 -18
  178. package/src/command-manager.ts +4 -3
  179. package/src/commands/create.ts +17 -83
  180. package/src/commands/edit.ts +16 -132
  181. package/src/commands/export.ts +8 -29
  182. package/src/commands/import.ts +4 -6
  183. package/src/commands/move.ts +144 -179
  184. package/src/commands/remove.ts +9 -52
  185. package/src/commands/rename.ts +2 -7
  186. package/src/commands/show.ts +50 -143
  187. package/src/commands/transition.ts +30 -33
  188. package/src/commands/update.ts +27 -9
  189. package/src/commands/validate.ts +21 -36
  190. package/src/containers/card-container.ts +200 -360
  191. package/src/containers/project/calculation-engine.ts +21 -13
  192. package/src/containers/project/card-cache.ts +497 -0
  193. package/src/containers/project/resource-collector.ts +9 -1
  194. package/src/containers/project.ts +529 -327
  195. package/src/containers/template.ts +109 -127
  196. package/src/index.ts +1 -0
  197. package/src/interfaces/folder-content-interfaces.ts +7 -1
  198. package/src/interfaces/macros.ts +2 -0
  199. package/src/interfaces/project-interfaces.ts +7 -1
  200. package/src/interfaces/resource-interfaces.ts +12 -24
  201. package/src/macros/createCards/index.ts +1 -12
  202. package/src/macros/createCards/types.ts +46 -0
  203. package/src/macros/graph/index.ts +3 -7
  204. package/src/macros/graph/types.ts +24 -0
  205. package/src/macros/image/index.ts +50 -61
  206. package/src/macros/image/types.ts +39 -0
  207. package/src/macros/include/index.ts +6 -15
  208. package/src/macros/include/types.ts +32 -0
  209. package/src/macros/percentage/index.ts +1 -7
  210. package/src/macros/percentage/types.ts +32 -0
  211. package/src/macros/report/index.ts +1 -4
  212. package/src/macros/report/types.ts +20 -0
  213. package/src/macros/scoreCard/index.ts +1 -7
  214. package/src/macros/scoreCard/types.ts +32 -0
  215. package/src/macros/types.ts +48 -0
  216. package/src/macros/vega/index.ts +1 -4
  217. package/src/macros/vega/types.ts +21 -0
  218. package/src/macros/vegalite/index.ts +1 -4
  219. package/src/macros/vegalite/types.ts +22 -0
  220. package/src/macros/xref/index.ts +6 -20
  221. package/src/macros/xref/types.ts +20 -0
  222. package/src/module-manager.ts +5 -5
  223. package/src/project-settings.ts +1 -1
  224. package/src/resources/calculation-resource.ts +6 -76
  225. package/src/resources/card-type-resource.ts +24 -59
  226. package/src/resources/field-type-resource.ts +22 -51
  227. package/src/resources/file-resource.ts +27 -409
  228. package/src/resources/folder-resource.ts +98 -124
  229. package/src/resources/graph-model-resource.ts +17 -74
  230. package/src/resources/graph-view-resource.ts +14 -54
  231. package/src/resources/link-type-resource.ts +13 -40
  232. package/src/resources/report-resource.ts +17 -57
  233. package/src/resources/resource-object.ts +435 -32
  234. package/src/resources/template-resource.ts +16 -29
  235. package/src/resources/workflow-resource.ts +26 -50
  236. package/src/utils/card-utils.ts +217 -31
  237. package/src/utils/clingo-facts.ts +13 -3
  238. package/src/utils/clingo-parser.ts +1 -1
  239. package/src/utils/constants.ts +6 -0
  240. package/src/utils/csv.ts +1 -1
@@ -11,16 +11,11 @@
11
11
  License along with this program. If not, see <https://www.gnu.org/licenses/>.
12
12
  */
13
13
 
14
- // node
15
- import { basename, join, sep } from 'node:path';
16
- import { mkdir, rename } from 'node:fs/promises';
17
- import { readFile } from 'node:fs/promises';
18
-
19
14
  import type {
20
15
  Card,
21
- FetchCardDetails,
22
16
  ResourceFolderType,
23
17
  } from '../interfaces/project-interfaces.js';
18
+ import type { ShowReturnType } from './resource-object.js';
24
19
  import {
25
20
  type AddOperation,
26
21
  type ChangeOperation,
@@ -29,28 +24,15 @@ import {
29
24
  ResourceObject,
30
25
  } from './resource-object.js';
31
26
  import { DefaultContent } from './create-defaults.js';
32
- import { deleteFile, pathExists } from '../utils/file-utils.js';
33
- import { hasCode } from '../utils/error-utils.js';
34
27
  import { Project, ResourcesFrom } from '../containers/project.js';
35
- import {
36
- readJsonFile,
37
- readJsonFileSync,
38
- writeJsonFile,
39
- } from '../utils/json.js';
40
- import type {
41
- ResourceBaseMetadata,
42
- ResourceContent,
43
- } from '../interfaces/resource-interfaces.js';
28
+ import type { ResourceBaseMetadata } from '../interfaces/resource-interfaces.js';
44
29
  import {
45
30
  type ResourceName,
46
31
  resourceName,
47
- resourceNameToPath,
48
32
  resourceNameToString,
49
33
  } from '../utils/resource-utils.js';
50
- import type { Resource } from '../interfaces/project-interfaces.js';
51
34
  import { sortCards } from '../utils/card-utils.js';
52
35
  import { Template } from '../containers/template.js';
53
- import { Validate } from '../commands/index.js';
54
36
 
55
37
  export {
56
38
  type AddOperation,
@@ -70,351 +52,38 @@ export {
70
52
  /**
71
53
  * Base class for file based resources (card types, field types, link types, workflows, ...)
72
54
  */
73
- export class FileResource extends ResourceObject {
74
- public fileName: string = '';
75
- protected content: ResourceBaseMetadata = {
76
- name: '',
77
- displayName: '',
78
- };
79
- private cache: Map<string, JSON>;
80
-
81
- constructor(
82
- project: Project,
83
- resourceName: ResourceName,
84
- protected type: ResourceFolderType,
85
- ) {
86
- super(project, resourceName);
87
- this.cache = this.project.resourceCache;
55
+ export abstract class FileResource<
56
+ T extends ResourceBaseMetadata,
57
+ > extends ResourceObject<T, never> {
58
+ constructor(project: Project, name: ResourceName, type: ResourceFolderType) {
59
+ super(project, name, type);
60
+ this.initialize();
88
61
  }
89
-
90
- // Type of resource.
91
- private resourceType(): ResourceFolderType {
92
- return this.type as ResourceFolderType;
93
- }
94
-
95
- // Converts resource name to Resource object.
96
- private resourceObjectToResource(object: FileResource): Resource {
97
- return {
98
- name: object.data ? object.data.name : '',
99
- path: object.fileName.substring(0, object.fileName.lastIndexOf(sep)),
100
- };
101
- }
102
-
103
- private toCache() {
104
- this.cache.set(
105
- resourceNameToString(this.resourceName),
106
- this.content as unknown as JSON,
107
- );
108
- }
109
-
110
62
  // Collects cards that are using the 'cardTypeName'.
111
- protected async collectCards(
112
- cardContent: FetchCardDetails,
113
- cardTypeName: string,
114
- ) {
115
- async function filteredCards(
116
- cardSource: Promise<Card[]>,
117
- cardTypeName: string,
118
- ): Promise<Card[]> {
119
- const cards = await cardSource;
63
+ protected async collectCards(cardTypeName: string) {
64
+ function filteredCards(cardSource: Card[], cardTypeName: string): Card[] {
65
+ const cards = cardSource;
120
66
  return cards.filter((card) => card.metadata?.cardType === cardTypeName);
121
67
  }
122
68
 
123
69
  // Collect both project cards ...
124
- const projectCardsPromise = filteredCards(
125
- this.project.cards(this.project.paths.cardRootFolder, cardContent),
70
+ const projectCards = filteredCards(
71
+ this.project.cards(this.project.paths.cardRootFolder),
126
72
  cardTypeName,
127
73
  );
128
74
  // ... and cards from each template that would be affected.
129
75
  const templates = await this.project.templates(ResourcesFrom.localOnly);
130
- const templateCardsPromises = templates.map((template) => {
76
+ const templateCards = templates.map((template) => {
131
77
  const templateObject = new Template(this.project, template);
132
- return filteredCards(templateObject.cards('', cardContent), cardTypeName);
78
+ return filteredCards(templateObject.cards(), cardTypeName);
133
79
  });
134
80
  // Return all affected cards
135
- const cards = (
136
- await Promise.all([projectCardsPromise, ...templateCardsPromises])
137
- ).reduce((accumulator, value) => accumulator.concat(value), []);
138
- return cards;
139
- }
140
-
141
- // Get (resource folder) type name
142
- protected get getType() {
143
- return super.getType;
144
- }
145
-
146
- // Get logger instance
147
- protected get logger() {
148
- return super.getLogger(this.getType);
149
- }
150
-
151
- // Initialize the resource.
152
- protected initialize() {
153
- if (this.resourceName.type === '') {
154
- this.resourceName.type = this.type;
155
- }
156
- if (this.resourceName.prefix === '') {
157
- this.resourceName.prefix = this.project.projectPrefix;
158
- }
159
- if (this.type) {
160
- this.moduleResource =
161
- this.resourceName.prefix !== this.project.projectPrefix;
162
- this.resourceFolder = this.moduleResource
163
- ? join(
164
- this.project.paths.modulesFolder,
165
- this.resourceName.prefix,
166
- this.resourceName.type,
167
- )
168
- : this.project.paths.resourcePath(this.type);
169
- this.fileName = resourceNameToPath(this.project, this.resourceName);
170
- }
171
- // Read from cache, if entry exists...
172
- if (this.cache.has(resourceNameToString(this.resourceName))) {
173
- this.content = this.cache.get(
174
- resourceNameToString(this.resourceName),
175
- ) as unknown as ResourceBaseMetadata;
176
- return;
177
- }
178
- //... otherwise read from disk and add to cache
179
- try {
180
- this.content = readJsonFileSync(this.fileName);
181
- this.toCache();
182
- } catch {
183
- // do nothing, it is possible that file has not been created yet.
184
- }
185
- }
186
-
187
- // Creates resource.
188
- protected async create(newContent?: ResourceContent) {
189
- if (pathExists(this.fileName)) {
190
- throw new Error(
191
- `Resource '${this.resourceName.identifier}' already exists in the project`,
192
- );
193
- }
194
-
195
- if (this.resourceFolder === '') {
196
- this.resourceName = resourceName(
197
- `${this.project.projectPrefix}/${this.type}/${this.resourceName.identifier}`,
198
- );
199
- this.resourceFolder = this.project.paths.resourcePath(
200
- this.resourceName.type as ResourceFolderType,
201
- );
202
- }
203
-
204
- const validName = await Validate.getInstance().validResourceName(
205
- this.resourceType(),
206
- resourceNameToString(this.resourceName),
207
- await this.project.projectPrefixes(),
208
- );
209
-
210
- let validContent = {} as ResourceContent;
211
- if (newContent) {
212
- validContent = newContent;
213
- validContent.name = validName;
214
- } else {
215
- validContent.description = '';
216
- validContent.displayName = '';
217
- }
218
-
219
- this.content = validContent;
220
- await this.write();
221
-
222
- // Notify project & collector
223
- this.project.addResource(
224
- this.resourceObjectToResource(this),
225
- this.content as unknown as JSON,
226
- );
227
- }
228
-
229
- // Calculations that use this resource.
230
- protected async calculations(): Promise<string[]> {
231
- const references: string[] = [];
232
- const resourceName = resourceNameToString(this.resourceName);
233
- for (const calculation of await this.project.calculations(
234
- ResourcesFrom.all,
235
- )) {
236
- const fileNameWithExtension = calculation.name.endsWith('.lp')
237
- ? calculation.name
238
- : calculation.name + '.lp';
239
- const filename = join(calculation.path, basename(fileNameWithExtension));
240
- try {
241
- const content = await readFile(filename, 'utf-8');
242
- if (content.includes(resourceName)) {
243
- references.push(calculation.name);
244
- }
245
- } catch (error) {
246
- // Skip files that don't exist (they may have been renamed or deleted)
247
- if (hasCode(error) && error.code === 'ENOENT') {
248
- this.logger.warn(`Skipping non-existent file: ${filename}`);
249
- continue;
250
- }
251
- throw new Error(
252
- `Failed to process file ${filename}: ${(error as Error).message}`,
253
- );
254
- }
255
- }
256
- return references;
257
- }
258
-
259
- // Cards from project.
260
- protected async cards(): Promise<Card[]> {
261
- return [
262
- ...(await this.project.cards(undefined, {
263
- content: true,
264
- metadata: true,
265
- })),
266
- ...(await this.project.allTemplateCards({
267
- content: true,
268
- metadata: true,
269
- })),
270
- ];
271
- }
272
-
273
- // Returns memory resident data as JSON.
274
- // This is basically same as 'show' but doesn't do any checks; just returns the current content.
275
- public get data() {
276
- return this.content.name !== '' ? this.content : undefined;
277
- }
278
-
279
- // Deletes resource.
280
- protected async delete() {
281
- if (this.moduleResource) {
282
- throw new Error(`Cannot delete module resources`);
283
- }
284
- if (!this.fileName.endsWith('.json')) {
285
- this.fileName += '.json';
286
- }
287
- if (!pathExists(this.fileName)) {
288
- throw new Error(
289
- `Resource '${this.resourceName.identifier}' does not exist in the project`,
290
- );
291
- }
292
- const usedIn = await this.usage();
293
- if (usedIn.length > 0) {
294
- throw new Error(
295
- `Cannot delete resource ${resourceNameToString(this.resourceName)}. It is used by: ${usedIn.join(', ')}`,
296
- );
297
- }
298
- await deleteFile(this.fileName);
299
- this.project.removeResource(this.resourceObjectToResource(this));
300
- this.fileName = '';
301
- }
302
-
303
- protected async validName(newName: ResourceName) {
304
- const validName = await Validate.getInstance().validResourceName(
305
- this.resourceType(),
306
- resourceNameToString(newName),
307
- await this.project.projectPrefixes(),
81
+ const cards = [projectCards, ...templateCards].reduce(
82
+ (accumulator, value) => accumulator.concat(value),
83
+ [],
308
84
  );
309
- return validName;
310
- }
311
-
312
- // Called after inherited class has finished 'update' operation.
313
- protected async postUpdate<Type>(
314
- content: ResourceContent,
315
- key: string,
316
- op: Operation<Type>,
317
- ) {
318
- function toValue(op: Operation<Type>) {
319
- if (op.name === 'rank') return op.newIndex;
320
- if (op.name === 'add') return JSON.stringify(op.target);
321
- if (op.name === 'remove') return JSON.stringify(op.target);
322
- if (op.name === 'change') return JSON.stringify(op.to);
323
- }
324
-
325
- // Check that new name is valid.
326
- if (op.name === 'change' && key === 'name') {
327
- const newName = resourceName(
328
- (op as ChangeOperation<string>).to as string,
329
- );
330
- content.name = await this.validName(newName);
331
- }
332
-
333
- // Once changes have been made; validate the content.
334
- try {
335
- await this.validate(content);
336
- } catch (error) {
337
- if (error instanceof Error) {
338
- const errorValue = typeof op === 'object' ? toValue(op) : op;
339
- throw new Error(
340
- `Cannot ${op.name} '${key}' --> '${errorValue}: ${error.message}'`,
341
- );
342
- }
343
- }
344
-
345
- this.content = content;
346
- await this.write();
347
- }
348
-
349
- // Reads content from file to memory.
350
- protected async read() {
351
- this.content = await readJsonFile(this.fileName);
352
- }
353
-
354
- // Renames resource.
355
- protected async rename(newName: ResourceName) {
356
- this.cache.delete(resourceNameToString(this.resourceName));
357
- if (this.moduleResource) {
358
- throw new Error(`Cannot rename module resources`);
359
- }
360
- if (!pathExists(this.fileName)) {
361
- throw new Error(
362
- `Resource '${this.resourceName.identifier}' does not exist`,
363
- );
364
- }
365
- if (newName.prefix !== this.project.projectPrefix) {
366
- throw new Error('Can only rename project resources');
367
- }
368
- if (newName.type !== this.resourceName.type) {
369
- throw new Error('Cannot change resource type');
370
- }
371
- await Validate.getInstance().validResourceName(
372
- this.resourceType(),
373
- resourceNameToString(newName),
374
- await this.project.projectPrefixes(),
375
- );
376
- const newFilename = join(
377
- this.project.paths.resourcePath(newName.type as ResourceFolderType),
378
- newName.identifier + '.json',
379
- );
380
- await rename(this.fileName, newFilename);
381
-
382
- this.fileName = newFilename;
383
- this.content.name = resourceNameToString(newName);
384
- this.resourceName = newName;
385
- this.toCache();
386
- }
387
-
388
- // Show resource data as JSON.
389
- protected async show(): Promise<ResourceContent> {
390
- if (!pathExists(this.fileName)) {
391
- const resourceType = `${this.type[0].toUpperCase()}${this.type.slice(1, this.type.length - 1)}`;
392
- const name = resourceNameToString(this.resourceName);
393
- throw new Error(
394
- `${resourceType} '${name}' does not exist in the project`,
395
- );
396
- }
397
- return this.content as ResourceContent;
398
- }
399
-
400
- // Update resource; the base class makes some checks only.
401
- protected async update<Type>(
402
- key: string,
403
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
404
- _op: Operation<Type>,
405
- ): Promise<void> {
406
- if (!this.data) {
407
- const name = resourceNameToString(this.resourceName);
408
- throw new Error(`Resource '${name}' does not exist in the project`);
409
- }
410
- if (this.moduleResource) {
411
- throw new Error(`Cannot update module resources`);
412
- }
413
- if (key === '' || key === undefined) {
414
- throw new Error(`Cannot update empty key`);
415
- }
85
+ return cards;
416
86
  }
417
-
418
87
  // Updates resource key to a new prefix
419
88
  protected updatePrefixInResourceName(name: string, prefixes: string[]) {
420
89
  const { identifier, prefix, type } = resourceName(name);
@@ -426,64 +95,13 @@ export class FileResource extends ResourceObject {
426
95
  : name;
427
96
  }
428
97
 
429
- // Check if there are references to the resource in the card content.
430
- protected async usage(cards?: Card[]): Promise<string[]> {
431
- if (!pathExists(this.fileName)) {
432
- throw new Error(
433
- `Resource '${this.resourceName.identifier}' does not exist in the project`,
434
- );
435
- }
436
- const cardArray = cards?.length
437
- ? cards
438
- : await this.project.cards(undefined, {
439
- content: true,
440
- metadata: true,
441
- });
442
-
443
- return cardArray
444
- .filter((card) =>
445
- card.content?.includes(resourceNameToString(this.resourceName)),
446
- )
447
- .map((card) => card.key);
448
- }
449
-
450
- // Write the content from memory to disk.
451
- protected async write() {
452
- if (this.moduleResource) {
453
- throw new Error(`Cannot change module resources`);
454
- }
455
-
456
- // Create folder for resources and add correct .schema file.
457
- await mkdir(this.resourceFolder, { recursive: true });
458
- await writeJsonFile(
459
- join(this.resourceFolder, '.schema'),
460
- this.contentSchema,
461
- {
462
- flag: 'wx',
463
- },
464
- );
465
- // Check if "name" has changed. Changing "name" means renaming the file.
466
- const nameInContent = resourceName(this.content.name).identifier + '.json';
467
- const currentFileName = basename(this.fileName);
468
-
469
- if (nameInContent !== currentFileName) {
470
- const newFileName = join(this.resourceFolder, nameInContent);
471
- await rename(this.fileName, newFileName);
472
- this.fileName = newFileName;
473
- }
474
-
475
- await writeJsonFile(this.fileName, this.content);
476
- this.toCache();
477
- }
478
-
479
- // Validate that current memory-based 'content' is valid.
480
- protected async validate(content?: object) {
481
- const invalidJson = Validate.getInstance().validateJson(
482
- content ? content : this.content,
483
- this.contentSchemaId,
484
- );
485
- if (invalidJson.length) {
486
- throw new Error(`Invalid content JSON: ${invalidJson}`);
487
- }
98
+ /**
99
+ * Returns the resource metadata content.
100
+ * @returns metadata content
101
+ * @throws if resource does not exist.
102
+ */
103
+ public async show(): Promise<ShowReturnType<T>> {
104
+ this.assertResourceExists();
105
+ return this.content;
488
106
  }
489
107
  }