@cyberismo/data-handler 0.0.14 → 0.0.16

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 (280) hide show
  1. package/dist/card-metadata-updater.js +8 -4
  2. package/dist/card-metadata-updater.js.map +1 -1
  3. package/dist/command-handler.d.ts +4 -0
  4. package/dist/command-handler.js +29 -19
  5. package/dist/command-handler.js.map +1 -1
  6. package/dist/command-manager.d.ts +25 -2
  7. package/dist/command-manager.js +30 -5
  8. package/dist/command-manager.js.map +1 -1
  9. package/dist/commands/create.d.ts +1 -1
  10. package/dist/commands/create.js +45 -93
  11. package/dist/commands/create.js.map +1 -1
  12. package/dist/commands/edit.d.ts +1 -15
  13. package/dist/commands/edit.js +15 -89
  14. package/dist/commands/edit.js.map +1 -1
  15. package/dist/commands/export.d.ts +11 -2
  16. package/dist/commands/export.js +58 -58
  17. package/dist/commands/export.js.map +1 -1
  18. package/dist/commands/import.d.ts +9 -1
  19. package/dist/commands/import.js +17 -11
  20. package/dist/commands/import.js.map +1 -1
  21. package/dist/commands/move.d.ts +1 -2
  22. package/dist/commands/move.js +107 -146
  23. package/dist/commands/move.js.map +1 -1
  24. package/dist/commands/remove.d.ts +8 -1
  25. package/dist/commands/remove.js +17 -48
  26. package/dist/commands/remove.js.map +1 -1
  27. package/dist/commands/rename.d.ts +4 -9
  28. package/dist/commands/rename.js +34 -108
  29. package/dist/commands/rename.js.map +1 -1
  30. package/dist/commands/show.d.ts +22 -34
  31. package/dist/commands/show.js +103 -151
  32. package/dist/commands/show.js.map +1 -1
  33. package/dist/commands/transition.d.ts +9 -2
  34. package/dist/commands/transition.js +49 -44
  35. package/dist/commands/transition.js.map +1 -1
  36. package/dist/commands/update.d.ts +18 -12
  37. package/dist/commands/update.js +34 -18
  38. package/dist/commands/update.js.map +1 -1
  39. package/dist/commands/validate.d.ts +18 -10
  40. package/dist/commands/validate.js +101 -47
  41. package/dist/commands/validate.js.map +1 -1
  42. package/dist/containers/card-container.d.ts +87 -24
  43. package/dist/containers/card-container.js +183 -279
  44. package/dist/containers/card-container.js.map +1 -1
  45. package/dist/containers/project/calculation-engine.d.ts +13 -4
  46. package/dist/containers/project/calculation-engine.js +79 -77
  47. package/dist/containers/project/calculation-engine.js.map +1 -1
  48. package/dist/containers/project/card-cache.d.ts +146 -0
  49. package/dist/containers/project/card-cache.js +411 -0
  50. package/dist/containers/project/card-cache.js.map +1 -0
  51. package/dist/containers/project/project-paths.d.ts +5 -4
  52. package/dist/containers/project/project-paths.js +16 -12
  53. package/dist/containers/project/project-paths.js.map +1 -1
  54. package/dist/containers/project/resource-cache.d.ts +169 -0
  55. package/dist/containers/project/resource-cache.js +507 -0
  56. package/dist/containers/project/resource-cache.js.map +1 -0
  57. package/dist/containers/project/resource-handler.d.ts +129 -0
  58. package/dist/containers/project/resource-handler.js +206 -0
  59. package/dist/containers/project/resource-handler.js.map +1 -0
  60. package/dist/containers/project.d.ts +114 -195
  61. package/dist/containers/project.js +425 -535
  62. package/dist/containers/project.js.map +1 -1
  63. package/dist/containers/template.d.ts +22 -32
  64. package/dist/containers/template.js +113 -115
  65. package/dist/containers/template.js.map +1 -1
  66. package/dist/index.d.ts +1 -0
  67. package/dist/index.js +1 -0
  68. package/dist/index.js.map +1 -1
  69. package/dist/interfaces/folder-content-interfaces.d.ts +7 -4
  70. package/dist/interfaces/folder-content-interfaces.js +3 -3
  71. package/dist/interfaces/folder-content-interfaces.js.map +1 -1
  72. package/dist/interfaces/macros.d.ts +1 -0
  73. package/dist/interfaces/macros.js +1 -1
  74. package/dist/interfaces/macros.js.map +1 -1
  75. package/dist/interfaces/project-interfaces.d.ts +7 -5
  76. package/dist/interfaces/project-interfaces.js.map +1 -1
  77. package/dist/interfaces/resource-interfaces.d.ts +25 -22
  78. package/dist/interfaces/resource-interfaces.js +3 -0
  79. package/dist/interfaces/resource-interfaces.js.map +1 -1
  80. package/dist/macros/common.d.ts +10 -10
  81. package/dist/macros/createCards/index.d.ts +0 -13
  82. package/dist/macros/createCards/index.js.map +1 -1
  83. package/dist/macros/createCards/types.d.ts +44 -0
  84. package/dist/macros/createCards/types.js +15 -0
  85. package/dist/macros/createCards/types.js.map +1 -0
  86. package/dist/macros/graph/index.d.ts +2 -6
  87. package/dist/macros/graph/index.js +14 -28
  88. package/dist/macros/graph/index.js.map +1 -1
  89. package/dist/macros/graph/types.d.ts +23 -0
  90. package/dist/macros/graph/types.js +15 -0
  91. package/dist/macros/graph/types.js.map +1 -0
  92. package/dist/macros/image/index.d.ts +8 -16
  93. package/dist/macros/image/index.js +36 -33
  94. package/dist/macros/image/index.js.map +1 -1
  95. package/dist/macros/image/types.d.ts +38 -0
  96. package/dist/macros/image/types.js +15 -0
  97. package/dist/macros/image/types.js.map +1 -0
  98. package/dist/macros/include/index.d.ts +1 -6
  99. package/dist/macros/include/index.js +4 -7
  100. package/dist/macros/include/index.js.map +1 -1
  101. package/dist/macros/include/types.d.ts +31 -0
  102. package/dist/macros/include/types.js +15 -0
  103. package/dist/macros/include/types.js.map +1 -0
  104. package/dist/macros/index.d.ts +1 -1
  105. package/dist/macros/index.js +2 -2
  106. package/dist/macros/index.js.map +1 -1
  107. package/dist/macros/percentage/index.d.ts +0 -6
  108. package/dist/macros/percentage/index.js.map +1 -1
  109. package/dist/macros/percentage/types.d.ts +31 -0
  110. package/dist/macros/percentage/types.js +15 -0
  111. package/dist/macros/percentage/types.js.map +1 -0
  112. package/dist/macros/report/index.d.ts +0 -3
  113. package/dist/macros/report/index.js +3 -6
  114. package/dist/macros/report/index.js.map +1 -1
  115. package/dist/macros/report/types.d.ts +19 -0
  116. package/dist/macros/report/types.js +15 -0
  117. package/dist/macros/report/types.js.map +1 -0
  118. package/dist/macros/scoreCard/index.d.ts +0 -6
  119. package/dist/macros/scoreCard/index.js.map +1 -1
  120. package/dist/macros/scoreCard/types.d.ts +31 -0
  121. package/dist/macros/scoreCard/types.js +15 -0
  122. package/dist/macros/scoreCard/types.js.map +1 -0
  123. package/dist/macros/types.d.ts +25 -0
  124. package/dist/macros/types.js +2 -0
  125. package/dist/macros/types.js.map +1 -0
  126. package/dist/macros/vega/index.d.ts +0 -4
  127. package/dist/macros/vega/index.js.map +1 -1
  128. package/dist/macros/vega/types.d.ts +20 -0
  129. package/dist/macros/vega/types.js +2 -0
  130. package/dist/macros/vega/types.js.map +1 -0
  131. package/dist/macros/vegalite/index.d.ts +0 -4
  132. package/dist/macros/vegalite/index.js.map +1 -1
  133. package/dist/macros/vegalite/types.d.ts +20 -0
  134. package/dist/macros/vegalite/types.js +15 -0
  135. package/dist/macros/vegalite/types.js.map +1 -0
  136. package/dist/macros/xref/index.d.ts +0 -3
  137. package/dist/macros/xref/index.js +5 -14
  138. package/dist/macros/xref/index.js.map +1 -1
  139. package/dist/macros/xref/types.d.ts +19 -0
  140. package/dist/macros/xref/types.js +15 -0
  141. package/dist/macros/xref/types.js.map +1 -0
  142. package/dist/module-manager.d.ts +16 -3
  143. package/dist/module-manager.js +55 -23
  144. package/dist/module-manager.js.map +1 -1
  145. package/dist/project-settings.d.ts +16 -3
  146. package/dist/project-settings.js +79 -14
  147. package/dist/project-settings.js.map +1 -1
  148. package/dist/resources/calculation-resource.d.ts +6 -33
  149. package/dist/resources/calculation-resource.js +11 -60
  150. package/dist/resources/calculation-resource.js.map +1 -1
  151. package/dist/resources/card-type-resource.d.ts +10 -22
  152. package/dist/resources/card-type-resource.js +46 -66
  153. package/dist/resources/card-type-resource.js.map +1 -1
  154. package/dist/resources/create-defaults.d.ts +3 -2
  155. package/dist/resources/create-defaults.js +3 -2
  156. package/dist/resources/create-defaults.js.map +1 -1
  157. package/dist/resources/field-type-resource.d.ts +8 -22
  158. package/dist/resources/field-type-resource.js +35 -60
  159. package/dist/resources/field-type-resource.js.map +1 -1
  160. package/dist/resources/file-resource.d.ts +14 -35
  161. package/dist/resources/file-resource.js +22 -301
  162. package/dist/resources/file-resource.js.map +1 -1
  163. package/dist/resources/folder-resource.d.ts +44 -66
  164. package/dist/resources/folder-resource.js +102 -149
  165. package/dist/resources/folder-resource.js.map +1 -1
  166. package/dist/resources/graph-model-resource.d.ts +9 -34
  167. package/dist/resources/graph-model-resource.js +18 -64
  168. package/dist/resources/graph-model-resource.js.map +1 -1
  169. package/dist/resources/graph-view-resource.d.ts +9 -29
  170. package/dist/resources/graph-view-resource.js +13 -48
  171. package/dist/resources/graph-view-resource.js.map +1 -1
  172. package/dist/resources/link-type-resource.d.ts +9 -23
  173. package/dist/resources/link-type-resource.js +11 -33
  174. package/dist/resources/link-type-resource.js.map +1 -1
  175. package/dist/resources/report-resource.d.ts +10 -23
  176. package/dist/resources/report-resource.js +20 -67
  177. package/dist/resources/report-resource.js.map +1 -1
  178. package/dist/resources/resource-object.d.ts +143 -23
  179. package/dist/resources/resource-object.js +369 -48
  180. package/dist/resources/resource-object.js.map +1 -1
  181. package/dist/resources/template-resource.d.ts +10 -17
  182. package/dist/resources/template-resource.js +19 -27
  183. package/dist/resources/template-resource.js.map +1 -1
  184. package/dist/resources/workflow-resource.d.ts +9 -25
  185. package/dist/resources/workflow-resource.js +25 -55
  186. package/dist/resources/workflow-resource.js.map +1 -1
  187. package/dist/utils/card-utils.d.ts +69 -19
  188. package/dist/utils/card-utils.js +179 -30
  189. package/dist/utils/card-utils.js.map +1 -1
  190. package/dist/utils/clingo-fact-builder.d.ts +25 -14
  191. package/dist/utils/clingo-fact-builder.js +27 -5
  192. package/dist/utils/clingo-fact-builder.js.map +1 -1
  193. package/dist/utils/clingo-facts.js +14 -7
  194. package/dist/utils/clingo-facts.js.map +1 -1
  195. package/dist/utils/clingo-parser.js +1 -1
  196. package/dist/utils/clingo-parser.js.map +1 -1
  197. package/dist/utils/constants.d.ts +2 -0
  198. package/dist/utils/constants.js +4 -0
  199. package/dist/utils/constants.js.map +1 -1
  200. package/dist/utils/csv.js +1 -1
  201. package/dist/utils/csv.js.map +1 -1
  202. package/dist/utils/resource-utils.d.ts +1 -0
  203. package/dist/utils/resource-utils.js +2 -1
  204. package/dist/utils/resource-utils.js.map +1 -1
  205. package/package.json +11 -11
  206. package/src/card-metadata-updater.ts +9 -7
  207. package/src/command-handler.ts +35 -23
  208. package/src/command-manager.ts +32 -19
  209. package/src/commands/create.ts +59 -160
  210. package/src/commands/edit.ts +16 -132
  211. package/src/commands/export.ts +71 -81
  212. package/src/commands/import.ts +26 -18
  213. package/src/commands/move.ts +143 -179
  214. package/src/commands/remove.ts +20 -59
  215. package/src/commands/rename.ts +45 -156
  216. package/src/commands/show.ts +153 -211
  217. package/src/commands/transition.ts +53 -58
  218. package/src/commands/update.ts +44 -23
  219. package/src/commands/validate.ts +108 -82
  220. package/src/containers/card-container.ts +200 -360
  221. package/src/containers/project/calculation-engine.ts +81 -105
  222. package/src/containers/project/card-cache.ts +497 -0
  223. package/src/containers/project/project-paths.ts +21 -13
  224. package/src/containers/project/resource-cache.ts +648 -0
  225. package/src/containers/project/resource-handler.ts +265 -0
  226. package/src/containers/project.ts +551 -693
  227. package/src/containers/template.ts +129 -142
  228. package/src/index.ts +1 -0
  229. package/src/interfaces/folder-content-interfaces.ts +14 -7
  230. package/src/interfaces/macros.ts +2 -0
  231. package/src/interfaces/project-interfaces.ts +14 -7
  232. package/src/interfaces/resource-interfaces.ts +30 -27
  233. package/src/macros/createCards/index.ts +1 -12
  234. package/src/macros/createCards/types.ts +46 -0
  235. package/src/macros/graph/index.ts +27 -52
  236. package/src/macros/graph/types.ts +24 -0
  237. package/src/macros/image/index.ts +50 -61
  238. package/src/macros/image/types.ts +39 -0
  239. package/src/macros/include/index.ts +6 -15
  240. package/src/macros/include/types.ts +32 -0
  241. package/src/macros/index.ts +2 -2
  242. package/src/macros/percentage/index.ts +1 -7
  243. package/src/macros/percentage/types.ts +32 -0
  244. package/src/macros/report/index.ts +4 -13
  245. package/src/macros/report/types.ts +20 -0
  246. package/src/macros/scoreCard/index.ts +1 -7
  247. package/src/macros/scoreCard/types.ts +32 -0
  248. package/src/macros/types.ts +48 -0
  249. package/src/macros/vega/index.ts +1 -4
  250. package/src/macros/vega/types.ts +21 -0
  251. package/src/macros/vegalite/index.ts +1 -4
  252. package/src/macros/vegalite/types.ts +22 -0
  253. package/src/macros/xref/index.ts +6 -20
  254. package/src/macros/xref/types.ts +20 -0
  255. package/src/module-manager.ts +79 -22
  256. package/src/project-settings.ts +84 -15
  257. package/src/resources/calculation-resource.ts +21 -91
  258. package/src/resources/card-type-resource.ts +74 -109
  259. package/src/resources/create-defaults.ts +3 -2
  260. package/src/resources/field-type-resource.ts +61 -104
  261. package/src/resources/file-resource.ts +33 -441
  262. package/src/resources/folder-resource.ts +130 -207
  263. package/src/resources/graph-model-resource.ts +36 -95
  264. package/src/resources/graph-view-resource.ts +28 -70
  265. package/src/resources/link-type-resource.ts +23 -53
  266. package/src/resources/report-resource.ts +34 -96
  267. package/src/resources/resource-object.ts +511 -66
  268. package/src/resources/template-resource.ts +32 -44
  269. package/src/resources/workflow-resource.ts +42 -85
  270. package/src/utils/card-utils.ts +217 -31
  271. package/src/utils/clingo-fact-builder.ts +28 -16
  272. package/src/utils/clingo-facts.ts +16 -7
  273. package/src/utils/clingo-parser.ts +1 -1
  274. package/src/utils/constants.ts +6 -0
  275. package/src/utils/csv.ts +1 -1
  276. package/src/utils/resource-utils.ts +2 -1
  277. package/dist/containers/project/resource-collector.d.ts +0 -87
  278. package/dist/containers/project/resource-collector.js +0 -337
  279. package/dist/containers/project/resource-collector.js.map +0 -1
  280. package/src/containers/project/resource-collector.ts +0 -396
@@ -1,25 +1,161 @@
1
1
  /**
2
- Cyberismo
3
- Copyright © Cyberismo Ltd and contributors 2024
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/>.
12
+ */
4
13
 
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.
14
+ import { sep } from 'node:path';
15
+ import { CARD_KEY_SEPARATOR, ROOT } from './constants.js';
6
16
 
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.
17
+ import type {
18
+ Card,
19
+ CardWithChildrenCards,
20
+ } from '../interfaces/project-interfaces.js';
21
+ import type { Project } from '../containers/project.js';
8
22
 
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/>.
11
- */
23
+ /**
24
+ * Builds card hierarchy from flat card list with nested card objects.
25
+ * This converts the cards hierarchy (where children are string[]) to
26
+ * CardWithChildrenCards[] (where children are Card[]).
27
+ * @param flatCards Cards in a flat array.
28
+ * @returns Cards in hierarchical array with nested card objects
29
+ */
30
+ export const buildCardHierarchy = (
31
+ flatCards: Card[],
32
+ ): CardWithChildrenCards[] => {
33
+ const cardMap = new Map(flatCards.map((card) => [card.key, card]));
12
34
 
13
- import { sep } from 'node:path';
35
+ // Helper to get cards as a map
36
+ function cards(flatCards: Card[]) {
37
+ const cardMap = new Map(
38
+ flatCards.map((card) => [
39
+ card.key,
40
+ { ...card, children: [] as string[] },
41
+ ]),
42
+ );
43
+
44
+ const rootCards: Card[] = [];
45
+ cardMap.forEach((card) => {
46
+ if (card.parent && cardMap.has(card.parent)) {
47
+ const parentCard = cardMap.get(card.parent);
48
+ if (parentCard) {
49
+ parentCard.children.push(card.key);
50
+ }
51
+ } else {
52
+ rootCards.push(card);
53
+ }
54
+ });
55
+
56
+ return rootCards;
57
+ }
58
+
59
+ // Helper to convert from string[] => Card[] children
60
+ function convert(card: Card): CardWithChildrenCards {
61
+ const childrenCards = card.children.map((childKey) => {
62
+ const childCard = cardMap.get(childKey)!;
63
+ return convert(childCard);
64
+ });
65
+
66
+ return {
67
+ ...card,
68
+ childrenCards,
69
+ };
70
+ }
71
+
72
+ const rootCards = cards(flatCards);
73
+ return rootCards.map(convert);
74
+ };
75
+
76
+ /**
77
+ * Breaks card path to logical parts: cardKey, list of parents, prefix and template
78
+ * If this is a root card, then parents is undefined.
79
+ * If this is not a template card, then template is undefined.
80
+ * @param prefix Project prefix.
81
+ * @param cardPath Card path to break into parts.
82
+ * @returns Card parts
83
+ * @todo: This could also return module name, if card is from a module
84
+ */
85
+ export const cardPathParts = (
86
+ prefix: string,
87
+ cardPath: string,
88
+ ): {
89
+ cardKey: string | undefined;
90
+ parents: (string | undefined)[];
91
+ prefix: string;
92
+ template: string;
93
+ } => {
94
+ const pathParts = cardPath.split(sep);
95
+ const cardKey = pathParts.at(pathParts.length - 1);
96
+ const parents = [];
97
+ let template = '';
98
+ let startIndex = -1;
99
+ let templatesNameIndex = -1;
100
+
101
+ const cardRootIndex = pathParts.indexOf('cardRoot');
102
+ const projectInternalsIndex = pathParts.indexOf('.cards');
14
103
 
15
- import type { Card } from '../interfaces/project-interfaces.js';
104
+ if (projectInternalsIndex === -1 && cardRootIndex >= 0) {
105
+ startIndex = projectInternalsIndex;
106
+ } else if (projectInternalsIndex >= 0 && cardRootIndex === -1) {
107
+ const templatesIndex = pathParts.indexOf('templates');
108
+ startIndex = templatesIndex;
109
+ if (templatesIndex === -1) {
110
+ throw new Error(
111
+ `Invalid card path. Template card must have 'templates' in path`,
112
+ );
113
+ }
114
+ const modulesIndex = pathParts.indexOf('modules');
115
+ if (modulesIndex !== -1) {
116
+ prefix = pathParts.at(modulesIndex + 1) || '';
117
+ }
118
+ templatesNameIndex = templatesIndex + 1;
119
+ template = `${prefix}/templates/${pathParts.at(templatesNameIndex)}`;
120
+ } else {
121
+ throw new Error(`Card must be either project card, or template card`);
122
+ }
123
+
124
+ // Look for parents in the path.
125
+ let previousWasParent = false;
126
+ for (let index = startIndex; index <= pathParts.length; index++) {
127
+ if (previousWasParent) {
128
+ previousWasParent = false;
129
+ parents.push(pathParts.at(index - 2));
130
+ }
131
+ const cardsSubFolder = pathParts.at(index) === 'c';
132
+ const ignoreOrNotTemplatesParent =
133
+ index - 1 !== templatesNameIndex || templatesNameIndex === -1;
134
+ if (cardsSubFolder && ignoreOrNotTemplatesParent) {
135
+ previousWasParent = true;
136
+ }
137
+ }
16
138
 
17
- // Helper function to find the parent path from a card path
139
+ return {
140
+ cardKey: cardKey,
141
+ parents: parents,
142
+ prefix: prefix,
143
+ template: template,
144
+ };
145
+ };
146
+
147
+ /**
148
+ * Find parent path from a card path
149
+ * @param cardPath Card path to find parent path from.
150
+ * @returns Parent path
151
+ */
18
152
  export const findParentPath = (cardPath: string): string | null => {
19
153
  const pathParts = cardPath.split(sep);
20
154
  const hasChildren = pathParts.lastIndexOf('c');
21
155
 
22
- if (hasChildren <= 0) return null;
156
+ if (hasChildren <= 0) {
157
+ return null;
158
+ }
23
159
 
24
160
  const parentPathParts = [...pathParts];
25
161
  parentPathParts.splice(hasChildren, 1);
@@ -28,21 +164,53 @@ export const findParentPath = (cardPath: string): string | null => {
28
164
 
29
165
  /**
30
166
  * Flattens card tree so that children are shown on same level regardless of nesting level.
31
- * @param array card tree
32
- * @returns flattened card tree.
167
+ * @param array Card tree to flatten
168
+ * @param project Project to use
169
+ * @returns Flattened card tree.
33
170
  */
34
- export const flattenCardArray = (array: Card[]) => {
171
+ export const flattenCardArray = (array: Card[], project: Project) => {
35
172
  const result: Card[] = [];
173
+
36
174
  array.forEach((item) => {
37
175
  const { key, path, children, attachments, metadata } = item;
38
- result.push({ key, path, children, attachments, metadata });
176
+ const childCardIds = project
177
+ .cardKeysToCards(children)
178
+ .map((item) => item.key);
179
+
180
+ result.push({
181
+ key,
182
+ path,
183
+ children: [...childCardIds],
184
+ attachments,
185
+ metadata,
186
+ });
39
187
  if (children) {
40
- result.push(...flattenCardArray(children));
188
+ result.push(
189
+ ...flattenCardArray(project.cardKeysToCards(children), project),
190
+ );
41
191
  }
42
192
  });
43
193
  return result;
44
194
  };
45
195
 
196
+ /**
197
+ * Checks if given card is in some module.
198
+ * @param card Card object to check
199
+ * @returns true if card exists in a module; false otherwise
200
+ */
201
+ export const isModuleCard = (card: Card) => {
202
+ return card.path.includes(`${sep}modules${sep}`);
203
+ };
204
+
205
+ /**
206
+ * Checks if given path is from a module.
207
+ * @param path Path to check
208
+ * @returns true if path is from a module; false otherwise
209
+ */
210
+ export const isModulePath = (path: string) => {
211
+ return path.includes(`${sep}modules${sep}`);
212
+ };
213
+
46
214
  /**
47
215
  * Checks if given card is in some template.
48
216
  * @param card card object to check
@@ -55,6 +223,37 @@ export const isTemplateCard = (card: Card) => {
55
223
  );
56
224
  };
57
225
 
226
+ /**
227
+ * Returns module name from card key
228
+ * @param cardKey Card key
229
+ * @returns module name
230
+ * @todo: should be renamed to modulePrefixFromCardKey
231
+ */
232
+ export const moduleNameFromCardKey = (cardKey: string) => {
233
+ const parts = cardKey.split(CARD_KEY_SEPARATOR);
234
+ if (parts.length !== 2) {
235
+ throw new Error(`Invalid card key: ${cardKey}`);
236
+ }
237
+ return parts[0];
238
+ };
239
+
240
+ /**
241
+ * Finds parent card key
242
+ * @param cardPath Card path from which to find parent card
243
+ * @returns Parent card key
244
+ */
245
+ export function parentCard(cardPath: string) {
246
+ const pathParts = cardPath.split(sep);
247
+ if (
248
+ pathParts.at(pathParts.length - 2) === 'cardRoot' ||
249
+ (pathParts.length > 3 && pathParts.at(pathParts.length - 4) === 'templates')
250
+ ) {
251
+ return ROOT;
252
+ } else {
253
+ return pathParts.at(pathParts.length - 3);
254
+ }
255
+ }
256
+
58
257
  /**
59
258
  * Sorts array of cards first using prefix and then using ID.
60
259
  * Prefixes are returned in alphabetical order, and then in numeric order within same prefix.
@@ -64,8 +263,8 @@ export const isTemplateCard = (card: Card) => {
64
263
  * @returns Cards ordered; first by prefixes, then by ID.
65
264
  */
66
265
  export const sortCards = (a: string, b: string) => {
67
- const aParts = a.split('_');
68
- const bParts = b.split('_');
266
+ const aParts = a.split(CARD_KEY_SEPARATOR);
267
+ const bParts = b.split(CARD_KEY_SEPARATOR);
69
268
  if (aParts[0] !== bParts[0]) {
70
269
  if (aParts[0] > bParts[0]) return 1;
71
270
  if (aParts[0] < bParts[0]) return -1;
@@ -81,16 +280,3 @@ export const sortCards = (a: string, b: string) => {
81
280
  if (aParts[1] < bParts[1]) return -1;
82
281
  return 0;
83
282
  };
84
-
85
- /**
86
- * Returns module name from card key
87
- * @param cardKey card key
88
- * @returns module name
89
- */
90
- export const moduleNameFromCardKey = (cardKey: string) => {
91
- const parts = cardKey.split('_');
92
- if (parts.length !== 2) {
93
- throw new Error(`Invalid card key: ${cardKey}`);
94
- }
95
- return parts[0];
96
- };
@@ -1,17 +1,19 @@
1
- import { INT32_MAX } from './constants.js';
2
- import { getChildLogger } from './log-utils.js';
3
-
4
1
  /**
5
- Cyberismo
6
- Copyright © Cyberismo Ltd and contributors 2024
7
-
8
- 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.
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/>.
12
+ */
9
13
 
10
- 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.
14
+ import { INT32_MAX } from './constants.js';
15
+ import { getChildLogger } from './log-utils.js';
11
16
 
12
- You should have received a copy of the GNU Affero General Public
13
- License along with this program. If not, see <https://www.gnu.org/licenses/>.
14
- */
15
17
  export type AllowedClingoType = string | number | boolean;
16
18
 
17
19
  export type NestedBuilder = (builder: ClingoFactBuilder) => ClingoFactBuilder;
@@ -26,6 +28,8 @@ export type ClingoArgument = ClingoArgumentInternal | NestedBuilder;
26
28
  /**
27
29
  * This function takes care of encoding chars, which might produce issues in clingo
28
30
  * This should be done for user provided values
31
+ * @param value Clingo value to encode.
32
+ * @returns Encoded clingo value.
29
33
  */
30
34
  export function encodeClingoValue(value: string) {
31
35
  return value.replace(/[\n\\"]/g, (char) => {
@@ -36,6 +40,9 @@ export function encodeClingoValue(value: string) {
36
40
  });
37
41
  }
38
42
 
43
+ /**
44
+ * Clingo fact builder.
45
+ */
39
46
  export class ClingoFactBuilder {
40
47
  protected predicate: string;
41
48
  private end: string;
@@ -46,6 +53,11 @@ export class ClingoFactBuilder {
46
53
  });
47
54
  }
48
55
 
56
+ /**
57
+ * Constructs an instance of ClingoFactBuilder.
58
+ * @param predicate Predicate
59
+ * @param end End character; by default a dot ('.')
60
+ */
49
61
  constructor(predicate: string, end: string = '.') {
50
62
  this.predicate = predicate;
51
63
  this.end = end;
@@ -71,8 +83,8 @@ export class ClingoFactBuilder {
71
83
 
72
84
  /**
73
85
  * Helper for adding multiple arguments, because it's common
74
- * @param args
75
- * @returns this for chaining
86
+ * @param args Arguments array
87
+ * @returns 'this' for chaining
76
88
  */
77
89
  addArguments(...args: (ClingoArgument | null)[]): ClingoFactBuilder {
78
90
  args.forEach((arg) => this.addArgument(arg));
@@ -82,7 +94,7 @@ export class ClingoFactBuilder {
82
94
  /**
83
95
  * Adds a literal argument, which means that it will not have quotes
84
96
  * @param literal The literal argument to add
85
- * @returns this for chaining
97
+ * @returns 'this' for chaining
86
98
  */
87
99
  addLiteralArgument(literal: string): ClingoFactBuilder {
88
100
  this.arguments.push(new LiteralBuilder(literal));
@@ -91,8 +103,8 @@ export class ClingoFactBuilder {
91
103
 
92
104
  /**
93
105
  * Helper for adding multiple literal arguments, because it's common
94
- * @param literal
95
- * @returns this for chaining
106
+ * @param literals Array of literals
107
+ * @returns 'this' for chaining
96
108
  */
97
109
  addLiteralArguments(...literals: string[]): ClingoFactBuilder {
98
110
  literals.forEach((literal) => this.addLiteralArgument(literal));
@@ -168,6 +168,7 @@ export const createWorkflowFacts = (workflow: Workflow) => {
168
168
  */
169
169
  export const createCardFacts = async (card: Card, project: Project) => {
170
170
  // Small helper to deduce parent path
171
+ // todo: Should use card-utils
171
172
  function parentPath(cardPath: string) {
172
173
  const pathParts = cardPath.split(sep);
173
174
  if (pathParts.at(pathParts.length - 2) === 'cardRoot') {
@@ -178,6 +179,7 @@ export const createCardFacts = async (card: Card, project: Project) => {
178
179
  }
179
180
 
180
181
  // Helper to deduce template parent path.
182
+ // todo: Should use card-utils
181
183
  function parentPathFromTemplate(card: Card) {
182
184
  const cardPath = card.path;
183
185
  const pathParts = cardPath.split(sep);
@@ -201,10 +203,18 @@ export const createCardFacts = async (card: Card, project: Project) => {
201
203
  }
202
204
  }
203
205
 
204
- const parentsPath = isTemplateCard(card)
205
- ? parentPathFromTemplate(card)
206
- : parentPath(card.path);
206
+ // Use card.parent if available, otherwise fall back to path-based calculation
207
+ const parentsPath =
208
+ card.parent && card.parent !== 'root'
209
+ ? card.parent
210
+ : isTemplateCard(card)
211
+ ? parentPathFromTemplate(card)
212
+ : parentPath(card.path);
213
+
207
214
  const builder = new ClingoProgramBuilder().addComment(card.key);
215
+ if (!isTemplateCard(card)) {
216
+ builder.addCustomFact('card', (b) => b.addLiteralArgument(card.key));
217
+ }
208
218
 
209
219
  if (card.metadata) {
210
220
  for (const [field, value] of Object.entries(card.metadata)) {
@@ -233,10 +243,9 @@ export const createCardFacts = async (card: Card, project: Project) => {
233
243
 
234
244
  if (!isPredefinedField(field)) {
235
245
  // field is a custom field, find it
236
- const fieldType = await project.resource<FieldType>(field);
237
- if (!fieldType) {
238
- continue;
239
- }
246
+ const fieldType = project.resources
247
+ .byType(field, 'fieldTypes')
248
+ .show();
240
249
 
241
250
  // if it's a list, let's generate multiple values
242
251
  if (fieldType.dataType === 'list') {
@@ -103,7 +103,7 @@ class ClingoParser {
103
103
  childObject: (parentKey: string, name: string, collection: string) => {
104
104
  this.childObjectQueue.push({ parentKey, name, collection });
105
105
  },
106
- field: async (
106
+ field: (
107
107
  key: string,
108
108
  fieldName: string,
109
109
  fieldValue: string,
@@ -13,11 +13,17 @@
13
13
 
14
14
  import type { PredefinedCardMetadata } from '../interfaces/project-interfaces.js';
15
15
 
16
+ // Card key separator between project prefix and card ID
17
+ export const CARD_KEY_SEPARATOR = '_';
18
+
16
19
  export const INT32_MAX = 2147483647; // 2^31-1
17
20
 
18
21
  // Maximum level offset for includeMacro
19
22
  export const MAX_LEVEL_OFFSET = 5;
20
23
 
24
+ // Root parent name
25
+ export const ROOT = 'root';
26
+
21
27
  /**
22
28
  * These are file names that are valid for folder resources.
23
29
  * Note that the folders might still contain other files, such as .schema files,
package/src/utils/csv.ts CHANGED
@@ -10,7 +10,7 @@
10
10
  License along with this program. If not, see <https://www.gnu.org/licenses/>.
11
11
  */
12
12
 
13
- import { readFile } from 'fs/promises';
13
+ import { readFile } from 'node:fs/promises';
14
14
  import { parse } from 'csv-parse/sync';
15
15
  import type { CSVRowRaw } from '../interfaces/project-interfaces.js';
16
16
 
@@ -88,6 +88,7 @@ export function resourceName(
88
88
  * Converts resource name to path.
89
89
  * @param project Project
90
90
  * @param resourceName Name of the resource (e.g. <prefix>/<type>/<name>)
91
+ * @param extension Extension of resource. By default '.json'.
91
92
  * @returns path to resource metadata file
92
93
  */
93
94
  export function resourceNameToPath(
@@ -204,7 +205,7 @@ export function resourceNameToString(resourceName: ResourceName): string {
204
205
  ) {
205
206
  throw new Error(`Not a valid resource name. Prefix is missing.`);
206
207
  }
207
- return resourceName.prefix && resourceName.type && resourceName.prefix
208
+ return resourceName.prefix && resourceName.type && resourceName.identifier
208
209
  ? `${resourceName.prefix}/${resourceName.type}/${resourceName.identifier}`
209
210
  : `${resourceName.identifier}`;
210
211
  }
@@ -1,87 +0,0 @@
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
6
- the terms of the GNU Affero General Public License version 3 as published by
7
- the Free Software Foundation. This program is distributed in the hope that it
8
- will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
9
- of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
- See the GNU Affero General Public License for more details.
11
- You should have received a copy of the GNU Affero General Public
12
- License along with this program. If not, see <https://www.gnu.org/licenses/>.
13
- */
14
- import type { Project } from '../project.js';
15
- import type { Resource, ResourceFolderType } from '../../interfaces/project-interfaces.js';
16
- /**
17
- * Defines where resources are collected from.
18
- * all - everywhere
19
- * importOnly - only from imported modules
20
- * localOnly - only from the project itself; excluding imported modules
21
- */
22
- export declare enum ResourcesFrom {
23
- all = "all",
24
- importedOnly = "imported",
25
- localOnly = "local"
26
- }
27
- /**
28
- * This class handles local and modules resources.
29
- */
30
- export declare class ResourceCollector {
31
- private project;
32
- private local;
33
- private modules;
34
- private modulesCollected;
35
- private paths;
36
- constructor(project: Project);
37
- private addResources;
38
- private addModuleResources;
39
- private addResourcesFromModules;
40
- private joinResources;
41
- private localResources;
42
- private resourcesSync;
43
- /**
44
- * Collects all local resources.
45
- */
46
- collectLocalResources(): void;
47
- /**
48
- * Collect specific resource from modules.
49
- * @param type Type of resource (e.g. 'templates').
50
- * @param moduleName Name of the module to collect resources from
51
- * @returns array of collected items.
52
- */
53
- collectResourcesFromModules(type: ResourceFolderType, moduleName?: string): Promise<string[]>;
54
- /**
55
- * Add a given 'resource' to the local resource arrays.
56
- * @param resource Resource to add.
57
- */
58
- add(resource: Resource): void;
59
- /**
60
- * Re-collects local resources.
61
- */
62
- changed(): void;
63
- /**
64
- * Re-collects imported module resources.
65
- */
66
- moduleImported(): Promise<void>;
67
- /**
68
- * Removes a resource from Project.
69
- * @param resource Resource to remove.
70
- * @returns the modified array.
71
- */
72
- remove(resource: Resource): Resource[];
73
- /**
74
- * Checks if resource of 'type' with 'name' exists.
75
- * @param type Type of resource (e.g. 'templates').
76
- * @param name Name of the resource.
77
- * @returns true, if resource exits, false otherwise.
78
- */
79
- resourceExists(type: ResourceFolderType, name: string): Promise<boolean>;
80
- /**
81
- * Returns resources of 'type'. Returned resources are either local, or from modules or all of them.
82
- * @param type Type of resource (e.g. 'templates').
83
- * @param from Defines where resources are collected from.
84
- * @returns Array of resources.
85
- */
86
- resources(type: ResourceFolderType, from?: ResourcesFrom): Promise<Resource[]>;
87
- }