@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,573 @@
1
+ /**
2
+ Cyberismo
3
+ Copyright © Cyberismo Ltd and contributors 2024
4
+ This program is free software: you can redistribute it and/or modify it under
5
+ the terms of the GNU Affero General Public License version 3 as published by
6
+ the Free Software Foundation.
7
+ This program is distributed in the hope that it will be useful, but WITHOUT
8
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
9
+ FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
10
+ details. You should have received a copy of the GNU Affero General Public
11
+ License along with this program. If not, see <https://www.gnu.org/licenses/>.
12
+ */
13
+
14
+ // node
15
+ import { basename, join, resolve, sep } from 'node:path';
16
+ import { copyFile, mkdir, readdir, rm, writeFile } from 'node:fs/promises';
17
+ import { type Dirent, readdirSync } from 'node:fs';
18
+
19
+ // Base class
20
+ import { CardContainer } from './card-container.js';
21
+
22
+ import {
23
+ type Card,
24
+ type CardAttachment,
25
+ CardNameRegEx,
26
+ type FetchCardDetails,
27
+ type FileContentType,
28
+ type Resource,
29
+ } from '../interfaces/project-interfaces.js';
30
+ import type { CardType, Workflow } from '../interfaces/resource-interfaces.js';
31
+ import { pathExists, stripExtension } from '../utils/file-utils.js';
32
+ import { DefaultContent } from '../resources/create-defaults.js';
33
+
34
+ import {
35
+ EMPTY_RANK,
36
+ FIRST_RANK,
37
+ getRankAfter,
38
+ sortItems,
39
+ } from '../utils/lexorank.js';
40
+ import { logger } from '../utils/log-utils.js';
41
+ import { readJsonFile } from '../utils/json.js';
42
+ import { Project } from './project.js';
43
+ import { resourceName } from '../utils/resource-utils.js';
44
+
45
+ // creates template instance based on a project path and name
46
+ export class Template extends CardContainer {
47
+ private templatePath: string;
48
+ private templateCardsPath: string;
49
+ private project: Project;
50
+
51
+ constructor(project: Project, template: Resource) {
52
+ // Templates might come from modules. Remove module name from template name.
53
+ const templateName = stripExtension(basename(template.name));
54
+ super(template.path!, templateName);
55
+
56
+ // prevent constructing a new project object, if one is passed to this class.
57
+ this.project = project;
58
+ // optimization - if template.path is set - use it
59
+ this.templatePath =
60
+ template.path && template.path.length > 0
61
+ ? join(template.path, templateName)
62
+ : this.setTemplatePath(template.name);
63
+ this.templateCardsPath = join(this.templatePath, 'c');
64
+ }
65
+
66
+ // Fetches project top level cards only.
67
+ // Top level cards are those that have parent as 'root'.
68
+ // todo: This should be in 'project' or 'card-container'
69
+ private async rootLevelProjectCards(): Promise<Card[]> {
70
+ const entries = (
71
+ await readdir(this.project.paths.cardRootFolder, {
72
+ withFileTypes: true,
73
+ })
74
+ ).filter((entry) => entry.isDirectory() && CardNameRegEx.test(entry.name));
75
+ const cardPromises = entries.map(async (entry) => {
76
+ const currentPath = join(entry.parentPath, entry.name);
77
+ return {
78
+ key: entry.name,
79
+ path: currentPath,
80
+ metadata: await readJsonFile(
81
+ join(currentPath, CardContainer.cardMetadataFile),
82
+ ),
83
+ children: [],
84
+ attachments: [],
85
+ };
86
+ });
87
+ return Promise.all(cardPromises);
88
+ }
89
+
90
+ // Creates card(s) as project cards from template.
91
+ private async doCreateCards(
92
+ cards: Card[],
93
+ parentCard?: Card,
94
+ ): Promise<Card[]> {
95
+ const templateIDMap = new Map<string, string>();
96
+ // Create ID mapping and update ranks
97
+ const createMappingAndRanks = async () => {
98
+ const cardIds = await this.project.listCardIds();
99
+ const newCardIds = this.project.newCardKeys(cards.length, cardIds);
100
+
101
+ // Create mapping table
102
+ cards.forEach((card, index) => {
103
+ templateIDMap.set(card.key, newCardIds.at(index) || '');
104
+ });
105
+
106
+ // Handle ranking for parent cards
107
+ const parentCards = sortItems(
108
+ cards.filter((c) => c.parent === 'root'),
109
+ (c) => c?.metadata?.rank || '',
110
+ );
111
+
112
+ const futureSiblings = parentCard
113
+ ? parentCard.children || []
114
+ : await this.rootLevelProjectCards();
115
+
116
+ let latestRank =
117
+ sortItems(
118
+ futureSiblings.filter((c) => c.metadata?.rank !== undefined),
119
+ (c) => c.metadata?.rank || '',
120
+ ).pop()?.metadata?.rank || FIRST_RANK;
121
+
122
+ // Update ranks
123
+ parentCards.forEach((card) => {
124
+ latestRank = getRankAfter(latestRank);
125
+ if (card.metadata) {
126
+ card.metadata.rank = latestRank;
127
+ }
128
+ });
129
+
130
+ return parentCards;
131
+ };
132
+
133
+ // Update paths and keys
134
+ const updateCardPaths = (
135
+ card: Card,
136
+ templateIDMap: Map<string, string>,
137
+ templatesFolder: string,
138
+ ) => {
139
+ const updatePathPart = (part: string) =>
140
+ CardNameRegEx.test(part)
141
+ ? `${sep}${templateIDMap.get(part) || part}`
142
+ : `${sep}${part}`;
143
+
144
+ card.path = card.path
145
+ .split(sep)
146
+ .map(updatePathPart)
147
+ .join('')
148
+ .substring(1);
149
+
150
+ if (card.path.includes(`${sep}c${sep}`) && !parentCard) {
151
+ card.path = card.path.replace(
152
+ `${templatesFolder}${sep}c`,
153
+ this.project.paths.cardRootFolder,
154
+ );
155
+ } else {
156
+ card.path = card.path.replace(
157
+ templatesFolder,
158
+ parentCard ? parentCard.path : this.project.paths.cardRootFolder,
159
+ );
160
+ }
161
+
162
+ card.key = templateIDMap.get(card.key) || card.key;
163
+ };
164
+
165
+ // Process attachments
166
+ const processAttachments = async (card: Card) => {
167
+ if (!card.attachments.length) return card;
168
+
169
+ const attachmentsFolder = join(card.path, 'a');
170
+ await mkdir(attachmentsFolder, { recursive: true });
171
+
172
+ let content = card.content;
173
+ await Promise.all(
174
+ card.attachments.map(async (attachment) => {
175
+ const attachmentUniqueName = `${card.key}-${attachment.fileName}`;
176
+ content = content?.replace(
177
+ new RegExp(`image::${attachment.fileName}`, 'g'),
178
+ `image::${attachmentUniqueName}`,
179
+ );
180
+ await copyFile(
181
+ join(attachment.path, attachment.fileName),
182
+ join(card.path, 'a', attachmentUniqueName),
183
+ );
184
+ }),
185
+ );
186
+ return { ...card, content };
187
+ };
188
+
189
+ // Process metadata
190
+ const processMetadata = async (card: Card, parentCards: Card[]) => {
191
+ if (!card.metadata) return card;
192
+
193
+ const cardType = await this.project.resource<CardType>(
194
+ card.metadata.cardType || '',
195
+ );
196
+ if (!cardType) {
197
+ throw new Error(
198
+ `Card type '${card.metadata.cardType}' of card ${card.key} cannot be found`,
199
+ );
200
+ }
201
+
202
+ const workflow = await this.project.resource<Workflow>(cardType.workflow);
203
+ if (!workflow) {
204
+ throw new Error(`Workflow '${cardType.workflow}' cannot be found`);
205
+ }
206
+ const initialWorkflowState = workflow.transitions.find(
207
+ (item) => item.fromState.includes('') || item.fromState.length === 0,
208
+ );
209
+ if (!initialWorkflowState) {
210
+ throw new Error(
211
+ `Workflow '${cardType.workflow}' initial state cannot be found`,
212
+ );
213
+ }
214
+
215
+ const cardWithRank = parentCards.find((c) => c.key === card.key);
216
+ const customFields = cardType.customFields
217
+ .filter((item) => !item.isCalculated)
218
+ .reduce(
219
+ (acc, field) => ({
220
+ ...acc,
221
+ [field.name]: card.metadata?.[field.name] || null,
222
+ }),
223
+ {},
224
+ );
225
+
226
+ const newMetadata = {
227
+ ...card.metadata,
228
+ ...customFields,
229
+ templateCardKey: [...templateIDMap]
230
+ .find(([, value]) => value === card.key)!
231
+ .at(0),
232
+ workflowState: initialWorkflowState.toState,
233
+ cardType: cardType.name,
234
+ rank: cardWithRank?.metadata?.rank || card.metadata.rank || EMPTY_RANK,
235
+ };
236
+
237
+ return { ...card, metadata: newMetadata };
238
+ };
239
+
240
+ try {
241
+ // Create mapping and handle ranks
242
+ const parentCards = await createMappingAndRanks();
243
+ const templatesFolder = this.templateFolder();
244
+
245
+ // Process all cards in parallel
246
+ const processedCards = await Promise.all(
247
+ cards.map(async (card) => {
248
+ // Update paths and keys
249
+ updateCardPaths(card, templateIDMap, templatesFolder);
250
+
251
+ // Process metadata and attachments in parallel
252
+ const [processedCard, processedAttachments] = await Promise.all([
253
+ processMetadata(card, parentCards),
254
+ processAttachments(card),
255
+ ]);
256
+
257
+ // Create directory and write files
258
+ await mkdir(processedCard.path, { recursive: true });
259
+
260
+ await Promise.all([
261
+ processedCard.metadata && this.saveCardMetadata(processedCard),
262
+ writeFile(
263
+ join(processedCard.path, Project.cardContentFile),
264
+ processedAttachments.content || '',
265
+ ),
266
+ ]);
267
+ return processedCard;
268
+ }),
269
+ );
270
+ return processedCards;
271
+ } catch (error) {
272
+ await this.removeCards(templateIDMap);
273
+ if (error instanceof Error) {
274
+ throw new Error(`Failed to create cards: ${error.message}`);
275
+ }
276
+ throw error;
277
+ }
278
+ }
279
+
280
+ // fetches path to module.
281
+ private moduleTemplatePath(templateName: string): string {
282
+ // If template path has already been deduced, return it.
283
+ if (pathExists(this.templatePath)) {
284
+ return this.templatePath;
285
+ }
286
+ let modules: Dirent[] = [];
287
+ try {
288
+ modules = readdirSync(this.project.paths.modulesFolder, {
289
+ withFileTypes: true,
290
+ }).filter((item) => item.isDirectory());
291
+ } catch {
292
+ // do nothing, if modules folder does not exist
293
+ }
294
+ for (const module of modules) {
295
+ const templateFolderInModule = join(
296
+ module.parentPath,
297
+ module.name,
298
+ 'templates',
299
+ templateName,
300
+ );
301
+ const exists = pathExists(templateFolderInModule);
302
+ if (exists) {
303
+ return templateFolderInModule;
304
+ }
305
+ }
306
+ return '';
307
+ }
308
+
309
+ // Removes cards
310
+ private async removeCards(cardMap: Map<string, string>) {
311
+ const tasks: Promise<Card | undefined>[] = [];
312
+ // Find all cards that need to be removed.
313
+ cardMap.forEach((createdCard) => {
314
+ tasks.push(this.project.findSpecificCard(createdCard));
315
+ });
316
+ // Remove empty results.
317
+ const cards = (await Promise.all(tasks)).filter(
318
+ (item) => item !== undefined,
319
+ );
320
+ // Delete card folders.
321
+ const deleteAll: Promise<void>[] = [];
322
+ cards.forEach((card) => {
323
+ deleteAll.push(rm(card.path, { force: true, recursive: true }));
324
+ });
325
+ await Promise.all(deleteAll);
326
+ }
327
+
328
+ // Set path to template location.
329
+ private setTemplatePath(templateName: string): string {
330
+ const { prefix, identifier } = resourceName(templateName);
331
+ const localTemplate = join(this.project.paths.templatesFolder, identifier);
332
+
333
+ // Template can either be local ...
334
+ if (prefix === this.project.projectPrefix) {
335
+ const localTemplate = join(
336
+ this.project.paths.templatesFolder,
337
+ identifier,
338
+ );
339
+ const createdLocalTemplate = pathExists(resolve(localTemplate));
340
+ if (createdLocalTemplate) {
341
+ return resolve(localTemplate);
342
+ }
343
+ }
344
+
345
+ // ... or from module ...
346
+ const createdModuleTemplatePath = this.moduleTemplatePath(identifier);
347
+ if (createdModuleTemplatePath !== '') {
348
+ return resolve(createdModuleTemplatePath);
349
+ }
350
+
351
+ // ... or not created yet; in case assume it will be 'local' (you cannot create templates to modules)
352
+ return resolve(localTemplate);
353
+ }
354
+
355
+ /**
356
+ * Adds a new card to template.
357
+ * @param cardTypeName card type
358
+ * @param parentCard parent card; optional - if missing will create a top-level card
359
+ * @returns next available card key ID
360
+ */
361
+ public async addCard(
362
+ cardTypeName: string,
363
+ parentCard?: Card,
364
+ ): Promise<string> {
365
+ const destinationCardPath = parentCard
366
+ ? join(await this.cardFolder(parentCard.key), 'c')
367
+ : this.templateCardsPath;
368
+ let newCardKey = '';
369
+
370
+ try {
371
+ if (!pathExists(this.templateFolder())) {
372
+ throw new Error(`Template '${this.containerName}' does not exist`);
373
+ }
374
+ const cardType = await this.project.resource<CardType>(cardTypeName);
375
+ if (cardType === undefined) {
376
+ throw new Error(`Card type '${cardTypeName}' does not exist`);
377
+ }
378
+ if (parentCard && !this.hasCard(parentCard.key)) {
379
+ throw new Error(
380
+ `Card '${parentCard.key}' does not exist in template '${this.containerName}'`,
381
+ );
382
+ }
383
+
384
+ const cardIds = await this.project.listCardIds();
385
+ newCardKey = this.project.newCardKey(cardIds);
386
+ const templateCardToCreate = parentCard
387
+ ? join(destinationCardPath, newCardKey)
388
+ : join(this.templateCardsPath, newCardKey);
389
+
390
+ const templateCards = parentCard
391
+ ? parentCard.children || []
392
+ : await this.cards();
393
+ const defaultContent = DefaultContent.card(cardType, templateCards);
394
+
395
+ await mkdir(templateCardToCreate, { recursive: true });
396
+ const defaultCard: Card = {
397
+ key: basename(templateCardToCreate),
398
+ path: templateCardToCreate,
399
+ metadata: defaultContent,
400
+ children: [],
401
+ attachments: [],
402
+ content: '',
403
+ };
404
+ await this.saveCard(defaultCard);
405
+ } catch (error) {
406
+ if (error instanceof Error) {
407
+ throw new Error(error.message);
408
+ }
409
+ }
410
+ return newCardKey;
411
+ }
412
+
413
+ /**
414
+ * Return all attachment in the template.
415
+ * @returns all attachments in the template.
416
+ */
417
+ public async attachments(): Promise<CardAttachment[]> {
418
+ return super.attachments(this.templateCardsPath);
419
+ }
420
+
421
+ /**
422
+ * Returns path to card's attachment folder.
423
+ * @param cardKey card key
424
+ * @returns path to card's attachment folder.
425
+ */
426
+ public async cardAttachmentFolder(cardKey: string): Promise<string> {
427
+ const pathToCard = await this.cardFolder(cardKey);
428
+ if (!pathToCard) {
429
+ return '';
430
+ }
431
+ return join(pathToCard, 'a');
432
+ }
433
+
434
+ /**
435
+ * Returns details (as defined by cardDetails) of a card.
436
+ * @param cardKey card key (project prefix and a number, e.g. test_1)
437
+ * @param cardDetails which card details are returned.
438
+ * @returns Card details, or undefined if the card cannot be found.
439
+ */
440
+ public async cardDetailsById(
441
+ cardKey: string,
442
+ cardDetails: FetchCardDetails,
443
+ ): Promise<Card | undefined> {
444
+ return super.findCard(this.templateCardsPath, cardKey, cardDetails);
445
+ }
446
+
447
+ /**
448
+ * returns path to card's folder.
449
+ * @param cardKey card key
450
+ * @returns path to card's folder.
451
+ */
452
+ public async cardFolder(cardKey: string): Promise<string> {
453
+ const found = await super.findCard(this.templateCardsPath, cardKey);
454
+ return found ? found.path : '';
455
+ }
456
+
457
+ /**
458
+ * Returns all cards in the template. Cards have content and metadata.
459
+ * @param placeHolderPath This is not used. Needed to be compatible with base class.
460
+ * @param details Optional. Which details are returned for each card. If missing, default value will be used.
461
+ * @returns Template cards in the template.
462
+ */
463
+ public async cards(
464
+ placeHolderPath?: string,
465
+ details?: FetchCardDetails,
466
+ ): Promise<Card[]> {
467
+ if (placeHolderPath) {
468
+ logger.warn('A non-used variable was used in the cards method');
469
+ }
470
+ const cardDetails = details
471
+ ? details
472
+ : {
473
+ content: true,
474
+ contentType: 'adoc' as FileContentType,
475
+ metadata: true,
476
+ };
477
+ return super.cards(this.templateCardsPath, cardDetails);
478
+ }
479
+
480
+ /**
481
+ * Creates cards from a template. If parent card is specified, then cards are created to underneath a parent.
482
+ * @param parentCard parent card
483
+ * @returns array of created card keys
484
+ */
485
+ public async createCards(parentCard?: Card): Promise<Card[]> {
486
+ const cards = await this.cards('', {
487
+ content: true,
488
+ contentType: 'adoc',
489
+ metadata: true,
490
+ attachments: true,
491
+ parent: true,
492
+ });
493
+ if (cards.length === 0) {
494
+ throw new Error(
495
+ `No cards in template '${this.containerName}'. Please add template cards with 'add' command first.`,
496
+ );
497
+ }
498
+ return this.doCreateCards(cards, parentCard);
499
+ }
500
+
501
+ /**
502
+ * Returns specific card.
503
+ * @param cardKey Card key to find from template.
504
+ * @param details Card details to include in return value.
505
+ * @returns specific card details
506
+ */
507
+ public async findSpecificCard(
508
+ cardKey: string,
509
+ details: FetchCardDetails = {},
510
+ ): Promise<Card | undefined> {
511
+ const cardPrefix = cardKey.split('_').at(0);
512
+ const moduleCardFromProject =
513
+ this.basePath.includes('local') &&
514
+ this.project.projectPrefix !== cardPrefix;
515
+ const projectCardFromModule =
516
+ this.basePath.includes('modules') &&
517
+ this.project.projectPrefix === cardPrefix;
518
+ // If the result is impossible, return undefined.
519
+ if (moduleCardFromProject || projectCardFromModule) {
520
+ return undefined;
521
+ }
522
+ return super.findCard(this.templateCardsPath, cardKey, details);
523
+ }
524
+
525
+ /**
526
+ * Checks if a specific card key exists in a template.
527
+ * @param cardKey Card key to find from template.
528
+ * @return true if card with a given card key exists in the template, false otherwise.
529
+ */
530
+ public hasCard(cardKey: string): boolean {
531
+ return super.hasCard(cardKey, this.templateCardsPath);
532
+ }
533
+
534
+ /**
535
+ * Check if template name exists already in the project.
536
+ * @returns true, if template is exists in project; false otherwise
537
+ */
538
+ public isCreated(): boolean {
539
+ return pathExists(this.templateCardsPath);
540
+ }
541
+
542
+ /**
543
+ * Returns an array of all the cards in the project. Cards don't have content nor metadata.
544
+ * @returns all cards in the project.
545
+ */
546
+ public async listCards(): Promise<Card[]> {
547
+ return super.cards(this.templateCardsPath);
548
+ }
549
+
550
+ /**
551
+ * Returns path to 'templates/<name>/c' folder.
552
+ * @returns path to the template's folder for cards.
553
+ */
554
+ public templateCardsFolder(): string {
555
+ return this.templateCardsPath;
556
+ }
557
+
558
+ /**
559
+ * Path to template configuration json file.
560
+ * @returns path to the template's configuration file.
561
+ */
562
+ public templateConfigurationFilePath(): string {
563
+ return join(this.templatePath, '..', this.containerName + '.json');
564
+ }
565
+
566
+ /**
567
+ * Returns path to 'templates' folder.
568
+ * @returns path to the project's folder that contains templates.
569
+ */
570
+ public templateFolder(): string {
571
+ return this.templatePath;
572
+ }
573
+ }
@@ -0,0 +1,29 @@
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 type { ValidationError } from 'json-schema';
14
+
15
+ export class DHValidationError extends Error {
16
+ public errors?: ValidationError[];
17
+ constructor(message: string, errors?: ValidationError[]) {
18
+ super(message);
19
+ this.name = 'DHValidationError';
20
+ this.errors = errors;
21
+ }
22
+ }
23
+
24
+ export class SchemaNotFound extends Error {
25
+ constructor(message: string) {
26
+ super(message);
27
+ this.name = 'SchemaNotFound';
28
+ }
29
+ }
package/src/index.ts ADDED
@@ -0,0 +1,33 @@
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 {
14
+ CardsOptions,
15
+ Cmd,
16
+ Commands,
17
+ CommandManager,
18
+ ExportFormats,
19
+ } from './command-handler.js';
20
+ import { requestStatus } from './interfaces/request-status-interfaces.js';
21
+ import { UpdateOperations } from './resources/resource-object.js';
22
+ import { evaluateMacros } from './macros/index.js';
23
+
24
+ export {
25
+ CardsOptions,
26
+ Cmd,
27
+ CommandManager,
28
+ Commands,
29
+ ExportFormats,
30
+ requestStatus,
31
+ UpdateOperations,
32
+ evaluateMacros,
33
+ };
@@ -0,0 +1,18 @@
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
+ export type AdmonitionType =
14
+ | 'NOTE'
15
+ | 'TIP'
16
+ | 'IMPORTANT'
17
+ | 'CAUTION'
18
+ | 'WARNING';
@@ -0,0 +1,54 @@
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 type { macroMetadata } from '../macros/common.js';
14
+
15
+ type Mode = 'validate' | 'static' | 'inject';
16
+
17
+ export interface MacroGenerationContext {
18
+ projectPath: string;
19
+ mode: Mode;
20
+ cardKey: string;
21
+ }
22
+
23
+ export interface MacroMetadata {
24
+ /**
25
+ * The name of the macro. This is the name that will be used in the content
26
+ */
27
+ name: string;
28
+ /**
29
+ * The tag name of the macro. This is the name that will be used in the HTML. This is separated for clarity since tags cannot have uppercase letters
30
+ */
31
+ tagName: string;
32
+
33
+ /**
34
+ * The schema of the macro. This is used to validate the data passed to the macro
35
+ */
36
+ schema?: string;
37
+ }
38
+
39
+ export interface MacroTaskState {
40
+ globalId: string;
41
+ localId: number;
42
+ promise: Promise<void>;
43
+ placeholder: string;
44
+ promiseResult: string | null;
45
+ macro: string;
46
+ }
47
+
48
+ // Handlebars options is not documented
49
+ // It contains various context specific parameters
50
+ export interface HandlebarsOptions {
51
+ fn: (arg0: unknown) => string;
52
+ }
53
+
54
+ export type MacroName = keyof typeof macroMetadata;