@cyberismo/data-handler 0.0.2
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/LICENSE +702 -0
- package/dist/card-metadata-updater.d.ts +33 -0
- package/dist/card-metadata-updater.js +121 -0
- package/dist/card-metadata-updater.js.map +1 -0
- package/dist/command-handler.d.ts +96 -0
- package/dist/command-handler.js +557 -0
- package/dist/command-handler.js.map +1 -0
- package/dist/command-manager.d.ts +43 -0
- package/dist/command-manager.js +73 -0
- package/dist/command-manager.js.map +1 -0
- package/dist/commands/calculate.d.ts +86 -0
- package/dist/commands/calculate.js +444 -0
- package/dist/commands/calculate.js.map +1 -0
- package/dist/commands/create.d.ts +114 -0
- package/dist/commands/create.js +389 -0
- package/dist/commands/create.js.map +1 -0
- package/dist/commands/edit.d.ts +37 -0
- package/dist/commands/edit.js +99 -0
- package/dist/commands/edit.js.map +1 -0
- package/dist/commands/export-site.d.ts +45 -0
- package/dist/commands/export-site.js +301 -0
- package/dist/commands/export-site.js.map +1 -0
- package/dist/commands/export.d.ts +53 -0
- package/dist/commands/export.js +251 -0
- package/dist/commands/export.js.map +1 -0
- package/dist/commands/import.d.ts +53 -0
- package/dist/commands/import.js +133 -0
- package/dist/commands/import.js.map +1 -0
- package/dist/commands/index.d.ts +26 -0
- package/dist/commands/index.js +27 -0
- package/dist/commands/index.js.map +1 -0
- package/dist/commands/move.d.ts +55 -0
- package/dist/commands/move.js +341 -0
- package/dist/commands/move.js.map +1 -0
- package/dist/commands/remove.d.ts +38 -0
- package/dist/commands/remove.js +192 -0
- package/dist/commands/remove.js.map +1 -0
- package/dist/commands/rename.d.ts +46 -0
- package/dist/commands/rename.js +289 -0
- package/dist/commands/rename.js.map +1 -0
- package/dist/commands/show.d.ts +124 -0
- package/dist/commands/show.js +345 -0
- package/dist/commands/show.js.map +1 -0
- package/dist/commands/transition.d.ts +27 -0
- package/dist/commands/transition.js +92 -0
- package/dist/commands/transition.js.map +1 -0
- package/dist/commands/update.d.ts +29 -0
- package/dist/commands/update.js +64 -0
- package/dist/commands/update.js.map +1 -0
- package/dist/commands/validate.d.ts +143 -0
- package/dist/commands/validate.js +689 -0
- package/dist/commands/validate.js.map +1 -0
- package/dist/containers/card-container.d.ts +44 -0
- package/dist/containers/card-container.js +282 -0
- package/dist/containers/card-container.js.map +1 -0
- package/dist/containers/project/project-paths.d.ts +46 -0
- package/dist/containers/project/project-paths.js +105 -0
- package/dist/containers/project/project-paths.js.map +1 -0
- package/dist/containers/project/resource-collector.d.ts +86 -0
- package/dist/containers/project/resource-collector.js +331 -0
- package/dist/containers/project/resource-collector.js.map +1 -0
- package/dist/containers/project.d.ts +351 -0
- package/dist/containers/project.js +896 -0
- package/dist/containers/project.js.map +1 -0
- package/dist/containers/template.d.ts +108 -0
- package/dist/containers/template.js +433 -0
- package/dist/containers/template.js.map +1 -0
- package/dist/exceptions/index.d.ts +19 -0
- package/dist/exceptions/index.js +26 -0
- package/dist/exceptions/index.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/interfaces/adoc.d.ts +12 -0
- package/dist/interfaces/adoc.js +13 -0
- package/dist/interfaces/adoc.js.map +1 -0
- package/dist/interfaces/macros.d.ts +45 -0
- package/dist/interfaces/macros.js +13 -0
- package/dist/interfaces/macros.js.map +1 -0
- package/dist/interfaces/project-interfaces.d.ts +121 -0
- package/dist/interfaces/project-interfaces.js +21 -0
- package/dist/interfaces/project-interfaces.js.map +1 -0
- package/dist/interfaces/request-status-interfaces.d.ts +28 -0
- package/dist/interfaces/request-status-interfaces.js +20 -0
- package/dist/interfaces/request-status-interfaces.js.map +1 -0
- package/dist/interfaces/resource-interfaces.d.ts +117 -0
- package/dist/interfaces/resource-interfaces.js +20 -0
- package/dist/interfaces/resource-interfaces.js.map +1 -0
- package/dist/macros/base-macro.d.ts +31 -0
- package/dist/macros/base-macro.js +126 -0
- package/dist/macros/base-macro.js.map +1 -0
- package/dist/macros/common.d.ts +17 -0
- package/dist/macros/common.js +23 -0
- package/dist/macros/common.js.map +1 -0
- package/dist/macros/createCards/index.d.ts +36 -0
- package/dist/macros/createCards/index.js +35 -0
- package/dist/macros/createCards/index.js.map +1 -0
- package/dist/macros/createCards/metadata.d.ts +14 -0
- package/dist/macros/createCards/metadata.js +18 -0
- package/dist/macros/createCards/metadata.js.map +1 -0
- package/dist/macros/graph/index.d.ts +29 -0
- package/dist/macros/graph/index.js +91 -0
- package/dist/macros/graph/index.js.map +1 -0
- package/dist/macros/graph/metadata.d.ts +14 -0
- package/dist/macros/graph/metadata.js +18 -0
- package/dist/macros/graph/metadata.js.map +1 -0
- package/dist/macros/index.d.ts +93 -0
- package/dist/macros/index.js +237 -0
- package/dist/macros/index.js.map +1 -0
- package/dist/macros/report/index.d.ts +26 -0
- package/dist/macros/report/index.js +70 -0
- package/dist/macros/report/index.js.map +1 -0
- package/dist/macros/report/metadata.d.ts +14 -0
- package/dist/macros/report/metadata.js +18 -0
- package/dist/macros/report/metadata.js.map +1 -0
- package/dist/macros/scoreCard/index.d.ts +30 -0
- package/dist/macros/scoreCard/index.js +38 -0
- package/dist/macros/scoreCard/index.js.map +1 -0
- package/dist/macros/scoreCard/metadata.d.ts +14 -0
- package/dist/macros/scoreCard/metadata.js +18 -0
- package/dist/macros/scoreCard/metadata.js.map +1 -0
- package/dist/macros/task-queue.d.ts +46 -0
- package/dist/macros/task-queue.js +69 -0
- package/dist/macros/task-queue.js.map +1 -0
- package/dist/module-manager.d.ts +62 -0
- package/dist/module-manager.js +350 -0
- package/dist/module-manager.js.map +1 -0
- package/dist/permissions/action-guard.d.ts +28 -0
- package/dist/permissions/action-guard.js +61 -0
- package/dist/permissions/action-guard.js.map +1 -0
- package/dist/project-settings.d.ts +42 -0
- package/dist/project-settings.js +120 -0
- package/dist/project-settings.js.map +1 -0
- package/dist/resources/array-handler.d.ts +28 -0
- package/dist/resources/array-handler.js +116 -0
- package/dist/resources/array-handler.js.map +1 -0
- package/dist/resources/card-type-resource.d.ts +72 -0
- package/dist/resources/card-type-resource.js +334 -0
- package/dist/resources/card-type-resource.js.map +1 -0
- package/dist/resources/create-defaults.d.ts +81 -0
- package/dist/resources/create-defaults.js +184 -0
- package/dist/resources/create-defaults.js.map +1 -0
- package/dist/resources/field-type-resource.d.ts +88 -0
- package/dist/resources/field-type-resource.js +411 -0
- package/dist/resources/field-type-resource.js.map +1 -0
- package/dist/resources/file-resource.d.ts +50 -0
- package/dist/resources/file-resource.js +301 -0
- package/dist/resources/file-resource.js.map +1 -0
- package/dist/resources/folder-resource.d.ts +66 -0
- package/dist/resources/folder-resource.js +100 -0
- package/dist/resources/folder-resource.js.map +1 -0
- package/dist/resources/graph-model-resource.d.ts +78 -0
- package/dist/resources/graph-model-resource.js +164 -0
- package/dist/resources/graph-model-resource.js.map +1 -0
- package/dist/resources/graph-view-resource.d.ts +78 -0
- package/dist/resources/graph-view-resource.js +163 -0
- package/dist/resources/graph-view-resource.js.map +1 -0
- package/dist/resources/link-type-resource.d.ts +62 -0
- package/dist/resources/link-type-resource.js +150 -0
- package/dist/resources/link-type-resource.js.map +1 -0
- package/dist/resources/report-resource.d.ts +77 -0
- package/dist/resources/report-resource.js +171 -0
- package/dist/resources/report-resource.js.map +1 -0
- package/dist/resources/resource-object.d.ts +108 -0
- package/dist/resources/resource-object.js +147 -0
- package/dist/resources/resource-object.js.map +1 -0
- package/dist/resources/template-resource.d.ts +82 -0
- package/dist/resources/template-resource.js +173 -0
- package/dist/resources/template-resource.js.map +1 -0
- package/dist/resources/workflow-resource.d.ts +67 -0
- package/dist/resources/workflow-resource.js +156 -0
- package/dist/resources/workflow-resource.js.map +1 -0
- package/dist/types/queries.d.ts +142 -0
- package/dist/types/queries.js +16 -0
- package/dist/types/queries.js.map +1 -0
- package/dist/utils/card-utils.d.ts +34 -0
- package/dist/utils/card-utils.js +78 -0
- package/dist/utils/card-utils.js.map +1 -0
- package/dist/utils/clingo-fact-builder.d.ts +58 -0
- package/dist/utils/clingo-fact-builder.js +126 -0
- package/dist/utils/clingo-fact-builder.js.map +1 -0
- package/dist/utils/clingo-facts.d.ts +97 -0
- package/dist/utils/clingo-facts.js +352 -0
- package/dist/utils/clingo-facts.js.map +1 -0
- package/dist/utils/clingo-parser.d.ts +59 -0
- package/dist/utils/clingo-parser.js +403 -0
- package/dist/utils/clingo-parser.js.map +1 -0
- package/dist/utils/clingo-program-builder.d.ts +39 -0
- package/dist/utils/clingo-program-builder.js +57 -0
- package/dist/utils/clingo-program-builder.js.map +1 -0
- package/dist/utils/common-utils.d.ts +24 -0
- package/dist/utils/common-utils.js +47 -0
- package/dist/utils/common-utils.js.map +1 -0
- package/dist/utils/constants.d.ts +18 -0
- package/dist/utils/constants.js +27 -0
- package/dist/utils/constants.js.map +1 -0
- package/dist/utils/csv.d.ts +18 -0
- package/dist/utils/csv.js +45 -0
- package/dist/utils/csv.js.map +1 -0
- package/dist/utils/file-utils.d.ts +69 -0
- package/dist/utils/file-utils.js +158 -0
- package/dist/utils/file-utils.js.map +1 -0
- package/dist/utils/json.d.ts +61 -0
- package/dist/utils/json.js +108 -0
- package/dist/utils/json.js.map +1 -0
- package/dist/utils/lexorank.d.ts +59 -0
- package/dist/utils/lexorank.js +159 -0
- package/dist/utils/lexorank.js.map +1 -0
- package/dist/utils/log-utils.d.ts +40 -0
- package/dist/utils/log-utils.js +109 -0
- package/dist/utils/log-utils.js.map +1 -0
- package/dist/utils/random.d.ts +19 -0
- package/dist/utils/random.js +34 -0
- package/dist/utils/random.js.map +1 -0
- package/dist/utils/resource-utils.d.ts +45 -0
- package/dist/utils/resource-utils.js +137 -0
- package/dist/utils/resource-utils.js.map +1 -0
- package/dist/utils/sanitize-svg.d.ts +18 -0
- package/dist/utils/sanitize-svg.js +38 -0
- package/dist/utils/sanitize-svg.js.map +1 -0
- package/dist/utils/user-preferences.d.ts +64 -0
- package/dist/utils/user-preferences.js +106 -0
- package/dist/utils/user-preferences.js.map +1 -0
- package/dist/utils/validate.d.ts +26 -0
- package/dist/utils/validate.js +53 -0
- package/dist/utils/validate.js.map +1 -0
- package/dist/utils/value-utils.d.ts +58 -0
- package/dist/utils/value-utils.js +181 -0
- package/dist/utils/value-utils.js.map +1 -0
- package/package.json +67 -0
- package/src/card-metadata-updater.ts +182 -0
- package/src/command-handler.ts +686 -0
- package/src/command-manager.ts +99 -0
- package/src/commands/calculate.ts +591 -0
- package/src/commands/create.ts +559 -0
- package/src/commands/edit.ts +123 -0
- package/src/commands/export-site.ts +356 -0
- package/src/commands/export.ts +315 -0
- package/src/commands/import.ts +169 -0
- package/src/commands/index.ts +42 -0
- package/src/commands/move.ts +451 -0
- package/src/commands/remove.ts +244 -0
- package/src/commands/rename.ts +378 -0
- package/src/commands/show.ts +442 -0
- package/src/commands/transition.ts +127 -0
- package/src/commands/update.ts +76 -0
- package/src/commands/validate.ts +962 -0
- package/src/containers/card-container.ts +378 -0
- package/src/containers/project/project-paths.ts +127 -0
- package/src/containers/project/resource-collector.ts +379 -0
- package/src/containers/project.ts +1135 -0
- package/src/containers/template.ts +573 -0
- package/src/exceptions/index.ts +29 -0
- package/src/index.ts +33 -0
- package/src/interfaces/adoc.ts +18 -0
- package/src/interfaces/macros.ts +54 -0
- package/src/interfaces/project-interfaces.ts +208 -0
- package/src/interfaces/request-status-interfaces.ts +30 -0
- package/src/interfaces/resource-interfaces.ts +179 -0
- package/src/macros/base-macro.ts +176 -0
- package/src/macros/common.ts +24 -0
- package/src/macros/createCards/index.ts +57 -0
- package/src/macros/createCards/metadata.ts +21 -0
- package/src/macros/graph/index.ts +130 -0
- package/src/macros/graph/metadata.ts +21 -0
- package/src/macros/index.ts +321 -0
- package/src/macros/report/index.ts +88 -0
- package/src/macros/report/metadata.ts +21 -0
- package/src/macros/scoreCard/index.ts +55 -0
- package/src/macros/scoreCard/metadata.ts +21 -0
- package/src/macros/task-queue.ts +79 -0
- package/src/module-manager.ts +443 -0
- package/src/permissions/action-guard.ts +77 -0
- package/src/project-settings.ts +140 -0
- package/src/resources/array-handler.ts +141 -0
- package/src/resources/card-type-resource.ts +455 -0
- package/src/resources/create-defaults.ts +216 -0
- package/src/resources/field-type-resource.ts +533 -0
- package/src/resources/file-resource.ts +433 -0
- package/src/resources/folder-resource.ts +140 -0
- package/src/resources/graph-model-resource.ts +205 -0
- package/src/resources/graph-view-resource.ts +199 -0
- package/src/resources/link-type-resource.ts +191 -0
- package/src/resources/report-resource.ts +224 -0
- package/src/resources/resource-object.ts +246 -0
- package/src/resources/template-resource.ts +210 -0
- package/src/resources/workflow-resource.ts +205 -0
- package/src/types/queries.ts +149 -0
- package/src/utils/card-utils.ts +83 -0
- package/src/utils/clingo-fact-builder.ts +167 -0
- package/src/utils/clingo-facts.ts +550 -0
- package/src/utils/clingo-parser.ts +519 -0
- package/src/utils/clingo-program-builder.ts +71 -0
- package/src/utils/common-utils.ts +54 -0
- package/src/utils/constants.ts +32 -0
- package/src/utils/csv.ts +53 -0
- package/src/utils/file-utils.ts +182 -0
- package/src/utils/json.ts +118 -0
- package/src/utils/lexorank.ts +180 -0
- package/src/utils/log-utils.ts +127 -0
- package/src/utils/random.ts +37 -0
- package/src/utils/resource-utils.ts +180 -0
- package/src/utils/sanitize-svg.ts +46 -0
- package/src/utils/user-preferences.ts +126 -0
- package/src/utils/validate.ts +66 -0
- package/src/utils/value-utils.ts +189 -0
|
@@ -0,0 +1,433 @@
|
|
|
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
|
+
|
|
15
|
+
// node
|
|
16
|
+
import { basename, join, sep } from 'node:path';
|
|
17
|
+
import { mkdir, rename } from 'node:fs/promises';
|
|
18
|
+
import { readFile } from 'node:fs/promises';
|
|
19
|
+
|
|
20
|
+
import type {
|
|
21
|
+
Card,
|
|
22
|
+
ResourceFolderType,
|
|
23
|
+
} from '../interfaces/project-interfaces.js';
|
|
24
|
+
import {
|
|
25
|
+
type AddOperation,
|
|
26
|
+
type ChangeOperation,
|
|
27
|
+
type Operation,
|
|
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 { Project, ResourcesFrom } from '../containers/project.js';
|
|
34
|
+
import {
|
|
35
|
+
readJsonFile,
|
|
36
|
+
readJsonFileSync,
|
|
37
|
+
writeJsonFile,
|
|
38
|
+
} from '../utils/json.js';
|
|
39
|
+
import type {
|
|
40
|
+
ResourceBaseMetadata,
|
|
41
|
+
ResourceContent,
|
|
42
|
+
} from '../interfaces/resource-interfaces.js';
|
|
43
|
+
import {
|
|
44
|
+
type ResourceName,
|
|
45
|
+
resourceName,
|
|
46
|
+
resourceNameToPath,
|
|
47
|
+
resourceNameToString,
|
|
48
|
+
} from '../utils/resource-utils.js';
|
|
49
|
+
import type { Resource } from '../interfaces/project-interfaces.js';
|
|
50
|
+
import { sortCards } from '../utils/card-utils.js';
|
|
51
|
+
import { Validate } from '../commands/index.js';
|
|
52
|
+
|
|
53
|
+
export {
|
|
54
|
+
type AddOperation,
|
|
55
|
+
type Card,
|
|
56
|
+
type ChangeOperation,
|
|
57
|
+
DefaultContent,
|
|
58
|
+
type Operation,
|
|
59
|
+
Project,
|
|
60
|
+
RemoveOperation,
|
|
61
|
+
ResourcesFrom,
|
|
62
|
+
resourceName,
|
|
63
|
+
type ResourceName,
|
|
64
|
+
resourceNameToString,
|
|
65
|
+
sortCards,
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Base class for file based resources (card types, field types, link types, workflows, ...)
|
|
70
|
+
*/
|
|
71
|
+
export class FileResource extends ResourceObject {
|
|
72
|
+
public fileName: string = '';
|
|
73
|
+
protected content: ResourceBaseMetadata = { name: '' };
|
|
74
|
+
private cache: Map<string, JSON>;
|
|
75
|
+
|
|
76
|
+
constructor(
|
|
77
|
+
project: Project,
|
|
78
|
+
resourceName: ResourceName,
|
|
79
|
+
protected type: ResourceFolderType,
|
|
80
|
+
) {
|
|
81
|
+
super(project, resourceName);
|
|
82
|
+
this.cache = this.project.resourceCache;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Type of resource.
|
|
86
|
+
private resourceType(): ResourceFolderType {
|
|
87
|
+
return this.type as ResourceFolderType;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Converts resource name to Resource object.
|
|
91
|
+
private resourceObjectToResource(object: FileResource): Resource {
|
|
92
|
+
return {
|
|
93
|
+
name: object.data ? object.data.name : '',
|
|
94
|
+
path: object.fileName.substring(0, object.fileName.lastIndexOf(sep)),
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
private toCache() {
|
|
99
|
+
this.cache.set(
|
|
100
|
+
resourceNameToString(this.resourceName),
|
|
101
|
+
this.content as unknown as JSON,
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Initialize the resource.
|
|
106
|
+
protected initialize() {
|
|
107
|
+
if (this.resourceName.type === '') {
|
|
108
|
+
this.resourceName.type = this.type;
|
|
109
|
+
}
|
|
110
|
+
if (this.resourceName.prefix === '') {
|
|
111
|
+
this.resourceName.prefix = this.project.projectPrefix;
|
|
112
|
+
}
|
|
113
|
+
if (this.type) {
|
|
114
|
+
this.moduleResource =
|
|
115
|
+
this.resourceName.prefix !== this.project.projectPrefix;
|
|
116
|
+
this.resourceFolder = this.moduleResource
|
|
117
|
+
? join(
|
|
118
|
+
this.project.paths.modulesFolder,
|
|
119
|
+
this.resourceName.prefix,
|
|
120
|
+
this.resourceName.type,
|
|
121
|
+
)
|
|
122
|
+
: this.project.paths.resourcePath(this.type);
|
|
123
|
+
this.fileName = resourceNameToPath(this.project, this.resourceName);
|
|
124
|
+
}
|
|
125
|
+
// Read from cache, if entry exists...
|
|
126
|
+
if (this.cache.has(resourceNameToString(this.resourceName))) {
|
|
127
|
+
this.content = this.cache.get(
|
|
128
|
+
resourceNameToString(this.resourceName),
|
|
129
|
+
) as unknown as ResourceBaseMetadata;
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
//... otherwise read from disk and add to cache
|
|
133
|
+
try {
|
|
134
|
+
this.content = readJsonFileSync(this.fileName);
|
|
135
|
+
this.toCache();
|
|
136
|
+
} catch {
|
|
137
|
+
// do nothing, it is possible that file has not been created yet.
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Creates resource.
|
|
142
|
+
protected async create(newContent?: ResourceContent) {
|
|
143
|
+
if (pathExists(this.fileName)) {
|
|
144
|
+
throw new Error(
|
|
145
|
+
`Resource '${this.resourceName.identifier}' already exists in the project`,
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (this.resourceFolder === '') {
|
|
150
|
+
this.resourceName = resourceName(
|
|
151
|
+
`${this.project.projectPrefix}/${this.type}/${this.resourceName.identifier}`,
|
|
152
|
+
);
|
|
153
|
+
this.resourceFolder = this.project.paths.resourcePath(
|
|
154
|
+
this.resourceName.type as ResourceFolderType,
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const validName = await Validate.getInstance().validResourceName(
|
|
159
|
+
this.resourceType(),
|
|
160
|
+
resourceNameToString(this.resourceName),
|
|
161
|
+
await this.project.projectPrefixes(),
|
|
162
|
+
);
|
|
163
|
+
|
|
164
|
+
let validContent = {} as ResourceContent;
|
|
165
|
+
if (newContent) {
|
|
166
|
+
validContent = newContent as unknown as ResourceContent;
|
|
167
|
+
validContent.name = validName;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
this.content = validContent;
|
|
171
|
+
await this.write();
|
|
172
|
+
|
|
173
|
+
// Notify project & collector
|
|
174
|
+
this.project.addResource(
|
|
175
|
+
this.resourceObjectToResource(this),
|
|
176
|
+
this.content as unknown as JSON,
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// Calculations that use this resource.
|
|
181
|
+
protected async calculations(): Promise<string[]> {
|
|
182
|
+
const references: string[] = [];
|
|
183
|
+
const resourceName = resourceNameToString(this.resourceName);
|
|
184
|
+
for (const calculation of await this.project.calculations(
|
|
185
|
+
ResourcesFrom.all,
|
|
186
|
+
)) {
|
|
187
|
+
const fileNameWithExtension = calculation.name.endsWith('.lp')
|
|
188
|
+
? calculation.name
|
|
189
|
+
: calculation.name + '.lp';
|
|
190
|
+
const filename = join(calculation.path, basename(fileNameWithExtension));
|
|
191
|
+
try {
|
|
192
|
+
const content = await readFile(filename, 'utf-8');
|
|
193
|
+
if (content.includes(resourceName)) {
|
|
194
|
+
references.push(calculation.name);
|
|
195
|
+
}
|
|
196
|
+
} catch (error) {
|
|
197
|
+
throw new Error(
|
|
198
|
+
`Failed to process file ${filename}: ${(error as Error).message}`,
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return references;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// Cards from project.
|
|
206
|
+
protected async cards(): Promise<Card[]> {
|
|
207
|
+
return [
|
|
208
|
+
...(await this.project.cards(undefined, {
|
|
209
|
+
content: true,
|
|
210
|
+
metadata: true,
|
|
211
|
+
})),
|
|
212
|
+
...(await this.project.allTemplateCards({
|
|
213
|
+
content: true,
|
|
214
|
+
metadata: true,
|
|
215
|
+
})),
|
|
216
|
+
];
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// Returns memory resident data as JSON.
|
|
220
|
+
// This is basically same as 'show' but doesn't do any checks; just returns the current content.
|
|
221
|
+
public get data() {
|
|
222
|
+
return this.content.name !== '' ? this.content : undefined;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// Deletes resource.
|
|
226
|
+
protected async delete() {
|
|
227
|
+
if (this.moduleResource) {
|
|
228
|
+
throw new Error(`Cannot delete module resources`);
|
|
229
|
+
}
|
|
230
|
+
if (!this.fileName.endsWith('.json')) {
|
|
231
|
+
this.fileName += '.json';
|
|
232
|
+
}
|
|
233
|
+
if (!pathExists(this.fileName)) {
|
|
234
|
+
throw new Error(
|
|
235
|
+
`Resource '${this.resourceName.identifier}' does not exist in the project`,
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
const usedIn = await this.usage();
|
|
239
|
+
if (usedIn.length > 0) {
|
|
240
|
+
throw new Error(
|
|
241
|
+
`Cannot delete resource ${resourceNameToString(this.resourceName)}. It is used by: ${usedIn.join(', ')}`,
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
await deleteFile(this.fileName);
|
|
245
|
+
this.project.removeResource(this.resourceObjectToResource(this));
|
|
246
|
+
this.fileName = '';
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
protected async validName(newName: ResourceName) {
|
|
250
|
+
const validName = await Validate.getInstance().validResourceName(
|
|
251
|
+
this.resourceType(),
|
|
252
|
+
resourceNameToString(newName),
|
|
253
|
+
await this.project.projectPrefixes(),
|
|
254
|
+
);
|
|
255
|
+
return validName;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// Called after inherited class has finished 'update' operation.
|
|
259
|
+
protected async postUpdate<Type>(
|
|
260
|
+
content: ResourceContent,
|
|
261
|
+
key: string,
|
|
262
|
+
op: Operation<Type>,
|
|
263
|
+
) {
|
|
264
|
+
function toValue(op: Operation<Type>) {
|
|
265
|
+
if (op.name === 'rank') return op.newIndex;
|
|
266
|
+
if (op.name === 'add') return op.target;
|
|
267
|
+
if (op.name === 'remove') return op.target;
|
|
268
|
+
if (op.name === 'change') return op.to;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// Check that new name is valid.
|
|
272
|
+
if (op.name === 'change' && key === 'name') {
|
|
273
|
+
const newName = resourceName(
|
|
274
|
+
(op as ChangeOperation<string>).to as string,
|
|
275
|
+
);
|
|
276
|
+
content.name = await this.validName(newName);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// Once changes have been made; validate the content.
|
|
280
|
+
try {
|
|
281
|
+
await this.validate(content);
|
|
282
|
+
} catch (error) {
|
|
283
|
+
if (error instanceof Error) {
|
|
284
|
+
const errorValue = typeof op === 'object' ? toValue(op) : op;
|
|
285
|
+
throw new Error(`Cannot ${op.name} '${key}' --> '${errorValue}'`);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
this.content = content;
|
|
290
|
+
await this.write();
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// Reads content from file to memory.
|
|
294
|
+
protected async read() {
|
|
295
|
+
this.content = await readJsonFile(this.fileName);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// Renames resource.
|
|
299
|
+
protected async rename(newName: ResourceName) {
|
|
300
|
+
this.cache.delete(resourceNameToString(this.resourceName));
|
|
301
|
+
if (this.moduleResource) {
|
|
302
|
+
throw new Error(`Cannot rename module resources`);
|
|
303
|
+
}
|
|
304
|
+
if (!pathExists(this.fileName)) {
|
|
305
|
+
throw new Error(
|
|
306
|
+
`Resource '${this.resourceName.identifier}' does not exist`,
|
|
307
|
+
);
|
|
308
|
+
}
|
|
309
|
+
if (newName.prefix !== this.project.projectPrefix) {
|
|
310
|
+
throw new Error('Can only rename project resources');
|
|
311
|
+
}
|
|
312
|
+
if (newName.type !== this.resourceName.type) {
|
|
313
|
+
throw new Error('Cannot change resource type');
|
|
314
|
+
}
|
|
315
|
+
await Validate.getInstance().validResourceName(
|
|
316
|
+
this.resourceType(),
|
|
317
|
+
resourceNameToString(newName),
|
|
318
|
+
await this.project.projectPrefixes(),
|
|
319
|
+
);
|
|
320
|
+
const newFilename = join(
|
|
321
|
+
this.project.paths.resourcePath(newName.type as ResourceFolderType),
|
|
322
|
+
newName.identifier + '.json',
|
|
323
|
+
);
|
|
324
|
+
await rename(this.fileName, newFilename);
|
|
325
|
+
|
|
326
|
+
this.fileName = newFilename;
|
|
327
|
+
this.content.name = resourceNameToString(newName);
|
|
328
|
+
this.resourceName = newName;
|
|
329
|
+
this.toCache();
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// Show resource data as JSON.
|
|
333
|
+
protected async show(): Promise<ResourceContent> {
|
|
334
|
+
if (!pathExists(this.fileName)) {
|
|
335
|
+
const resourceType = `${this.type[0].toUpperCase()}${this.type.slice(1, this.type.length - 1)}`;
|
|
336
|
+
const name = resourceNameToString(this.resourceName);
|
|
337
|
+
throw new Error(
|
|
338
|
+
`${resourceType} '${name}' does not exist in the project`,
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
return this.content as ResourceContent;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// Update resource; the base class makes some checks only.
|
|
345
|
+
protected async update<Type>(
|
|
346
|
+
key: string,
|
|
347
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
348
|
+
_op: Operation<Type>,
|
|
349
|
+
): Promise<void> {
|
|
350
|
+
const content = this.data;
|
|
351
|
+
if (!content) {
|
|
352
|
+
throw new Error(`Resource '${this.fileName}' does not exist`);
|
|
353
|
+
}
|
|
354
|
+
if (this.moduleResource) {
|
|
355
|
+
throw new Error(`Cannot update module resources`);
|
|
356
|
+
}
|
|
357
|
+
if (key === '' || key === undefined) {
|
|
358
|
+
throw new Error(`Cannot update empty key`);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// Updates resource key to a new prefix
|
|
363
|
+
protected updatePrefixInResourceName(name: string, prefixes: string[]) {
|
|
364
|
+
const { identifier, prefix, type } = resourceName(name);
|
|
365
|
+
if (this.moduleResource) {
|
|
366
|
+
return name;
|
|
367
|
+
}
|
|
368
|
+
return !prefixes.includes(prefix)
|
|
369
|
+
? `${this.project.configuration.cardKeyPrefix}/${type}/${identifier}`
|
|
370
|
+
: name;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// Check if there are references to the resource in the card content.
|
|
374
|
+
protected async usage(cards?: Card[]): Promise<string[]> {
|
|
375
|
+
if (!pathExists(this.fileName)) {
|
|
376
|
+
throw new Error(
|
|
377
|
+
`Resource '${this.resourceName.identifier}' does not exist in the project`,
|
|
378
|
+
);
|
|
379
|
+
}
|
|
380
|
+
const cardArray = cards?.length
|
|
381
|
+
? cards
|
|
382
|
+
: await this.project.cards(undefined, {
|
|
383
|
+
content: true,
|
|
384
|
+
metadata: true,
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
return cardArray
|
|
388
|
+
.filter((card) =>
|
|
389
|
+
card.content?.includes(resourceNameToString(this.resourceName)),
|
|
390
|
+
)
|
|
391
|
+
.map((card) => card.key);
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
// Write the content from memory to disk.
|
|
395
|
+
protected async write() {
|
|
396
|
+
if (this.moduleResource) {
|
|
397
|
+
throw new Error(`Cannot change module resources`);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// Create folder for resources and add correct .schema file.
|
|
401
|
+
await mkdir(this.resourceFolder, { recursive: true });
|
|
402
|
+
await writeJsonFile(
|
|
403
|
+
join(this.resourceFolder, '.schema'),
|
|
404
|
+
this.contentSchema,
|
|
405
|
+
{
|
|
406
|
+
flag: 'wx',
|
|
407
|
+
},
|
|
408
|
+
);
|
|
409
|
+
// Check if "name" has changed. Changing "name" means renaming the file.
|
|
410
|
+
const nameInContent = resourceName(this.content.name).identifier + '.json';
|
|
411
|
+
const currentFileName = basename(this.fileName);
|
|
412
|
+
|
|
413
|
+
if (nameInContent !== currentFileName) {
|
|
414
|
+
const newFileName = join(this.resourceFolder, nameInContent);
|
|
415
|
+
await rename(this.fileName, newFileName);
|
|
416
|
+
this.fileName = newFileName;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
await writeJsonFile(this.fileName, this.content);
|
|
420
|
+
this.toCache();
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
// Validate that current memory-based 'content' is valid.
|
|
424
|
+
protected async validate(content?: object) {
|
|
425
|
+
const invalidJson = Validate.getInstance().validateJson(
|
|
426
|
+
content ? content : this.content,
|
|
427
|
+
this.contentSchemaId,
|
|
428
|
+
);
|
|
429
|
+
if (invalidJson.length) {
|
|
430
|
+
throw new Error(`Invalid content JSON: ${invalidJson}`);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
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/>.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { basename, join } from 'node:path';
|
|
14
|
+
import { mkdir, rename, rm } from 'node:fs/promises';
|
|
15
|
+
|
|
16
|
+
import type { ResourceFolderType } from '../interfaces/project-interfaces.js';
|
|
17
|
+
import {
|
|
18
|
+
type Card,
|
|
19
|
+
DefaultContent,
|
|
20
|
+
FileResource,
|
|
21
|
+
type Operation,
|
|
22
|
+
Project,
|
|
23
|
+
resourceName,
|
|
24
|
+
type ResourceName,
|
|
25
|
+
resourceNameToString,
|
|
26
|
+
sortCards,
|
|
27
|
+
} from './file-resource.js';
|
|
28
|
+
import type { ResourceContent } from '../interfaces/resource-interfaces.js';
|
|
29
|
+
|
|
30
|
+
export {
|
|
31
|
+
type Card,
|
|
32
|
+
DefaultContent,
|
|
33
|
+
FileResource,
|
|
34
|
+
type Operation,
|
|
35
|
+
Project,
|
|
36
|
+
ResourceName,
|
|
37
|
+
resourceNameToString,
|
|
38
|
+
sortCards,
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Folder type resource class. These are resources that have their own folders for content.
|
|
43
|
+
*/
|
|
44
|
+
export class FolderResource extends FileResource {
|
|
45
|
+
protected internalFolder: string = '';
|
|
46
|
+
|
|
47
|
+
constructor(project: Project, name: ResourceName, type: ResourceFolderType) {
|
|
48
|
+
super(project, name, type);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Creates a new folder type object. Base class writes the object to disk automatically.
|
|
53
|
+
* @param newContent Content for the type.
|
|
54
|
+
*/
|
|
55
|
+
protected async create(newContent?: ResourceContent) {
|
|
56
|
+
await super.create(newContent);
|
|
57
|
+
await mkdir(this.internalFolder, { recursive: true });
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Returns content data.
|
|
62
|
+
*/
|
|
63
|
+
public get data() {
|
|
64
|
+
return super.data;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Deletes file(s) from disk and clears out the memory resident object.
|
|
69
|
+
*/
|
|
70
|
+
protected async delete() {
|
|
71
|
+
await super.delete();
|
|
72
|
+
await rm(this.internalFolder, { recursive: true, force: true });
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
protected initialize(): void {
|
|
76
|
+
super.initialize();
|
|
77
|
+
|
|
78
|
+
this.internalFolder = join(
|
|
79
|
+
this.resourceFolder,
|
|
80
|
+
this.resourceName.identifier,
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Renames resource metadata file and renames memory resident object 'name'.
|
|
86
|
+
* @param newName New name for the resource.
|
|
87
|
+
*/
|
|
88
|
+
protected async rename(newName: ResourceName) {
|
|
89
|
+
return super.rename(newName);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Shows metadata of the resource.
|
|
94
|
+
* @returns resource type's metadata.
|
|
95
|
+
*/
|
|
96
|
+
protected async show(): Promise<ResourceContent> {
|
|
97
|
+
return super.show() as Promise<ResourceContent>;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Updates resource.
|
|
102
|
+
* @param key Key to modify
|
|
103
|
+
* @param op Operation to perform on 'key'
|
|
104
|
+
*/
|
|
105
|
+
protected async update<Type>(key: string, op: Operation<Type>) {
|
|
106
|
+
return super.update(key, op);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Returns an array of card keys, and/or resource names where this resource is used.
|
|
111
|
+
* @param cards Optional. If defined, only these cards are checked.
|
|
112
|
+
* @returns an array of card keys, and/or resource names where this resource is used.
|
|
113
|
+
*/
|
|
114
|
+
protected async usage(cards?: Card[]): Promise<string[]> {
|
|
115
|
+
return super.usage(cards);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Writes resource content to disk.
|
|
120
|
+
*/
|
|
121
|
+
protected async write() {
|
|
122
|
+
const folderName = basename(this.internalFolder);
|
|
123
|
+
|
|
124
|
+
// Check if "name" has changed. Changing "name" means renaming the file.
|
|
125
|
+
const nameInContent = resourceName(this.content.name).identifier;
|
|
126
|
+
if (folderName !== nameInContent) {
|
|
127
|
+
const newFolderName = join(this.resourceFolder, nameInContent);
|
|
128
|
+
await rename(this.internalFolder, newFolderName);
|
|
129
|
+
this.internalFolder = newFolderName;
|
|
130
|
+
}
|
|
131
|
+
return super.write();
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Validates the resource. If object is invalid, throws.
|
|
136
|
+
*/
|
|
137
|
+
protected async validate(content?: object) {
|
|
138
|
+
return super.validate(content);
|
|
139
|
+
}
|
|
140
|
+
}
|