@cyberismo/data-handler 0.0.6 → 0.0.8

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 (175) hide show
  1. package/README.md +1 -0
  2. package/dist/card-metadata-updater.d.ts +1 -0
  3. package/dist/card-metadata-updater.js +7 -2
  4. package/dist/card-metadata-updater.js.map +1 -1
  5. package/dist/command-handler.d.ts +6 -1
  6. package/dist/command-handler.js +16 -15
  7. package/dist/command-handler.js.map +1 -1
  8. package/dist/command-manager.d.ts +15 -4
  9. package/dist/command-manager.js +41 -9
  10. package/dist/command-manager.js.map +1 -1
  11. package/dist/commands/calculate.d.ts +4 -10
  12. package/dist/commands/calculate.js +67 -78
  13. package/dist/commands/calculate.js.map +1 -1
  14. package/dist/commands/create.d.ts +1 -1
  15. package/dist/commands/create.js +15 -10
  16. package/dist/commands/create.js.map +1 -1
  17. package/dist/commands/export.js +3 -3
  18. package/dist/commands/export.js.map +1 -1
  19. package/dist/commands/import.d.ts +3 -8
  20. package/dist/commands/import.js +17 -15
  21. package/dist/commands/import.js.map +1 -1
  22. package/dist/commands/index.d.ts +1 -2
  23. package/dist/commands/index.js +1 -2
  24. package/dist/commands/index.js.map +1 -1
  25. package/dist/commands/remove.d.ts +1 -1
  26. package/dist/commands/remove.js +4 -10
  27. package/dist/commands/remove.js.map +1 -1
  28. package/dist/commands/show.d.ts +6 -3
  29. package/dist/commands/show.js +8 -5
  30. package/dist/commands/show.js.map +1 -1
  31. package/dist/commands/validate.d.ts +0 -8
  32. package/dist/commands/validate.js +6 -36
  33. package/dist/commands/validate.js.map +1 -1
  34. package/dist/containers/project/project-content-watcher.d.ts +28 -0
  35. package/dist/containers/project/project-content-watcher.js +54 -0
  36. package/dist/containers/project/project-content-watcher.js.map +1 -0
  37. package/dist/containers/project/project-paths.js +1 -1
  38. package/dist/containers/project/project-paths.js.map +1 -1
  39. package/dist/containers/project/resource-collector.d.ts +2 -1
  40. package/dist/containers/project/resource-collector.js +33 -23
  41. package/dist/containers/project/resource-collector.js.map +1 -1
  42. package/dist/containers/project.d.ts +9 -7
  43. package/dist/containers/project.js +58 -18
  44. package/dist/containers/project.js.map +1 -1
  45. package/dist/containers/template.d.ts +1 -0
  46. package/dist/containers/template.js +7 -2
  47. package/dist/containers/template.js.map +1 -1
  48. package/dist/exceptions/index.d.ts +20 -0
  49. package/dist/exceptions/index.js +16 -0
  50. package/dist/exceptions/index.js.map +1 -1
  51. package/dist/index.d.ts +2 -1
  52. package/dist/index.js.map +1 -1
  53. package/dist/interfaces/macros.d.ts +5 -1
  54. package/dist/interfaces/project-interfaces.d.ts +5 -0
  55. package/dist/interfaces/project-interfaces.js.map +1 -1
  56. package/dist/interfaces/resource-interfaces.d.ts +14 -22
  57. package/dist/interfaces/resource-interfaces.js +10 -9
  58. package/dist/interfaces/resource-interfaces.js.map +1 -1
  59. package/dist/macros/base-macro.d.ts +2 -0
  60. package/dist/macros/base-macro.js +66 -19
  61. package/dist/macros/base-macro.js.map +1 -1
  62. package/dist/macros/graph/index.d.ts +0 -1
  63. package/dist/macros/graph/index.js +16 -12
  64. package/dist/macros/graph/index.js.map +1 -1
  65. package/dist/macros/index.d.ts +30 -3
  66. package/dist/macros/index.js +36 -6
  67. package/dist/macros/index.js.map +1 -1
  68. package/dist/macros/report/index.d.ts +13 -10
  69. package/dist/macros/report/index.js +35 -38
  70. package/dist/macros/report/index.js.map +1 -1
  71. package/dist/module-manager.d.ts +32 -11
  72. package/dist/module-manager.js +301 -147
  73. package/dist/module-manager.js.map +1 -1
  74. package/dist/project-settings.js +8 -8
  75. package/dist/project-settings.js.map +1 -1
  76. package/dist/resources/array-handler.js +6 -1
  77. package/dist/resources/array-handler.js.map +1 -1
  78. package/dist/resources/card-type-resource.d.ts +13 -9
  79. package/dist/resources/card-type-resource.js +47 -23
  80. package/dist/resources/card-type-resource.js.map +1 -1
  81. package/dist/resources/create-defaults.d.ts +10 -9
  82. package/dist/resources/create-defaults.js +15 -12
  83. package/dist/resources/create-defaults.js.map +1 -1
  84. package/dist/resources/field-type-resource.d.ts +0 -1
  85. package/dist/resources/field-type-resource.js +2 -10
  86. package/dist/resources/field-type-resource.js.map +1 -1
  87. package/dist/resources/file-resource.d.ts +7 -7
  88. package/dist/resources/file-resource.js +32 -7
  89. package/dist/resources/file-resource.js.map +1 -1
  90. package/dist/resources/folder-resource.d.ts +10 -9
  91. package/dist/resources/folder-resource.js +10 -9
  92. package/dist/resources/folder-resource.js.map +1 -1
  93. package/dist/resources/report-resource.d.ts +5 -6
  94. package/dist/resources/report-resource.js +16 -7
  95. package/dist/resources/report-resource.js.map +1 -1
  96. package/dist/resources/template-resource.d.ts +5 -6
  97. package/dist/resources/template-resource.js +7 -6
  98. package/dist/resources/template-resource.js.map +1 -1
  99. package/dist/resources/workflow-resource.d.ts +15 -8
  100. package/dist/resources/workflow-resource.js +124 -8
  101. package/dist/resources/workflow-resource.js.map +1 -1
  102. package/dist/types/queries.d.ts +11 -10
  103. package/dist/types/queries.js +10 -9
  104. package/dist/types/queries.js.map +1 -1
  105. package/dist/utils/clingo-fact-builder.d.ts +1 -0
  106. package/dist/utils/clingo-fact-builder.js +8 -3
  107. package/dist/utils/clingo-fact-builder.js.map +1 -1
  108. package/dist/utils/clingo-facts.js +15 -11
  109. package/dist/utils/clingo-facts.js.map +1 -1
  110. package/dist/utils/constants.d.ts +18 -12
  111. package/dist/utils/constants.js +18 -11
  112. package/dist/utils/constants.js.map +1 -1
  113. package/dist/utils/log-utils.d.ts +15 -2
  114. package/dist/utils/log-utils.js +20 -37
  115. package/dist/utils/log-utils.js.map +1 -1
  116. package/dist/utils/report.d.ts +27 -0
  117. package/dist/utils/report.js +60 -0
  118. package/dist/utils/report.js.map +1 -0
  119. package/dist/utils/resource-utils.js +3 -0
  120. package/dist/utils/resource-utils.js.map +1 -1
  121. package/dist/utils/sanitize-svg.d.ts +3 -4
  122. package/dist/utils/sanitize-svg.js +4 -7
  123. package/dist/utils/sanitize-svg.js.map +1 -1
  124. package/dist/utils/validate.js +2 -1
  125. package/dist/utils/validate.js.map +1 -1
  126. package/package.json +9 -11
  127. package/src/card-metadata-updater.ts +7 -2
  128. package/src/command-handler.ts +23 -13
  129. package/src/command-manager.ts +54 -13
  130. package/src/commands/calculate.ts +90 -106
  131. package/src/commands/create.ts +18 -10
  132. package/src/commands/export.ts +3 -2
  133. package/src/commands/import.ts +30 -17
  134. package/src/commands/index.ts +0 -2
  135. package/src/commands/remove.ts +7 -12
  136. package/src/commands/show.ts +13 -5
  137. package/src/commands/validate.ts +14 -44
  138. package/src/containers/project/project-content-watcher.ts +65 -0
  139. package/src/containers/project/project-paths.ts +1 -1
  140. package/src/containers/project/resource-collector.ts +33 -14
  141. package/src/containers/project.ts +96 -19
  142. package/src/containers/template.ts +7 -2
  143. package/src/exceptions/index.ts +36 -0
  144. package/src/index.ts +2 -0
  145. package/src/interfaces/macros.ts +5 -1
  146. package/src/interfaces/project-interfaces.ts +8 -0
  147. package/src/interfaces/resource-interfaces.ts +15 -22
  148. package/src/macros/base-macro.ts +89 -25
  149. package/src/macros/graph/index.ts +22 -12
  150. package/src/macros/index.ts +61 -8
  151. package/src/macros/report/index.ts +42 -43
  152. package/src/module-manager.ts +383 -177
  153. package/src/project-settings.ts +9 -11
  154. package/src/resources/array-handler.ts +7 -2
  155. package/src/resources/card-type-resource.ts +61 -46
  156. package/src/resources/create-defaults.ts +15 -12
  157. package/src/resources/field-type-resource.ts +2 -17
  158. package/src/resources/file-resource.ts +46 -8
  159. package/src/resources/folder-resource.ts +11 -10
  160. package/src/resources/report-resource.ts +19 -7
  161. package/src/resources/template-resource.ts +7 -6
  162. package/src/resources/workflow-resource.ts +155 -8
  163. package/src/types/queries.ts +11 -10
  164. package/src/utils/clingo-fact-builder.ts +8 -3
  165. package/src/utils/clingo-facts.ts +18 -12
  166. package/src/utils/constants.ts +20 -12
  167. package/src/utils/log-utils.ts +24 -45
  168. package/src/utils/report.ts +86 -0
  169. package/src/utils/resource-utils.ts +4 -0
  170. package/src/utils/sanitize-svg.ts +4 -9
  171. package/src/utils/validate.ts +3 -2
  172. package/dist/commands/export-site.d.ts +0 -45
  173. package/dist/commands/export-site.js +0 -301
  174. package/dist/commands/export-site.js.map +0 -1
  175. package/src/commands/export-site.ts +0 -356
@@ -18,7 +18,7 @@ import report from './report/index.js';
18
18
  import scoreCard from './scoreCard/index.js';
19
19
 
20
20
  import { validateJson } from '../utils/validate.js';
21
- import { DHValidationError } from '../exceptions/index.js';
21
+ import { DHValidationError, MacroError } from '../exceptions/index.js';
22
22
  import type { AdmonitionType } from '../interfaces/adoc.js';
23
23
  import type { Validator } from 'jsonschema';
24
24
  import type {
@@ -28,21 +28,46 @@ import type {
28
28
  } from '../interfaces/macros.js';
29
29
  import type BaseMacro from './base-macro.js';
30
30
  import TaskQueue from './task-queue.js';
31
-
31
+ import type { Calculate } from '../commands/index.js';
32
+ import { ClingoError } from '@cyberismo/node-clingo';
32
33
  const CURLY_LEFT = '{';
33
34
  const CURLY_RIGHT = '}';
34
35
 
35
- export interface MacroConstructor {
36
- new (tasks: TaskQueue): BaseMacro; // Constructor signature
36
+ /**
37
+ * Constructor for all macros except report macros
38
+ */
39
+ export interface SimpleMacroConstructor {
40
+ new (tasks: TaskQueue): BaseMacro;
41
+ }
42
+
43
+ /**
44
+ * Constructor for report macros
45
+ */
46
+ export interface ReportMacroConstructor {
47
+ new (tasks: TaskQueue, calculate: Calculate): BaseMacro;
37
48
  }
38
49
 
39
- export const macros: { [K in MacroName]: MacroConstructor } = {
50
+ /**
51
+ * Constructor for all macros
52
+ */
53
+ export type MacroConstructor = SimpleMacroConstructor | ReportMacroConstructor;
54
+
55
+ export const macros: {
56
+ [K in MacroName]: MacroConstructor;
57
+ } = {
40
58
  createCards,
41
59
  graph,
42
60
  report,
43
61
  scoreCard,
44
62
  };
45
63
 
64
+ /**
65
+ * Validates the content inside a macro
66
+ * @param macro - The macro to validate the content of
67
+ * @param data - The data to validate
68
+ * @param validator - The validator to use
69
+ * @returns The validated data
70
+ */
46
71
  export function validateMacroContent<T>(
47
72
  macro: MacroMetadata,
48
73
  data: unknown,
@@ -74,11 +99,12 @@ export function registerMacros(
74
99
  instance: typeof Handlebars,
75
100
  context: MacroGenerationContext,
76
101
  tasks: TaskQueue,
102
+ calculate: Calculate,
77
103
  ) {
78
104
  const macroInstances: BaseMacro[] = [];
79
105
  for (const macro of Object.keys(macros) as MacroName[]) {
80
106
  const MacroClass = macros[macro];
81
- const macroInstance = new MacroClass(tasks);
107
+ const macroInstance = new MacroClass(tasks, calculate);
82
108
  instance.registerHelper(macro, function (this: unknown, options) {
83
109
  if (
84
110
  this != null &&
@@ -134,11 +160,12 @@ export function registerEmptyMacros(instance: typeof Handlebars) {
134
160
  export async function evaluateMacros(
135
161
  content: string,
136
162
  context: MacroGenerationContext,
163
+ calculate: Calculate,
137
164
  maxTries: number = 10,
138
165
  ) {
139
166
  const handlebars = Handlebars.create();
140
167
  const tasks = new TaskQueue();
141
- registerMacros(handlebars, context, tasks);
168
+ registerMacros(handlebars, context, tasks, calculate);
142
169
  let result = content;
143
170
  while (maxTries-- > 0) {
144
171
  tasks.reset();
@@ -185,7 +212,14 @@ export function applyMacroResults(
185
212
  context: MacroGenerationContext,
186
213
  ) {
187
214
  for (const item of tasks) {
188
- if (item.promiseResult === null) {
215
+ if (item.error) {
216
+ input = input.replace(
217
+ item.placeholder,
218
+ handleMacroError(item.error, item.macro, context),
219
+ );
220
+ } // It should not be possible that promiseResult is null if there never was an error
221
+ // Unless the function itself returns null / undefined
222
+ else if (item.promiseResult == null) {
189
223
  input = handleMacroError(
190
224
  new Error(
191
225
  `Tried to access result before it was resolved for ${item.placeholder}`,
@@ -214,7 +248,17 @@ export function handleMacroError(
214
248
  let message = `Error: ${error instanceof Error ? error.message : 'Unknown error'}`;
215
249
  if (error instanceof DHValidationError) {
216
250
  message = `Check json syntax of macro ${macro}: ${error.errors?.map((e) => e.message).join(', ')}`;
251
+ } else if (error instanceof MacroError) {
252
+ const { cardKey, macroName, dependency } = error.context;
253
+ message = `Macro error in card '${cardKey}' in macro '${macroName}':\n\n${error.message}.`;
254
+
255
+ if (dependency) {
256
+ message += `\n\nParameters:\n\n${context.mode === 'validate' ? dependency.parameters : createCodeBlock(dependency.parameters)}.\n\n${dependency.output ? `Output:\n\n${context.mode === 'validate' ? dependency.output : createCodeBlock(dependency.output)}` : ''}`;
257
+ }
258
+ } else if (error instanceof ClingoError) {
259
+ message = `Error running logic program in macro '${macro}':${error.details.errors.join('\n')}`;
217
260
  }
261
+
218
262
  if (
219
263
  typeof error === 'object' &&
220
264
  error != null &&
@@ -302,6 +346,15 @@ export function createAdmonition(
302
346
  return `[${type}]\n.${label}\n====\n${content}\n====\n\n`;
303
347
  }
304
348
 
349
+ /**
350
+ * Creates a code block
351
+ * @param content - The content of the code block
352
+ * @returns The code block as a string
353
+ */
354
+ export function createCodeBlock(content: string) {
355
+ return `\n\n----\n${content}\n----\n\n`;
356
+ }
357
+
305
358
  /**
306
359
  * Helper function for including base64 encoded images for now
307
360
  * @param image base64 encoded image
@@ -1,35 +1,40 @@
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. This program is distributed in the hope that it
7
+ will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
8
+ of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9
+ See the GNU Affero General Public License for more details.
10
+ 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
- import { registerEmptyMacros, validateMacroContent } from '../index.js';
14
+ import { validateMacroContent } from '../index.js';
14
15
  import type { MacroOptions } from '../index.js';
16
+
15
17
  import type { MacroGenerationContext } from '../../interfaces/macros.js';
16
18
  import macroMetadata from './metadata.js';
17
- import { Project } from '../../containers/project.js';
18
- import { Calculate } from '../../commands/index.js';
19
- import Handlebars from 'handlebars';
19
+ import type { Calculate } from '../../commands/index.js';
20
20
  import BaseMacro from '../base-macro.js';
21
21
  import { validateJson } from '../../utils/validate.js';
22
22
  import type TaskQueue from '../task-queue.js';
23
23
  import { ReportResource } from '../../resources/report-resource.js';
24
24
  import { resourceName } from '../../utils/resource-utils.js';
25
+ import { generateReportContent } from '../../utils/report.js';
26
+ import { ClingoError } from '@cyberismo/node-clingo';
25
27
 
26
28
  export interface ReportOptions extends MacroOptions {
27
29
  name: string;
28
30
  }
29
31
 
30
32
  class ReportMacro extends BaseMacro {
31
- constructor(tasksQueue: TaskQueue) {
32
- super(macroMetadata, tasksQueue);
33
+ constructor(
34
+ tasks: TaskQueue,
35
+ private readonly calculate: Calculate,
36
+ ) {
37
+ super(macroMetadata, tasks);
33
38
  }
34
39
  handleValidate = (input: unknown) => {
35
40
  this.validate(input);
@@ -41,43 +46,37 @@ class ReportMacro extends BaseMacro {
41
46
 
42
47
  handleInject = async (context: MacroGenerationContext, data: unknown) => {
43
48
  const options = this.validate(data);
44
- const project = new Project(context.projectPath);
45
- const resource = new ReportResource(project, resourceName(options.name));
49
+ const resource = new ReportResource(
50
+ context.project,
51
+ resourceName(options.name),
52
+ );
46
53
  const report = await resource.show();
47
54
 
48
- if (!report) throw new Error(`Report ${options} does not exist`);
55
+ if (!report) throw new Error(`Report ${options.name} does not exist`);
49
56
 
50
57
  if (report.schema) {
51
58
  validateJson(options, {
52
59
  schema: report.schema,
53
60
  });
54
61
  }
55
-
56
- const handlebarsContext = {
57
- cardKey: context.cardKey,
58
- ...options,
59
- };
60
-
61
- const handlebars = Handlebars.create();
62
-
63
- const template = handlebars.compile(report.queryTemplate, {
64
- strict: true,
65
- });
66
-
67
- const calculate = new Calculate(project);
68
- const result = await calculate.runLogicProgram({
69
- query: template(handlebarsContext),
70
- });
71
- if (result.error) {
72
- throw new Error(result.error);
62
+ try {
63
+ return await generateReportContent({
64
+ calculate: this.calculate,
65
+ contentTemplate: report.contentTemplate,
66
+ queryTemplate: report.queryTemplate,
67
+ options: {
68
+ cardKey: context.cardKey,
69
+ ...options,
70
+ },
71
+ });
72
+ } catch (error) {
73
+ if (error instanceof ClingoError) {
74
+ throw new Error(
75
+ `Error running logic program in report '${options.name}':${error.details.errors.join('\n')}`,
76
+ );
77
+ }
78
+ throw error;
73
79
  }
74
- // register empty macros so that other macros aren't touched yet
75
- registerEmptyMacros(handlebars);
76
-
77
- return handlebars.compile(report.contentTemplate)({
78
- ...handlebarsContext,
79
- ...result,
80
- });
81
80
  };
82
81
 
83
82
  private validate(data: unknown): ReportOptions {