@cyberismo/data-handler 0.0.8 → 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 (253) 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 +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 +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 +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 -0
  40. package/dist/commands/validate.js +30 -3
  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 +8 -10
  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 +1 -1
  64. package/dist/interfaces/resource-interfaces.js.map +1 -1
  65. package/dist/macros/common.d.ts +16 -9
  66. package/dist/macros/common.js +22 -9
  67. package/dist/macros/common.js.map +1 -1
  68. package/dist/macros/createCards/index.d.ts +1 -2
  69. package/dist/macros/createCards/index.js.map +1 -1
  70. package/dist/macros/graph/index.d.ts +1 -2
  71. package/dist/macros/graph/index.js +1 -3
  72. package/dist/macros/graph/index.js.map +1 -1
  73. package/dist/macros/image/index.d.ts +39 -0
  74. package/dist/macros/image/index.js +78 -0
  75. package/dist/macros/image/index.js.map +1 -0
  76. package/dist/macros/image/metadata.d.ts +18 -0
  77. package/dist/macros/image/metadata.js +22 -0
  78. package/dist/macros/image/metadata.js.map +1 -0
  79. package/dist/macros/include/index.d.ts +32 -0
  80. package/dist/macros/include/index.js +97 -0
  81. package/dist/macros/include/index.js.map +1 -0
  82. package/dist/macros/include/metadata.d.ts +15 -0
  83. package/dist/macros/include/metadata.js +19 -0
  84. package/dist/macros/include/metadata.js.map +1 -0
  85. package/dist/macros/index.d.ts +33 -31
  86. package/dist/macros/index.js +142 -71
  87. package/dist/macros/index.js.map +1 -1
  88. package/dist/macros/percentage/index.d.ts +29 -0
  89. package/dist/macros/percentage/index.js +36 -0
  90. package/dist/macros/percentage/index.js.map +1 -0
  91. package/dist/macros/percentage/metadata.d.ts +15 -0
  92. package/dist/macros/percentage/metadata.js +19 -0
  93. package/dist/macros/percentage/metadata.js.map +1 -0
  94. package/dist/macros/report/index.d.ts +2 -5
  95. package/dist/macros/report/index.js +3 -4
  96. package/dist/macros/report/index.js.map +1 -1
  97. package/dist/macros/scoreCard/index.d.ts +15 -15
  98. package/dist/macros/scoreCard/index.js +16 -17
  99. package/dist/macros/scoreCard/index.js.map +1 -1
  100. package/dist/macros/vega/index.d.ts +28 -0
  101. package/dist/macros/vega/index.js +27 -0
  102. package/dist/macros/vega/index.js.map +1 -0
  103. package/dist/macros/vega/metadata.d.ts +15 -0
  104. package/dist/macros/vega/metadata.js +7 -0
  105. package/dist/macros/vega/metadata.js.map +1 -0
  106. package/dist/macros/vegalite/index.d.ts +27 -0
  107. package/dist/macros/vegalite/index.js +27 -0
  108. package/dist/macros/vegalite/index.js.map +1 -0
  109. package/dist/macros/vegalite/metadata.d.ts +15 -0
  110. package/dist/macros/vegalite/metadata.js +7 -0
  111. package/dist/macros/vegalite/metadata.js.map +1 -0
  112. package/dist/macros/xref/index.d.ts +26 -0
  113. package/dist/macros/xref/index.js +53 -0
  114. package/dist/macros/xref/index.js.map +1 -0
  115. package/dist/macros/xref/metadata.d.ts +15 -0
  116. package/dist/macros/xref/metadata.js +19 -0
  117. package/dist/macros/xref/metadata.js.map +1 -0
  118. package/dist/module-manager.d.ts +1 -0
  119. package/dist/module-manager.js +14 -4
  120. package/dist/module-manager.js.map +1 -1
  121. package/dist/permissions/action-guard.d.ts +2 -2
  122. package/dist/permissions/action-guard.js +1 -1
  123. package/dist/permissions/action-guard.js.map +1 -1
  124. package/dist/resources/card-type-resource.d.ts +2 -0
  125. package/dist/resources/card-type-resource.js +63 -0
  126. package/dist/resources/card-type-resource.js.map +1 -1
  127. package/dist/resources/file-resource.d.ts +2 -0
  128. package/dist/resources/file-resource.js +12 -4
  129. package/dist/resources/file-resource.js.map +1 -1
  130. package/dist/resources/folder-resource.d.ts +19 -0
  131. package/dist/resources/folder-resource.js +51 -2
  132. package/dist/resources/folder-resource.js.map +1 -1
  133. package/dist/resources/graph-model-resource.js +1 -1
  134. package/dist/resources/graph-model-resource.js.map +1 -1
  135. package/dist/resources/graph-view-resource.js +1 -1
  136. package/dist/resources/graph-view-resource.js.map +1 -1
  137. package/dist/resources/report-resource.js +1 -1
  138. package/dist/resources/report-resource.js.map +1 -1
  139. package/dist/resources/resource-object.d.ts +8 -0
  140. package/dist/resources/resource-object.js +9 -0
  141. package/dist/resources/resource-object.js.map +1 -1
  142. package/dist/resources/template-resource.js +1 -1
  143. package/dist/resources/template-resource.js.map +1 -1
  144. package/dist/resources/workflow-resource.d.ts +2 -0
  145. package/dist/resources/workflow-resource.js +53 -9
  146. package/dist/resources/workflow-resource.js.map +1 -1
  147. package/dist/svg/index.d.ts +15 -0
  148. package/dist/svg/index.js +16 -0
  149. package/dist/svg/index.js.map +1 -0
  150. package/dist/svg/lib.d.ts +9 -0
  151. package/dist/svg/lib.js +26 -0
  152. package/dist/svg/lib.js.map +1 -0
  153. package/dist/svg/percentage.d.ts +25 -0
  154. package/dist/svg/percentage.js +90 -0
  155. package/dist/svg/percentage.js.map +1 -0
  156. package/dist/svg/scoreCard.d.ts +19 -0
  157. package/dist/svg/scoreCard.js +55 -0
  158. package/dist/svg/scoreCard.js.map +1 -0
  159. package/dist/types/queries.d.ts +8 -7
  160. package/dist/types/queries.js.map +1 -1
  161. package/dist/utils/card-utils.d.ts +6 -0
  162. package/dist/utils/card-utils.js +12 -0
  163. package/dist/utils/card-utils.js.map +1 -1
  164. package/dist/utils/clingo-facts.d.ts +2 -1
  165. package/dist/utils/clingo-facts.js +19 -2
  166. package/dist/utils/clingo-facts.js.map +1 -1
  167. package/dist/utils/clingo-parser.d.ts +1 -0
  168. package/dist/utils/clingo-parser.js +22 -100
  169. package/dist/utils/clingo-parser.js.map +1 -1
  170. package/dist/utils/constants.d.ts +7 -0
  171. package/dist/utils/constants.js +14 -0
  172. package/dist/utils/constants.js.map +1 -1
  173. package/dist/utils/csv.js.map +1 -1
  174. package/dist/utils/report.d.ts +17 -3
  175. package/dist/utils/report.js +38 -2
  176. package/dist/utils/report.js.map +1 -1
  177. package/dist/utils/resource-utils.d.ts +1 -0
  178. package/dist/utils/resource-utils.js +9 -0
  179. package/dist/utils/resource-utils.js.map +1 -1
  180. package/dist/utils/user-preferences.d.ts +1 -11
  181. package/dist/utils/user-preferences.js +30 -13
  182. package/dist/utils/user-preferences.js.map +1 -1
  183. package/dist/utils/validate.d.ts +2 -3
  184. package/dist/utils/validate.js +2 -2
  185. package/dist/utils/validate.js.map +1 -1
  186. package/package.json +8 -5
  187. package/src/command-handler.ts +96 -17
  188. package/src/command-manager.ts +8 -8
  189. package/src/commands/calculate.ts +11 -525
  190. package/src/commands/create.ts +35 -6
  191. package/src/commands/edit.ts +53 -11
  192. package/src/commands/export.ts +108 -34
  193. package/src/commands/import.ts +16 -0
  194. package/src/commands/move.ts +12 -15
  195. package/src/commands/remove.ts +4 -8
  196. package/src/commands/rename.ts +3 -12
  197. package/src/commands/show.ts +121 -8
  198. package/src/commands/transition.ts +3 -7
  199. package/src/commands/update.ts +6 -0
  200. package/src/commands/validate.ts +39 -12
  201. package/src/containers/card-container.ts +74 -0
  202. package/src/containers/project/calculation-engine.ts +535 -0
  203. package/src/containers/project/resource-collector.ts +12 -12
  204. package/src/containers/project.ts +30 -66
  205. package/src/containers/template.ts +16 -0
  206. package/src/index.ts +13 -2
  207. package/src/interfaces/macros.ts +4 -1
  208. package/src/interfaces/project-interfaces.ts +27 -0
  209. package/src/interfaces/resource-interfaces.ts +1 -0
  210. package/src/macros/common.ts +22 -9
  211. package/src/macros/createCards/index.ts +1 -2
  212. package/src/macros/graph/index.ts +6 -6
  213. package/src/macros/image/index.ts +121 -0
  214. package/src/macros/image/metadata.ts +25 -0
  215. package/src/macros/include/index.ts +147 -0
  216. package/src/macros/include/metadata.ts +22 -0
  217. package/src/macros/index.ts +150 -98
  218. package/src/macros/percentage/index.ts +54 -0
  219. package/src/macros/percentage/metadata.ts +22 -0
  220. package/src/macros/report/index.ts +4 -8
  221. package/src/macros/scoreCard/index.ts +23 -23
  222. package/src/macros/vega/index.ts +55 -0
  223. package/src/macros/vega/metadata.ts +21 -0
  224. package/src/macros/vegalite/index.ts +50 -0
  225. package/src/macros/vegalite/metadata.ts +21 -0
  226. package/src/macros/xref/index.ts +73 -0
  227. package/src/macros/xref/metadata.ts +22 -0
  228. package/src/module-manager.ts +15 -5
  229. package/src/permissions/action-guard.ts +3 -3
  230. package/src/resources/card-type-resource.ts +100 -0
  231. package/src/resources/file-resource.ts +16 -4
  232. package/src/resources/folder-resource.ts +59 -2
  233. package/src/resources/graph-model-resource.ts +1 -1
  234. package/src/resources/graph-view-resource.ts +1 -1
  235. package/src/resources/report-resource.ts +1 -1
  236. package/src/resources/resource-object.ts +14 -0
  237. package/src/resources/template-resource.ts +1 -1
  238. package/src/resources/workflow-resource.ts +68 -13
  239. package/src/svg/index.ts +15 -0
  240. package/src/svg/lib.ts +31 -0
  241. package/src/svg/percentage.ts +97 -0
  242. package/src/svg/scoreCard.ts +88 -0
  243. package/src/types/queries.ts +8 -7
  244. package/src/types/string-pixel-width.d.ts +23 -0
  245. package/src/utils/card-utils.ts +13 -0
  246. package/src/utils/clingo-facts.ts +65 -3
  247. package/src/utils/clingo-parser.ts +31 -144
  248. package/src/utils/constants.ts +16 -0
  249. package/src/utils/csv.ts +1 -1
  250. package/src/utils/report.ts +45 -4
  251. package/src/utils/resource-utils.ts +9 -0
  252. package/src/utils/user-preferences.ts +32 -14
  253. package/src/utils/validate.ts +3 -3
@@ -16,18 +16,23 @@ import { homedir } from 'node:os';
16
16
  import { spawnSync } from 'node:child_process';
17
17
 
18
18
  import { ActionGuard } from '../permissions/action-guard.js';
19
- import type { Calculate } from './index.js';
20
- import type { MetadataContent } from '../interfaces/project-interfaces.js';
19
+ import type {
20
+ MetadataContent,
21
+ ResourceFolderType,
22
+ } from '../interfaces/project-interfaces.js';
21
23
  import { Project } from '../containers/project.js';
22
24
  import { UserPreferences } from '../utils/user-preferences.js';
25
+ import {
26
+ type ResourceName,
27
+ resourceNameToString,
28
+ } from '../utils/resource-utils.js';
29
+ import { FolderResource } from '../resources/folder-resource.js';
23
30
 
24
31
  export class Edit {
25
32
  private project: Project;
26
- private calculateCmd: Calculate;
27
33
 
28
- constructor(project: Project, calculateCmd: Calculate) {
34
+ constructor(project: Project) {
29
35
  this.project = project;
30
- this.calculateCmd = calculateCmd;
31
36
  }
32
37
 
33
38
  /**
@@ -83,13 +88,16 @@ export class Edit {
83
88
  * @param changedContent New content for the card.
84
89
  */
85
90
  public async editCardContent(cardKey: string, changedContent: string) {
86
- // Determine the card path
91
+ const isTemplateCard = await this.project.isTemplateCard(cardKey);
92
+ if (isTemplateCard) {
93
+ return this.project.updateCardContent(cardKey, changedContent);
94
+ }
87
95
  const cardPath = this.project.pathToCard(cardKey);
88
96
  if (!cardPath) {
89
97
  throw new Error(`Card '${cardKey}' does not exist in the project`);
90
98
  }
91
99
 
92
- const actionGuard = new ActionGuard(this.calculateCmd);
100
+ const actionGuard = new ActionGuard(this.project.calculationEngine);
93
101
  await actionGuard.checkPermission('editContent', cardKey);
94
102
 
95
103
  await this.project.updateCardContent(cardKey, changedContent);
@@ -106,18 +114,52 @@ export class Edit {
106
114
  changedKey: string,
107
115
  newValue: MetadataContent,
108
116
  ) {
117
+ if (!changedKey) {
118
+ throw new Error(`Changed key cannot be empty`);
119
+ }
120
+ const isTemplateCard = await this.project.isTemplateCard(cardKey);
121
+ if (isTemplateCard) {
122
+ return this.project.updateCardMetadataKey(cardKey, changedKey, newValue);
123
+ }
124
+
109
125
  // Determine the card path
110
126
  const cardPath = this.project.pathToCard(cardKey);
111
127
  if (!cardPath) {
112
128
  throw new Error(`Card '${cardKey}' does not exist in the project`);
113
129
  }
114
- if (!changedKey) {
115
- throw new Error(`Changed key cannot be empty`);
116
- }
117
130
 
118
131
  // check for editing rights
119
- const actionGuard = new ActionGuard(this.calculateCmd);
132
+ const actionGuard = new ActionGuard(this.project.calculationEngine);
120
133
  await actionGuard.checkPermission('editField', cardKey, changedKey);
121
134
  await this.project.updateCardMetadataKey(cardKey, changedKey, newValue);
122
135
  }
136
+
137
+ /**
138
+ * Update a file of a folder resource. Cannot be used to create a new file.
139
+ * @param resourceName The name of the resource to update.
140
+ * @param fileName The name of the file to update.
141
+ * @param changedContent The new content for the file.
142
+ */
143
+ public async editResourceContent(
144
+ resourceName: ResourceName,
145
+ fileName: string,
146
+ changedContent: string,
147
+ ) {
148
+ const resourceNameStr = resourceNameToString(resourceName);
149
+ if (
150
+ !(await this.project.resourceExists(
151
+ resourceName.type as ResourceFolderType,
152
+ resourceNameStr,
153
+ ))
154
+ ) {
155
+ throw new Error(
156
+ `Resource '${resourceNameStr}' does not exist in the project`,
157
+ );
158
+ }
159
+ const resource = Project.resourceObject(this.project, resourceName);
160
+ if (!(resource instanceof FolderResource)) {
161
+ throw new Error(`Resource '${resourceNameStr}' is not a folder resource`);
162
+ }
163
+ return resource.updateFile(fileName, changedContent);
164
+ }
123
165
  }
@@ -13,27 +13,36 @@
13
13
  */
14
14
 
15
15
  // node
16
- import { appendFile, copyFile, mkdir, truncate } from 'node:fs/promises';
16
+ import {
17
+ appendFile,
18
+ copyFile,
19
+ mkdir,
20
+ truncate,
21
+ writeFile,
22
+ } from 'node:fs/promises';
17
23
  import { dirname, join } from 'node:path';
18
24
 
19
- // asciidoctor
20
- import Processor from '@asciidoctor/core';
21
- import type { Calculate, Show } from './index.js';
25
+ import { Project } from '../containers/project.js';
26
+ import { sortItems } from '../utils/lexorank.js';
27
+
22
28
  import type {
23
29
  Card,
24
30
  FetchCardDetails,
25
31
  } from '../interfaces/project-interfaces.js';
26
- import type { CardType } from '../interfaces/resource-interfaces.js';
27
- import { Project } from '../containers/project.js';
28
32
  import type { QueryResult } from '../types/queries.js';
29
- import { sortItems } from '../utils/lexorank.js';
33
+ import type { CardType } from '../interfaces/resource-interfaces.js';
34
+ import type { Show } from './index.js';
35
+ import { generateReportContent } from '../utils/report.js';
36
+ import { getStaticDirectoryPath, pdfReport } from '@cyberismo/assets';
37
+ import { spawn } from 'node:child_process';
38
+ import { evaluateMacros } from '../macros/index.js';
39
+ import type { ExportPdfOptions } from '../interfaces/project-interfaces.js';
30
40
 
31
41
  const attachmentFolder: string = 'a';
32
42
 
33
43
  export class Export {
34
44
  constructor(
35
45
  protected project: Project,
36
- protected calculateCmd: Calculate,
37
46
  protected showCmd: Show,
38
47
  ) {}
39
48
 
@@ -202,11 +211,11 @@ export class Export {
202
211
  asciiDocContent = await evaluateMacros(
203
212
  cardDetailsResponse.content || '',
204
213
  {
214
+ context: 'exportedDocument',
205
215
  mode: 'static',
206
216
  project,
207
217
  cardKey: card.key,
208
218
  },
209
- this.calculateCmd,
210
219
  );
211
220
  } catch (error) {
212
221
  asciiDocContent = `Macro error: ${error instanceof Error ? error.message : 'Unknown error'}\n\n${asciiDocContent}`;
@@ -225,6 +234,91 @@ export class Export {
225
234
  return card;
226
235
  }
227
236
 
237
+ private async runAsciidoctorPdf(content: string): Promise<Buffer> {
238
+ const staticRootDir = await getStaticDirectoryPath();
239
+ const proc = spawn(
240
+ 'asciidoctor-pdf',
241
+ [
242
+ '-a',
243
+ 'pdf-theme=cyberismo',
244
+ '-a',
245
+ `pdf-themesdir=${join(staticRootDir, 'pdf-themes')}`,
246
+ '-a',
247
+ `pdf-fontsdir=${join(staticRootDir, 'pdf-themes', 'fonts')};GEM_FONTS_DIR`,
248
+ '-',
249
+ ],
250
+ {
251
+ timeout: 100000,
252
+ shell: process.platform === 'win32',
253
+ },
254
+ );
255
+ proc.stdin.end(content);
256
+ const result = await new Promise<Buffer>((resolve, reject) => {
257
+ const chunks: Buffer[] = [];
258
+ proc.stdout.on('data', (chunk) => {
259
+ chunks.push(chunk);
260
+ });
261
+ proc.stderr.on('data', (chunk) => {
262
+ process.stderr.write(chunk);
263
+ });
264
+ proc.on('error', (error) => {
265
+ if ('code' in error && error.code === 'ENOENT') {
266
+ reject(
267
+ new Error(
268
+ 'Asciidoctor-pdf not found. Please install asciidoctor-pdf to use this feature.',
269
+ ),
270
+ );
271
+ }
272
+ reject(error);
273
+ });
274
+ proc.on('close', (code) => {
275
+ if (code === 0) {
276
+ resolve(Buffer.concat(chunks));
277
+ } else {
278
+ reject(new Error(`Asciidoctor-pdf failed with code ${code}`));
279
+ }
280
+ });
281
+ });
282
+ return result;
283
+ }
284
+
285
+ /**
286
+ * Exports the card(s) to pdf.
287
+ * @param destination Path to where the resulting file(s) will be created.
288
+ * @param options Export options.
289
+ * @returns status message
290
+ */
291
+ public async exportPdf(
292
+ destination: string,
293
+ options: ExportPdfOptions,
294
+ ): Promise<string> {
295
+ const opts = {
296
+ ...options,
297
+ date: options.date?.toISOString().split('T')[0],
298
+ recursive: options.recursive ?? false,
299
+ };
300
+
301
+ await this.project.calculationEngine.generate();
302
+
303
+ const result = await generateReportContent({
304
+ calculate: this.project.calculationEngine,
305
+ contentTemplate: pdfReport.content,
306
+ queryTemplate: pdfReport.query,
307
+ context: 'exportedDocument',
308
+ options: opts,
309
+ });
310
+
311
+ const evaluated = await evaluateMacros(result, {
312
+ context: 'exportedDocument',
313
+ mode: 'static',
314
+ project: this.project,
315
+ cardKey: '', // top level report does not contain any macros that use cardKey
316
+ });
317
+ const pdf = await this.runAsciidoctorPdf(evaluated);
318
+ await writeFile(destination, pdf);
319
+ return `Content exported as PDF to ${destination}`;
320
+ }
321
+
228
322
  /**
229
323
  * Exports the card(s) to ascii doc.
230
324
  * @param destination Path to where the resulting file(s) will be created.
@@ -254,8 +348,11 @@ export class Export {
254
348
  });
255
349
  }
256
350
 
257
- await this.calculateCmd.generate();
258
- const tree = await this.calculateCmd.runQuery('tree');
351
+ await this.project.calculationEngine.generate();
352
+ const tree = await this.project.calculationEngine.runQuery(
353
+ 'tree',
354
+ 'exportedDocument',
355
+ );
259
356
 
260
357
  if (cardKey) {
261
358
  const targetCard = this.findCardInTree(tree, cardKey);
@@ -290,27 +387,4 @@ export class Export {
290
387
  await this.toAdocFile(resultDocumentPath, cards);
291
388
  return message;
292
389
  }
293
-
294
- /**
295
- * Exports the card(s) to HTML and opens the browser.
296
- * @param destination Path to where the resulting file(s) will be created.
297
- * @param cardKey Optional; If not exporting the whole card tree, card key of parent card.
298
- */
299
- public async exportToHTML(
300
- destination: string,
301
- cardKey?: string,
302
- ): Promise<string> {
303
- let message = '';
304
- await this.exportToADoc(destination, cardKey).then((msg) => {
305
- message = msg;
306
- const asciiDocProcessor = Processor();
307
- const adocFile = join(destination, Project.cardContentFile);
308
- asciiDocProcessor.convertFile(adocFile, {
309
- safe: 'safe',
310
- base_dir: '/',
311
- standalone: true,
312
- });
313
- });
314
- return message;
315
- }
316
390
  }
@@ -172,6 +172,22 @@ export class Import {
172
172
  }
173
173
  }
174
174
 
175
+ /**
176
+ * Updates a specific imported module.
177
+ * @param moduleName Name (prefix) of module to update.
178
+ * @param credentials Optional credentials for a private module.
179
+ * @throws if module is not part of the project
180
+ */
181
+ public async updateModule(moduleName: string, credentials?: Credentials) {
182
+ const module = this.project.configuration.modules.find(
183
+ (item) => item.name === moduleName,
184
+ );
185
+ if (!module) {
186
+ throw new Error(`Module '${moduleName}' is not part of the project`);
187
+ }
188
+ return this.moduleManager.updateModule(module, credentials);
189
+ }
190
+
175
191
  /**
176
192
  * Updates all imported modules.
177
193
  * @param credentials Optional credentials for private modules.
@@ -1,13 +1,14 @@
1
1
  /**
2
- Cyberismo
3
- Copyright © Cyberismo Ltd and contributors 2024
4
-
5
- This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License version 3 as published by the Free Software Foundation.
6
-
7
- This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
8
-
9
- You should have received a copy of the GNU Affero General Public
10
- License along with this program. If not, see <https://www.gnu.org/licenses/>.
2
+ Cyberismo
3
+ Copyright © Cyberismo Ltd and contributors 2024
4
+ This program is free software: you can redistribute it and/or modify it under
5
+ the terms of the GNU Affero General Public License version 3 as published by
6
+ the Free Software Foundation.
7
+ This program is distributed in the hope that it will be useful, but WITHOUT
8
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
9
+ FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
10
+ details. You should have received a copy of the GNU Affero General Public
11
+ License along with this program. If not, see <https://www.gnu.org/licenses/>.
11
12
  */
12
13
 
13
14
  // node
@@ -15,7 +16,6 @@ import { join, sep } from 'node:path';
15
16
 
16
17
  import { ActionGuard } from '../permissions/action-guard.js';
17
18
  import { copyDir, deleteDir } from '../utils/file-utils.js';
18
- import type { Calculate } from './index.js';
19
19
  import type {
20
20
  Card,
21
21
  FetchCardDetails,
@@ -37,10 +37,7 @@ import { TemplateResource } from '../resources/template-resource.js';
37
37
  const ROOT: string = 'root';
38
38
 
39
39
  export class Move {
40
- constructor(
41
- private project: Project,
42
- private calculateCmd: Calculate,
43
- ) {}
40
+ constructor(private project: Project) {}
44
41
 
45
42
  // Fetches a card (either template or project card).
46
43
  private async getCard(cardKey: string, options: FetchCardDetails) {
@@ -192,7 +189,7 @@ export class Move {
192
189
  }
193
190
 
194
191
  // if both are project cards, make sure source card can be moved
195
- const actionGuard = new ActionGuard(this.calculateCmd);
192
+ const actionGuard = new ActionGuard(this.project.calculationEngine);
196
193
  await actionGuard.checkPermission('move', source);
197
194
 
198
195
  // rerank the card in the new location
@@ -15,7 +15,6 @@
15
15
  import { join, sep } from 'node:path';
16
16
 
17
17
  import { ActionGuard } from '../permissions/action-guard.js';
18
- import type { Calculate } from './index.js';
19
18
  import { deleteDir, deleteFile } from '../utils/file-utils.js';
20
19
  import { ModuleManager } from '../module-manager.js';
21
20
  import { Project } from '../containers/project.js';
@@ -29,10 +28,7 @@ const MODULES_PATH = `${sep}modules${sep}`;
29
28
  */
30
29
  export class Remove {
31
30
  private moduleManager: ModuleManager;
32
- constructor(
33
- private project: Project,
34
- private calculateCmd: Calculate,
35
- ) {
31
+ constructor(private project: Project) {
36
32
  this.moduleManager = new ModuleManager(this.project);
37
33
  }
38
34
 
@@ -87,7 +83,7 @@ export class Remove {
87
83
 
88
84
  // Make sure card can be removed if it's a project card
89
85
  if (!(await this.project.isTemplateCard(cardKey))) {
90
- const actionGuard = new ActionGuard(this.calculateCmd);
86
+ const actionGuard = new ActionGuard(this.project.calculationEngine);
91
87
  await actionGuard.checkPermission('delete', cardKey);
92
88
  }
93
89
 
@@ -115,10 +111,10 @@ export class Remove {
115
111
  content: false,
116
112
  parent: false,
117
113
  });
114
+ await deleteDir(cardFolder);
118
115
  if (card) {
119
- await this.calculateCmd.handleDeleteCard(card);
116
+ await this.project.handleDeleteCard(card);
120
117
  }
121
- await deleteDir(cardFolder);
122
118
  }
123
119
 
124
120
  // removes label from project
@@ -16,7 +16,6 @@ import { assert } from 'node:console';
16
16
  import { join } from 'node:path';
17
17
  import { rename, readdir, readFile, writeFile } from 'node:fs/promises';
18
18
 
19
- import type { Calculate } from './index.js';
20
19
  import type { Card } from '../interfaces/project-interfaces.js';
21
20
  import { isTemplateCard } from '../utils/card-utils.js';
22
21
  import { type Project, ResourcesFrom } from '../containers/project.js';
@@ -41,10 +40,7 @@ export class Rename {
41
40
  private from: string = '';
42
41
  private to: string = '';
43
42
 
44
- constructor(
45
- private project: Project,
46
- private calculateCmd: Calculate,
47
- ) {}
43
+ constructor(private project: Project) {}
48
44
 
49
45
  // Renames a card and all of its attachments (if it is a project card).
50
46
  private async renameCard(re: RegExp, card: Card): Promise<void> {
@@ -110,12 +106,7 @@ export class Rename {
110
106
  join(attachment.path, attachment.fileName),
111
107
  join(attachment.path, newAttachmentFileName),
112
108
  );
113
-
114
- const attachmentRe = new RegExp(`image::${attachment.fileName}`, 'g');
115
- card.content = card.content?.replace(
116
- attachmentRe,
117
- `image::${newAttachmentFileName}`,
118
- );
109
+ // NOTE: content is renamed by updateFiles method
119
110
  }),
120
111
  );
121
112
  }
@@ -373,6 +364,6 @@ export class Rename {
373
364
  this.project.collectLocalResources();
374
365
  console.info('Collected renamed resources');
375
366
 
376
- return this.calculateCmd.generate();
367
+ return this.project.calculationEngine.generate();
377
368
  }
378
369
  }
@@ -30,6 +30,8 @@ import type {
30
30
  ProjectMetadata,
31
31
  Resource,
32
32
  CardLocation,
33
+ Context,
34
+ ResourceFolderType,
33
35
  } from '../interfaces/project-interfaces.js';
34
36
  import type {
35
37
  CardType,
@@ -38,13 +40,18 @@ import type {
38
40
  Workflow,
39
41
  } from '../interfaces/resource-interfaces.js';
40
42
  import { Project, type ResourcesFrom } from '../containers/project.js';
41
- import { resourceName } from '../utils/resource-utils.js';
43
+ import {
44
+ type ResourceName,
45
+ resourceName,
46
+ resourceNameToString,
47
+ } from '../utils/resource-utils.js';
42
48
  import { TemplateResource } from '../resources/template-resource.js';
43
49
  import { UserPreferences } from '../utils/user-preferences.js';
44
50
 
45
51
  import ReportMacro from '../macros/report/index.js';
46
52
  import TaskQueue from '../macros/task-queue.js';
47
- import type { Calculate } from './calculate.js';
53
+ import { evaluateMacros } from '../macros/index.js';
54
+ import { FolderResource } from '../resources/folder-resource.js';
48
55
 
49
56
  /**
50
57
  * Show command.
@@ -54,11 +61,9 @@ export class Show {
54
61
  string,
55
62
  (from?: ResourcesFrom) => Promise<Resource[]>
56
63
  >;
57
- constructor(
58
- private project: Project,
59
- private calculate: Calculate,
60
- ) {
64
+ constructor(private project: Project) {
61
65
  this.resourceFunction = new Map([
66
+ ['calculations', this.project.calculations.bind(this.project)],
62
67
  ['cardTypes', this.project.cardTypes.bind(this.project)],
63
68
  ['fieldTypes', this.project.fieldTypes.bind(this.project)],
64
69
  ['graphModels', this.project.graphModels.bind(this.project)],
@@ -119,6 +124,27 @@ export class Show {
119
124
  }
120
125
  }
121
126
 
127
+ /**
128
+ * Shows all template cards in a project.
129
+ * @returns all template cards in a project.
130
+ */
131
+ public async showAllTemplateCards(): Promise<
132
+ { name: string; cards: Card[] }[]
133
+ > {
134
+ return Promise.all(
135
+ (await this.project.templates()).map(async (template) => {
136
+ const templateResource = new TemplateResource(
137
+ this.project,
138
+ resourceName(template.name),
139
+ );
140
+ return {
141
+ name: template.name,
142
+ cards: await templateResource.templateObject().showTemplateCards(),
143
+ };
144
+ }),
145
+ );
146
+ }
147
+
122
148
  /**
123
149
  * Shows all attachments (either template or project attachments) from a project.
124
150
  * @returns array of card attachments
@@ -264,6 +290,15 @@ export class Show {
264
290
  return this.project.listCards(cardsFrom);
265
291
  }
266
292
 
293
+ /**
294
+ * Shows the content of a logic program.
295
+ * @param cardKey The key of the card.
296
+ * @returns the content of the logic program.
297
+ */
298
+ public async showCardLogicProgram(cardKey: string) {
299
+ return this.project.calculationEngine.cardLogicProgram(cardKey);
300
+ }
301
+
267
302
  /**
268
303
  * Shows all card types in a project.
269
304
  * @returns array of card type details
@@ -282,6 +317,57 @@ export class Show {
282
317
  return results.filter((item) => item);
283
318
  }
284
319
 
320
+ /**
321
+ * Shows the content of a file in a resource.
322
+ * @param resourceName Name of the resource.
323
+ * @param fileName Name of the file to show.
324
+ * @returns the content of the file.
325
+ */
326
+ public async showFile(
327
+ resourceName: ResourceName,
328
+ fileName: string,
329
+ ): Promise<string> {
330
+ const resourceNameStr = resourceNameToString(resourceName);
331
+ if (
332
+ !(await this.project.resourceExists(
333
+ resourceName.type as ResourceFolderType,
334
+ resourceNameStr,
335
+ ))
336
+ ) {
337
+ throw new Error(
338
+ `Resource '${resourceNameStr}' does not exist in the project`,
339
+ );
340
+ }
341
+ const resource = Project.resourceObject(this.project, resourceName);
342
+ if (!(resource instanceof FolderResource)) {
343
+ throw new Error(`Resource '${resourceNameStr}' is not a folder resource`);
344
+ }
345
+ return resource.showFile(fileName);
346
+ }
347
+
348
+ /**
349
+ * Shows all file names in a folder resource.
350
+ * @param resourceName Name of the resource.
351
+ * @returns all file names in the resource.
352
+ */
353
+ public async showFileNames(resourceName: ResourceName): Promise<string[]> {
354
+ const resourceNameStr = resourceNameToString(resourceName);
355
+ if (
356
+ !(await this.project.resourceExists(
357
+ resourceName.type as ResourceFolderType,
358
+ resourceNameStr,
359
+ ))
360
+ ) {
361
+ throw new Error(
362
+ `Resource '${resourceNameStr}' does not exist in the project`,
363
+ );
364
+ }
365
+ const resource = Project.resourceObject(this.project, resourceName);
366
+ if (!(resource instanceof FolderResource)) {
367
+ throw new Error(`Resource '${resourceNameStr}' is not a folder resource`);
368
+ }
369
+ return resource.showFileNames();
370
+ }
285
371
  /**
286
372
  * Returns all unique labels in a project
287
373
  * @returns labels in a list
@@ -297,6 +383,15 @@ export class Show {
297
383
  return Array.from(new Set(labels));
298
384
  }
299
385
 
386
+ /**
387
+ * Shows the content of a logic program.
388
+ * @param resource Name of the resource.
389
+ * @returns the content of the logic program.
390
+ */
391
+ public async showLogicProgram(resource: ResourceName) {
392
+ return this.project.calculationEngine.resourceLogicProgram(resource);
393
+ }
394
+
300
395
  /**
301
396
  * Shows details of a module.
302
397
  * @param moduleName name of a module
@@ -349,6 +444,7 @@ export class Show {
349
444
  reportName: string,
350
445
  cardKey: string,
351
446
  parameters: object,
447
+ context: Context,
352
448
  outputPath?: string,
353
449
  ): Promise<string> {
354
450
  if (
@@ -359,16 +455,25 @@ export class Show {
359
455
  throw new Error(`Report '${reportName}' does not exist`);
360
456
  }
361
457
 
362
- const reportMacro = new ReportMacro(new TaskQueue(), this.calculate);
363
- const result = await reportMacro.handleInject(
458
+ await this.project.calculationEngine.generate();
459
+ const reportMacro = new ReportMacro(new TaskQueue());
460
+ let result = await reportMacro.handleInject(
364
461
  {
365
462
  project: this.project,
366
463
  cardKey: cardKey,
367
464
  mode: 'static',
465
+ context,
368
466
  },
369
467
  { name: reportName, ...parameters },
370
468
  );
371
469
 
470
+ result = await evaluateMacros(result, {
471
+ project: this.project,
472
+ cardKey: cardKey,
473
+ mode: 'static',
474
+ context,
475
+ });
476
+
372
477
  // Show the results either in the console or write to a file.
373
478
  if (outputPath) {
374
479
  try {
@@ -393,6 +498,14 @@ export class Show {
393
498
  name: string,
394
499
  showUse: boolean = false,
395
500
  ): Promise<ResourceContent | undefined> {
501
+ // TODO: remove this workaround once calculations are implemented as a resource class
502
+ if (resourceName(name).type === 'calculations') {
503
+ return {
504
+ name,
505
+ displayName: resourceName(name).identifier,
506
+ };
507
+ }
508
+
396
509
  const strictNameCheck = true;
397
510
  const resource = Project.resourceObject(
398
511
  this.project,