@cyberismo/data-handler 0.0.8 → 0.0.10

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 (258) hide show
  1. package/dist/command-handler.d.ts +11 -2
  2. package/dist/command-handler.js +61 -18
  3. package/dist/command-handler.js.map +1 -1
  4. package/dist/command-manager.js +8 -8
  5. package/dist/command-manager.js.map +1 -1
  6. package/dist/commands/calculate.d.ts +7 -44
  7. package/dist/commands/calculate.js +8 -389
  8. package/dist/commands/calculate.js.map +1 -1
  9. package/dist/commands/create.d.ts +6 -3
  10. package/dist/commands/create.js +27 -5
  11. package/dist/commands/create.js.map +1 -1
  12. package/dist/commands/edit.d.ts +15 -3
  13. package/dist/commands/edit.js +52 -9
  14. package/dist/commands/edit.js.map +1 -1
  15. package/dist/commands/export.d.ts +11 -9
  16. package/dist/commands/export.js +80 -28
  17. package/dist/commands/export.js.map +1 -1
  18. package/dist/commands/import.d.ts +7 -0
  19. package/dist/commands/import.js +13 -0
  20. package/dist/commands/import.js.map +1 -1
  21. package/dist/commands/move.d.ts +11 -12
  22. package/dist/commands/move.js +12 -13
  23. package/dist/commands/move.js.map +1 -1
  24. package/dist/commands/remove.d.ts +1 -3
  25. package/dist/commands/remove.js +4 -6
  26. package/dist/commands/remove.js.map +1 -1
  27. package/dist/commands/rename.d.ts +1 -3
  28. package/dist/commands/rename.js +3 -6
  29. package/dist/commands/rename.js.map +1 -1
  30. package/dist/commands/show.d.ts +37 -5
  31. package/dist/commands/show.js +89 -8
  32. package/dist/commands/show.js.map +1 -1
  33. package/dist/commands/transition.d.ts +1 -3
  34. package/dist/commands/transition.js +3 -5
  35. package/dist/commands/transition.js.map +1 -1
  36. package/dist/commands/update.d.ts +5 -1
  37. package/dist/commands/update.js +7 -1
  38. package/dist/commands/update.js.map +1 -1
  39. package/dist/commands/validate.d.ts +7 -0
  40. package/dist/commands/validate.js +31 -4
  41. package/dist/commands/validate.js.map +1 -1
  42. package/dist/containers/card-container.d.ts +6 -0
  43. package/dist/containers/card-container.js +61 -0
  44. package/dist/containers/card-container.js.map +1 -1
  45. package/dist/containers/project/calculation-engine.d.ts +90 -0
  46. package/dist/containers/project/calculation-engine.js +402 -0
  47. package/dist/containers/project/calculation-engine.js.map +1 -0
  48. package/dist/containers/project/resource-collector.js +9 -13
  49. package/dist/containers/project/resource-collector.js.map +1 -1
  50. package/dist/containers/project.d.ts +18 -1
  51. package/dist/containers/project.js +28 -55
  52. package/dist/containers/project.js.map +1 -1
  53. package/dist/containers/template.d.ts +5 -0
  54. package/dist/containers/template.js +9 -0
  55. package/dist/containers/template.js.map +1 -1
  56. package/dist/index.d.ts +5 -2
  57. package/dist/index.js +5 -1
  58. package/dist/index.js.map +1 -1
  59. package/dist/interfaces/macros.d.ts +4 -2
  60. package/dist/interfaces/project-interfaces.d.ts +21 -0
  61. package/dist/interfaces/project-interfaces.js +4 -0
  62. package/dist/interfaces/project-interfaces.js.map +1 -1
  63. package/dist/interfaces/resource-interfaces.d.ts +4 -2
  64. package/dist/interfaces/resource-interfaces.js.map +1 -1
  65. package/dist/macros/base-macro.d.ts +2 -1
  66. package/dist/macros/base-macro.js +14 -2
  67. package/dist/macros/base-macro.js.map +1 -1
  68. package/dist/macros/common.d.ts +16 -9
  69. package/dist/macros/common.js +22 -9
  70. package/dist/macros/common.js.map +1 -1
  71. package/dist/macros/createCards/index.d.ts +2 -2
  72. package/dist/macros/createCards/index.js +4 -0
  73. package/dist/macros/createCards/index.js.map +1 -1
  74. package/dist/macros/graph/index.d.ts +1 -3
  75. package/dist/macros/graph/index.js +1 -6
  76. package/dist/macros/graph/index.js.map +1 -1
  77. package/dist/macros/image/index.d.ts +39 -0
  78. package/dist/macros/image/index.js +82 -0
  79. package/dist/macros/image/index.js.map +1 -0
  80. package/dist/macros/image/metadata.d.ts +18 -0
  81. package/dist/macros/image/metadata.js +22 -0
  82. package/dist/macros/image/metadata.js.map +1 -0
  83. package/dist/macros/include/index.d.ts +31 -0
  84. package/dist/macros/include/index.js +94 -0
  85. package/dist/macros/include/index.js.map +1 -0
  86. package/dist/macros/include/metadata.d.ts +15 -0
  87. package/dist/macros/include/metadata.js +19 -0
  88. package/dist/macros/include/metadata.js.map +1 -0
  89. package/dist/macros/index.d.ts +33 -31
  90. package/dist/macros/index.js +142 -71
  91. package/dist/macros/index.js.map +1 -1
  92. package/dist/macros/percentage/index.d.ts +28 -0
  93. package/dist/macros/percentage/index.js +33 -0
  94. package/dist/macros/percentage/index.js.map +1 -0
  95. package/dist/macros/percentage/metadata.d.ts +15 -0
  96. package/dist/macros/percentage/metadata.js +19 -0
  97. package/dist/macros/percentage/metadata.js.map +1 -0
  98. package/dist/macros/report/index.d.ts +2 -6
  99. package/dist/macros/report/index.js +3 -7
  100. package/dist/macros/report/index.js.map +1 -1
  101. package/dist/macros/scoreCard/index.d.ts +14 -15
  102. package/dist/macros/scoreCard/index.js +14 -18
  103. package/dist/macros/scoreCard/index.js.map +1 -1
  104. package/dist/macros/vega/index.d.ts +28 -0
  105. package/dist/macros/vega/index.js +27 -0
  106. package/dist/macros/vega/index.js.map +1 -0
  107. package/dist/macros/vega/metadata.d.ts +15 -0
  108. package/dist/macros/vega/metadata.js +7 -0
  109. package/dist/macros/vega/metadata.js.map +1 -0
  110. package/dist/macros/vegalite/index.d.ts +26 -0
  111. package/dist/macros/vegalite/index.js +24 -0
  112. package/dist/macros/vegalite/index.js.map +1 -0
  113. package/dist/macros/vegalite/metadata.d.ts +15 -0
  114. package/dist/macros/vegalite/metadata.js +7 -0
  115. package/dist/macros/vegalite/metadata.js.map +1 -0
  116. package/dist/macros/xref/index.d.ts +26 -0
  117. package/dist/macros/xref/index.js +53 -0
  118. package/dist/macros/xref/index.js.map +1 -0
  119. package/dist/macros/xref/metadata.d.ts +15 -0
  120. package/dist/macros/xref/metadata.js +19 -0
  121. package/dist/macros/xref/metadata.js.map +1 -0
  122. package/dist/module-manager.d.ts +1 -0
  123. package/dist/module-manager.js +14 -4
  124. package/dist/module-manager.js.map +1 -1
  125. package/dist/permissions/action-guard.d.ts +2 -2
  126. package/dist/permissions/action-guard.js +1 -1
  127. package/dist/permissions/action-guard.js.map +1 -1
  128. package/dist/resources/card-type-resource.d.ts +2 -0
  129. package/dist/resources/card-type-resource.js +63 -0
  130. package/dist/resources/card-type-resource.js.map +1 -1
  131. package/dist/resources/file-resource.d.ts +2 -0
  132. package/dist/resources/file-resource.js +12 -4
  133. package/dist/resources/file-resource.js.map +1 -1
  134. package/dist/resources/folder-resource.d.ts +19 -0
  135. package/dist/resources/folder-resource.js +51 -2
  136. package/dist/resources/folder-resource.js.map +1 -1
  137. package/dist/resources/graph-model-resource.js +1 -1
  138. package/dist/resources/graph-model-resource.js.map +1 -1
  139. package/dist/resources/graph-view-resource.js +1 -1
  140. package/dist/resources/graph-view-resource.js.map +1 -1
  141. package/dist/resources/report-resource.js +1 -1
  142. package/dist/resources/report-resource.js.map +1 -1
  143. package/dist/resources/resource-object.d.ts +8 -0
  144. package/dist/resources/resource-object.js +10 -1
  145. package/dist/resources/resource-object.js.map +1 -1
  146. package/dist/resources/template-resource.js +1 -1
  147. package/dist/resources/template-resource.js.map +1 -1
  148. package/dist/resources/workflow-resource.d.ts +2 -0
  149. package/dist/resources/workflow-resource.js +53 -9
  150. package/dist/resources/workflow-resource.js.map +1 -1
  151. package/dist/svg/index.d.ts +15 -0
  152. package/dist/svg/index.js +16 -0
  153. package/dist/svg/index.js.map +1 -0
  154. package/dist/svg/lib.d.ts +9 -0
  155. package/dist/svg/lib.js +26 -0
  156. package/dist/svg/lib.js.map +1 -0
  157. package/dist/svg/percentage.d.ts +25 -0
  158. package/dist/svg/percentage.js +90 -0
  159. package/dist/svg/percentage.js.map +1 -0
  160. package/dist/svg/scoreCard.d.ts +19 -0
  161. package/dist/svg/scoreCard.js +55 -0
  162. package/dist/svg/scoreCard.js.map +1 -0
  163. package/dist/types/queries.d.ts +8 -7
  164. package/dist/types/queries.js.map +1 -1
  165. package/dist/utils/card-utils.d.ts +6 -0
  166. package/dist/utils/card-utils.js +12 -0
  167. package/dist/utils/card-utils.js.map +1 -1
  168. package/dist/utils/clingo-facts.d.ts +2 -1
  169. package/dist/utils/clingo-facts.js +19 -2
  170. package/dist/utils/clingo-facts.js.map +1 -1
  171. package/dist/utils/clingo-parser.d.ts +1 -0
  172. package/dist/utils/clingo-parser.js +22 -100
  173. package/dist/utils/clingo-parser.js.map +1 -1
  174. package/dist/utils/constants.d.ts +7 -0
  175. package/dist/utils/constants.js +14 -0
  176. package/dist/utils/constants.js.map +1 -1
  177. package/dist/utils/csv.js.map +1 -1
  178. package/dist/utils/report.d.ts +17 -3
  179. package/dist/utils/report.js +38 -2
  180. package/dist/utils/report.js.map +1 -1
  181. package/dist/utils/resource-utils.d.ts +2 -1
  182. package/dist/utils/resource-utils.js +12 -3
  183. package/dist/utils/resource-utils.js.map +1 -1
  184. package/dist/utils/user-preferences.d.ts +1 -11
  185. package/dist/utils/user-preferences.js +30 -13
  186. package/dist/utils/user-preferences.js.map +1 -1
  187. package/dist/utils/validate.d.ts +2 -3
  188. package/dist/utils/validate.js +2 -2
  189. package/dist/utils/validate.js.map +1 -1
  190. package/package.json +8 -6
  191. package/src/command-handler.ts +96 -17
  192. package/src/command-manager.ts +8 -8
  193. package/src/commands/calculate.ts +11 -525
  194. package/src/commands/create.ts +35 -6
  195. package/src/commands/edit.ts +94 -11
  196. package/src/commands/export.ts +104 -31
  197. package/src/commands/import.ts +16 -0
  198. package/src/commands/move.ts +12 -15
  199. package/src/commands/remove.ts +4 -8
  200. package/src/commands/rename.ts +3 -12
  201. package/src/commands/show.ts +126 -9
  202. package/src/commands/transition.ts +3 -7
  203. package/src/commands/update.ts +6 -0
  204. package/src/commands/validate.ts +41 -13
  205. package/src/containers/card-container.ts +74 -0
  206. package/src/containers/project/calculation-engine.ts +535 -0
  207. package/src/containers/project/resource-collector.ts +13 -15
  208. package/src/containers/project.ts +30 -66
  209. package/src/containers/template.ts +16 -0
  210. package/src/index.ts +13 -2
  211. package/src/interfaces/macros.ts +4 -1
  212. package/src/interfaces/project-interfaces.ts +27 -0
  213. package/src/interfaces/resource-interfaces.ts +5 -2
  214. package/src/macros/base-macro.ts +19 -4
  215. package/src/macros/common.ts +22 -9
  216. package/src/macros/createCards/index.ts +6 -2
  217. package/src/macros/graph/index.ts +6 -10
  218. package/src/macros/image/index.ts +128 -0
  219. package/src/macros/image/metadata.ts +25 -0
  220. package/src/macros/include/index.ts +143 -0
  221. package/src/macros/include/metadata.ts +22 -0
  222. package/src/macros/index.ts +150 -98
  223. package/src/macros/percentage/index.ts +50 -0
  224. package/src/macros/percentage/metadata.ts +22 -0
  225. package/src/macros/report/index.ts +4 -12
  226. package/src/macros/scoreCard/index.ts +21 -25
  227. package/src/macros/vega/index.ts +55 -0
  228. package/src/macros/vega/metadata.ts +21 -0
  229. package/src/macros/vegalite/index.ts +46 -0
  230. package/src/macros/vegalite/metadata.ts +21 -0
  231. package/src/macros/xref/index.ts +73 -0
  232. package/src/macros/xref/metadata.ts +22 -0
  233. package/src/module-manager.ts +15 -5
  234. package/src/permissions/action-guard.ts +3 -3
  235. package/src/resources/card-type-resource.ts +100 -0
  236. package/src/resources/file-resource.ts +16 -4
  237. package/src/resources/folder-resource.ts +59 -2
  238. package/src/resources/graph-model-resource.ts +1 -1
  239. package/src/resources/graph-view-resource.ts +1 -1
  240. package/src/resources/report-resource.ts +1 -1
  241. package/src/resources/resource-object.ts +19 -1
  242. package/src/resources/template-resource.ts +1 -1
  243. package/src/resources/workflow-resource.ts +68 -13
  244. package/src/svg/index.ts +15 -0
  245. package/src/svg/lib.ts +31 -0
  246. package/src/svg/percentage.ts +97 -0
  247. package/src/svg/scoreCard.ts +88 -0
  248. package/src/types/queries.ts +8 -7
  249. package/src/types/string-pixel-width.d.ts +23 -0
  250. package/src/utils/card-utils.ts +13 -0
  251. package/src/utils/clingo-facts.ts +65 -3
  252. package/src/utils/clingo-parser.ts +31 -144
  253. package/src/utils/constants.ts +16 -0
  254. package/src/utils/csv.ts +1 -1
  255. package/src/utils/report.ts +45 -4
  256. package/src/utils/resource-utils.ts +12 -2
  257. package/src/utils/user-preferences.ts +32 -14
  258. package/src/utils/validate.ts +3 -3
@@ -13,19 +13,18 @@
13
13
  */
14
14
 
15
15
  // node
16
- import { type Dirent } from 'node:fs';
17
16
  import { dirname, join, resolve, sep } from 'node:path';
18
17
  import { readdir } from 'node:fs/promises';
19
18
 
20
19
  import { CardContainer } from './card-container.js'; // base class
21
20
 
21
+ import { CalculationEngine } from './project/calculation-engine.js';
22
22
  import {
23
23
  type Card,
24
24
  type CardAttachment,
25
25
  CardLocation,
26
26
  type CardListContainer,
27
27
  type CardMetadata,
28
- CardNameRegEx,
29
28
  type FetchCardDetails,
30
29
  type MetadataContent,
31
30
  type ModuleContent,
@@ -36,7 +35,6 @@ import {
36
35
  type Resource,
37
36
  type ResourceFolderType,
38
37
  } from '../interfaces/project-interfaces.js';
39
- import { findParentPath } from '../utils/card-utils.js';
40
38
  import { getFilesSync, pathExists } from '../utils/file-utils.js';
41
39
  import { generateRandomString } from '../utils/random.js';
42
40
  import { isTemplateCard } from '../utils/card-utils.js';
@@ -74,6 +72,7 @@ export { ResourcesFrom };
74
72
  * Represents project folder.
75
73
  */
76
74
  export class Project extends CardContainer {
75
+ public calculationEngine: CalculationEngine;
77
76
  private resources: ResourceCollector;
78
77
  private projectPaths: ProjectPaths;
79
78
  private settings: ProjectConfiguration;
@@ -90,6 +89,8 @@ export class Project extends CardContainer {
90
89
  ) {
91
90
  super(path, '');
92
91
 
92
+ this.calculationEngine = new CalculationEngine(this);
93
+
93
94
  this.settings = new ProjectConfiguration(
94
95
  join(path, '.cards', 'local', Project.projectConfigFileName),
95
96
  );
@@ -183,7 +184,7 @@ export class Project extends CardContainer {
183
184
  * @returns array of all calculation files in the project.
184
185
  */
185
186
  public async calculations(
186
- from: ResourcesFrom = ResourcesFrom.localOnly,
187
+ from: ResourcesFrom = ResourcesFrom.all,
187
188
  ): Promise<Resource[]> {
188
189
  return this.resources.resources('calculations', from);
189
190
  }
@@ -477,6 +478,30 @@ export class Project extends CardContainer {
477
478
  return this.resources.resources('graphViews', from);
478
479
  }
479
480
 
481
+ /**
482
+ * When card changes.
483
+ * @param changedCard Card that was changed.
484
+ */
485
+ public async handleCardChanged(changedCard: Card) {
486
+ return this.calculationEngine.handleCardChanged(changedCard);
487
+ }
488
+
489
+ /**
490
+ * When cards are removed.
491
+ * @param deletedCard Card that is to be removed.
492
+ */
493
+ public async handleDeleteCard(deletedCard: Card) {
494
+ return this.calculationEngine.handleDeleteCard(deletedCard);
495
+ }
496
+
497
+ /**
498
+ * When new cards are added.
499
+ * @param cards Added cards.
500
+ */
501
+ public async handleNewCards(cards: Card[]) {
502
+ return this.calculationEngine.handleNewCards(cards);
503
+ }
504
+
480
505
  /**
481
506
  * Checks if a given card is part of this project.
482
507
  * @param cardKey card to check.
@@ -986,68 +1011,7 @@ export class Project extends CardContainer {
986
1011
  * @returns an array of all project cards in the project.
987
1012
  */
988
1013
  public async showProjectCards(): Promise<Card[]> {
989
- const cards: Card[] = [];
990
- const cardPathMap = new Map<string, Card>();
991
- const entries = await readdir(this.paths.cardRootFolder, {
992
- withFileTypes: true,
993
- recursive: true,
994
- });
995
-
996
- // Checks if Dirent folder is a card folder
997
- function cardFolder(
998
- entry: Dirent,
999
- cardPathMap: Map<string, Card>,
1000
- ): Card | undefined {
1001
- const fullPath = join(entry.parentPath, entry.name);
1002
- if (!cardPathMap.has(fullPath)) {
1003
- const newCard: Card = {
1004
- key: entry.name,
1005
- path: fullPath,
1006
- children: [],
1007
- attachments: [],
1008
- };
1009
- cardPathMap.set(fullPath, newCard);
1010
- return newCard;
1011
- }
1012
- }
1013
-
1014
- // Process card directories first
1015
- entries
1016
- .filter((entry) => entry.isDirectory() && CardNameRegEx.test(entry.name))
1017
- .forEach((entry) => {
1018
- const card = cardFolder(entry, cardPathMap);
1019
- if (card) cards.push(card);
1020
- });
1021
-
1022
- // Process metadata files in parallel
1023
- await Promise.all(
1024
- entries
1025
- .filter(
1026
- (entry) => entry.isFile() && entry.name === Project.cardMetadataFile,
1027
- )
1028
- .map(async (entry) => {
1029
- const parentCard = cardPathMap.get(entry.parentPath);
1030
- if (!parentCard) return;
1031
- parentCard.metadata = (await readJsonFile(
1032
- join(entry.parentPath, entry.name),
1033
- )) as CardMetadata;
1034
- }),
1035
- );
1036
-
1037
- // Finally, build the card hierarchy
1038
- Array.from(cardPathMap.entries()).map(([cardPath, card]) => {
1039
- const parentPath = findParentPath(cardPath);
1040
- if (!parentPath) return;
1041
- const parentCard = cardPathMap.get(parentPath);
1042
- if (!parentCard) return;
1043
-
1044
- parentCard.children.push(card);
1045
- const index = cards.indexOf(card);
1046
- if (index > -1) {
1047
- cards.splice(index, 1);
1048
- }
1049
- });
1050
- return cards;
1014
+ return this.showCards(this.paths.cardRootFolder);
1051
1015
  }
1052
1016
 
1053
1017
  /**
@@ -178,6 +178,14 @@ export class Template extends CardContainer {
178
178
  await Promise.all(
179
179
  card.attachments.map(async (attachment) => {
180
180
  const attachmentUniqueName = `${card.key}-${attachment.fileName}`;
181
+ content = content?.replace(
182
+ new RegExp(
183
+ `(\\{\\{#image\\}\\}[^}]*)"fileName": "${attachment.fileName}"([^}]*\\{\\{\\/image\\}\\})`,
184
+ 'g',
185
+ ),
186
+ `$1"fileName": "${attachmentUniqueName}"$2`,
187
+ );
188
+ // keep fallback
181
189
  content = content?.replace(
182
190
  new RegExp(`image::${attachment.fileName}`, 'g'),
183
191
  `image::${attachmentUniqueName}`,
@@ -575,4 +583,12 @@ export class Template extends CardContainer {
575
583
  public templateFolder(): string {
576
584
  return this.templatePath;
577
585
  }
586
+
587
+ /**
588
+ * Show cards of a template with hierarchy structure.
589
+ * @returns an array of all template cards with proper parent-child relationships.
590
+ */
591
+ public async showTemplateCards(): Promise<Card[]> {
592
+ return this.showCards(this.templateCardsPath);
593
+ }
578
594
  }
package/src/index.ts CHANGED
@@ -17,19 +17,30 @@ import {
17
17
  CommandManager,
18
18
  ExportFormats,
19
19
  } from './command-handler.js';
20
- import type { Credentials } from './interfaces/project-interfaces.js';
20
+ import { Validate } from './commands/validate.js';
21
+ export * from './interfaces/project-interfaces.js';
21
22
  import { requestStatus } from './interfaces/request-status-interfaces.js';
22
23
  import { UpdateOperations } from './resources/resource-object.js';
23
24
  import { evaluateMacros } from './macros/index.js';
25
+ import {
26
+ isResourceFolderType,
27
+ resourceName,
28
+ resourceNameToString,
29
+ } from './utils/resource-utils.js';
30
+ import { moduleNameFromCardKey } from './utils/card-utils.js';
24
31
 
25
32
  export {
26
33
  CardsOptions,
27
34
  Cmd,
28
35
  CommandManager,
29
36
  Commands,
30
- Credentials,
31
37
  ExportFormats,
38
+ isResourceFolderType,
39
+ moduleNameFromCardKey,
32
40
  requestStatus,
41
+ resourceName,
42
+ resourceNameToString,
33
43
  UpdateOperations,
44
+ Validate,
34
45
  evaluateMacros,
35
46
  };
@@ -13,13 +13,16 @@
13
13
  import type { macroMetadata } from '../macros/common.js';
14
14
  import type { Project } from '../containers/project.js';
15
15
  import type { MacroError } from '../exceptions/index.js';
16
+ import type { Context } from './project-interfaces.js';
16
17
 
17
- type Mode = 'validate' | 'static' | 'inject';
18
+ export type Mode = 'validate' | 'static' | 'inject' | 'staticSite';
18
19
 
19
20
  export interface MacroGenerationContext {
21
+ context: Context;
20
22
  project: Project;
21
23
  mode: Mode;
22
24
  cardKey: string;
25
+ maxTries?: number;
23
26
  }
24
27
 
25
28
  export interface MacroMetadata {
@@ -62,6 +62,14 @@ export interface CardMetadata extends PredefinedCardMetadata {
62
62
  [key: string]: MetadataContent;
63
63
  }
64
64
 
65
+ export const validContexts = ['localApp', 'exportedSite', 'exportedDocument'];
66
+
67
+ export type Context = 'localApp' | 'exportedSite' | 'exportedDocument';
68
+
69
+ export const isContext = (context: string): context is Context => {
70
+ return validContexts.includes(context);
71
+ };
72
+
65
73
  // Content in project (apart from cards) is either .schema files or cardsConfig.json.
66
74
  type ContentType = DotSchemaContent | ProjectSettings;
67
75
 
@@ -97,6 +105,25 @@ export interface ProjectFetchCardDetails extends FetchCardDetails {
97
105
  location?: CardLocation;
98
106
  }
99
107
 
108
+ /**
109
+ * Options for exporting to pdf.
110
+ * @param name - Name of the exported document.
111
+ * @param title - Title of the exported document.
112
+ * @param date - Date of the exported document.
113
+ * @param cardKey - Key of the card to be exported.
114
+ * @param recursive - Whether to export recursively ie. export all cards below the given card.
115
+ * @param revremark - Revision remark of the exported document.
116
+ */
117
+ export interface ExportPdfOptions {
118
+ name: string;
119
+ title: string;
120
+ date?: Date;
121
+ cardKey?: string;
122
+ recursive?: boolean;
123
+ revremark?: string;
124
+ version?: string;
125
+ }
126
+
100
127
  export type FileContentType = 'adoc' | 'html';
101
128
 
102
129
  // Metadata content type.
@@ -19,8 +19,10 @@ import type { Schema } from 'jsonschema';
19
19
  * in a correct place in folder structure.
20
20
  */
21
21
 
22
- // Calculation does not have own metadata.
23
- export type CalculationMetadata = ResourceBaseMetadata;
22
+ // Calculation metadata.
23
+ export interface CalculationMetadata extends ResourceBaseMetadata {
24
+ calculation: string;
25
+ }
24
26
 
25
27
  // Card type content.
26
28
  export interface CardType extends ResourceBaseMetadata {
@@ -123,6 +125,7 @@ export interface ResourceBaseMetadata {
123
125
 
124
126
  // All resources metadata content.
125
127
  export type ResourceContent =
128
+ | CalculationMetadata
126
129
  | CardType
127
130
  | FieldType
128
131
  | GraphModel
@@ -42,10 +42,21 @@ abstract class BaseMacro {
42
42
  this.globalId = `${generateRandomString(36, 10)}`;
43
43
  }
44
44
 
45
- protected abstract handleInject(
45
+ // Default: exportSite uses inject output unless overridden by subclass
46
+ protected handleStaticSite(
46
47
  context: MacroGenerationContext,
47
48
  input: unknown,
48
- ): Promise<string>;
49
+ ): Promise<string> {
50
+ return this.handleInject(context, input);
51
+ }
52
+
53
+ // Default: inject uses static output unless overridden by subclass
54
+ protected handleInject(
55
+ context: MacroGenerationContext,
56
+ input: unknown,
57
+ ): Promise<string> {
58
+ return this.handleStatic(context, input);
59
+ }
49
60
 
50
61
  protected abstract handleStatic(
51
62
  context: MacroGenerationContext,
@@ -181,10 +192,14 @@ abstract class BaseMacro {
181
192
 
182
193
  // Select the function to execute based on context mode
183
194
  const functionToCall =
184
- context.mode === 'inject' ? this.handleInject : this.handleStatic;
195
+ context.mode === 'inject'
196
+ ? this.handleInject
197
+ : context.mode === 'staticSite'
198
+ ? this.handleStaticSite
199
+ : this.handleStatic;
185
200
 
186
201
  // Execute the function and handle its result
187
- return functionToCall(context, parsed);
202
+ return functionToCall.call(this, context, parsed);
188
203
  })
189
204
  .then((result) => {
190
205
  // undefined is used to indicate that the macro did not run for some reason
@@ -1,24 +1,37 @@
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
  // important that this file imports only the metadata
14
15
  import createCards from './createCards/metadata.js';
15
16
  import graph from './graph/metadata.js';
17
+ import image from './image/metadata.js';
18
+ import include from './include/metadata.js';
16
19
  import report from './report/metadata.js';
17
20
  import scoreCard from './scoreCard/metadata.js';
21
+ import xref from './xref/metadata.js';
22
+ import percentage from './percentage/metadata.js';
23
+ import vega from './vega/metadata.js';
24
+ import vegaLite from './vegalite/metadata.js';
18
25
 
19
26
  export const macroMetadata = {
20
27
  createCards,
21
28
  graph,
29
+ image,
30
+ include,
22
31
  report,
23
32
  scoreCard,
33
+ xref,
34
+ percentage,
35
+ vega,
36
+ vegaLite,
24
37
  };
@@ -10,7 +10,6 @@
10
10
  License along with this program. If not, see <https://www.gnu.org/licenses/>.
11
11
  */
12
12
 
13
- import type { MacroOptions } from '../index.js';
14
13
  import { createHtmlPlaceholder, validateMacroContent } from '../index.js';
15
14
 
16
15
  import type { MacroGenerationContext } from '../../interfaces/macros.js';
@@ -18,7 +17,7 @@ import macroMetadata from './metadata.js';
18
17
  import BaseMacro from '../base-macro.js';
19
18
  import type TaskQueue from '../task-queue.js';
20
19
 
21
- export interface CreateCardsOptions extends MacroOptions {
20
+ export interface CreateCardsOptions {
22
21
  buttonLabel: string;
23
22
  template: string;
24
23
  cardKey?: string;
@@ -49,6 +48,11 @@ class CreateCardsMacro extends BaseMacro {
49
48
  return createHtmlPlaceholder(this.metadata, options);
50
49
  };
51
50
 
51
+ handleStaticSite = async () => {
52
+ // export site does not support createCards
53
+ return '';
54
+ };
55
+
52
56
  private validate(input: unknown): CreateCardsOptions {
53
57
  return validateMacroContent<CreateCardsOptions>(this.metadata, input);
54
58
  }
@@ -12,8 +12,6 @@
12
12
  */
13
13
 
14
14
  import BaseMacro from '../base-macro.js';
15
- import { Calculate } from '../../commands/index.js';
16
- import type { MacroOptions } from '../index.js';
17
15
  import { createImage, validateMacroContent } from '../index.js';
18
16
  import Handlebars from 'handlebars';
19
17
  import { join } from 'node:path';
@@ -27,7 +25,7 @@ import { validateJson } from '../../utils/validate.js';
27
25
  import type TaskQueue from '../task-queue.js';
28
26
  import { ClingoError } from '@cyberismo/node-clingo';
29
27
 
30
- export interface GraphOptions extends MacroOptions {
28
+ export interface GraphOptions {
31
29
  model: string;
32
30
  view: string;
33
31
  }
@@ -42,12 +40,6 @@ class ReportMacro extends BaseMacro {
42
40
  };
43
41
 
44
42
  handleStatic = async (context: MacroGenerationContext, input: unknown) => {
45
- return this.handleInject(context, input);
46
- };
47
-
48
- handleInject = async (context: MacroGenerationContext, input: unknown) => {
49
- const calculate = new Calculate(context.project);
50
-
51
43
  const resourceNameToPath = (name: string, fileName: string) => {
52
44
  const { identifier, prefix, type } = resourceName(name);
53
45
  if (prefix === context.project.projectPrefix) {
@@ -114,7 +106,11 @@ class ReportMacro extends BaseMacro {
114
106
  const modelContent = await readFile(modelLocation, { encoding: 'utf-8' });
115
107
  let result: string;
116
108
  try {
117
- result = await calculate.runGraph(modelContent, view);
109
+ result = await context.project.calculationEngine.runGraph(
110
+ modelContent,
111
+ view,
112
+ context.context,
113
+ );
118
114
  } catch (error) {
119
115
  if (error instanceof ClingoError) {
120
116
  throw new Error(
@@ -0,0 +1,128 @@
1
+ /**
2
+ Cyberismo
3
+ Copyright © Cyberismo Ltd and contributors 2025
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/>.
12
+ */
13
+
14
+ import { existsSync, readFileSync } from 'node:fs';
15
+ import { join } from 'node:path';
16
+ import mime from 'mime-types';
17
+
18
+ import { validateMacroContent } from '../index.js';
19
+
20
+ import type { MacroGenerationContext } from '../../interfaces/macros.js';
21
+ import macroMetadata from './metadata.js';
22
+ import BaseMacro from '../base-macro.js';
23
+ import type TaskQueue from '../task-queue.js';
24
+
25
+ /**
26
+ * Options for the image macro.
27
+ * @param fileName - Name of the file to include.
28
+ * @param cardKey - Key of the card to include the file from.
29
+ * @param alt - Alternative text for the image.
30
+ * @param title - Title of the image.
31
+ */
32
+ export interface ImageMacroOptions {
33
+ fileName: string;
34
+ cardKey?: string;
35
+ alt?: string;
36
+ title?: string;
37
+ }
38
+
39
+ /**
40
+ * Macro for including images in the content
41
+ */
42
+ export default class ImageMacro extends BaseMacro {
43
+ constructor(tasksQueue: TaskQueue) {
44
+ super(macroMetadata, tasksQueue);
45
+ }
46
+
47
+ handleValidate = (input: unknown) => {
48
+ this.validate(input);
49
+ };
50
+
51
+ handleStatic = async (
52
+ context: MacroGenerationContext,
53
+ input: unknown,
54
+ ): Promise<string> => {
55
+ const options = this.validate(input);
56
+ const cardKey = options.cardKey || context.cardKey;
57
+
58
+ // Get the attachment folder path
59
+ const attachmentFolder =
60
+ await context.project.cardAttachmentFolder(cardKey);
61
+ if (!attachmentFolder) {
62
+ throw new Error(`Card '${cardKey}' not found`);
63
+ }
64
+
65
+ // Read the file and convert to base64
66
+ const attachmentPath = join(attachmentFolder, options.fileName);
67
+ if (!existsSync(attachmentPath)) {
68
+ throw new Error(
69
+ `Attachment file '${options.fileName}' not found in card '${cardKey}'`,
70
+ );
71
+ }
72
+
73
+ const fileBuffer = readFileSync(attachmentPath);
74
+ const base64Data = fileBuffer.toString('base64');
75
+
76
+ // Get mime type
77
+ const mimeType = mime.lookup(attachmentPath) || 'application/octet-stream';
78
+
79
+ // Build image attributes
80
+ const attributes = this.buildImageAttributes(options);
81
+
82
+ // Return as data URI for static mode (for export/PDF generation)
83
+ return `image::data:${mimeType};base64,${base64Data}[${attributes}]`;
84
+ };
85
+
86
+ handleInject = async (context: MacroGenerationContext, input: unknown) => {
87
+ const options = this.validate(input);
88
+ const cardKey = options.cardKey || context.cardKey;
89
+
90
+ // Verify that the card and attachment folder exist
91
+ const attachmentFolder =
92
+ await context.project.cardAttachmentFolder(cardKey);
93
+ if (!attachmentFolder) {
94
+ throw new Error(`Card '${cardKey}' not found`);
95
+ }
96
+
97
+ const attachmentPath = join(attachmentFolder, options.fileName);
98
+ if (!existsSync(attachmentPath)) {
99
+ throw new Error(
100
+ `Attachment file '${options.fileName}' not found in card '${cardKey}'`,
101
+ );
102
+ }
103
+
104
+ // Build image attributes
105
+ const attributes = this.buildImageAttributes(options);
106
+
107
+ // In inject mode, always use the API path for consistency
108
+ return `image::/api/cards/${cardKey}/a/${options.fileName}[${attributes}]`;
109
+ };
110
+
111
+ private buildImageAttributes(options: ImageMacroOptions): string {
112
+ const attributes: string[] = [];
113
+
114
+ if (options.alt !== undefined) {
115
+ attributes.push(`alt="${options.alt}"`);
116
+ }
117
+
118
+ if (options.title !== undefined) {
119
+ attributes.push(`title="${options.title}"`);
120
+ }
121
+
122
+ return attributes.join(',');
123
+ }
124
+
125
+ private validate(input: unknown): ImageMacroOptions {
126
+ return validateMacroContent<ImageMacroOptions>(this.metadata, input);
127
+ }
128
+ }
@@ -0,0 +1,25 @@
1
+ /**
2
+ Cyberismo
3
+ Copyright © Cyberismo Ltd and contributors 2025
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/>.
12
+ */
13
+
14
+ import type { MacroMetadata } from '../../interfaces/macros.js';
15
+
16
+ /**
17
+ * Metadata for the image macro.
18
+ */
19
+ const macroMetadata: MacroMetadata = {
20
+ name: 'image',
21
+ tagName: 'image',
22
+ schema: 'imageMacroSchema',
23
+ };
24
+
25
+ export default macroMetadata;