@cyberismo/data-handler 0.0.7 → 0.0.9

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 (265) 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 +7 -4
  10. package/dist/commands/create.js +42 -15
  11. package/dist/commands/create.js.map +1 -1
  12. package/dist/commands/edit.d.ts +9 -3
  13. package/dist/commands/edit.js +33 -9
  14. package/dist/commands/edit.js.map +1 -1
  15. package/dist/commands/export.d.ts +13 -11
  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 +21 -2
  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 +2 -4
  25. package/dist/commands/remove.js +8 -16
  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 +85 -7
  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 -8
  40. package/dist/commands/validate.js +30 -35
  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.d.ts +2 -1
  49. package/dist/containers/project/resource-collector.js +41 -33
  50. package/dist/containers/project/resource-collector.js.map +1 -1
  51. package/dist/containers/project.d.ts +18 -6
  52. package/dist/containers/project.js +37 -72
  53. package/dist/containers/project.js.map +1 -1
  54. package/dist/containers/template.d.ts +5 -0
  55. package/dist/containers/template.js +9 -0
  56. package/dist/containers/template.js.map +1 -1
  57. package/dist/exceptions/index.d.ts +20 -0
  58. package/dist/exceptions/index.js +16 -0
  59. package/dist/exceptions/index.js.map +1 -1
  60. package/dist/index.d.ts +5 -2
  61. package/dist/index.js +5 -1
  62. package/dist/index.js.map +1 -1
  63. package/dist/interfaces/macros.d.ts +7 -2
  64. package/dist/interfaces/project-interfaces.d.ts +21 -0
  65. package/dist/interfaces/project-interfaces.js +4 -0
  66. package/dist/interfaces/project-interfaces.js.map +1 -1
  67. package/dist/interfaces/resource-interfaces.d.ts +1 -1
  68. package/dist/interfaces/resource-interfaces.js.map +1 -1
  69. package/dist/macros/base-macro.d.ts +2 -0
  70. package/dist/macros/base-macro.js +66 -19
  71. package/dist/macros/base-macro.js.map +1 -1
  72. package/dist/macros/common.d.ts +16 -9
  73. package/dist/macros/common.js +22 -9
  74. package/dist/macros/common.js.map +1 -1
  75. package/dist/macros/createCards/index.d.ts +1 -2
  76. package/dist/macros/createCards/index.js.map +1 -1
  77. package/dist/macros/graph/index.d.ts +1 -3
  78. package/dist/macros/graph/index.js +11 -9
  79. package/dist/macros/graph/index.js.map +1 -1
  80. package/dist/macros/image/index.d.ts +39 -0
  81. package/dist/macros/image/index.js +78 -0
  82. package/dist/macros/image/index.js.map +1 -0
  83. package/dist/macros/image/metadata.d.ts +18 -0
  84. package/dist/macros/image/metadata.js +22 -0
  85. package/dist/macros/image/metadata.js.map +1 -0
  86. package/dist/macros/include/index.d.ts +32 -0
  87. package/dist/macros/include/index.js +97 -0
  88. package/dist/macros/include/index.js.map +1 -0
  89. package/dist/macros/include/metadata.d.ts +15 -0
  90. package/dist/macros/include/metadata.js +19 -0
  91. package/dist/macros/include/metadata.js.map +1 -0
  92. package/dist/macros/index.d.ts +39 -31
  93. package/dist/macros/index.js +167 -73
  94. package/dist/macros/index.js.map +1 -1
  95. package/dist/macros/percentage/index.d.ts +29 -0
  96. package/dist/macros/percentage/index.js +36 -0
  97. package/dist/macros/percentage/index.js.map +1 -0
  98. package/dist/macros/percentage/metadata.d.ts +15 -0
  99. package/dist/macros/percentage/metadata.js +19 -0
  100. package/dist/macros/percentage/metadata.js.map +1 -0
  101. package/dist/macros/report/index.d.ts +2 -5
  102. package/dist/macros/report/index.js +20 -12
  103. package/dist/macros/report/index.js.map +1 -1
  104. package/dist/macros/scoreCard/index.d.ts +15 -15
  105. package/dist/macros/scoreCard/index.js +16 -17
  106. package/dist/macros/scoreCard/index.js.map +1 -1
  107. package/dist/macros/vega/index.d.ts +28 -0
  108. package/dist/macros/vega/index.js +27 -0
  109. package/dist/macros/vega/index.js.map +1 -0
  110. package/dist/macros/vega/metadata.d.ts +15 -0
  111. package/dist/macros/vega/metadata.js +7 -0
  112. package/dist/macros/vega/metadata.js.map +1 -0
  113. package/dist/macros/vegalite/index.d.ts +27 -0
  114. package/dist/macros/vegalite/index.js +27 -0
  115. package/dist/macros/vegalite/index.js.map +1 -0
  116. package/dist/macros/vegalite/metadata.d.ts +15 -0
  117. package/dist/macros/vegalite/metadata.js +7 -0
  118. package/dist/macros/vegalite/metadata.js.map +1 -0
  119. package/dist/macros/xref/index.d.ts +26 -0
  120. package/dist/macros/xref/index.js +53 -0
  121. package/dist/macros/xref/index.js.map +1 -0
  122. package/dist/macros/xref/metadata.d.ts +15 -0
  123. package/dist/macros/xref/metadata.js +19 -0
  124. package/dist/macros/xref/metadata.js.map +1 -0
  125. package/dist/module-manager.d.ts +17 -4
  126. package/dist/module-manager.js +192 -58
  127. package/dist/module-manager.js.map +1 -1
  128. package/dist/permissions/action-guard.d.ts +2 -2
  129. package/dist/permissions/action-guard.js +1 -1
  130. package/dist/permissions/action-guard.js.map +1 -1
  131. package/dist/project-settings.js +2 -8
  132. package/dist/project-settings.js.map +1 -1
  133. package/dist/resources/card-type-resource.d.ts +2 -0
  134. package/dist/resources/card-type-resource.js +63 -0
  135. package/dist/resources/card-type-resource.js.map +1 -1
  136. package/dist/resources/file-resource.d.ts +2 -0
  137. package/dist/resources/file-resource.js +12 -4
  138. package/dist/resources/file-resource.js.map +1 -1
  139. package/dist/resources/folder-resource.d.ts +19 -0
  140. package/dist/resources/folder-resource.js +51 -2
  141. package/dist/resources/folder-resource.js.map +1 -1
  142. package/dist/resources/graph-model-resource.js +1 -1
  143. package/dist/resources/graph-model-resource.js.map +1 -1
  144. package/dist/resources/graph-view-resource.js +1 -1
  145. package/dist/resources/graph-view-resource.js.map +1 -1
  146. package/dist/resources/report-resource.js +1 -1
  147. package/dist/resources/report-resource.js.map +1 -1
  148. package/dist/resources/resource-object.d.ts +8 -0
  149. package/dist/resources/resource-object.js +9 -0
  150. package/dist/resources/resource-object.js.map +1 -1
  151. package/dist/resources/template-resource.js +1 -1
  152. package/dist/resources/template-resource.js.map +1 -1
  153. package/dist/resources/workflow-resource.d.ts +2 -0
  154. package/dist/resources/workflow-resource.js +53 -9
  155. package/dist/resources/workflow-resource.js.map +1 -1
  156. package/dist/svg/index.d.ts +15 -0
  157. package/dist/svg/index.js +16 -0
  158. package/dist/svg/index.js.map +1 -0
  159. package/dist/svg/lib.d.ts +9 -0
  160. package/dist/svg/lib.js +26 -0
  161. package/dist/svg/lib.js.map +1 -0
  162. package/dist/svg/percentage.d.ts +25 -0
  163. package/dist/svg/percentage.js +90 -0
  164. package/dist/svg/percentage.js.map +1 -0
  165. package/dist/svg/scoreCard.d.ts +19 -0
  166. package/dist/svg/scoreCard.js +55 -0
  167. package/dist/svg/scoreCard.js.map +1 -0
  168. package/dist/types/queries.d.ts +8 -7
  169. package/dist/types/queries.js.map +1 -1
  170. package/dist/utils/card-utils.d.ts +6 -0
  171. package/dist/utils/card-utils.js +12 -0
  172. package/dist/utils/card-utils.js.map +1 -1
  173. package/dist/utils/clingo-facts.d.ts +2 -1
  174. package/dist/utils/clingo-facts.js +19 -2
  175. package/dist/utils/clingo-facts.js.map +1 -1
  176. package/dist/utils/clingo-parser.d.ts +1 -0
  177. package/dist/utils/clingo-parser.js +22 -100
  178. package/dist/utils/clingo-parser.js.map +1 -1
  179. package/dist/utils/constants.d.ts +7 -0
  180. package/dist/utils/constants.js +14 -0
  181. package/dist/utils/constants.js.map +1 -1
  182. package/dist/utils/csv.js.map +1 -1
  183. package/dist/utils/report.d.ts +17 -3
  184. package/dist/utils/report.js +38 -2
  185. package/dist/utils/report.js.map +1 -1
  186. package/dist/utils/resource-utils.d.ts +1 -0
  187. package/dist/utils/resource-utils.js +9 -0
  188. package/dist/utils/resource-utils.js.map +1 -1
  189. package/dist/utils/user-preferences.d.ts +1 -11
  190. package/dist/utils/user-preferences.js +30 -13
  191. package/dist/utils/user-preferences.js.map +1 -1
  192. package/dist/utils/validate.d.ts +2 -3
  193. package/dist/utils/validate.js +2 -2
  194. package/dist/utils/validate.js.map +1 -1
  195. package/package.json +8 -6
  196. package/src/command-handler.ts +96 -17
  197. package/src/command-manager.ts +8 -8
  198. package/src/commands/calculate.ts +11 -525
  199. package/src/commands/create.ts +53 -16
  200. package/src/commands/edit.ts +53 -11
  201. package/src/commands/export.ts +108 -34
  202. package/src/commands/import.ts +31 -2
  203. package/src/commands/move.ts +12 -15
  204. package/src/commands/remove.ts +10 -19
  205. package/src/commands/rename.ts +3 -12
  206. package/src/commands/show.ts +121 -8
  207. package/src/commands/transition.ts +3 -7
  208. package/src/commands/update.ts +6 -0
  209. package/src/commands/validate.ts +39 -47
  210. package/src/containers/card-container.ts +74 -0
  211. package/src/containers/project/calculation-engine.ts +535 -0
  212. package/src/containers/project/resource-collector.ts +45 -26
  213. package/src/containers/project.ts +66 -84
  214. package/src/containers/template.ts +16 -0
  215. package/src/exceptions/index.ts +36 -0
  216. package/src/index.ts +13 -2
  217. package/src/interfaces/macros.ts +7 -1
  218. package/src/interfaces/project-interfaces.ts +27 -0
  219. package/src/interfaces/resource-interfaces.ts +1 -0
  220. package/src/macros/base-macro.ts +89 -25
  221. package/src/macros/common.ts +22 -9
  222. package/src/macros/createCards/index.ts +1 -2
  223. package/src/macros/graph/index.ts +17 -12
  224. package/src/macros/image/index.ts +121 -0
  225. package/src/macros/image/metadata.ts +25 -0
  226. package/src/macros/include/index.ts +147 -0
  227. package/src/macros/include/metadata.ts +22 -0
  228. package/src/macros/index.ts +179 -100
  229. package/src/macros/percentage/index.ts +54 -0
  230. package/src/macros/percentage/metadata.ts +22 -0
  231. package/src/macros/report/index.ts +22 -17
  232. package/src/macros/scoreCard/index.ts +23 -23
  233. package/src/macros/vega/index.ts +55 -0
  234. package/src/macros/vega/metadata.ts +21 -0
  235. package/src/macros/vegalite/index.ts +50 -0
  236. package/src/macros/vegalite/metadata.ts +21 -0
  237. package/src/macros/xref/index.ts +73 -0
  238. package/src/macros/xref/metadata.ts +22 -0
  239. package/src/module-manager.ts +241 -69
  240. package/src/permissions/action-guard.ts +3 -3
  241. package/src/project-settings.ts +2 -11
  242. package/src/resources/card-type-resource.ts +100 -0
  243. package/src/resources/file-resource.ts +16 -4
  244. package/src/resources/folder-resource.ts +59 -2
  245. package/src/resources/graph-model-resource.ts +1 -1
  246. package/src/resources/graph-view-resource.ts +1 -1
  247. package/src/resources/report-resource.ts +1 -1
  248. package/src/resources/resource-object.ts +14 -0
  249. package/src/resources/template-resource.ts +1 -1
  250. package/src/resources/workflow-resource.ts +68 -13
  251. package/src/svg/index.ts +15 -0
  252. package/src/svg/lib.ts +31 -0
  253. package/src/svg/percentage.ts +97 -0
  254. package/src/svg/scoreCard.ts +88 -0
  255. package/src/types/queries.ts +8 -7
  256. package/src/types/string-pixel-width.d.ts +23 -0
  257. package/src/utils/card-utils.ts +13 -0
  258. package/src/utils/clingo-facts.ts +65 -3
  259. package/src/utils/clingo-parser.ts +31 -144
  260. package/src/utils/constants.ts +16 -0
  261. package/src/utils/csv.ts +1 -1
  262. package/src/utils/report.ts +45 -4
  263. package/src/utils/resource-utils.ts +9 -0
  264. package/src/utils/user-preferences.ts +32 -14
  265. 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.
@@ -660,31 +685,58 @@ export class Project extends CardContainer {
660
685
  path: modulePath,
661
686
  cardKeyPrefix: moduleConfig.cardKeyPrefix,
662
687
  calculations: [
663
- ...(await this.resources.collectResourcesFromModules('calculations')),
688
+ ...(await this.resources.collectResourcesFromModules(
689
+ 'calculations',
690
+ moduleName,
691
+ )),
664
692
  ],
665
693
  cardTypes: [
666
- ...(await this.resources.collectResourcesFromModules('cardTypes')),
694
+ ...(await this.resources.collectResourcesFromModules(
695
+ 'cardTypes',
696
+ moduleName,
697
+ )),
667
698
  ],
668
699
  fieldTypes: [
669
- ...(await this.resources.collectResourcesFromModules('fieldTypes')),
700
+ ...(await this.resources.collectResourcesFromModules(
701
+ 'fieldTypes',
702
+ moduleName,
703
+ )),
670
704
  ],
671
705
  graphModels: [
672
- ...(await this.resources.collectResourcesFromModules('graphModels')),
706
+ ...(await this.resources.collectResourcesFromModules(
707
+ 'graphModels',
708
+ moduleName,
709
+ )),
673
710
  ],
674
711
  graphViews: [
675
- ...(await this.resources.collectResourcesFromModules('graphViews')),
712
+ ...(await this.resources.collectResourcesFromModules(
713
+ 'graphViews',
714
+ moduleName,
715
+ )),
676
716
  ],
677
717
  linkTypes: [
678
- ...(await this.resources.collectResourcesFromModules('linkTypes')),
718
+ ...(await this.resources.collectResourcesFromModules(
719
+ 'linkTypes',
720
+ moduleName,
721
+ )),
679
722
  ],
680
723
  reports: [
681
- ...(await this.resources.collectResourcesFromModules('reports')),
724
+ ...(await this.resources.collectResourcesFromModules(
725
+ 'reports',
726
+ moduleName,
727
+ )),
682
728
  ],
683
729
  templates: [
684
- ...(await this.resources.collectResourcesFromModules('templates')),
730
+ ...(await this.resources.collectResourcesFromModules(
731
+ 'templates',
732
+ moduleName,
733
+ )),
685
734
  ],
686
735
  workflows: [
687
- ...(await this.resources.collectResourcesFromModules('workflows')),
736
+ ...(await this.resources.collectResourcesFromModules(
737
+ 'workflows',
738
+ moduleName,
739
+ )),
688
740
  ],
689
741
  };
690
742
  }
@@ -823,15 +875,6 @@ export class Project extends CardContainer {
823
875
  return prefixes;
824
876
  }
825
877
 
826
- /**
827
- * Removes a module from project.
828
- * @param moduleName Name of the module
829
- */
830
- public async removeModule(moduleName: string) {
831
- await this.configuration.removeModule(moduleName);
832
- await this.collectModuleResources();
833
- }
834
-
835
878
  /**
836
879
  * Array of reports in the project.
837
880
  * @param from Defines where resources are collected from.
@@ -968,68 +1011,7 @@ export class Project extends CardContainer {
968
1011
  * @returns an array of all project cards in the project.
969
1012
  */
970
1013
  public async showProjectCards(): Promise<Card[]> {
971
- const cards: Card[] = [];
972
- const cardPathMap = new Map<string, Card>();
973
- const entries = await readdir(this.paths.cardRootFolder, {
974
- withFileTypes: true,
975
- recursive: true,
976
- });
977
-
978
- // Checks if Dirent folder is a card folder
979
- function cardFolder(
980
- entry: Dirent,
981
- cardPathMap: Map<string, Card>,
982
- ): Card | undefined {
983
- const fullPath = join(entry.parentPath, entry.name);
984
- if (!cardPathMap.has(fullPath)) {
985
- const newCard: Card = {
986
- key: entry.name,
987
- path: fullPath,
988
- children: [],
989
- attachments: [],
990
- };
991
- cardPathMap.set(fullPath, newCard);
992
- return newCard;
993
- }
994
- }
995
-
996
- // Process card directories first
997
- entries
998
- .filter((entry) => entry.isDirectory() && CardNameRegEx.test(entry.name))
999
- .forEach((entry) => {
1000
- const card = cardFolder(entry, cardPathMap);
1001
- if (card) cards.push(card);
1002
- });
1003
-
1004
- // Process metadata files in parallel
1005
- await Promise.all(
1006
- entries
1007
- .filter(
1008
- (entry) => entry.isFile() && entry.name === Project.cardMetadataFile,
1009
- )
1010
- .map(async (entry) => {
1011
- const parentCard = cardPathMap.get(entry.parentPath);
1012
- if (!parentCard) return;
1013
- parentCard.metadata = (await readJsonFile(
1014
- join(entry.parentPath, entry.name),
1015
- )) as CardMetadata;
1016
- }),
1017
- );
1018
-
1019
- // Finally, build the card hierarchy
1020
- Array.from(cardPathMap.entries()).map(([cardPath, card]) => {
1021
- const parentPath = findParentPath(cardPath);
1022
- if (!parentPath) return;
1023
- const parentCard = cardPathMap.get(parentPath);
1024
- if (!parentCard) return;
1025
-
1026
- parentCard.children.push(card);
1027
- const index = cards.indexOf(card);
1028
- if (index > -1) {
1029
- cards.splice(index, 1);
1030
- }
1031
- });
1032
- return cards;
1014
+ return this.showCards(this.paths.cardRootFolder);
1033
1015
  }
1034
1016
 
1035
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
  }
@@ -27,3 +27,39 @@ export class SchemaNotFound extends Error {
27
27
  this.name = 'SchemaNotFound';
28
28
  }
29
29
  }
30
+ /**
31
+ * Stores the context of a macro error that originated from another macro
32
+ */
33
+ export interface MacroDependency {
34
+ macroName: string;
35
+ parameters: string;
36
+ output?: string;
37
+ }
38
+ /**
39
+ * Thrown when a macro fails to execute.
40
+ */
41
+ export class MacroError extends Error {
42
+ public context: {
43
+ cardKey: string;
44
+ macroName: string;
45
+ parameters: string;
46
+ dependency?: MacroDependency;
47
+ };
48
+
49
+ constructor(
50
+ message: string,
51
+ cardKey: string,
52
+ macroName: string,
53
+ parameters: string,
54
+ dependency?: MacroDependency,
55
+ ) {
56
+ super(message);
57
+ this.name = 'MacroError';
58
+ this.context = {
59
+ cardKey,
60
+ macroName,
61
+ parameters,
62
+ dependency,
63
+ };
64
+ }
65
+ }
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
  };
@@ -12,13 +12,17 @@
12
12
 
13
13
  import type { macroMetadata } from '../macros/common.js';
14
14
  import type { Project } from '../containers/project.js';
15
+ import type { MacroError } from '../exceptions/index.js';
16
+ import type { Context } from './project-interfaces.js';
15
17
 
16
- type Mode = 'validate' | 'static' | 'inject';
18
+ export type Mode = 'validate' | 'static' | 'inject';
17
19
 
18
20
  export interface MacroGenerationContext {
21
+ context: Context;
19
22
  project: Project;
20
23
  mode: Mode;
21
24
  cardKey: string;
25
+ maxTries?: number;
22
26
  }
23
27
 
24
28
  export interface MacroMetadata {
@@ -44,6 +48,8 @@ export interface MacroTaskState {
44
48
  placeholder: string;
45
49
  promiseResult: string | null;
46
50
  macro: string;
51
+ parameters: string;
52
+ error: MacroError | null;
47
53
  }
48
54
 
49
55
  // Handlebars options is not documented
@@ -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.
@@ -123,6 +123,7 @@ export interface ResourceBaseMetadata {
123
123
 
124
124
  // All resources metadata content.
125
125
  export type ResourceContent =
126
+ | CalculationMetadata
126
127
  | CardType
127
128
  | FieldType
128
129
  | GraphModel
@@ -17,13 +17,23 @@ import type {
17
17
  MacroTaskState,
18
18
  } from '../interfaces/macros.js';
19
19
  import { generateRandomString } from '../utils/random.js';
20
- import { handleMacroError } from './index.js';
20
+ import { MacroError } from '../exceptions/index.js';
21
21
  import type TaskQueue from './task-queue.js';
22
+ import { ClingoError } from '@cyberismo/node-clingo';
23
+ import { getChildLogger } from '../utils/log-utils.js';
22
24
 
23
25
  abstract class BaseMacro {
24
26
  private globalId: string;
25
27
  private localCounter: number = 0;
26
28
 
29
+ // Macros share the same logger
30
+ protected get logger() {
31
+ return getChildLogger({
32
+ module: 'macro',
33
+ macro: this.macroMetadata.name,
34
+ });
35
+ }
36
+
27
37
  constructor(
28
38
  protected macroMetadata: MacroMetadata,
29
39
  private readonly tasks: TaskQueue,
@@ -64,7 +74,7 @@ abstract class BaseMacro {
64
74
  if (task) {
65
75
  dependencies.push(task);
66
76
  } else {
67
- console.warn(
77
+ this.logger.warn(
68
78
  `Dependency not found for placeholder: ${placeholder} (globalId: ${globalId}, localId: ${localId})`,
69
79
  );
70
80
  }
@@ -81,6 +91,16 @@ abstract class BaseMacro {
81
91
  };
82
92
  }
83
93
 
94
+ private findTask(globalId: string, localId: number) {
95
+ const task = this.tasks.find(globalId, localId);
96
+ if (!task) {
97
+ this.logger.warn(
98
+ `Task not found for global id ${globalId}, local id ${localId}.`,
99
+ );
100
+ }
101
+ return task;
102
+ }
103
+
84
104
  /**
85
105
  * Function responsible for starting the promise and storing it along with its localId.
86
106
  */
@@ -111,20 +131,53 @@ abstract class BaseMacro {
111
131
  const dependencies = this.findDependencies(rawInput);
112
132
 
113
133
  // Create a promise to resolve dependencies, execute the macro, and handle the results
114
- const promise = Promise.all(dependencies.map((dep) => dep.promise))
134
+ const promise = Promise.allSettled(dependencies.map((dep) => dep.promise))
115
135
  .then(() => {
116
136
  for (const dependency of dependencies) {
137
+ if (dependency.error) {
138
+ const task = this.findTask(this.globalId, localId);
139
+ if (task) {
140
+ // There could be a better way, but multi-nested macros are rare
141
+ task.error = new MacroError(
142
+ dependency.error.message,
143
+ context.cardKey,
144
+ this.metadata.name,
145
+ dependency.error.context.parameters,
146
+ {
147
+ macroName: dependency.macro,
148
+ parameters: dependency.parameters,
149
+ },
150
+ );
151
+ }
152
+ return;
153
+ }
117
154
  input = input.replace(
118
155
  dependency.placeholder,
119
156
  dependency.promiseResult || '',
120
157
  );
158
+ // parse json after each dep, so we know the exact macro which produced the error
159
+ try {
160
+ JSON.parse(input);
161
+ } catch {
162
+ const task = this.findTask(this.globalId, localId);
163
+ if (task) {
164
+ task.error = new MacroError(
165
+ 'Invalid JSON produced by macro dependency',
166
+ context.cardKey,
167
+ this.metadata.name,
168
+ input,
169
+ {
170
+ macroName: dependency.macro,
171
+ parameters: dependency.parameters,
172
+ output: input,
173
+ },
174
+ );
175
+ }
176
+ return;
177
+ }
121
178
  }
122
- let parsed;
123
- try {
124
- parsed = JSON.parse(input);
125
- } catch {
126
- return 'Invalid JSON';
127
- }
179
+ // This will never throw in practice, thus no need to catch
180
+ const parsed = JSON.parse(input);
128
181
 
129
182
  // Select the function to execute based on context mode
130
183
  const functionToCall =
@@ -134,28 +187,37 @@ abstract class BaseMacro {
134
187
  return functionToCall(context, parsed);
135
188
  })
136
189
  .then((result) => {
137
- const task = this.tasks.find(this.globalId, localId);
190
+ // undefined is used to indicate that the macro did not run for some reason
191
+ if (result === undefined) {
192
+ return;
193
+ }
194
+ const task = this.findTask(this.globalId, localId);
138
195
  if (task) {
139
196
  task.promiseResult = result;
140
- } else {
141
- console.error(
142
- `Task not found after execution: macro ${this.metadata.name}, local id ${localId}.`,
143
- );
144
197
  }
145
198
  })
146
199
  .catch((err) => {
147
- const task = this.tasks.find(this.globalId, localId);
200
+ if (!(err instanceof Error)) {
201
+ this.logger.error(err, 'Unknown error');
202
+ err = new Error('Unknown error');
203
+ }
204
+ const message =
205
+ err instanceof ClingoError
206
+ ? err.details.errors.join('\n')
207
+ : err.message;
208
+ const error =
209
+ err instanceof MacroError
210
+ ? err
211
+ : new MacroError(
212
+ message,
213
+ context.cardKey,
214
+ this.metadata.name,
215
+ input,
216
+ );
217
+
218
+ const task = this.findTask(this.globalId, localId);
148
219
  if (task) {
149
- task.promiseResult = handleMacroError(
150
- err,
151
- this.metadata.name,
152
- context,
153
- );
154
- } else {
155
- console.error(
156
- `Error handling task for macro ${this.metadata.name}, local id ${localId}:`,
157
- err,
158
- );
220
+ task.error = error;
159
221
  }
160
222
  });
161
223
 
@@ -167,6 +229,8 @@ abstract class BaseMacro {
167
229
  placeholder,
168
230
  promiseResult: null,
169
231
  macro: this.macroMetadata.name,
232
+ parameters: rawInput,
233
+ error: null,
170
234
  });
171
235
  // Return the placeholder
172
236
  return placeholder;
@@ -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;