@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.
Files changed (306) hide show
  1. package/LICENSE +702 -0
  2. package/dist/card-metadata-updater.d.ts +33 -0
  3. package/dist/card-metadata-updater.js +121 -0
  4. package/dist/card-metadata-updater.js.map +1 -0
  5. package/dist/command-handler.d.ts +96 -0
  6. package/dist/command-handler.js +557 -0
  7. package/dist/command-handler.js.map +1 -0
  8. package/dist/command-manager.d.ts +43 -0
  9. package/dist/command-manager.js +73 -0
  10. package/dist/command-manager.js.map +1 -0
  11. package/dist/commands/calculate.d.ts +86 -0
  12. package/dist/commands/calculate.js +444 -0
  13. package/dist/commands/calculate.js.map +1 -0
  14. package/dist/commands/create.d.ts +114 -0
  15. package/dist/commands/create.js +389 -0
  16. package/dist/commands/create.js.map +1 -0
  17. package/dist/commands/edit.d.ts +37 -0
  18. package/dist/commands/edit.js +99 -0
  19. package/dist/commands/edit.js.map +1 -0
  20. package/dist/commands/export-site.d.ts +45 -0
  21. package/dist/commands/export-site.js +301 -0
  22. package/dist/commands/export-site.js.map +1 -0
  23. package/dist/commands/export.d.ts +53 -0
  24. package/dist/commands/export.js +251 -0
  25. package/dist/commands/export.js.map +1 -0
  26. package/dist/commands/import.d.ts +53 -0
  27. package/dist/commands/import.js +133 -0
  28. package/dist/commands/import.js.map +1 -0
  29. package/dist/commands/index.d.ts +26 -0
  30. package/dist/commands/index.js +27 -0
  31. package/dist/commands/index.js.map +1 -0
  32. package/dist/commands/move.d.ts +55 -0
  33. package/dist/commands/move.js +341 -0
  34. package/dist/commands/move.js.map +1 -0
  35. package/dist/commands/remove.d.ts +38 -0
  36. package/dist/commands/remove.js +192 -0
  37. package/dist/commands/remove.js.map +1 -0
  38. package/dist/commands/rename.d.ts +46 -0
  39. package/dist/commands/rename.js +289 -0
  40. package/dist/commands/rename.js.map +1 -0
  41. package/dist/commands/show.d.ts +124 -0
  42. package/dist/commands/show.js +345 -0
  43. package/dist/commands/show.js.map +1 -0
  44. package/dist/commands/transition.d.ts +27 -0
  45. package/dist/commands/transition.js +92 -0
  46. package/dist/commands/transition.js.map +1 -0
  47. package/dist/commands/update.d.ts +29 -0
  48. package/dist/commands/update.js +64 -0
  49. package/dist/commands/update.js.map +1 -0
  50. package/dist/commands/validate.d.ts +143 -0
  51. package/dist/commands/validate.js +689 -0
  52. package/dist/commands/validate.js.map +1 -0
  53. package/dist/containers/card-container.d.ts +44 -0
  54. package/dist/containers/card-container.js +282 -0
  55. package/dist/containers/card-container.js.map +1 -0
  56. package/dist/containers/project/project-paths.d.ts +46 -0
  57. package/dist/containers/project/project-paths.js +105 -0
  58. package/dist/containers/project/project-paths.js.map +1 -0
  59. package/dist/containers/project/resource-collector.d.ts +86 -0
  60. package/dist/containers/project/resource-collector.js +331 -0
  61. package/dist/containers/project/resource-collector.js.map +1 -0
  62. package/dist/containers/project.d.ts +351 -0
  63. package/dist/containers/project.js +896 -0
  64. package/dist/containers/project.js.map +1 -0
  65. package/dist/containers/template.d.ts +108 -0
  66. package/dist/containers/template.js +433 -0
  67. package/dist/containers/template.js.map +1 -0
  68. package/dist/exceptions/index.d.ts +19 -0
  69. package/dist/exceptions/index.js +26 -0
  70. package/dist/exceptions/index.js.map +1 -0
  71. package/dist/index.d.ts +16 -0
  72. package/dist/index.js +15 -0
  73. package/dist/index.js.map +1 -0
  74. package/dist/interfaces/adoc.d.ts +12 -0
  75. package/dist/interfaces/adoc.js +13 -0
  76. package/dist/interfaces/adoc.js.map +1 -0
  77. package/dist/interfaces/macros.d.ts +45 -0
  78. package/dist/interfaces/macros.js +13 -0
  79. package/dist/interfaces/macros.js.map +1 -0
  80. package/dist/interfaces/project-interfaces.d.ts +121 -0
  81. package/dist/interfaces/project-interfaces.js +21 -0
  82. package/dist/interfaces/project-interfaces.js.map +1 -0
  83. package/dist/interfaces/request-status-interfaces.d.ts +28 -0
  84. package/dist/interfaces/request-status-interfaces.js +20 -0
  85. package/dist/interfaces/request-status-interfaces.js.map +1 -0
  86. package/dist/interfaces/resource-interfaces.d.ts +117 -0
  87. package/dist/interfaces/resource-interfaces.js +20 -0
  88. package/dist/interfaces/resource-interfaces.js.map +1 -0
  89. package/dist/macros/base-macro.d.ts +31 -0
  90. package/dist/macros/base-macro.js +126 -0
  91. package/dist/macros/base-macro.js.map +1 -0
  92. package/dist/macros/common.d.ts +17 -0
  93. package/dist/macros/common.js +23 -0
  94. package/dist/macros/common.js.map +1 -0
  95. package/dist/macros/createCards/index.d.ts +36 -0
  96. package/dist/macros/createCards/index.js +35 -0
  97. package/dist/macros/createCards/index.js.map +1 -0
  98. package/dist/macros/createCards/metadata.d.ts +14 -0
  99. package/dist/macros/createCards/metadata.js +18 -0
  100. package/dist/macros/createCards/metadata.js.map +1 -0
  101. package/dist/macros/graph/index.d.ts +29 -0
  102. package/dist/macros/graph/index.js +91 -0
  103. package/dist/macros/graph/index.js.map +1 -0
  104. package/dist/macros/graph/metadata.d.ts +14 -0
  105. package/dist/macros/graph/metadata.js +18 -0
  106. package/dist/macros/graph/metadata.js.map +1 -0
  107. package/dist/macros/index.d.ts +93 -0
  108. package/dist/macros/index.js +237 -0
  109. package/dist/macros/index.js.map +1 -0
  110. package/dist/macros/report/index.d.ts +26 -0
  111. package/dist/macros/report/index.js +70 -0
  112. package/dist/macros/report/index.js.map +1 -0
  113. package/dist/macros/report/metadata.d.ts +14 -0
  114. package/dist/macros/report/metadata.js +18 -0
  115. package/dist/macros/report/metadata.js.map +1 -0
  116. package/dist/macros/scoreCard/index.d.ts +30 -0
  117. package/dist/macros/scoreCard/index.js +38 -0
  118. package/dist/macros/scoreCard/index.js.map +1 -0
  119. package/dist/macros/scoreCard/metadata.d.ts +14 -0
  120. package/dist/macros/scoreCard/metadata.js +18 -0
  121. package/dist/macros/scoreCard/metadata.js.map +1 -0
  122. package/dist/macros/task-queue.d.ts +46 -0
  123. package/dist/macros/task-queue.js +69 -0
  124. package/dist/macros/task-queue.js.map +1 -0
  125. package/dist/module-manager.d.ts +62 -0
  126. package/dist/module-manager.js +350 -0
  127. package/dist/module-manager.js.map +1 -0
  128. package/dist/permissions/action-guard.d.ts +28 -0
  129. package/dist/permissions/action-guard.js +61 -0
  130. package/dist/permissions/action-guard.js.map +1 -0
  131. package/dist/project-settings.d.ts +42 -0
  132. package/dist/project-settings.js +120 -0
  133. package/dist/project-settings.js.map +1 -0
  134. package/dist/resources/array-handler.d.ts +28 -0
  135. package/dist/resources/array-handler.js +116 -0
  136. package/dist/resources/array-handler.js.map +1 -0
  137. package/dist/resources/card-type-resource.d.ts +72 -0
  138. package/dist/resources/card-type-resource.js +334 -0
  139. package/dist/resources/card-type-resource.js.map +1 -0
  140. package/dist/resources/create-defaults.d.ts +81 -0
  141. package/dist/resources/create-defaults.js +184 -0
  142. package/dist/resources/create-defaults.js.map +1 -0
  143. package/dist/resources/field-type-resource.d.ts +88 -0
  144. package/dist/resources/field-type-resource.js +411 -0
  145. package/dist/resources/field-type-resource.js.map +1 -0
  146. package/dist/resources/file-resource.d.ts +50 -0
  147. package/dist/resources/file-resource.js +301 -0
  148. package/dist/resources/file-resource.js.map +1 -0
  149. package/dist/resources/folder-resource.d.ts +66 -0
  150. package/dist/resources/folder-resource.js +100 -0
  151. package/dist/resources/folder-resource.js.map +1 -0
  152. package/dist/resources/graph-model-resource.d.ts +78 -0
  153. package/dist/resources/graph-model-resource.js +164 -0
  154. package/dist/resources/graph-model-resource.js.map +1 -0
  155. package/dist/resources/graph-view-resource.d.ts +78 -0
  156. package/dist/resources/graph-view-resource.js +163 -0
  157. package/dist/resources/graph-view-resource.js.map +1 -0
  158. package/dist/resources/link-type-resource.d.ts +62 -0
  159. package/dist/resources/link-type-resource.js +150 -0
  160. package/dist/resources/link-type-resource.js.map +1 -0
  161. package/dist/resources/report-resource.d.ts +77 -0
  162. package/dist/resources/report-resource.js +171 -0
  163. package/dist/resources/report-resource.js.map +1 -0
  164. package/dist/resources/resource-object.d.ts +108 -0
  165. package/dist/resources/resource-object.js +147 -0
  166. package/dist/resources/resource-object.js.map +1 -0
  167. package/dist/resources/template-resource.d.ts +82 -0
  168. package/dist/resources/template-resource.js +173 -0
  169. package/dist/resources/template-resource.js.map +1 -0
  170. package/dist/resources/workflow-resource.d.ts +67 -0
  171. package/dist/resources/workflow-resource.js +156 -0
  172. package/dist/resources/workflow-resource.js.map +1 -0
  173. package/dist/types/queries.d.ts +142 -0
  174. package/dist/types/queries.js +16 -0
  175. package/dist/types/queries.js.map +1 -0
  176. package/dist/utils/card-utils.d.ts +34 -0
  177. package/dist/utils/card-utils.js +78 -0
  178. package/dist/utils/card-utils.js.map +1 -0
  179. package/dist/utils/clingo-fact-builder.d.ts +58 -0
  180. package/dist/utils/clingo-fact-builder.js +126 -0
  181. package/dist/utils/clingo-fact-builder.js.map +1 -0
  182. package/dist/utils/clingo-facts.d.ts +97 -0
  183. package/dist/utils/clingo-facts.js +352 -0
  184. package/dist/utils/clingo-facts.js.map +1 -0
  185. package/dist/utils/clingo-parser.d.ts +59 -0
  186. package/dist/utils/clingo-parser.js +403 -0
  187. package/dist/utils/clingo-parser.js.map +1 -0
  188. package/dist/utils/clingo-program-builder.d.ts +39 -0
  189. package/dist/utils/clingo-program-builder.js +57 -0
  190. package/dist/utils/clingo-program-builder.js.map +1 -0
  191. package/dist/utils/common-utils.d.ts +24 -0
  192. package/dist/utils/common-utils.js +47 -0
  193. package/dist/utils/common-utils.js.map +1 -0
  194. package/dist/utils/constants.d.ts +18 -0
  195. package/dist/utils/constants.js +27 -0
  196. package/dist/utils/constants.js.map +1 -0
  197. package/dist/utils/csv.d.ts +18 -0
  198. package/dist/utils/csv.js +45 -0
  199. package/dist/utils/csv.js.map +1 -0
  200. package/dist/utils/file-utils.d.ts +69 -0
  201. package/dist/utils/file-utils.js +158 -0
  202. package/dist/utils/file-utils.js.map +1 -0
  203. package/dist/utils/json.d.ts +61 -0
  204. package/dist/utils/json.js +108 -0
  205. package/dist/utils/json.js.map +1 -0
  206. package/dist/utils/lexorank.d.ts +59 -0
  207. package/dist/utils/lexorank.js +159 -0
  208. package/dist/utils/lexorank.js.map +1 -0
  209. package/dist/utils/log-utils.d.ts +40 -0
  210. package/dist/utils/log-utils.js +109 -0
  211. package/dist/utils/log-utils.js.map +1 -0
  212. package/dist/utils/random.d.ts +19 -0
  213. package/dist/utils/random.js +34 -0
  214. package/dist/utils/random.js.map +1 -0
  215. package/dist/utils/resource-utils.d.ts +45 -0
  216. package/dist/utils/resource-utils.js +137 -0
  217. package/dist/utils/resource-utils.js.map +1 -0
  218. package/dist/utils/sanitize-svg.d.ts +18 -0
  219. package/dist/utils/sanitize-svg.js +38 -0
  220. package/dist/utils/sanitize-svg.js.map +1 -0
  221. package/dist/utils/user-preferences.d.ts +64 -0
  222. package/dist/utils/user-preferences.js +106 -0
  223. package/dist/utils/user-preferences.js.map +1 -0
  224. package/dist/utils/validate.d.ts +26 -0
  225. package/dist/utils/validate.js +53 -0
  226. package/dist/utils/validate.js.map +1 -0
  227. package/dist/utils/value-utils.d.ts +58 -0
  228. package/dist/utils/value-utils.js +181 -0
  229. package/dist/utils/value-utils.js.map +1 -0
  230. package/package.json +67 -0
  231. package/src/card-metadata-updater.ts +182 -0
  232. package/src/command-handler.ts +686 -0
  233. package/src/command-manager.ts +99 -0
  234. package/src/commands/calculate.ts +591 -0
  235. package/src/commands/create.ts +559 -0
  236. package/src/commands/edit.ts +123 -0
  237. package/src/commands/export-site.ts +356 -0
  238. package/src/commands/export.ts +315 -0
  239. package/src/commands/import.ts +169 -0
  240. package/src/commands/index.ts +42 -0
  241. package/src/commands/move.ts +451 -0
  242. package/src/commands/remove.ts +244 -0
  243. package/src/commands/rename.ts +378 -0
  244. package/src/commands/show.ts +442 -0
  245. package/src/commands/transition.ts +127 -0
  246. package/src/commands/update.ts +76 -0
  247. package/src/commands/validate.ts +962 -0
  248. package/src/containers/card-container.ts +378 -0
  249. package/src/containers/project/project-paths.ts +127 -0
  250. package/src/containers/project/resource-collector.ts +379 -0
  251. package/src/containers/project.ts +1135 -0
  252. package/src/containers/template.ts +573 -0
  253. package/src/exceptions/index.ts +29 -0
  254. package/src/index.ts +33 -0
  255. package/src/interfaces/adoc.ts +18 -0
  256. package/src/interfaces/macros.ts +54 -0
  257. package/src/interfaces/project-interfaces.ts +208 -0
  258. package/src/interfaces/request-status-interfaces.ts +30 -0
  259. package/src/interfaces/resource-interfaces.ts +179 -0
  260. package/src/macros/base-macro.ts +176 -0
  261. package/src/macros/common.ts +24 -0
  262. package/src/macros/createCards/index.ts +57 -0
  263. package/src/macros/createCards/metadata.ts +21 -0
  264. package/src/macros/graph/index.ts +130 -0
  265. package/src/macros/graph/metadata.ts +21 -0
  266. package/src/macros/index.ts +321 -0
  267. package/src/macros/report/index.ts +88 -0
  268. package/src/macros/report/metadata.ts +21 -0
  269. package/src/macros/scoreCard/index.ts +55 -0
  270. package/src/macros/scoreCard/metadata.ts +21 -0
  271. package/src/macros/task-queue.ts +79 -0
  272. package/src/module-manager.ts +443 -0
  273. package/src/permissions/action-guard.ts +77 -0
  274. package/src/project-settings.ts +140 -0
  275. package/src/resources/array-handler.ts +141 -0
  276. package/src/resources/card-type-resource.ts +455 -0
  277. package/src/resources/create-defaults.ts +216 -0
  278. package/src/resources/field-type-resource.ts +533 -0
  279. package/src/resources/file-resource.ts +433 -0
  280. package/src/resources/folder-resource.ts +140 -0
  281. package/src/resources/graph-model-resource.ts +205 -0
  282. package/src/resources/graph-view-resource.ts +199 -0
  283. package/src/resources/link-type-resource.ts +191 -0
  284. package/src/resources/report-resource.ts +224 -0
  285. package/src/resources/resource-object.ts +246 -0
  286. package/src/resources/template-resource.ts +210 -0
  287. package/src/resources/workflow-resource.ts +205 -0
  288. package/src/types/queries.ts +149 -0
  289. package/src/utils/card-utils.ts +83 -0
  290. package/src/utils/clingo-fact-builder.ts +167 -0
  291. package/src/utils/clingo-facts.ts +550 -0
  292. package/src/utils/clingo-parser.ts +519 -0
  293. package/src/utils/clingo-program-builder.ts +71 -0
  294. package/src/utils/common-utils.ts +54 -0
  295. package/src/utils/constants.ts +32 -0
  296. package/src/utils/csv.ts +53 -0
  297. package/src/utils/file-utils.ts +182 -0
  298. package/src/utils/json.ts +118 -0
  299. package/src/utils/lexorank.ts +180 -0
  300. package/src/utils/log-utils.ts +127 -0
  301. package/src/utils/random.ts +37 -0
  302. package/src/utils/resource-utils.ts +180 -0
  303. package/src/utils/sanitize-svg.ts +46 -0
  304. package/src/utils/user-preferences.ts +126 -0
  305. package/src/utils/validate.ts +66 -0
  306. package/src/utils/value-utils.ts +189 -0
@@ -0,0 +1,301 @@
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
+ // node
15
+ import { basename, join, sep } from 'node:path';
16
+ import { mkdir, rename } from 'node:fs/promises';
17
+ import { readFile } from 'node:fs/promises';
18
+ import { ResourceObject, } from './resource-object.js';
19
+ import { DefaultContent } from './create-defaults.js';
20
+ import { deleteFile, pathExists } from '../utils/file-utils.js';
21
+ import { Project, ResourcesFrom } from '../containers/project.js';
22
+ import { readJsonFile, readJsonFileSync, writeJsonFile, } from '../utils/json.js';
23
+ import { resourceName, resourceNameToPath, resourceNameToString, } from '../utils/resource-utils.js';
24
+ import { sortCards } from '../utils/card-utils.js';
25
+ import { Validate } from '../commands/index.js';
26
+ export { DefaultContent, Project, ResourcesFrom, resourceName, resourceNameToString, sortCards, };
27
+ /**
28
+ * Base class for file based resources (card types, field types, link types, workflows, ...)
29
+ */
30
+ export class FileResource extends ResourceObject {
31
+ type;
32
+ fileName = '';
33
+ content = { name: '' };
34
+ cache;
35
+ constructor(project, resourceName, type) {
36
+ super(project, resourceName);
37
+ this.type = type;
38
+ this.cache = this.project.resourceCache;
39
+ }
40
+ // Type of resource.
41
+ resourceType() {
42
+ return this.type;
43
+ }
44
+ // Converts resource name to Resource object.
45
+ resourceObjectToResource(object) {
46
+ return {
47
+ name: object.data ? object.data.name : '',
48
+ path: object.fileName.substring(0, object.fileName.lastIndexOf(sep)),
49
+ };
50
+ }
51
+ toCache() {
52
+ this.cache.set(resourceNameToString(this.resourceName), this.content);
53
+ }
54
+ // Initialize the resource.
55
+ initialize() {
56
+ if (this.resourceName.type === '') {
57
+ this.resourceName.type = this.type;
58
+ }
59
+ if (this.resourceName.prefix === '') {
60
+ this.resourceName.prefix = this.project.projectPrefix;
61
+ }
62
+ if (this.type) {
63
+ this.moduleResource =
64
+ this.resourceName.prefix !== this.project.projectPrefix;
65
+ this.resourceFolder = this.moduleResource
66
+ ? join(this.project.paths.modulesFolder, this.resourceName.prefix, this.resourceName.type)
67
+ : this.project.paths.resourcePath(this.type);
68
+ this.fileName = resourceNameToPath(this.project, this.resourceName);
69
+ }
70
+ // Read from cache, if entry exists...
71
+ if (this.cache.has(resourceNameToString(this.resourceName))) {
72
+ this.content = this.cache.get(resourceNameToString(this.resourceName));
73
+ return;
74
+ }
75
+ //... otherwise read from disk and add to cache
76
+ try {
77
+ this.content = readJsonFileSync(this.fileName);
78
+ this.toCache();
79
+ }
80
+ catch {
81
+ // do nothing, it is possible that file has not been created yet.
82
+ }
83
+ }
84
+ // Creates resource.
85
+ async create(newContent) {
86
+ if (pathExists(this.fileName)) {
87
+ throw new Error(`Resource '${this.resourceName.identifier}' already exists in the project`);
88
+ }
89
+ if (this.resourceFolder === '') {
90
+ this.resourceName = resourceName(`${this.project.projectPrefix}/${this.type}/${this.resourceName.identifier}`);
91
+ this.resourceFolder = this.project.paths.resourcePath(this.resourceName.type);
92
+ }
93
+ const validName = await Validate.getInstance().validResourceName(this.resourceType(), resourceNameToString(this.resourceName), await this.project.projectPrefixes());
94
+ let validContent = {};
95
+ if (newContent) {
96
+ validContent = newContent;
97
+ validContent.name = validName;
98
+ }
99
+ this.content = validContent;
100
+ await this.write();
101
+ // Notify project & collector
102
+ this.project.addResource(this.resourceObjectToResource(this), this.content);
103
+ }
104
+ // Calculations that use this resource.
105
+ async calculations() {
106
+ const references = [];
107
+ const resourceName = resourceNameToString(this.resourceName);
108
+ for (const calculation of await this.project.calculations(ResourcesFrom.all)) {
109
+ const fileNameWithExtension = calculation.name.endsWith('.lp')
110
+ ? calculation.name
111
+ : calculation.name + '.lp';
112
+ const filename = join(calculation.path, basename(fileNameWithExtension));
113
+ try {
114
+ const content = await readFile(filename, 'utf-8');
115
+ if (content.includes(resourceName)) {
116
+ references.push(calculation.name);
117
+ }
118
+ }
119
+ catch (error) {
120
+ throw new Error(`Failed to process file ${filename}: ${error.message}`);
121
+ }
122
+ }
123
+ return references;
124
+ }
125
+ // Cards from project.
126
+ async cards() {
127
+ return [
128
+ ...(await this.project.cards(undefined, {
129
+ content: true,
130
+ metadata: true,
131
+ })),
132
+ ...(await this.project.allTemplateCards({
133
+ content: true,
134
+ metadata: true,
135
+ })),
136
+ ];
137
+ }
138
+ // Returns memory resident data as JSON.
139
+ // This is basically same as 'show' but doesn't do any checks; just returns the current content.
140
+ get data() {
141
+ return this.content.name !== '' ? this.content : undefined;
142
+ }
143
+ // Deletes resource.
144
+ async delete() {
145
+ if (this.moduleResource) {
146
+ throw new Error(`Cannot delete module resources`);
147
+ }
148
+ if (!this.fileName.endsWith('.json')) {
149
+ this.fileName += '.json';
150
+ }
151
+ if (!pathExists(this.fileName)) {
152
+ throw new Error(`Resource '${this.resourceName.identifier}' does not exist in the project`);
153
+ }
154
+ const usedIn = await this.usage();
155
+ if (usedIn.length > 0) {
156
+ throw new Error(`Cannot delete resource ${resourceNameToString(this.resourceName)}. It is used by: ${usedIn.join(', ')}`);
157
+ }
158
+ await deleteFile(this.fileName);
159
+ this.project.removeResource(this.resourceObjectToResource(this));
160
+ this.fileName = '';
161
+ }
162
+ async validName(newName) {
163
+ const validName = await Validate.getInstance().validResourceName(this.resourceType(), resourceNameToString(newName), await this.project.projectPrefixes());
164
+ return validName;
165
+ }
166
+ // Called after inherited class has finished 'update' operation.
167
+ async postUpdate(content, key, op) {
168
+ function toValue(op) {
169
+ if (op.name === 'rank')
170
+ return op.newIndex;
171
+ if (op.name === 'add')
172
+ return op.target;
173
+ if (op.name === 'remove')
174
+ return op.target;
175
+ if (op.name === 'change')
176
+ return op.to;
177
+ }
178
+ // Check that new name is valid.
179
+ if (op.name === 'change' && key === 'name') {
180
+ const newName = resourceName(op.to);
181
+ content.name = await this.validName(newName);
182
+ }
183
+ // Once changes have been made; validate the content.
184
+ try {
185
+ await this.validate(content);
186
+ }
187
+ catch (error) {
188
+ if (error instanceof Error) {
189
+ const errorValue = typeof op === 'object' ? toValue(op) : op;
190
+ throw new Error(`Cannot ${op.name} '${key}' --> '${errorValue}'`);
191
+ }
192
+ }
193
+ this.content = content;
194
+ await this.write();
195
+ }
196
+ // Reads content from file to memory.
197
+ async read() {
198
+ this.content = await readJsonFile(this.fileName);
199
+ }
200
+ // Renames resource.
201
+ async rename(newName) {
202
+ this.cache.delete(resourceNameToString(this.resourceName));
203
+ if (this.moduleResource) {
204
+ throw new Error(`Cannot rename module resources`);
205
+ }
206
+ if (!pathExists(this.fileName)) {
207
+ throw new Error(`Resource '${this.resourceName.identifier}' does not exist`);
208
+ }
209
+ if (newName.prefix !== this.project.projectPrefix) {
210
+ throw new Error('Can only rename project resources');
211
+ }
212
+ if (newName.type !== this.resourceName.type) {
213
+ throw new Error('Cannot change resource type');
214
+ }
215
+ await Validate.getInstance().validResourceName(this.resourceType(), resourceNameToString(newName), await this.project.projectPrefixes());
216
+ const newFilename = join(this.project.paths.resourcePath(newName.type), newName.identifier + '.json');
217
+ await rename(this.fileName, newFilename);
218
+ this.fileName = newFilename;
219
+ this.content.name = resourceNameToString(newName);
220
+ this.resourceName = newName;
221
+ this.toCache();
222
+ }
223
+ // Show resource data as JSON.
224
+ async show() {
225
+ if (!pathExists(this.fileName)) {
226
+ const resourceType = `${this.type[0].toUpperCase()}${this.type.slice(1, this.type.length - 1)}`;
227
+ const name = resourceNameToString(this.resourceName);
228
+ throw new Error(`${resourceType} '${name}' does not exist in the project`);
229
+ }
230
+ return this.content;
231
+ }
232
+ // Update resource; the base class makes some checks only.
233
+ async update(key,
234
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
235
+ _op) {
236
+ const content = this.data;
237
+ if (!content) {
238
+ throw new Error(`Resource '${this.fileName}' does not exist`);
239
+ }
240
+ if (this.moduleResource) {
241
+ throw new Error(`Cannot update module resources`);
242
+ }
243
+ if (key === '' || key === undefined) {
244
+ throw new Error(`Cannot update empty key`);
245
+ }
246
+ }
247
+ // Updates resource key to a new prefix
248
+ updatePrefixInResourceName(name, prefixes) {
249
+ const { identifier, prefix, type } = resourceName(name);
250
+ if (this.moduleResource) {
251
+ return name;
252
+ }
253
+ return !prefixes.includes(prefix)
254
+ ? `${this.project.configuration.cardKeyPrefix}/${type}/${identifier}`
255
+ : name;
256
+ }
257
+ // Check if there are references to the resource in the card content.
258
+ async usage(cards) {
259
+ if (!pathExists(this.fileName)) {
260
+ throw new Error(`Resource '${this.resourceName.identifier}' does not exist in the project`);
261
+ }
262
+ const cardArray = cards?.length
263
+ ? cards
264
+ : await this.project.cards(undefined, {
265
+ content: true,
266
+ metadata: true,
267
+ });
268
+ return cardArray
269
+ .filter((card) => card.content?.includes(resourceNameToString(this.resourceName)))
270
+ .map((card) => card.key);
271
+ }
272
+ // Write the content from memory to disk.
273
+ async write() {
274
+ if (this.moduleResource) {
275
+ throw new Error(`Cannot change module resources`);
276
+ }
277
+ // Create folder for resources and add correct .schema file.
278
+ await mkdir(this.resourceFolder, { recursive: true });
279
+ await writeJsonFile(join(this.resourceFolder, '.schema'), this.contentSchema, {
280
+ flag: 'wx',
281
+ });
282
+ // Check if "name" has changed. Changing "name" means renaming the file.
283
+ const nameInContent = resourceName(this.content.name).identifier + '.json';
284
+ const currentFileName = basename(this.fileName);
285
+ if (nameInContent !== currentFileName) {
286
+ const newFileName = join(this.resourceFolder, nameInContent);
287
+ await rename(this.fileName, newFileName);
288
+ this.fileName = newFileName;
289
+ }
290
+ await writeJsonFile(this.fileName, this.content);
291
+ this.toCache();
292
+ }
293
+ // Validate that current memory-based 'content' is valid.
294
+ async validate(content) {
295
+ const invalidJson = Validate.getInstance().validateJson(content ? content : this.content, this.contentSchemaId);
296
+ if (invalidJson.length) {
297
+ throw new Error(`Invalid content JSON: ${invalidJson}`);
298
+ }
299
+ }
300
+ }
301
+ //# sourceMappingURL=file-resource.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"file-resource.js","sourceRoot":"","sources":["../../src/resources/file-resource.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;EAYE;AAEF,OAAO;AACP,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,GAAG,EAAE,MAAM,WAAW,CAAC;AAChD,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAM5C,OAAO,EAKL,cAAc,GACf,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAChE,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAClE,OAAO,EACL,YAAY,EACZ,gBAAgB,EAChB,aAAa,GACd,MAAM,kBAAkB,CAAC;AAK1B,OAAO,EAEL,YAAY,EACZ,kBAAkB,EAClB,oBAAoB,GACrB,MAAM,4BAA4B,CAAC;AAEpC,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACnD,OAAO,EAAE,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S,EAAE,IAAI,EAAE,CAAC,CAAC;QACtD,MAAM,aAAa,CACjB,IAAI,CAAC,IAAI,CAAC,cAAc,EAAE,SAAS,CAAC,EACpC,IAAI,CAAC,aAAa,EAClB;YACE,IAAI,EAAE,IAAI;SACX,CACF,CAAC;QACF,wEAAwE;QACxE,MAAM,aAAa,GAAG,YAAY,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,UAAU,GAAG,OAAO,CAAC;QAC3E,MAAM,eAAe,GAAG,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QAEhD,IAAI,aAAa,KAAK,eAAe,EAAE,CAAC;YACtC,MAAM,WAAW,GAAG,IAAI,CAAC,IAAI,CAAC,cAAc,EAAE,aAAa,CAAC,CAAC;YAC7D,MAAM,MAAM,CAAC,IAAI,CAAC,QAAQ,EAAE,WAAW,CAAC,CAAC;YACzC,IAAI,CAAC,QAAQ,GAAG,WAAW,CAAC;QAC9B,CAAC;QAED,MAAM,aAAa,CAAC,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;QACjD,IAAI,CAAC,OAAO,EAAE,CAAC;IACjB,CAAC;IAED,yDAAyD;IAC/C,KAAK,CAAC,QAAQ,CAAC,OAAgB;QACvC,MAAM,WAAW,GAAG,QAAQ,CAAC,WAAW,EAAE,CAAC,YAAY,CACrD,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,EAChC,IAAI,CAAC,eAAe,CACrB,CAAC;QACF,IAAI,WAAW,CAAC,MAAM,EAAE,CAAC;YACvB,MAAM,IAAI,KAAK,CAAC,yBAAyB,WAAW,EAAE,CAAC,CAAC;QAC1D,CAAC;IACH,CAAC;CACF"}
@@ -0,0 +1,66 @@
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
+ import type { ResourceFolderType } from '../interfaces/project-interfaces.js';
13
+ import { type Card, DefaultContent, FileResource, type Operation, Project, type ResourceName, resourceNameToString, sortCards } from './file-resource.js';
14
+ import type { ResourceContent } from '../interfaces/resource-interfaces.js';
15
+ export { type Card, DefaultContent, FileResource, type Operation, Project, ResourceName, resourceNameToString, sortCards, };
16
+ /**
17
+ * Folder type resource class. These are resources that have their own folders for content.
18
+ */
19
+ export declare class FolderResource extends FileResource {
20
+ protected internalFolder: string;
21
+ constructor(project: Project, name: ResourceName, type: ResourceFolderType);
22
+ /**
23
+ * Creates a new folder type object. Base class writes the object to disk automatically.
24
+ * @param newContent Content for the type.
25
+ */
26
+ protected create(newContent?: ResourceContent): Promise<void>;
27
+ /**
28
+ * Returns content data.
29
+ */
30
+ get data(): import("../interfaces/resource-interfaces.js").ResourceBaseMetadata | undefined;
31
+ /**
32
+ * Deletes file(s) from disk and clears out the memory resident object.
33
+ */
34
+ protected delete(): Promise<void>;
35
+ protected initialize(): void;
36
+ /**
37
+ * Renames resource metadata file and renames memory resident object 'name'.
38
+ * @param newName New name for the resource.
39
+ */
40
+ protected rename(newName: ResourceName): Promise<void>;
41
+ /**
42
+ * Shows metadata of the resource.
43
+ * @returns resource type's metadata.
44
+ */
45
+ protected show(): Promise<ResourceContent>;
46
+ /**
47
+ * Updates resource.
48
+ * @param key Key to modify
49
+ * @param op Operation to perform on 'key'
50
+ */
51
+ protected update<Type>(key: string, op: Operation<Type>): Promise<void>;
52
+ /**
53
+ * Returns an array of card keys, and/or resource names where this resource is used.
54
+ * @param cards Optional. If defined, only these cards are checked.
55
+ * @returns an array of card keys, and/or resource names where this resource is used.
56
+ */
57
+ protected usage(cards?: Card[]): Promise<string[]>;
58
+ /**
59
+ * Writes resource content to disk.
60
+ */
61
+ protected write(): Promise<void>;
62
+ /**
63
+ * Validates the resource. If object is invalid, throws.
64
+ */
65
+ protected validate(content?: object): Promise<void>;
66
+ }
@@ -0,0 +1,100 @@
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
+ import { basename, join } from 'node:path';
13
+ import { mkdir, rename, rm } from 'node:fs/promises';
14
+ import { DefaultContent, FileResource, Project, resourceName, resourceNameToString, sortCards, } from './file-resource.js';
15
+ export { DefaultContent, FileResource, Project, resourceNameToString, sortCards, };
16
+ /**
17
+ * Folder type resource class. These are resources that have their own folders for content.
18
+ */
19
+ export class FolderResource extends FileResource {
20
+ internalFolder = '';
21
+ constructor(project, name, type) {
22
+ super(project, name, type);
23
+ }
24
+ /**
25
+ * Creates a new folder type object. Base class writes the object to disk automatically.
26
+ * @param newContent Content for the type.
27
+ */
28
+ async create(newContent) {
29
+ await super.create(newContent);
30
+ await mkdir(this.internalFolder, { recursive: true });
31
+ }
32
+ /**
33
+ * Returns content data.
34
+ */
35
+ get data() {
36
+ return super.data;
37
+ }
38
+ /**
39
+ * Deletes file(s) from disk and clears out the memory resident object.
40
+ */
41
+ async delete() {
42
+ await super.delete();
43
+ await rm(this.internalFolder, { recursive: true, force: true });
44
+ }
45
+ initialize() {
46
+ super.initialize();
47
+ this.internalFolder = join(this.resourceFolder, this.resourceName.identifier);
48
+ }
49
+ /**
50
+ * Renames resource metadata file and renames memory resident object 'name'.
51
+ * @param newName New name for the resource.
52
+ */
53
+ async rename(newName) {
54
+ return super.rename(newName);
55
+ }
56
+ /**
57
+ * Shows metadata of the resource.
58
+ * @returns resource type's metadata.
59
+ */
60
+ async show() {
61
+ return super.show();
62
+ }
63
+ /**
64
+ * Updates resource.
65
+ * @param key Key to modify
66
+ * @param op Operation to perform on 'key'
67
+ */
68
+ async update(key, op) {
69
+ return super.update(key, op);
70
+ }
71
+ /**
72
+ * Returns an array of card keys, and/or resource names where this resource is used.
73
+ * @param cards Optional. If defined, only these cards are checked.
74
+ * @returns an array of card keys, and/or resource names where this resource is used.
75
+ */
76
+ async usage(cards) {
77
+ return super.usage(cards);
78
+ }
79
+ /**
80
+ * Writes resource content to disk.
81
+ */
82
+ async write() {
83
+ const folderName = basename(this.internalFolder);
84
+ // Check if "name" has changed. Changing "name" means renaming the file.
85
+ const nameInContent = resourceName(this.content.name).identifier;
86
+ if (folderName !== nameInContent) {
87
+ const newFolderName = join(this.resourceFolder, nameInContent);
88
+ await rename(this.internalFolder, newFolderName);
89
+ this.internalFolder = newFolderName;
90
+ }
91
+ return super.write();
92
+ }
93
+ /**
94
+ * Validates the resource. If object is invalid, throws.
95
+ */
96
+ async validate(content) {
97
+ return super.validate(content);
98
+ }
99
+ }
100
+ //# sourceMappingURL=folder-resource.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"folder-resource.js","sourceRoot":"","sources":["../../src/resources/folder-resource.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;EAUE;AAEF,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,kBAAkB,CAAC;AAGrD,OAAO,EAEL,cAAc,EACd,YAAY,EAEZ,OAAO,EACP,YAAY,EAEZ,oBAAoB,EACpB,SAAS,GACV,MAAM,oBAAoB,CAAC;AAG5B,OAAO,EAEL,cAAc,EACd,YAAY,EAEZ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C,CAAC;IAC/B,CAAC;IAED;;;;OAIG;IACO,KAAK,CAAC,KAAK,CAAC,KAAc;QAClC,OAAO,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IAC5B,CAAC;IAED;;OAEG;IACO,KAAK,CAAC,KAAK;QACnB,MAAM,UAAU,GAAG,QAAQ,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC;QAEjD,wEAAwE;QACxE,MAAM,aAAa,GAAG,YAAY,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,UAAU,CAAC;QACjE,IAAI,UAAU,KAAK,aAAa,EAAE,CAAC;YACjC,MAAM,aAAa,GAAG,IAAI,CAAC,IAAI,CAAC,cAAc,EAAE,aAAa,CAAC,CAAC;YAC/D,MAAM,MAAM,CAAC,IAAI,CAAC,cAAc,EAAE,aAAa,CAAC,CAAC;YACjD,IAAI,CAAC,cAAc,GAAG,aAAa,CAAC;QACtC,CAAC;QACD,OAAO,KAAK,CAAC,KAAK,EAAE,CAAC;IACvB,CAAC;IAED;;OAEG;IACO,KAAK,CAAC,QAAQ,CAAC,OAAgB;QACvC,OAAO,KAAK,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;IACjC,CAAC;CACF"}
@@ -0,0 +1,78 @@
1
+ /**
2
+ Cyberismo
3
+ Copyright © Cyberismo Ltd and contributors 2025
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 { Card, Operation, Project, ResourceName } from './folder-resource.js';
15
+ import { FolderResource } from './folder-resource.js';
16
+ import type { GraphModel, GraphModelMetadata } from '../interfaces/resource-interfaces.js';
17
+ /**
18
+ * Graph model resource class.
19
+ */
20
+ export declare class GraphModelResource extends FolderResource {
21
+ constructor(project: Project, name: ResourceName);
22
+ private handleNameChange;
23
+ /**
24
+ * Sets new metadata into the graph model object graph model.
25
+ * @param newContent metadata content for the graph model.
26
+ * @throws if 'newContent' is not valid.
27
+ */
28
+ create(newContent?: GraphModelMetadata): Promise<void>;
29
+ /**
30
+ * Returns resource content.
31
+ */
32
+ get data(): GraphModel;
33
+ /**
34
+ * Deletes file and folder that this resource is based on.
35
+ */
36
+ delete(): Promise<void>;
37
+ /**
38
+ * Returns calculation file that this graph model has.
39
+ * @returns calculation file name that this graph model has.
40
+ */
41
+ calculationFile(nameOnly?: boolean): Promise<string>;
42
+ /**
43
+ * Renames the object and the file.
44
+ * @param newName New name for the resource.
45
+ */
46
+ rename(newName: ResourceName): Promise<void>;
47
+ /**
48
+ * Shows metadata of the resource.
49
+ * @returns graph model metadata.
50
+ */
51
+ show(): Promise<GraphModel>;
52
+ /**
53
+ * Updates graph model resource.
54
+ * @param key Key to modify
55
+ * @param op Operation to perform on 'key'
56
+ * @throws if key is unknown.
57
+ */
58
+ update<Type>(key: string, op: Operation<Type>): Promise<void>;
59
+ /**
60
+ * List where this resource is used.
61
+ * Always returns card key references first, then calculation references.
62
+ *
63
+ * @param cards Optional. Check these cards for usage of this resource. If undefined, will check all cards.
64
+ * @returns array of card keys and calculation filenames that refer this resource.
65
+ */
66
+ usage(cards?: Card[]): Promise<string[]>;
67
+ /**
68
+ * Validates graphModel.
69
+ * @throws when there are validation errors.
70
+ * @param content Content to be validated.
71
+ * @note If content is not provided, base class validation will use resource's current content.
72
+ */
73
+ validate(content?: object): Promise<void>;
74
+ /**
75
+ * Create the graph model's folder and calculation file.
76
+ */
77
+ write(): Promise<void>;
78
+ }