@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
@@ -12,29 +12,25 @@
12
12
  */
13
13
 
14
14
  // node
15
- import { basename, join, resolve, sep } from 'node:path';
16
- import {
17
- constants as fsConstants,
18
- copyFile,
19
- mkdir,
20
- writeFile,
21
- } from 'node:fs/promises';
22
-
23
- import { errorFunction } from '../utils/log-utils.js';
15
+ import { join, resolve } from 'node:path';
16
+ import { mkdir, writeFile } from 'node:fs/promises';
17
+
18
+ import { errorFunction } from '../utils/error-utils.js';
24
19
  import { Project } from '../containers/project.js';
25
20
  import { Validate } from './index.js';
26
21
 
27
22
  import { EMPTY_RANK, sortItems } from '../utils/lexorank.js';
23
+ import { isModulePath } from '../utils/card-utils.js';
28
24
  import type {
29
25
  DataType,
30
26
  Link,
31
27
  LinkType,
32
28
  } from '../interfaces/resource-interfaces.js';
33
- import { pathExists } from '../utils/file-utils.js';
34
29
  import type { Card, ProjectFile } from '../interfaces/project-interfaces.js';
35
30
  import { resourceName, resourceNameToString } from '../utils/resource-utils.js';
36
31
  import { writeJsonFile } from '../utils/json.js';
37
32
 
33
+ import { CalculationResource } from '../resources/calculation-resource.js';
38
34
  import { CardTypeResource } from '../resources/card-type-resource.js';
39
35
  import { FieldTypeResource } from '../resources/field-type-resource.js';
40
36
  import { GraphModelResource } from '../resources/graph-model-resource.js';
@@ -44,8 +40,6 @@ import { ReportResource } from '../resources/report-resource.js';
44
40
  import { TemplateResource } from '../resources/template-resource.js';
45
41
  import { WorkflowResource } from '../resources/workflow-resource.js';
46
42
 
47
- const MODULES_PATH = `${sep}modules${sep}`;
48
-
49
43
  // todo: Is there a easy to way to make JSON schema into a TypeScript interface/type?
50
44
  // Check this out: https://www.npmjs.com/package/json-schema-to-ts
51
45
 
@@ -113,24 +107,14 @@ export class Create {
113
107
  if (cardTypeName === undefined) {
114
108
  throw new Error(`Input validation error: card type cannot be empty`);
115
109
  }
116
- // Use slice to get a copy of a string.
117
- const origTemplateName = templateName.slice(0);
118
110
  const templateResource = new TemplateResource(
119
111
  this.project,
120
112
  resourceName(templateName),
121
113
  );
122
114
  const templateObject = templateResource.templateObject();
115
+ const specificCard = card ? templateObject.findCard(card) : undefined;
123
116
 
124
- const specificCard = card
125
- ? await templateObject.findSpecificCard(card)
126
- : undefined;
127
- if (card && !specificCard) {
128
- throw new Error(
129
- `Card '${card}' was not found from template '${origTemplateName}'`,
130
- );
131
- }
132
-
133
- if (templateObject.templateFolder().includes(`${sep}modules${sep}`)) {
117
+ if (isModulePath(templateObject.templateFolder())) {
134
118
  throw new Error(`Cannot add cards to imported module templates`);
135
119
  }
136
120
 
@@ -181,66 +165,27 @@ export class Create {
181
165
  attachment: string,
182
166
  buffer?: Buffer,
183
167
  ) {
184
- if (!buffer && !pathExists(attachment)) {
185
- throw new Error(
186
- `Input validation error: cannot find attachment '${attachment}'`,
187
- );
188
- }
189
- const attachmentFolder = await this.project.cardAttachmentFolder(cardKey);
190
-
191
- // Imported templates cannot be modified.
192
- // @todo: make MODULES_PATH project level constant
193
- if (attachmentFolder.includes(MODULES_PATH)) {
194
- throw new Error(`Cannot modify imported module`);
195
- }
196
-
197
168
  try {
198
- await mkdir(attachmentFolder, { recursive: true }).then(async () => {
199
- if (!buffer) {
200
- return copyFile(
201
- attachment,
202
- join(attachmentFolder, basename(attachment)),
203
- fsConstants.COPYFILE_EXCL,
204
- );
205
- }
206
- return writeFile(join(attachmentFolder, basename(attachment)), buffer, {
207
- flag: 'wx',
208
- });
209
- });
169
+ await this.project.createCardAttachment(
170
+ cardKey,
171
+ attachment,
172
+ buffer || attachment,
173
+ );
210
174
  } catch (error) {
211
175
  throw new Error(errorFunction(error));
212
176
  }
213
177
  }
214
178
 
215
179
  /**
216
- * Creates a calculation file.
217
- * @param fileName name for the calculation file (without .lp extension)
180
+ * Creates a calculation resource.
181
+ * @param calculationName name for the calculation resource
218
182
  */
219
- public async createCalculation(fileName: string) {
220
- const calculationFolder = this.project.paths.calculationProjectFolder;
221
-
222
- await mkdir(calculationFolder, { recursive: true });
223
-
224
- const calculationFileName = fileName.endsWith('.lp')
225
- ? fileName
226
- : `${fileName}.lp`;
227
- const calculationFilePath = join(calculationFolder, calculationFileName);
228
-
229
- const calculationContent = `% Calculation file: ${calculationFileName}
230
- % Add your logic programming rules here
231
- `;
232
-
233
- await writeFile(calculationFilePath, calculationContent, { flag: 'wx' });
234
-
235
- // add to cache manually
236
- // TODO: remove once calculations are a proper resource
237
- const resourceName = `${this.project.projectPrefix}/calculations/${fileName.replace('.lp', '')}`;
238
- const resource = {
239
- name: resourceName,
240
- path: calculationFolder,
241
- };
242
-
243
- this.project.addResource(resource, {} as JSON);
183
+ public async createCalculation(calculationName: string) {
184
+ const calculation = new CalculationResource(
185
+ this.project,
186
+ resourceName(calculationName),
187
+ );
188
+ await calculation.create();
244
189
  }
245
190
 
246
191
  /**
@@ -258,7 +203,7 @@ export class Create {
258
203
  resourceName(templateName),
259
204
  );
260
205
 
261
- await Validate.getInstance().validResourceName(
206
+ Validate.getInstance().validResourceName(
262
207
  'templates',
263
208
  resourceNameToString(resourceName(templateName)),
264
209
  await this.project.projectPrefixes(),
@@ -267,15 +212,8 @@ export class Create {
267
212
  await templateResource.validate();
268
213
 
269
214
  const specificCard = parentCardKey
270
- ? await this.project.findSpecificCard(parentCardKey, {
271
- metadata: true,
272
- children: true,
273
- })
215
+ ? this.project.findCard(parentCardKey)
274
216
  : undefined;
275
- if (parentCardKey && !specificCard) {
276
- throw new Error(`Card '${parentCardKey}' not found from project`);
277
- }
278
-
279
217
  const templateObject = templateResource.templateObject();
280
218
  if (!templateObject || !templateObject.isCreated()) {
281
219
  throw new Error(`Template '${templateName}' not found from project`);
@@ -283,7 +221,6 @@ export class Create {
283
221
 
284
222
  const createdCards = await templateObject.createCards(specificCard);
285
223
  if (createdCards.length > 0) {
286
- await this.project.handleNewCards(createdCards);
287
224
  // Note: This assumes that parent keys will be ahead of 'a' in the sort order.
288
225
  const sorted = sortItems(createdCards, (item) => {
289
226
  return `${item.parent === 'root' ? 'a' : item.parent}${item.metadata?.rank || EMPTY_RANK}`;
@@ -354,13 +291,8 @@ export class Create {
354
291
  throw new Error(`Not a valid label name'`);
355
292
  }
356
293
 
357
- const card = await this.project.findSpecificCard(cardKey, {
358
- metadata: true,
359
- });
360
- if (!card) {
361
- throw new Error(`Card '${cardKey}' does not exist in the project`);
362
- }
363
- const labels = card.metadata?.labels ?? [];
294
+ const card = this.project.findCard(cardKey);
295
+ const labels = structuredClone(card.metadata?.labels) ?? [];
364
296
 
365
297
  if (labels.includes(label)) {
366
298
  throw new Error('Label already exists');
@@ -400,26 +332,10 @@ export class Create {
400
332
  }
401
333
 
402
334
  // Determine the card path
403
- const card = await this.project.findSpecificCard(cardKey, {
404
- metadata: true,
405
- });
406
- if (!card) {
407
- throw new Error(`Card '${cardKey}' does not exist in the project`);
408
- }
409
-
410
- const destinationCard = await this.project.findSpecificCard(
411
- destinationCardKey,
412
- {
413
- metadata: true,
414
- },
415
- );
416
- if (!destinationCard) {
417
- throw new Error(
418
- `Card '${destinationCardKey}' does not exist in the project`,
419
- );
420
- }
335
+ const card = this.project.findCard(cardKey);
336
+ const destinationCard = this.project.findCard(destinationCardKey);
421
337
  // make sure the link type exists
422
- const linkTypeObject = await this.project.resource<LinkType>(linkType);
338
+ const linkTypeObject = this.project.resource<LinkType>(linkType);
423
339
  if (!linkTypeObject) {
424
340
  throw new Error(`Link type '${linkType}' does not exist in the project`);
425
341
  }
@@ -1,13 +1,14 @@
1
1
  /**
2
- Cyberismo
3
- Copyright © Cyberismo Ltd and contributors 2024
4
-
5
- This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License version 3 as published by the Free Software Foundation.
6
-
7
- This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
8
-
9
- You should have received a copy of the GNU Affero General Public
10
- License along with this program. If not, see <https://www.gnu.org/licenses/>.
2
+ Cyberismo
3
+ Copyright © Cyberismo Ltd and contributors 2024
4
+ This program is free software: you can redistribute it and/or modify it under
5
+ the terms of the GNU Affero General Public License version 3 as published by
6
+ the Free Software Foundation.
7
+ This program is distributed in the hope that it will be useful, but WITHOUT
8
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
9
+ FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
10
+ details. You should have received a copy of the GNU Affero General Public
11
+ License along with this program. If not, see <https://www.gnu.org/licenses/>.
11
12
  */
12
13
 
13
14
  // node
@@ -16,18 +17,10 @@ import { homedir } from 'node:os';
16
17
  import { spawnSync } from 'node:child_process';
17
18
 
18
19
  import { ActionGuard } from '../permissions/action-guard.js';
19
- import type {
20
- MetadataContent,
21
- ResourceFolderType,
22
- } from '../interfaces/project-interfaces.js';
23
20
  import { Project } from '../containers/project.js';
24
21
  import { UserPreferences } from '../utils/user-preferences.js';
25
- import {
26
- type ResourceName,
27
- resourceNameToString,
28
- } from '../utils/resource-utils.js';
29
- import { FolderResource } from '../resources/folder-resource.js';
30
- import { writeFile } from 'node:fs/promises';
22
+
23
+ import type { MetadataContent } from '../interfaces/project-interfaces.js';
31
24
 
32
25
  export class Edit {
33
26
  private project: Project;
@@ -36,54 +29,12 @@ export class Edit {
36
29
  this.project = project;
37
30
  }
38
31
 
39
- /**
40
- * Updates a calculation file.
41
- * @param resourceName The name of the resource to update.
42
- * @param changedContent The new content for the calculation.
43
- */
44
- public async editCalculation(
45
- resourceName: ResourceName,
46
- changedContent: string,
47
- ) {
48
- if (resourceName.prefix !== this.project.projectPrefix) {
49
- throw new Error(
50
- `Resource '${resourceName.identifier}' is not a local resource`,
51
- );
52
- }
53
- const resourceNameString = resourceNameToString(resourceName);
54
- if (
55
- !(await this.project.resourceExists(
56
- resourceName.type as ResourceFolderType,
57
- resourceNameString,
58
- ))
59
- ) {
60
- throw new Error(
61
- `Resource '${resourceNameString}' does not exist in the project`,
62
- );
63
- }
64
- await writeFile(
65
- join(
66
- this.project.paths.calculationProjectFolder,
67
- resourceName.identifier + '.lp',
68
- ),
69
- changedContent,
70
- {
71
- encoding: 'utf-8',
72
- flag: 'r+',
73
- },
74
- );
75
- }
76
-
77
32
  /**
78
33
  * Opens the content and metadata files for a card in the code editor
79
34
  * @param cardKey - The key of the card to open. Required.
80
35
  */
81
- public async editCard(cardKey: string) {
82
- // Determine the card path
83
- const cardPath = this.project.pathToCard(cardKey);
84
- if (!cardPath) {
85
- throw new Error(`Card '${cardKey}' does not exist in the project`);
86
- }
36
+ public editCard(cardKey: string) {
37
+ const card = this.project.findCard(cardKey);
87
38
 
88
39
  // Read the user preferences
89
40
  const prefs = new UserPreferences(
@@ -91,9 +42,8 @@ export class Edit {
91
42
  ).getPreferences();
92
43
 
93
44
  // Construct paths for the card components (json and adoc)
94
- const cardDirPath = join(this.project.paths.cardRootFolder, cardPath);
95
- const cardContentPath = join(cardDirPath, Project.cardContentFile);
96
- const cardJsonPath = join(cardDirPath, Project.cardMetadataFile);
45
+ const cardContentPath = join(card.path, Project.cardContentFile);
46
+ const cardJsonPath = join(card.path, Project.cardMetadataFile);
97
47
 
98
48
  // Extract the editor settings from the preferences.
99
49
  const editorPrefs = prefs.editCommand[process.platform];
@@ -104,7 +54,7 @@ export class Edit {
104
54
  const editorArgs = editorArgPrefs.map((arg) => {
105
55
  arg = arg.replace(/\{\{\s*cardContentPath\s*\}\}/, cardContentPath);
106
56
  arg = arg.replace(/\{\{\s*cardJsonPath\s*\}\}/, cardJsonPath);
107
- arg = arg.replace(/\{\{\s*cardDirPath\s*\}\}/, cardDirPath);
57
+ arg = arg.replace(/\{\{\s*cardDirPath\s*\}\}/, card.path);
108
58
 
109
59
  return arg;
110
60
  });
@@ -127,19 +77,14 @@ export class Edit {
127
77
  * @param changedContent New content for the card.
128
78
  */
129
79
  public async editCardContent(cardKey: string, changedContent: string) {
130
- const isTemplateCard = await this.project.isTemplateCard(cardKey);
131
- if (isTemplateCard) {
80
+ if (this.project.hasTemplateCard(cardKey)) {
132
81
  return this.project.updateCardContent(cardKey, changedContent);
133
82
  }
134
- const cardPath = this.project.pathToCard(cardKey);
135
- if (!cardPath) {
136
- throw new Error(`Card '${cardKey}' does not exist in the project`);
83
+ if (this.project.findCard(cardKey)) {
84
+ const actionGuard = new ActionGuard(this.project.calculationEngine);
85
+ await actionGuard.checkPermission('editContent', cardKey);
86
+ await this.project.updateCardContent(cardKey, changedContent);
137
87
  }
138
-
139
- const actionGuard = new ActionGuard(this.project.calculationEngine);
140
- await actionGuard.checkPermission('editContent', cardKey);
141
-
142
- await this.project.updateCardContent(cardKey, changedContent);
143
88
  }
144
89
 
145
90
  /**
@@ -156,51 +101,15 @@ export class Edit {
156
101
  if (!changedKey) {
157
102
  throw new Error(`Changed key cannot be empty`);
158
103
  }
159
- const isTemplateCard = await this.project.isTemplateCard(cardKey);
160
- if (isTemplateCard) {
104
+ if (this.project.hasTemplateCard(cardKey)) {
161
105
  return this.project.updateCardMetadataKey(cardKey, changedKey, newValue);
162
106
  }
163
107
 
164
- // Determine the card path
165
- const cardPath = this.project.pathToCard(cardKey);
166
- if (!cardPath) {
167
- throw new Error(`Card '${cardKey}' does not exist in the project`);
168
- }
169
-
170
108
  // check for editing rights
171
- const actionGuard = new ActionGuard(this.project.calculationEngine);
172
- await actionGuard.checkPermission('editField', cardKey, changedKey);
173
- await this.project.updateCardMetadataKey(cardKey, changedKey, newValue);
174
- }
175
-
176
- /**
177
- * Update a file of a folder resource. Cannot be used to create a new file.
178
- * @param resourceName The name of the resource to update.
179
- * @param fileName The name of the file to update.
180
- * @param changedContent The new content for the file.
181
- */
182
- public async editResourceContent(
183
- resourceName: ResourceName,
184
- fileName: string,
185
- changedContent: string,
186
- ) {
187
- const resourceNameString = resourceNameToString(resourceName);
188
- if (
189
- !(await this.project.resourceExists(
190
- resourceName.type as ResourceFolderType,
191
- resourceNameString,
192
- ))
193
- ) {
194
- throw new Error(
195
- `Resource '${resourceNameString}' does not exist in the project`,
196
- );
197
- }
198
- const resource = Project.resourceObject(this.project, resourceName);
199
- if (!(resource instanceof FolderResource)) {
200
- throw new Error(
201
- `Resource '${resourceNameString}' is not a folder resource`,
202
- );
109
+ if (this.project.findCard(cardKey)) {
110
+ const actionGuard = new ActionGuard(this.project.calculationEngine);
111
+ await actionGuard.checkPermission('editField', cardKey, changedKey);
112
+ await this.project.updateCardMetadataKey(cardKey, changedKey, newValue);
203
113
  }
204
- return resource.updateFile(fileName, changedContent);
205
114
  }
206
115
  }
@@ -23,10 +23,7 @@ import {
23
23
  import { dirname, join } from 'node:path';
24
24
  import { spawn } from 'node:child_process';
25
25
 
26
- import type {
27
- Card,
28
- FetchCardDetails,
29
- } from '../interfaces/project-interfaces.js';
26
+ import type { Card } from '../interfaces/project-interfaces.js';
30
27
  import type { CardType } from '../interfaces/resource-interfaces.js';
31
28
  import { evaluateMacros } from '../macros/index.js';
32
29
  import type { ExportPdfOptions } from '../interfaces/project-interfaces.js';
@@ -144,7 +141,10 @@ export class Export {
144
141
  }
145
142
 
146
143
  if (card.children) {
147
- await this.toAdocFileAsContent(path, card.children);
144
+ await this.toAdocFileAsContent(
145
+ path,
146
+ this.project.cardKeysToCards(card.children),
147
+ );
148
148
  }
149
149
  }
150
150
  }
@@ -187,22 +187,7 @@ export class Export {
187
187
  children: [],
188
188
  attachments: [],
189
189
  };
190
-
191
- // Get content and attachments separately, not included in queries
192
- const fetchCardDetails: FetchCardDetails = {
193
- attachments: true,
194
- children: false,
195
- content: true,
196
- contentType: 'adoc',
197
- metadata: true,
198
- parent: false,
199
- };
200
-
201
- const cardDetailsResponse = await this.showCmd.showCardDetails(
202
- fetchCardDetails,
203
- card.key,
204
- );
205
-
190
+ const cardDetailsResponse = this.showCmd.showCardDetails(card.key, 'adoc');
206
191
  let asciiDocContent = '';
207
192
  const project = this.project;
208
193
  try {
@@ -227,7 +212,7 @@ export class Export {
227
212
  card.attachments = cardDetailsResponse.attachments;
228
213
 
229
214
  for (const result of treeQueryResult.children ?? []) {
230
- card.children!.push(await this.treeQueryResultToCard(result));
215
+ card.children!.push((await this.treeQueryResultToCard(result)).key);
231
216
  }
232
217
 
233
218
  return card;
@@ -332,13 +317,7 @@ export class Export {
332
317
  let cards: Card[] = [];
333
318
 
334
319
  // If doing a partial tree export, put the parent information as it would have already been gathered.
335
- if (cardKey) {
336
- const card = await this.project.findSpecificCard(cardKey);
337
- if (!card) {
338
- throw new Error(
339
- `Input validation error: cannot find card '${cardKey}'`,
340
- );
341
- }
320
+ if (cardKey && this.project.findCard(cardKey)) {
342
321
  cards.push({
343
322
  key: cardKey,
344
323
  path: sourcePath,
@@ -18,7 +18,8 @@ import type { Project } from '../containers/project.js';
18
18
  import { writeJsonFile } from '../utils/json.js';
19
19
  import { validateJson } from '../utils/validate.js';
20
20
  import { type ModuleSetting } from '../interfaces/project-interfaces.js';
21
- import { errorFunction, getChildLogger } from '../utils/log-utils.js';
21
+ import { errorFunction } from '../utils/error-utils.js';
22
+ import { getChildLogger } from '../utils/log-utils.js';
22
23
 
23
24
  const FETCH_TIMEOUT = 30000; // 30s timeout for fetching a hub file.
24
25
  const MAX_RESPONSE_SIZE = 1024 * 1024; // 1MB limit for safety
@@ -62,7 +62,7 @@ export class Import {
62
62
  throw new Error(`Template '${template}' not found`);
63
63
  }
64
64
 
65
- const templateCards = await templateObject.cards();
65
+ const templateCards = templateObject.cards();
66
66
  if (templateCards.length !== 1) {
67
67
  console.warn(
68
68
  `Template '${template}' for card '${title}' does not have exactly one card. Skipping row.`,
@@ -77,11 +77,9 @@ export class Import {
77
77
  throw new Error('Card not created');
78
78
  }
79
79
  const cardKey = cards[0].key;
80
- const card = await this.project.findSpecificCard(cardKey, {
81
- metadata: true,
82
- });
83
- const cardType = await this.project.resource<CardType>(
84
- card?.metadata?.cardType || '',
80
+ const card = this.project.findCard(cardKey);
81
+ const cardType = this.project.resource<CardType>(
82
+ card.metadata?.cardType || '',
85
83
  );
86
84
 
87
85
  if (!cardType) {