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