@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.
- package/dist/card-metadata-updater.js +8 -4
- package/dist/card-metadata-updater.js.map +1 -1
- package/dist/command-handler.d.ts +4 -0
- package/dist/command-handler.js +29 -19
- package/dist/command-handler.js.map +1 -1
- package/dist/command-manager.d.ts +25 -2
- package/dist/command-manager.js +30 -5
- package/dist/command-manager.js.map +1 -1
- package/dist/commands/create.d.ts +1 -1
- package/dist/commands/create.js +45 -93
- package/dist/commands/create.js.map +1 -1
- package/dist/commands/edit.d.ts +1 -15
- package/dist/commands/edit.js +15 -89
- package/dist/commands/edit.js.map +1 -1
- package/dist/commands/export.d.ts +11 -2
- package/dist/commands/export.js +58 -58
- package/dist/commands/export.js.map +1 -1
- package/dist/commands/import.d.ts +9 -1
- package/dist/commands/import.js +17 -11
- package/dist/commands/import.js.map +1 -1
- package/dist/commands/move.d.ts +1 -2
- package/dist/commands/move.js +107 -146
- package/dist/commands/move.js.map +1 -1
- package/dist/commands/remove.d.ts +8 -1
- package/dist/commands/remove.js +17 -48
- package/dist/commands/remove.js.map +1 -1
- package/dist/commands/rename.d.ts +4 -9
- package/dist/commands/rename.js +34 -108
- package/dist/commands/rename.js.map +1 -1
- package/dist/commands/show.d.ts +22 -34
- package/dist/commands/show.js +103 -151
- package/dist/commands/show.js.map +1 -1
- package/dist/commands/transition.d.ts +9 -2
- package/dist/commands/transition.js +49 -44
- package/dist/commands/transition.js.map +1 -1
- package/dist/commands/update.d.ts +18 -12
- package/dist/commands/update.js +34 -18
- package/dist/commands/update.js.map +1 -1
- package/dist/commands/validate.d.ts +18 -10
- package/dist/commands/validate.js +101 -47
- package/dist/commands/validate.js.map +1 -1
- package/dist/containers/card-container.d.ts +87 -24
- package/dist/containers/card-container.js +183 -279
- package/dist/containers/card-container.js.map +1 -1
- package/dist/containers/project/calculation-engine.d.ts +13 -4
- package/dist/containers/project/calculation-engine.js +79 -77
- package/dist/containers/project/calculation-engine.js.map +1 -1
- package/dist/containers/project/card-cache.d.ts +146 -0
- package/dist/containers/project/card-cache.js +411 -0
- package/dist/containers/project/card-cache.js.map +1 -0
- package/dist/containers/project/project-paths.d.ts +5 -4
- package/dist/containers/project/project-paths.js +16 -12
- package/dist/containers/project/project-paths.js.map +1 -1
- package/dist/containers/project/resource-cache.d.ts +169 -0
- package/dist/containers/project/resource-cache.js +507 -0
- package/dist/containers/project/resource-cache.js.map +1 -0
- package/dist/containers/project/resource-handler.d.ts +129 -0
- package/dist/containers/project/resource-handler.js +206 -0
- package/dist/containers/project/resource-handler.js.map +1 -0
- package/dist/containers/project.d.ts +114 -195
- package/dist/containers/project.js +425 -535
- package/dist/containers/project.js.map +1 -1
- package/dist/containers/template.d.ts +22 -32
- package/dist/containers/template.js +113 -115
- package/dist/containers/template.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/interfaces/folder-content-interfaces.d.ts +7 -4
- package/dist/interfaces/folder-content-interfaces.js +3 -3
- package/dist/interfaces/folder-content-interfaces.js.map +1 -1
- package/dist/interfaces/macros.d.ts +1 -0
- package/dist/interfaces/macros.js +1 -1
- package/dist/interfaces/macros.js.map +1 -1
- package/dist/interfaces/project-interfaces.d.ts +7 -5
- package/dist/interfaces/project-interfaces.js.map +1 -1
- package/dist/interfaces/resource-interfaces.d.ts +25 -22
- package/dist/interfaces/resource-interfaces.js +3 -0
- package/dist/interfaces/resource-interfaces.js.map +1 -1
- package/dist/macros/common.d.ts +10 -10
- package/dist/macros/createCards/index.d.ts +0 -13
- package/dist/macros/createCards/index.js.map +1 -1
- package/dist/macros/createCards/types.d.ts +44 -0
- package/dist/macros/createCards/types.js +15 -0
- package/dist/macros/createCards/types.js.map +1 -0
- package/dist/macros/graph/index.d.ts +2 -6
- package/dist/macros/graph/index.js +14 -28
- package/dist/macros/graph/index.js.map +1 -1
- package/dist/macros/graph/types.d.ts +23 -0
- package/dist/macros/graph/types.js +15 -0
- package/dist/macros/graph/types.js.map +1 -0
- package/dist/macros/image/index.d.ts +8 -16
- package/dist/macros/image/index.js +36 -33
- package/dist/macros/image/index.js.map +1 -1
- package/dist/macros/image/types.d.ts +38 -0
- package/dist/macros/image/types.js +15 -0
- package/dist/macros/image/types.js.map +1 -0
- package/dist/macros/include/index.d.ts +1 -6
- package/dist/macros/include/index.js +4 -7
- package/dist/macros/include/index.js.map +1 -1
- package/dist/macros/include/types.d.ts +31 -0
- package/dist/macros/include/types.js +15 -0
- package/dist/macros/include/types.js.map +1 -0
- package/dist/macros/index.d.ts +1 -1
- package/dist/macros/index.js +2 -2
- package/dist/macros/index.js.map +1 -1
- package/dist/macros/percentage/index.d.ts +0 -6
- package/dist/macros/percentage/index.js.map +1 -1
- package/dist/macros/percentage/types.d.ts +31 -0
- package/dist/macros/percentage/types.js +15 -0
- package/dist/macros/percentage/types.js.map +1 -0
- package/dist/macros/report/index.d.ts +0 -3
- package/dist/macros/report/index.js +3 -6
- package/dist/macros/report/index.js.map +1 -1
- package/dist/macros/report/types.d.ts +19 -0
- package/dist/macros/report/types.js +15 -0
- package/dist/macros/report/types.js.map +1 -0
- package/dist/macros/scoreCard/index.d.ts +0 -6
- package/dist/macros/scoreCard/index.js.map +1 -1
- package/dist/macros/scoreCard/types.d.ts +31 -0
- package/dist/macros/scoreCard/types.js +15 -0
- package/dist/macros/scoreCard/types.js.map +1 -0
- package/dist/macros/types.d.ts +25 -0
- package/dist/macros/types.js +2 -0
- package/dist/macros/types.js.map +1 -0
- package/dist/macros/vega/index.d.ts +0 -4
- package/dist/macros/vega/index.js.map +1 -1
- package/dist/macros/vega/types.d.ts +20 -0
- package/dist/macros/vega/types.js +2 -0
- package/dist/macros/vega/types.js.map +1 -0
- package/dist/macros/vegalite/index.d.ts +0 -4
- package/dist/macros/vegalite/index.js.map +1 -1
- package/dist/macros/vegalite/types.d.ts +20 -0
- package/dist/macros/vegalite/types.js +15 -0
- package/dist/macros/vegalite/types.js.map +1 -0
- package/dist/macros/xref/index.d.ts +0 -3
- package/dist/macros/xref/index.js +5 -14
- package/dist/macros/xref/index.js.map +1 -1
- package/dist/macros/xref/types.d.ts +19 -0
- package/dist/macros/xref/types.js +15 -0
- package/dist/macros/xref/types.js.map +1 -0
- package/dist/module-manager.d.ts +16 -3
- package/dist/module-manager.js +55 -23
- package/dist/module-manager.js.map +1 -1
- package/dist/project-settings.d.ts +16 -3
- package/dist/project-settings.js +79 -14
- package/dist/project-settings.js.map +1 -1
- package/dist/resources/calculation-resource.d.ts +6 -33
- package/dist/resources/calculation-resource.js +11 -60
- package/dist/resources/calculation-resource.js.map +1 -1
- package/dist/resources/card-type-resource.d.ts +10 -22
- package/dist/resources/card-type-resource.js +46 -66
- package/dist/resources/card-type-resource.js.map +1 -1
- package/dist/resources/create-defaults.d.ts +3 -2
- package/dist/resources/create-defaults.js +3 -2
- package/dist/resources/create-defaults.js.map +1 -1
- package/dist/resources/field-type-resource.d.ts +8 -22
- package/dist/resources/field-type-resource.js +35 -60
- package/dist/resources/field-type-resource.js.map +1 -1
- package/dist/resources/file-resource.d.ts +14 -35
- package/dist/resources/file-resource.js +22 -301
- package/dist/resources/file-resource.js.map +1 -1
- package/dist/resources/folder-resource.d.ts +44 -66
- package/dist/resources/folder-resource.js +102 -149
- package/dist/resources/folder-resource.js.map +1 -1
- package/dist/resources/graph-model-resource.d.ts +9 -34
- package/dist/resources/graph-model-resource.js +18 -64
- package/dist/resources/graph-model-resource.js.map +1 -1
- package/dist/resources/graph-view-resource.d.ts +9 -29
- package/dist/resources/graph-view-resource.js +13 -48
- package/dist/resources/graph-view-resource.js.map +1 -1
- package/dist/resources/link-type-resource.d.ts +9 -23
- package/dist/resources/link-type-resource.js +11 -33
- package/dist/resources/link-type-resource.js.map +1 -1
- package/dist/resources/report-resource.d.ts +10 -23
- package/dist/resources/report-resource.js +20 -67
- package/dist/resources/report-resource.js.map +1 -1
- package/dist/resources/resource-object.d.ts +143 -23
- package/dist/resources/resource-object.js +369 -48
- package/dist/resources/resource-object.js.map +1 -1
- package/dist/resources/template-resource.d.ts +10 -17
- package/dist/resources/template-resource.js +19 -27
- package/dist/resources/template-resource.js.map +1 -1
- package/dist/resources/workflow-resource.d.ts +9 -25
- package/dist/resources/workflow-resource.js +25 -55
- package/dist/resources/workflow-resource.js.map +1 -1
- package/dist/utils/card-utils.d.ts +69 -19
- package/dist/utils/card-utils.js +179 -30
- package/dist/utils/card-utils.js.map +1 -1
- package/dist/utils/clingo-fact-builder.d.ts +25 -14
- package/dist/utils/clingo-fact-builder.js +27 -5
- package/dist/utils/clingo-fact-builder.js.map +1 -1
- package/dist/utils/clingo-facts.js +14 -7
- package/dist/utils/clingo-facts.js.map +1 -1
- package/dist/utils/clingo-parser.js +1 -1
- package/dist/utils/clingo-parser.js.map +1 -1
- package/dist/utils/constants.d.ts +2 -0
- package/dist/utils/constants.js +4 -0
- package/dist/utils/constants.js.map +1 -1
- package/dist/utils/csv.js +1 -1
- package/dist/utils/csv.js.map +1 -1
- package/dist/utils/resource-utils.d.ts +1 -0
- package/dist/utils/resource-utils.js +2 -1
- package/dist/utils/resource-utils.js.map +1 -1
- package/package.json +11 -11
- package/src/card-metadata-updater.ts +9 -7
- package/src/command-handler.ts +35 -23
- package/src/command-manager.ts +32 -19
- package/src/commands/create.ts +59 -160
- package/src/commands/edit.ts +16 -132
- package/src/commands/export.ts +71 -81
- package/src/commands/import.ts +26 -18
- package/src/commands/move.ts +143 -179
- package/src/commands/remove.ts +20 -59
- package/src/commands/rename.ts +45 -156
- package/src/commands/show.ts +153 -211
- package/src/commands/transition.ts +53 -58
- package/src/commands/update.ts +44 -23
- package/src/commands/validate.ts +108 -82
- package/src/containers/card-container.ts +200 -360
- package/src/containers/project/calculation-engine.ts +81 -105
- package/src/containers/project/card-cache.ts +497 -0
- package/src/containers/project/project-paths.ts +21 -13
- package/src/containers/project/resource-cache.ts +648 -0
- package/src/containers/project/resource-handler.ts +265 -0
- package/src/containers/project.ts +551 -693
- package/src/containers/template.ts +129 -142
- package/src/index.ts +1 -0
- package/src/interfaces/folder-content-interfaces.ts +14 -7
- package/src/interfaces/macros.ts +2 -0
- package/src/interfaces/project-interfaces.ts +14 -7
- package/src/interfaces/resource-interfaces.ts +30 -27
- package/src/macros/createCards/index.ts +1 -12
- package/src/macros/createCards/types.ts +46 -0
- package/src/macros/graph/index.ts +27 -52
- package/src/macros/graph/types.ts +24 -0
- package/src/macros/image/index.ts +50 -61
- package/src/macros/image/types.ts +39 -0
- package/src/macros/include/index.ts +6 -15
- package/src/macros/include/types.ts +32 -0
- package/src/macros/index.ts +2 -2
- package/src/macros/percentage/index.ts +1 -7
- package/src/macros/percentage/types.ts +32 -0
- package/src/macros/report/index.ts +4 -13
- package/src/macros/report/types.ts +20 -0
- package/src/macros/scoreCard/index.ts +1 -7
- package/src/macros/scoreCard/types.ts +32 -0
- package/src/macros/types.ts +48 -0
- package/src/macros/vega/index.ts +1 -4
- package/src/macros/vega/types.ts +21 -0
- package/src/macros/vegalite/index.ts +1 -4
- package/src/macros/vegalite/types.ts +22 -0
- package/src/macros/xref/index.ts +6 -20
- package/src/macros/xref/types.ts +20 -0
- package/src/module-manager.ts +79 -22
- package/src/project-settings.ts +84 -15
- package/src/resources/calculation-resource.ts +21 -91
- package/src/resources/card-type-resource.ts +74 -109
- package/src/resources/create-defaults.ts +3 -2
- package/src/resources/field-type-resource.ts +61 -104
- package/src/resources/file-resource.ts +33 -441
- package/src/resources/folder-resource.ts +130 -207
- package/src/resources/graph-model-resource.ts +36 -95
- package/src/resources/graph-view-resource.ts +28 -70
- package/src/resources/link-type-resource.ts +23 -53
- package/src/resources/report-resource.ts +34 -96
- package/src/resources/resource-object.ts +511 -66
- package/src/resources/template-resource.ts +32 -44
- package/src/resources/workflow-resource.ts +42 -85
- package/src/utils/card-utils.ts +217 -31
- package/src/utils/clingo-fact-builder.ts +28 -16
- package/src/utils/clingo-facts.ts +16 -7
- package/src/utils/clingo-parser.ts +1 -1
- package/src/utils/constants.ts +6 -0
- package/src/utils/csv.ts +1 -1
- package/src/utils/resource-utils.ts +2 -1
- package/dist/containers/project/resource-collector.d.ts +0 -87
- package/dist/containers/project/resource-collector.js +0 -337
- package/dist/containers/project/resource-collector.js.map +0 -1
- package/src/containers/project/resource-collector.ts +0 -396
package/src/utils/card-utils.ts
CHANGED
|
@@ -1,25 +1,161 @@
|
|
|
1
1
|
/**
|
|
2
|
-
|
|
3
|
-
|
|
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
|
-
|
|
14
|
+
import { sep } from 'node:path';
|
|
15
|
+
import { CARD_KEY_SEPARATOR, ROOT } from './constants.js';
|
|
6
16
|
|
|
7
|
-
|
|
17
|
+
import type {
|
|
18
|
+
Card,
|
|
19
|
+
CardWithChildrenCards,
|
|
20
|
+
} from '../interfaces/project-interfaces.js';
|
|
21
|
+
import type { Project } from '../containers/project.js';
|
|
8
22
|
|
|
9
|
-
|
|
10
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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)
|
|
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
|
|
32
|
-
* @
|
|
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
|
-
|
|
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(
|
|
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
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
205
|
-
|
|
206
|
-
|
|
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 =
|
|
237
|
-
|
|
238
|
-
|
|
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:
|
|
106
|
+
field: (
|
|
107
107
|
key: string,
|
|
108
108
|
fieldName: string,
|
|
109
109
|
fieldValue: string,
|
package/src/utils/constants.ts
CHANGED
|
@@ -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.
|
|
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
|
-
}
|