@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.
- package/README.md +1 -0
- package/dist/card-metadata-updater.d.ts +1 -0
- package/dist/card-metadata-updater.js +7 -2
- package/dist/card-metadata-updater.js.map +1 -1
- package/dist/command-handler.d.ts +6 -1
- package/dist/command-handler.js +16 -15
- package/dist/command-handler.js.map +1 -1
- package/dist/command-manager.d.ts +15 -4
- package/dist/command-manager.js +41 -9
- package/dist/command-manager.js.map +1 -1
- package/dist/commands/calculate.d.ts +4 -10
- package/dist/commands/calculate.js +67 -78
- package/dist/commands/calculate.js.map +1 -1
- package/dist/commands/create.d.ts +1 -1
- package/dist/commands/create.js +15 -10
- package/dist/commands/create.js.map +1 -1
- package/dist/commands/export.js +3 -3
- package/dist/commands/export.js.map +1 -1
- package/dist/commands/import.d.ts +3 -8
- package/dist/commands/import.js +17 -15
- package/dist/commands/import.js.map +1 -1
- package/dist/commands/index.d.ts +1 -2
- package/dist/commands/index.js +1 -2
- package/dist/commands/index.js.map +1 -1
- package/dist/commands/remove.d.ts +1 -1
- package/dist/commands/remove.js +4 -10
- package/dist/commands/remove.js.map +1 -1
- package/dist/commands/show.d.ts +6 -3
- package/dist/commands/show.js +8 -5
- package/dist/commands/show.js.map +1 -1
- package/dist/commands/validate.d.ts +0 -8
- package/dist/commands/validate.js +6 -36
- package/dist/commands/validate.js.map +1 -1
- package/dist/containers/project/project-content-watcher.d.ts +28 -0
- package/dist/containers/project/project-content-watcher.js +54 -0
- package/dist/containers/project/project-content-watcher.js.map +1 -0
- package/dist/containers/project/project-paths.js +1 -1
- package/dist/containers/project/project-paths.js.map +1 -1
- package/dist/containers/project/resource-collector.d.ts +2 -1
- package/dist/containers/project/resource-collector.js +33 -23
- package/dist/containers/project/resource-collector.js.map +1 -1
- package/dist/containers/project.d.ts +9 -7
- package/dist/containers/project.js +58 -18
- package/dist/containers/project.js.map +1 -1
- package/dist/containers/template.d.ts +1 -0
- package/dist/containers/template.js +7 -2
- package/dist/containers/template.js.map +1 -1
- package/dist/exceptions/index.d.ts +20 -0
- package/dist/exceptions/index.js +16 -0
- package/dist/exceptions/index.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js.map +1 -1
- package/dist/interfaces/macros.d.ts +5 -1
- package/dist/interfaces/project-interfaces.d.ts +5 -0
- package/dist/interfaces/project-interfaces.js.map +1 -1
- package/dist/interfaces/resource-interfaces.d.ts +14 -22
- package/dist/interfaces/resource-interfaces.js +10 -9
- package/dist/interfaces/resource-interfaces.js.map +1 -1
- package/dist/macros/base-macro.d.ts +2 -0
- package/dist/macros/base-macro.js +66 -19
- package/dist/macros/base-macro.js.map +1 -1
- package/dist/macros/graph/index.d.ts +0 -1
- package/dist/macros/graph/index.js +16 -12
- package/dist/macros/graph/index.js.map +1 -1
- package/dist/macros/index.d.ts +30 -3
- package/dist/macros/index.js +36 -6
- package/dist/macros/index.js.map +1 -1
- package/dist/macros/report/index.d.ts +13 -10
- package/dist/macros/report/index.js +35 -38
- package/dist/macros/report/index.js.map +1 -1
- package/dist/module-manager.d.ts +32 -11
- package/dist/module-manager.js +301 -147
- package/dist/module-manager.js.map +1 -1
- package/dist/project-settings.js +8 -8
- package/dist/project-settings.js.map +1 -1
- package/dist/resources/array-handler.js +6 -1
- package/dist/resources/array-handler.js.map +1 -1
- package/dist/resources/card-type-resource.d.ts +13 -9
- package/dist/resources/card-type-resource.js +47 -23
- package/dist/resources/card-type-resource.js.map +1 -1
- package/dist/resources/create-defaults.d.ts +10 -9
- package/dist/resources/create-defaults.js +15 -12
- package/dist/resources/create-defaults.js.map +1 -1
- package/dist/resources/field-type-resource.d.ts +0 -1
- package/dist/resources/field-type-resource.js +2 -10
- package/dist/resources/field-type-resource.js.map +1 -1
- package/dist/resources/file-resource.d.ts +7 -7
- package/dist/resources/file-resource.js +32 -7
- package/dist/resources/file-resource.js.map +1 -1
- package/dist/resources/folder-resource.d.ts +10 -9
- package/dist/resources/folder-resource.js +10 -9
- package/dist/resources/folder-resource.js.map +1 -1
- package/dist/resources/report-resource.d.ts +5 -6
- package/dist/resources/report-resource.js +16 -7
- package/dist/resources/report-resource.js.map +1 -1
- package/dist/resources/template-resource.d.ts +5 -6
- package/dist/resources/template-resource.js +7 -6
- package/dist/resources/template-resource.js.map +1 -1
- package/dist/resources/workflow-resource.d.ts +15 -8
- package/dist/resources/workflow-resource.js +124 -8
- package/dist/resources/workflow-resource.js.map +1 -1
- package/dist/types/queries.d.ts +11 -10
- package/dist/types/queries.js +10 -9
- package/dist/types/queries.js.map +1 -1
- package/dist/utils/clingo-fact-builder.d.ts +1 -0
- package/dist/utils/clingo-fact-builder.js +8 -3
- package/dist/utils/clingo-fact-builder.js.map +1 -1
- package/dist/utils/clingo-facts.js +15 -11
- package/dist/utils/clingo-facts.js.map +1 -1
- package/dist/utils/constants.d.ts +18 -12
- package/dist/utils/constants.js +18 -11
- package/dist/utils/constants.js.map +1 -1
- package/dist/utils/log-utils.d.ts +15 -2
- package/dist/utils/log-utils.js +20 -37
- package/dist/utils/log-utils.js.map +1 -1
- package/dist/utils/report.d.ts +27 -0
- package/dist/utils/report.js +60 -0
- package/dist/utils/report.js.map +1 -0
- package/dist/utils/resource-utils.js +3 -0
- package/dist/utils/resource-utils.js.map +1 -1
- package/dist/utils/sanitize-svg.d.ts +3 -4
- package/dist/utils/sanitize-svg.js +4 -7
- package/dist/utils/sanitize-svg.js.map +1 -1
- package/dist/utils/validate.js +2 -1
- package/dist/utils/validate.js.map +1 -1
- package/package.json +9 -11
- package/src/card-metadata-updater.ts +7 -2
- package/src/command-handler.ts +23 -13
- package/src/command-manager.ts +54 -13
- package/src/commands/calculate.ts +90 -106
- package/src/commands/create.ts +18 -10
- package/src/commands/export.ts +3 -2
- package/src/commands/import.ts +30 -17
- package/src/commands/index.ts +0 -2
- package/src/commands/remove.ts +7 -12
- package/src/commands/show.ts +13 -5
- package/src/commands/validate.ts +14 -44
- package/src/containers/project/project-content-watcher.ts +65 -0
- package/src/containers/project/project-paths.ts +1 -1
- package/src/containers/project/resource-collector.ts +33 -14
- package/src/containers/project.ts +96 -19
- package/src/containers/template.ts +7 -2
- package/src/exceptions/index.ts +36 -0
- package/src/index.ts +2 -0
- package/src/interfaces/macros.ts +5 -1
- package/src/interfaces/project-interfaces.ts +8 -0
- package/src/interfaces/resource-interfaces.ts +15 -22
- package/src/macros/base-macro.ts +89 -25
- package/src/macros/graph/index.ts +22 -12
- package/src/macros/index.ts +61 -8
- package/src/macros/report/index.ts +42 -43
- package/src/module-manager.ts +383 -177
- package/src/project-settings.ts +9 -11
- package/src/resources/array-handler.ts +7 -2
- package/src/resources/card-type-resource.ts +61 -46
- package/src/resources/create-defaults.ts +15 -12
- package/src/resources/field-type-resource.ts +2 -17
- package/src/resources/file-resource.ts +46 -8
- package/src/resources/folder-resource.ts +11 -10
- package/src/resources/report-resource.ts +19 -7
- package/src/resources/template-resource.ts +7 -6
- package/src/resources/workflow-resource.ts +155 -8
- package/src/types/queries.ts +11 -10
- package/src/utils/clingo-fact-builder.ts +8 -3
- package/src/utils/clingo-facts.ts +18 -12
- package/src/utils/constants.ts +20 -12
- package/src/utils/log-utils.ts +24 -45
- package/src/utils/report.ts +86 -0
- package/src/utils/resource-utils.ts +4 -0
- package/src/utils/sanitize-svg.ts +4 -9
- package/src/utils/validate.ts +3 -2
- package/dist/commands/export-site.d.ts +0 -45
- package/dist/commands/export-site.js +0 -301
- package/dist/commands/export-site.js.map +0 -1
- package/src/commands/export-site.ts +0 -356
package/src/macros/index.ts
CHANGED
|
@@ -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
|
-
|
|
36
|
-
|
|
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
|
-
|
|
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.
|
|
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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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 {
|
|
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 {
|
|
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(
|
|
32
|
-
|
|
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
|
|
45
|
-
|
|
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
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
})
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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 {
|