@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
|
@@ -11,410 +11,54 @@
|
|
|
11
11
|
License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
17
|
-
import { readFile } from 'node:fs/promises';
|
|
14
|
+
import { resourceName } from '../utils/resource-utils.js';
|
|
15
|
+
import { ResourceObject } from './resource-object.js';
|
|
16
|
+
import { ResourcesFrom } from '../containers/project.js';
|
|
18
17
|
|
|
19
18
|
import type {
|
|
20
19
|
Card,
|
|
21
|
-
FetchCardDetails,
|
|
22
20
|
ResourceFolderType,
|
|
23
21
|
} from '../interfaces/project-interfaces.js';
|
|
24
|
-
import {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
type RemoveOperation,
|
|
29
|
-
ResourceObject,
|
|
30
|
-
} from './resource-object.js';
|
|
31
|
-
import { DefaultContent } from './create-defaults.js';
|
|
32
|
-
import { deleteFile, pathExists } from '../utils/file-utils.js';
|
|
33
|
-
import { hasCode } from '../utils/error-utils.js';
|
|
34
|
-
import { Project, ResourcesFrom } from '../containers/project.js';
|
|
35
|
-
import {
|
|
36
|
-
readJsonFile,
|
|
37
|
-
readJsonFileSync,
|
|
38
|
-
writeJsonFile,
|
|
39
|
-
} from '../utils/json.js';
|
|
40
|
-
import type {
|
|
41
|
-
ResourceBaseMetadata,
|
|
42
|
-
ResourceContent,
|
|
43
|
-
} from '../interfaces/resource-interfaces.js';
|
|
44
|
-
import {
|
|
45
|
-
type ResourceName,
|
|
46
|
-
resourceName,
|
|
47
|
-
resourceNameToPath,
|
|
48
|
-
resourceNameToString,
|
|
49
|
-
} from '../utils/resource-utils.js';
|
|
50
|
-
import type { Resource } from '../interfaces/project-interfaces.js';
|
|
51
|
-
import { sortCards } from '../utils/card-utils.js';
|
|
52
|
-
import { Template } from '../containers/template.js';
|
|
53
|
-
import { Validate } from '../commands/index.js';
|
|
54
|
-
|
|
55
|
-
export {
|
|
56
|
-
type AddOperation,
|
|
57
|
-
type Card,
|
|
58
|
-
type ChangeOperation,
|
|
59
|
-
DefaultContent,
|
|
60
|
-
type Operation,
|
|
61
|
-
Project,
|
|
62
|
-
RemoveOperation,
|
|
63
|
-
ResourcesFrom,
|
|
64
|
-
resourceName,
|
|
65
|
-
type ResourceName,
|
|
66
|
-
resourceNameToString,
|
|
67
|
-
sortCards,
|
|
68
|
-
};
|
|
22
|
+
import type { Project } from '../containers/project.js';
|
|
23
|
+
import type { ResourceBaseMetadata } from '../interfaces/resource-interfaces.js';
|
|
24
|
+
import type { ResourceName } from '../utils/resource-utils.js';
|
|
25
|
+
import type { ShowReturnType } from './resource-object.js';
|
|
69
26
|
|
|
70
27
|
/**
|
|
71
28
|
* Base class for file based resources (card types, field types, link types, workflows, ...)
|
|
72
29
|
*/
|
|
73
|
-
export class FileResource
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
private cache: Map<string, JSON>;
|
|
80
|
-
|
|
81
|
-
constructor(
|
|
82
|
-
project: Project,
|
|
83
|
-
resourceName: ResourceName,
|
|
84
|
-
protected type: ResourceFolderType,
|
|
85
|
-
) {
|
|
86
|
-
super(project, resourceName);
|
|
87
|
-
this.cache = this.project.resourceCache;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// Type of resource.
|
|
91
|
-
private resourceType(): ResourceFolderType {
|
|
92
|
-
return this.type as ResourceFolderType;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
// Converts resource name to Resource object.
|
|
96
|
-
private resourceObjectToResource(object: FileResource): Resource {
|
|
97
|
-
return {
|
|
98
|
-
name: object.data ? object.data.name : '',
|
|
99
|
-
path: object.fileName.substring(0, object.fileName.lastIndexOf(sep)),
|
|
100
|
-
};
|
|
30
|
+
export abstract class FileResource<
|
|
31
|
+
T extends ResourceBaseMetadata,
|
|
32
|
+
> extends ResourceObject<T, never> {
|
|
33
|
+
constructor(project: Project, name: ResourceName, type: ResourceFolderType) {
|
|
34
|
+
super(project, name, type);
|
|
35
|
+
this.initialize();
|
|
101
36
|
}
|
|
102
|
-
|
|
103
|
-
private toCache() {
|
|
104
|
-
this.cache.set(
|
|
105
|
-
resourceNameToString(this.resourceName),
|
|
106
|
-
this.content as unknown as JSON,
|
|
107
|
-
);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
37
|
// Collects cards that are using the 'cardTypeName'.
|
|
111
|
-
protected async collectCards(
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
) {
|
|
115
|
-
async function filteredCards(
|
|
116
|
-
cardSource: Promise<Card[]>,
|
|
117
|
-
cardTypeName: string,
|
|
118
|
-
): Promise<Card[]> {
|
|
119
|
-
const cards = await cardSource;
|
|
38
|
+
protected async collectCards(cardTypeName: string) {
|
|
39
|
+
function filteredCards(cardSource: Card[], cardTypeName: string): Card[] {
|
|
40
|
+
const cards = cardSource;
|
|
120
41
|
return cards.filter((card) => card.metadata?.cardType === cardTypeName);
|
|
121
42
|
}
|
|
122
43
|
|
|
123
44
|
// Collect both project cards ...
|
|
124
|
-
const
|
|
125
|
-
this.project.cards(this.project.paths.cardRootFolder
|
|
45
|
+
const projectCards = filteredCards(
|
|
46
|
+
this.project.cards(this.project.paths.cardRootFolder),
|
|
126
47
|
cardTypeName,
|
|
127
48
|
);
|
|
128
49
|
// ... and cards from each template that would be affected.
|
|
129
|
-
const templates =
|
|
130
|
-
const
|
|
131
|
-
const templateObject =
|
|
132
|
-
return filteredCards(templateObject.cards(
|
|
50
|
+
const templates = this.project.resources.templates(ResourcesFrom.localOnly);
|
|
51
|
+
const templateCards = templates.map((template) => {
|
|
52
|
+
const templateObject = template.templateObject();
|
|
53
|
+
return filteredCards(templateObject.cards(), cardTypeName);
|
|
133
54
|
});
|
|
134
55
|
// Return all affected cards
|
|
135
|
-
const cards = (
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
return cards;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
// Get (resource folder) type name
|
|
142
|
-
protected get getType() {
|
|
143
|
-
return super.getType;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
// Get logger instance
|
|
147
|
-
protected get logger() {
|
|
148
|
-
return super.getLogger(this.getType);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
// Initialize the resource.
|
|
152
|
-
protected initialize() {
|
|
153
|
-
if (this.resourceName.type === '') {
|
|
154
|
-
this.resourceName.type = this.type;
|
|
155
|
-
}
|
|
156
|
-
if (this.resourceName.prefix === '') {
|
|
157
|
-
this.resourceName.prefix = this.project.projectPrefix;
|
|
158
|
-
}
|
|
159
|
-
if (this.type) {
|
|
160
|
-
this.moduleResource =
|
|
161
|
-
this.resourceName.prefix !== this.project.projectPrefix;
|
|
162
|
-
this.resourceFolder = this.moduleResource
|
|
163
|
-
? join(
|
|
164
|
-
this.project.paths.modulesFolder,
|
|
165
|
-
this.resourceName.prefix,
|
|
166
|
-
this.resourceName.type,
|
|
167
|
-
)
|
|
168
|
-
: this.project.paths.resourcePath(this.type);
|
|
169
|
-
this.fileName = resourceNameToPath(this.project, this.resourceName);
|
|
170
|
-
}
|
|
171
|
-
// Read from cache, if entry exists...
|
|
172
|
-
if (this.cache.has(resourceNameToString(this.resourceName))) {
|
|
173
|
-
this.content = this.cache.get(
|
|
174
|
-
resourceNameToString(this.resourceName),
|
|
175
|
-
) as unknown as ResourceBaseMetadata;
|
|
176
|
-
return;
|
|
177
|
-
}
|
|
178
|
-
//... otherwise read from disk and add to cache
|
|
179
|
-
try {
|
|
180
|
-
this.content = readJsonFileSync(this.fileName);
|
|
181
|
-
this.toCache();
|
|
182
|
-
} catch {
|
|
183
|
-
// do nothing, it is possible that file has not been created yet.
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
// Creates resource.
|
|
188
|
-
protected async create(newContent?: ResourceContent) {
|
|
189
|
-
if (pathExists(this.fileName)) {
|
|
190
|
-
throw new Error(
|
|
191
|
-
`Resource '${this.resourceName.identifier}' already exists in the project`,
|
|
192
|
-
);
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
if (this.resourceFolder === '') {
|
|
196
|
-
this.resourceName = resourceName(
|
|
197
|
-
`${this.project.projectPrefix}/${this.type}/${this.resourceName.identifier}`,
|
|
198
|
-
);
|
|
199
|
-
this.resourceFolder = this.project.paths.resourcePath(
|
|
200
|
-
this.resourceName.type as ResourceFolderType,
|
|
201
|
-
);
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
const validName = await Validate.getInstance().validResourceName(
|
|
205
|
-
this.resourceType(),
|
|
206
|
-
resourceNameToString(this.resourceName),
|
|
207
|
-
await this.project.projectPrefixes(),
|
|
208
|
-
);
|
|
209
|
-
|
|
210
|
-
let validContent = {} as ResourceContent;
|
|
211
|
-
if (newContent) {
|
|
212
|
-
validContent = newContent;
|
|
213
|
-
validContent.name = validName;
|
|
214
|
-
} else {
|
|
215
|
-
validContent.description = '';
|
|
216
|
-
validContent.displayName = '';
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
this.content = validContent;
|
|
220
|
-
await this.write();
|
|
221
|
-
|
|
222
|
-
// Notify project & collector
|
|
223
|
-
this.project.addResource(
|
|
224
|
-
this.resourceObjectToResource(this),
|
|
225
|
-
this.content as unknown as JSON,
|
|
56
|
+
const cards = [projectCards, ...templateCards].reduce(
|
|
57
|
+
(accumulator, value) => accumulator.concat(value),
|
|
58
|
+
[],
|
|
226
59
|
);
|
|
60
|
+
return cards;
|
|
227
61
|
}
|
|
228
|
-
|
|
229
|
-
// Calculations that use this resource.
|
|
230
|
-
protected async calculations(): Promise<string[]> {
|
|
231
|
-
const references: string[] = [];
|
|
232
|
-
const resourceName = resourceNameToString(this.resourceName);
|
|
233
|
-
for (const calculation of await this.project.calculations(
|
|
234
|
-
ResourcesFrom.all,
|
|
235
|
-
)) {
|
|
236
|
-
const fileNameWithExtension = calculation.name.endsWith('.lp')
|
|
237
|
-
? calculation.name
|
|
238
|
-
: calculation.name + '.lp';
|
|
239
|
-
const filename = join(calculation.path, basename(fileNameWithExtension));
|
|
240
|
-
try {
|
|
241
|
-
const content = await readFile(filename, 'utf-8');
|
|
242
|
-
if (content.includes(resourceName)) {
|
|
243
|
-
references.push(calculation.name);
|
|
244
|
-
}
|
|
245
|
-
} catch (error) {
|
|
246
|
-
// Skip files that don't exist (they may have been renamed or deleted)
|
|
247
|
-
if (hasCode(error) && error.code === 'ENOENT') {
|
|
248
|
-
this.logger.warn(`Skipping non-existent file: ${filename}`);
|
|
249
|
-
continue;
|
|
250
|
-
}
|
|
251
|
-
throw new Error(
|
|
252
|
-
`Failed to process file ${filename}: ${(error as Error).message}`,
|
|
253
|
-
);
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
return references;
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
// Cards from project.
|
|
260
|
-
protected async cards(): Promise<Card[]> {
|
|
261
|
-
return [
|
|
262
|
-
...(await this.project.cards(undefined, {
|
|
263
|
-
content: true,
|
|
264
|
-
metadata: true,
|
|
265
|
-
})),
|
|
266
|
-
...(await this.project.allTemplateCards({
|
|
267
|
-
content: true,
|
|
268
|
-
metadata: true,
|
|
269
|
-
})),
|
|
270
|
-
];
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
// Returns memory resident data as JSON.
|
|
274
|
-
// This is basically same as 'show' but doesn't do any checks; just returns the current content.
|
|
275
|
-
public get data() {
|
|
276
|
-
return this.content.name !== '' ? this.content : undefined;
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
// Deletes resource.
|
|
280
|
-
protected async delete() {
|
|
281
|
-
if (this.moduleResource) {
|
|
282
|
-
throw new Error(`Cannot delete module resources`);
|
|
283
|
-
}
|
|
284
|
-
if (!this.fileName.endsWith('.json')) {
|
|
285
|
-
this.fileName += '.json';
|
|
286
|
-
}
|
|
287
|
-
if (!pathExists(this.fileName)) {
|
|
288
|
-
throw new Error(
|
|
289
|
-
`Resource '${this.resourceName.identifier}' does not exist in the project`,
|
|
290
|
-
);
|
|
291
|
-
}
|
|
292
|
-
const usedIn = await this.usage();
|
|
293
|
-
if (usedIn.length > 0) {
|
|
294
|
-
throw new Error(
|
|
295
|
-
`Cannot delete resource ${resourceNameToString(this.resourceName)}. It is used by: ${usedIn.join(', ')}`,
|
|
296
|
-
);
|
|
297
|
-
}
|
|
298
|
-
await deleteFile(this.fileName);
|
|
299
|
-
this.project.removeResource(this.resourceObjectToResource(this));
|
|
300
|
-
this.fileName = '';
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
protected async validName(newName: ResourceName) {
|
|
304
|
-
const validName = await Validate.getInstance().validResourceName(
|
|
305
|
-
this.resourceType(),
|
|
306
|
-
resourceNameToString(newName),
|
|
307
|
-
await this.project.projectPrefixes(),
|
|
308
|
-
);
|
|
309
|
-
return validName;
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
// Called after inherited class has finished 'update' operation.
|
|
313
|
-
protected async postUpdate<Type>(
|
|
314
|
-
content: ResourceContent,
|
|
315
|
-
key: string,
|
|
316
|
-
op: Operation<Type>,
|
|
317
|
-
) {
|
|
318
|
-
function toValue(op: Operation<Type>) {
|
|
319
|
-
if (op.name === 'rank') return op.newIndex;
|
|
320
|
-
if (op.name === 'add') return JSON.stringify(op.target);
|
|
321
|
-
if (op.name === 'remove') return JSON.stringify(op.target);
|
|
322
|
-
if (op.name === 'change') return JSON.stringify(op.to);
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
// Check that new name is valid.
|
|
326
|
-
if (op.name === 'change' && key === 'name') {
|
|
327
|
-
const newName = resourceName(
|
|
328
|
-
(op as ChangeOperation<string>).to as string,
|
|
329
|
-
);
|
|
330
|
-
content.name = await this.validName(newName);
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
// Once changes have been made; validate the content.
|
|
334
|
-
try {
|
|
335
|
-
await this.validate(content);
|
|
336
|
-
} catch (error) {
|
|
337
|
-
if (error instanceof Error) {
|
|
338
|
-
const errorValue = typeof op === 'object' ? toValue(op) : op;
|
|
339
|
-
throw new Error(
|
|
340
|
-
`Cannot ${op.name} '${key}' --> '${errorValue}: ${error.message}'`,
|
|
341
|
-
);
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
this.content = content;
|
|
346
|
-
await this.write();
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
// Reads content from file to memory.
|
|
350
|
-
protected async read() {
|
|
351
|
-
this.content = await readJsonFile(this.fileName);
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
// Renames resource.
|
|
355
|
-
protected async rename(newName: ResourceName) {
|
|
356
|
-
this.cache.delete(resourceNameToString(this.resourceName));
|
|
357
|
-
if (this.moduleResource) {
|
|
358
|
-
throw new Error(`Cannot rename module resources`);
|
|
359
|
-
}
|
|
360
|
-
if (!pathExists(this.fileName)) {
|
|
361
|
-
throw new Error(
|
|
362
|
-
`Resource '${this.resourceName.identifier}' does not exist`,
|
|
363
|
-
);
|
|
364
|
-
}
|
|
365
|
-
if (newName.prefix !== this.project.projectPrefix) {
|
|
366
|
-
throw new Error('Can only rename project resources');
|
|
367
|
-
}
|
|
368
|
-
if (newName.type !== this.resourceName.type) {
|
|
369
|
-
throw new Error('Cannot change resource type');
|
|
370
|
-
}
|
|
371
|
-
await Validate.getInstance().validResourceName(
|
|
372
|
-
this.resourceType(),
|
|
373
|
-
resourceNameToString(newName),
|
|
374
|
-
await this.project.projectPrefixes(),
|
|
375
|
-
);
|
|
376
|
-
const newFilename = join(
|
|
377
|
-
this.project.paths.resourcePath(newName.type as ResourceFolderType),
|
|
378
|
-
newName.identifier + '.json',
|
|
379
|
-
);
|
|
380
|
-
await rename(this.fileName, newFilename);
|
|
381
|
-
|
|
382
|
-
this.fileName = newFilename;
|
|
383
|
-
this.content.name = resourceNameToString(newName);
|
|
384
|
-
this.resourceName = newName;
|
|
385
|
-
this.toCache();
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
// Show resource data as JSON.
|
|
389
|
-
protected async show(): Promise<ResourceContent> {
|
|
390
|
-
if (!pathExists(this.fileName)) {
|
|
391
|
-
const resourceType = `${this.type[0].toUpperCase()}${this.type.slice(1, this.type.length - 1)}`;
|
|
392
|
-
const name = resourceNameToString(this.resourceName);
|
|
393
|
-
throw new Error(
|
|
394
|
-
`${resourceType} '${name}' does not exist in the project`,
|
|
395
|
-
);
|
|
396
|
-
}
|
|
397
|
-
return this.content as ResourceContent;
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
// Update resource; the base class makes some checks only.
|
|
401
|
-
protected async update<Type>(
|
|
402
|
-
key: string,
|
|
403
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
404
|
-
_op: Operation<Type>,
|
|
405
|
-
): Promise<void> {
|
|
406
|
-
if (!this.data) {
|
|
407
|
-
const name = resourceNameToString(this.resourceName);
|
|
408
|
-
throw new Error(`Resource '${name}' does not exist in the project`);
|
|
409
|
-
}
|
|
410
|
-
if (this.moduleResource) {
|
|
411
|
-
throw new Error(`Cannot update module resources`);
|
|
412
|
-
}
|
|
413
|
-
if (key === '' || key === undefined) {
|
|
414
|
-
throw new Error(`Cannot update empty key`);
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
|
|
418
62
|
// Updates resource key to a new prefix
|
|
419
63
|
protected updatePrefixInResourceName(name: string, prefixes: string[]) {
|
|
420
64
|
const { identifier, prefix, type } = resourceName(name);
|
|
@@ -426,64 +70,12 @@ export class FileResource extends ResourceObject {
|
|
|
426
70
|
: name;
|
|
427
71
|
}
|
|
428
72
|
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
const cardArray = cards?.length
|
|
437
|
-
? cards
|
|
438
|
-
: await this.project.cards(undefined, {
|
|
439
|
-
content: true,
|
|
440
|
-
metadata: true,
|
|
441
|
-
});
|
|
442
|
-
|
|
443
|
-
return cardArray
|
|
444
|
-
.filter((card) =>
|
|
445
|
-
card.content?.includes(resourceNameToString(this.resourceName)),
|
|
446
|
-
)
|
|
447
|
-
.map((card) => card.key);
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
// Write the content from memory to disk.
|
|
451
|
-
protected async write() {
|
|
452
|
-
if (this.moduleResource) {
|
|
453
|
-
throw new Error(`Cannot change module resources`);
|
|
454
|
-
}
|
|
455
|
-
|
|
456
|
-
// Create folder for resources and add correct .schema file.
|
|
457
|
-
await mkdir(this.resourceFolder, { recursive: true });
|
|
458
|
-
await writeJsonFile(
|
|
459
|
-
join(this.resourceFolder, '.schema'),
|
|
460
|
-
this.contentSchema,
|
|
461
|
-
{
|
|
462
|
-
flag: 'wx',
|
|
463
|
-
},
|
|
464
|
-
);
|
|
465
|
-
// Check if "name" has changed. Changing "name" means renaming the file.
|
|
466
|
-
const nameInContent = resourceName(this.content.name).identifier + '.json';
|
|
467
|
-
const currentFileName = basename(this.fileName);
|
|
468
|
-
|
|
469
|
-
if (nameInContent !== currentFileName) {
|
|
470
|
-
const newFileName = join(this.resourceFolder, nameInContent);
|
|
471
|
-
await rename(this.fileName, newFileName);
|
|
472
|
-
this.fileName = newFileName;
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
await writeJsonFile(this.fileName, this.content);
|
|
476
|
-
this.toCache();
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
// Validate that current memory-based 'content' is valid.
|
|
480
|
-
protected async validate(content?: object) {
|
|
481
|
-
const invalidJson = Validate.getInstance().validateJson(
|
|
482
|
-
content ? content : this.content,
|
|
483
|
-
this.contentSchemaId,
|
|
484
|
-
);
|
|
485
|
-
if (invalidJson.length) {
|
|
486
|
-
throw new Error(`Invalid content JSON: ${invalidJson}`);
|
|
487
|
-
}
|
|
73
|
+
/**
|
|
74
|
+
* Returns the resource metadata content.
|
|
75
|
+
* @returns metadata content
|
|
76
|
+
*/
|
|
77
|
+
public show(): ShowReturnType<T> {
|
|
78
|
+
this.assertResourceExists();
|
|
79
|
+
return this.content;
|
|
488
80
|
}
|
|
489
81
|
}
|