@cyberismo/data-handler 0.0.15 → 0.0.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (173) hide show
  1. package/dist/card-metadata-updater.js +7 -1
  2. package/dist/card-metadata-updater.js.map +1 -1
  3. package/dist/command-handler.d.ts +4 -0
  4. package/dist/command-handler.js +19 -3
  5. package/dist/command-handler.js.map +1 -1
  6. package/dist/command-manager.d.ts +24 -1
  7. package/dist/command-manager.js +27 -3
  8. package/dist/command-manager.js.map +1 -1
  9. package/dist/commands/create.d.ts +1 -1
  10. package/dist/commands/create.js +34 -36
  11. package/dist/commands/create.js.map +1 -1
  12. package/dist/commands/export.d.ts +11 -2
  13. package/dist/commands/export.js +54 -41
  14. package/dist/commands/export.js.map +1 -1
  15. package/dist/commands/import.d.ts +9 -1
  16. package/dist/commands/import.js +15 -7
  17. package/dist/commands/import.js.map +1 -1
  18. package/dist/commands/move.js +0 -1
  19. package/dist/commands/move.js.map +1 -1
  20. package/dist/commands/remove.d.ts +8 -1
  21. package/dist/commands/remove.js +8 -4
  22. package/dist/commands/remove.js.map +1 -1
  23. package/dist/commands/rename.d.ts +4 -9
  24. package/dist/commands/rename.js +32 -101
  25. package/dist/commands/rename.js.map +1 -1
  26. package/dist/commands/show.d.ts +16 -10
  27. package/dist/commands/show.js +71 -55
  28. package/dist/commands/show.js.map +1 -1
  29. package/dist/commands/transition.d.ts +9 -2
  30. package/dist/commands/transition.js +25 -17
  31. package/dist/commands/transition.js.map +1 -1
  32. package/dist/commands/update.d.ts +16 -12
  33. package/dist/commands/update.js +19 -17
  34. package/dist/commands/update.js.map +1 -1
  35. package/dist/commands/validate.d.ts +17 -9
  36. package/dist/commands/validate.js +96 -35
  37. package/dist/commands/validate.js.map +1 -1
  38. package/dist/containers/project/calculation-engine.d.ts +7 -4
  39. package/dist/containers/project/calculation-engine.js +61 -66
  40. package/dist/containers/project/calculation-engine.js.map +1 -1
  41. package/dist/containers/project/project-paths.d.ts +5 -4
  42. package/dist/containers/project/project-paths.js +16 -12
  43. package/dist/containers/project/project-paths.js.map +1 -1
  44. package/dist/containers/project/resource-cache.d.ts +169 -0
  45. package/dist/containers/project/resource-cache.js +507 -0
  46. package/dist/containers/project/resource-cache.js.map +1 -0
  47. package/dist/containers/project/resource-handler.d.ts +129 -0
  48. package/dist/containers/project/resource-handler.js +206 -0
  49. package/dist/containers/project/resource-handler.js.map +1 -0
  50. package/dist/containers/project.d.ts +38 -153
  51. package/dist/containers/project.js +129 -405
  52. package/dist/containers/project.js.map +1 -1
  53. package/dist/containers/template.d.ts +8 -2
  54. package/dist/containers/template.js +20 -15
  55. package/dist/containers/template.js.map +1 -1
  56. package/dist/interfaces/folder-content-interfaces.d.ts +5 -3
  57. package/dist/interfaces/folder-content-interfaces.js +3 -3
  58. package/dist/interfaces/folder-content-interfaces.js.map +1 -1
  59. package/dist/interfaces/project-interfaces.d.ts +2 -4
  60. package/dist/interfaces/project-interfaces.js.map +1 -1
  61. package/dist/interfaces/resource-interfaces.d.ts +14 -1
  62. package/dist/interfaces/resource-interfaces.js.map +1 -1
  63. package/dist/macros/graph/index.js +12 -26
  64. package/dist/macros/graph/index.js.map +1 -1
  65. package/dist/macros/index.d.ts +1 -1
  66. package/dist/macros/index.js +2 -2
  67. package/dist/macros/index.js.map +1 -1
  68. package/dist/macros/report/index.js +3 -6
  69. package/dist/macros/report/index.js.map +1 -1
  70. package/dist/module-manager.d.ts +16 -3
  71. package/dist/module-manager.js +51 -19
  72. package/dist/module-manager.js.map +1 -1
  73. package/dist/project-settings.d.ts +16 -3
  74. package/dist/project-settings.js +79 -14
  75. package/dist/project-settings.js.map +1 -1
  76. package/dist/resources/calculation-resource.d.ts +4 -3
  77. package/dist/resources/calculation-resource.js +11 -5
  78. package/dist/resources/calculation-resource.js.map +1 -1
  79. package/dist/resources/card-type-resource.d.ts +6 -1
  80. package/dist/resources/card-type-resource.js +34 -23
  81. package/dist/resources/card-type-resource.js.map +1 -1
  82. package/dist/resources/create-defaults.d.ts +3 -2
  83. package/dist/resources/create-defaults.js +3 -2
  84. package/dist/resources/create-defaults.js.map +1 -1
  85. package/dist/resources/field-type-resource.d.ts +4 -1
  86. package/dist/resources/field-type-resource.js +22 -23
  87. package/dist/resources/field-type-resource.js.map +1 -1
  88. package/dist/resources/file-resource.d.ts +5 -9
  89. package/dist/resources/file-resource.js +6 -11
  90. package/dist/resources/file-resource.js.map +1 -1
  91. package/dist/resources/folder-resource.d.ts +29 -32
  92. package/dist/resources/folder-resource.js +59 -78
  93. package/dist/resources/folder-resource.js.map +1 -1
  94. package/dist/resources/graph-model-resource.d.ts +4 -1
  95. package/dist/resources/graph-model-resource.js +11 -4
  96. package/dist/resources/graph-model-resource.js.map +1 -1
  97. package/dist/resources/graph-view-resource.d.ts +5 -2
  98. package/dist/resources/graph-view-resource.js +7 -3
  99. package/dist/resources/graph-view-resource.js.map +1 -1
  100. package/dist/resources/link-type-resource.d.ts +5 -2
  101. package/dist/resources/link-type-resource.js +5 -2
  102. package/dist/resources/link-type-resource.js.map +1 -1
  103. package/dist/resources/report-resource.d.ts +6 -7
  104. package/dist/resources/report-resource.js +14 -23
  105. package/dist/resources/report-resource.js.map +1 -1
  106. package/dist/resources/resource-object.d.ts +93 -8
  107. package/dist/resources/resource-object.js +162 -110
  108. package/dist/resources/resource-object.js.map +1 -1
  109. package/dist/resources/template-resource.d.ts +7 -3
  110. package/dist/resources/template-resource.js +10 -3
  111. package/dist/resources/template-resource.js.map +1 -1
  112. package/dist/resources/workflow-resource.d.ts +5 -2
  113. package/dist/resources/workflow-resource.js +18 -22
  114. package/dist/resources/workflow-resource.js.map +1 -1
  115. package/dist/utils/card-utils.d.ts +2 -2
  116. package/dist/utils/card-utils.js +1 -1
  117. package/dist/utils/clingo-fact-builder.d.ts +25 -14
  118. package/dist/utils/clingo-fact-builder.js +27 -5
  119. package/dist/utils/clingo-fact-builder.js.map +1 -1
  120. package/dist/utils/clingo-facts.js +3 -4
  121. package/dist/utils/clingo-facts.js.map +1 -1
  122. package/dist/utils/resource-utils.d.ts +1 -0
  123. package/dist/utils/resource-utils.js +2 -1
  124. package/dist/utils/resource-utils.js.map +1 -1
  125. package/package.json +8 -8
  126. package/src/card-metadata-updater.ts +6 -2
  127. package/src/command-handler.ts +24 -5
  128. package/src/command-manager.ts +29 -17
  129. package/src/commands/create.ts +43 -78
  130. package/src/commands/export.ts +63 -52
  131. package/src/commands/import.ts +24 -14
  132. package/src/commands/move.ts +0 -1
  133. package/src/commands/remove.ts +11 -7
  134. package/src/commands/rename.ts +43 -149
  135. package/src/commands/show.ts +113 -78
  136. package/src/commands/transition.ts +26 -28
  137. package/src/commands/update.ts +25 -22
  138. package/src/commands/validate.ts +108 -67
  139. package/src/containers/project/calculation-engine.ts +61 -93
  140. package/src/containers/project/project-paths.ts +21 -13
  141. package/src/containers/project/resource-cache.ts +648 -0
  142. package/src/containers/project/resource-handler.ts +265 -0
  143. package/src/containers/project.ts +178 -522
  144. package/src/containers/template.ts +24 -19
  145. package/src/interfaces/folder-content-interfaces.ts +7 -6
  146. package/src/interfaces/project-interfaces.ts +7 -6
  147. package/src/interfaces/resource-interfaces.ts +18 -3
  148. package/src/macros/graph/index.ts +26 -47
  149. package/src/macros/index.ts +2 -2
  150. package/src/macros/report/index.ts +3 -9
  151. package/src/module-manager.ts +74 -17
  152. package/src/project-settings.ts +83 -14
  153. package/src/resources/calculation-resource.ts +18 -18
  154. package/src/resources/card-type-resource.ts +50 -50
  155. package/src/resources/create-defaults.ts +3 -2
  156. package/src/resources/field-type-resource.ts +41 -55
  157. package/src/resources/file-resource.ts +10 -36
  158. package/src/resources/folder-resource.ts +69 -120
  159. package/src/resources/graph-model-resource.ts +20 -22
  160. package/src/resources/graph-view-resource.ts +15 -17
  161. package/src/resources/link-type-resource.ts +10 -13
  162. package/src/resources/report-resource.ts +21 -43
  163. package/src/resources/resource-object.ts +194 -152
  164. package/src/resources/template-resource.ts +17 -16
  165. package/src/resources/workflow-resource.ts +25 -44
  166. package/src/utils/card-utils.ts +2 -2
  167. package/src/utils/clingo-fact-builder.ts +28 -16
  168. package/src/utils/clingo-facts.ts +3 -4
  169. package/src/utils/resource-utils.ts +2 -1
  170. package/dist/containers/project/resource-collector.d.ts +0 -110
  171. package/dist/containers/project/resource-collector.js +0 -344
  172. package/dist/containers/project/resource-collector.js.map +0 -1
  173. package/src/containers/project/resource-collector.ts +0 -404
@@ -27,7 +27,7 @@ import type { Card, Context } from '../../interfaces/project-interfaces.js';
27
27
  import ClingoParser from '../../utils/clingo-parser.js';
28
28
  import { Mutex } from 'async-mutex';
29
29
  import Handlebars from 'handlebars';
30
- import { type Project, ResourcesFrom } from '../../containers/project.js';
30
+ import type { Project } from '../../containers/project.js';
31
31
  import { getChildLogger } from '../../utils/log-utils.js';
32
32
  import {
33
33
  createCardFacts,
@@ -50,7 +50,6 @@ import type {
50
50
  TemplateMetadata,
51
51
  Workflow,
52
52
  } from '../../interfaces/resource-interfaces.js';
53
- import { CalculationResource } from '../../resources/calculation-resource.js';
54
53
  import {
55
54
  removeAllPrograms,
56
55
  solve,
@@ -61,7 +60,6 @@ import {
61
60
  import { generateReportContent } from '../../utils/report.js';
62
61
  import { lpFiles, graphvizReport } from '@cyberismo/assets';
63
62
  import {
64
- resourceName,
65
63
  type ResourceName,
66
64
  resourceNameToString,
67
65
  } from '../../utils/resource-utils.js';
@@ -122,7 +120,7 @@ export class CalculationEngine {
122
120
  await writeFile(destination, logicProgram);
123
121
  }
124
122
 
125
- // // Wrapper to run onCreation query.
123
+ // Wrapper to run onCreation query.
126
124
  private async creationQuery(cardKeys: string[], context: Context) {
127
125
  if (!cardKeys) return undefined;
128
126
  return this.runQuery('onCreation', context, {
@@ -145,10 +143,10 @@ export class CalculationEngine {
145
143
 
146
144
  // Generates logic programs related to modules (and project itself).
147
145
  private async generateModules() {
148
- const modules = await this.project.modules();
146
+ const modules = this.project.resources.moduleNames();
149
147
  let content = '';
150
148
  for (const module of await Promise.all(
151
- modules.map((mod) => this.project.module(mod.name)),
149
+ modules.map((mod) => this.project.module(mod)),
152
150
  )) {
153
151
  if (!module) continue;
154
152
  const moduleContent = createModuleFacts(module);
@@ -161,119 +159,81 @@ export class CalculationEngine {
161
159
 
162
160
  // Sets individual CardType programs
163
161
  private async setCardTypesPrograms() {
164
- const cardTypes = await this.project.cardTypes();
165
-
166
- for (const cardType of await Promise.all(
167
- cardTypes.map((c) => this.project.resource<CardType>(c.name)),
168
- )) {
169
- if (!cardType) continue;
170
-
171
- const cardTypeContent = createCardTypeFacts(cardType);
172
- setProgram(cardType.name, cardTypeContent, [ALL_CATEGORY]);
162
+ const cardTypes = this.project.resources.cardTypes();
163
+ for (const cardType of cardTypes) {
164
+ const ct = cardType.show();
165
+ const cardTypeContent = createCardTypeFacts(ct);
166
+ setProgram(ct.name, cardTypeContent, [ALL_CATEGORY]);
173
167
  }
174
168
  }
175
169
 
176
170
  // Sets individual FieldType programs
177
171
  private async setFieldTypesPrograms() {
178
- const fieldTypes = await this.project.fieldTypes();
179
-
180
- for (const fieldType of await Promise.all(
181
- fieldTypes.map((m) => this.project.resource<FieldType>(m.name)),
182
- )) {
183
- if (!fieldType) continue;
184
-
185
- const fieldTypeContent = createFieldTypeFacts(fieldType);
186
- setProgram(fieldType.name, fieldTypeContent, [ALL_CATEGORY]);
172
+ const fieldTypes = this.project.resources.fieldTypes();
173
+ for (const fieldType of fieldTypes) {
174
+ const ft = fieldType.show();
175
+ const fieldTypeContent = createFieldTypeFacts(ft);
176
+ setProgram(ft.name, fieldTypeContent, [ALL_CATEGORY]);
187
177
  }
188
178
  }
189
179
 
190
180
  // Sets individual LinkType programs
191
181
  private async setLinkTypesPrograms() {
192
- const linkTypes = await this.project.linkTypes();
193
-
194
- for (const linkType of await Promise.all(
195
- linkTypes.map((c) => this.project.resource<LinkType>(c.name)),
196
- )) {
197
- if (!linkType) continue;
198
-
199
- const linkTypeContent = createLinkTypeFacts(linkType);
200
- setProgram(linkType.name, linkTypeContent, [ALL_CATEGORY]);
182
+ const linkTypes = this.project.resources.linkTypes();
183
+ for (const linkType of linkTypes) {
184
+ const lt = linkType.show();
185
+ const linkTypeContent = createLinkTypeFacts(lt);
186
+ setProgram(lt.name, linkTypeContent, [ALL_CATEGORY]);
201
187
  }
202
188
  }
203
189
 
204
190
  // Sets individual Workflow programs
205
191
  private async setWorkflowsPrograms() {
206
- const workflows = await this.project.workflows();
207
-
208
- for (const workflow of await Promise.all(
209
- workflows.map((m) => this.project.resource<Workflow>(m.name)),
210
- )) {
211
- if (!workflow) continue;
212
-
213
- const workflowContent = createWorkflowFacts(workflow);
214
- setProgram(workflow.name, workflowContent, [ALL_CATEGORY]);
192
+ const workflows = this.project.resources.workflows();
193
+ for (const workflow of workflows) {
194
+ const wf = workflow.show();
195
+ const workflowContent = createWorkflowFacts(wf);
196
+ setProgram(wf.name, workflowContent, [ALL_CATEGORY]);
215
197
  }
216
198
  }
217
199
 
218
200
  // Sets individual Report programs
219
201
  private async setReportsPrograms() {
220
- const reports = await this.project.reports();
221
-
222
- for (const report of await Promise.all(
223
- reports.map((r) => this.project.resource<ReportMetadata>(r.name)),
224
- )) {
225
- if (!report) continue;
226
-
227
- const reportContent = createReportFacts(report);
228
- setProgram(report.name, reportContent, [ALL_CATEGORY]);
202
+ const reports = this.project.resources.reports();
203
+ for (const report of reports) {
204
+ const rep = report.show();
205
+ const reportContent = createReportFacts(rep);
206
+ setProgram(rep.name, reportContent, [ALL_CATEGORY]);
229
207
  }
230
208
  }
231
209
 
232
210
  // Sets individual Template programs
233
211
  private async setTemplatesPrograms() {
234
- const templates = await this.project.templates();
235
-
236
- for (const template of await Promise.all(
237
- templates.map((r) => this.project.resource<TemplateMetadata>(r.name)),
238
- )) {
239
- if (!template) continue;
240
-
241
- const templateContent = createTemplateFacts(template);
242
- const cards = this.getCards(template.name);
212
+ const templates = this.project.resources.templates();
213
+ for (const template of templates) {
214
+ const tem = template.show();
215
+ const templateContent = createTemplateFacts(tem);
216
+ const cards = this.getCards(tem.name);
243
217
  for (const card of cards) {
244
218
  const cardContent = await createCardFacts(card, this.project);
245
219
  setProgram(card.key, cardContent, [ALL_CATEGORY]);
246
220
  }
247
- setProgram(template.name, templateContent, [ALL_CATEGORY]);
221
+ setProgram(tem.name, templateContent, [ALL_CATEGORY]);
248
222
  }
249
223
  }
250
224
 
251
225
  // Sets individual Calculation programs
252
226
  private async setCalculationsPrograms() {
253
- const calculations = await this.project.calculations(ResourcesFrom.all);
254
-
255
- for (const calculationFile of calculations) {
227
+ const calculations = this.project.resources.calculations();
228
+ for (const calculation of calculations) {
256
229
  try {
257
- const calculationResource = new CalculationResource(
258
- this.project,
259
- resourceName(calculationFile.name),
260
- );
261
- if (calculationResource) {
262
- const resource = await calculationResource.show();
263
- if (!resource?.content.calculation) {
264
- this.logger.info(
265
- `Calculation resource '${resource.name}' does not have calculation file`,
266
- );
267
- continue;
268
- }
269
- setProgram(calculationFile.name, resource.content.calculation, [
270
- ALL_CATEGORY,
271
- ]);
272
- }
230
+ const content = calculation.contentData();
231
+ const calc = calculation.show();
232
+ setProgram(calc.name, content.calculation, [ALL_CATEGORY]);
273
233
  } catch (error) {
274
234
  this.logger.warn(
275
235
  error,
276
- `Failed to read calculation ${calculationFile.name}`,
236
+ `Failed to read calculation ${calculation.data!.name}`,
277
237
  );
278
238
  }
279
239
  }
@@ -296,6 +256,15 @@ export class CalculationEngine {
296
256
  return parser.parseInput(data.join('\n'));
297
257
  }
298
258
 
259
+ //
260
+ private queryContent(queryName: QueryName, options?: unknown) {
261
+ const content = lpFiles.queries[queryName];
262
+ const handlebars = Handlebars.create();
263
+ const compiled = handlebars.compile(content);
264
+ return compiled(options || {});
265
+ }
266
+
267
+ //
299
268
  private async run(query: string, context: Context): Promise<string[]> {
300
269
  try {
301
270
  const res = await CalculationEngine.mutex.runExclusive(async () => {
@@ -453,9 +422,12 @@ export class CalculationEngine {
453
422
  public async resourceLogicProgram(
454
423
  resourceName: ResourceName,
455
424
  ): Promise<string> {
456
- const resource = await this.project.resource(
457
- resourceNameToString(resourceName),
458
- );
425
+ let resource;
426
+ try {
427
+ resource = this.project.resources.byType(resourceName).data;
428
+ } catch {
429
+ resource = undefined;
430
+ }
459
431
  if (!resource) {
460
432
  throw new Error(
461
433
  `Resource '${resourceNameToString(resourceName)}' does not exist in the project`,
@@ -482,8 +454,9 @@ export class CalculationEngine {
482
454
 
483
455
  /**
484
456
  * Runs given logic program and creates a graph using clingraph
485
- * @param data Provide a query or/and a file which can be given to clingraph
486
- * @param timeout Maximum amount of milliseconds clingraph is allowed to run
457
+ * @param model Graph model to use.
458
+ * @param view Graph view to use.
459
+ * @param context In which type of context the query is run.
487
460
  * @returns a base64 encoded image as a string
488
461
  */
489
462
  public async runGraph(model: string, view: string, context: Context) {
@@ -521,6 +494,7 @@ export class CalculationEngine {
521
494
  /**
522
495
  * Runs a logic program using clingo.
523
496
  * @param query Logic program to be run
497
+ * @param context In which type of context the query is run.
524
498
  * @returns parsed program output
525
499
  */
526
500
  public async runLogicProgram(query: string, context: Context = 'localApp') {
@@ -528,16 +502,10 @@ export class CalculationEngine {
528
502
  return this.parseClingoResult(clingoOutput);
529
503
  }
530
504
 
531
- private queryContent(queryName: QueryName, options?: unknown) {
532
- const content = lpFiles.queries[queryName];
533
- const handlebars = Handlebars.create();
534
- const compiled = handlebars.compile(content);
535
- return compiled(options || {});
536
- }
537
-
538
505
  /**
539
506
  * Runs a pre-defined query.
540
507
  * @param queryName Name of the query file without extension
508
+ * @param context In which type of context the query is run.
541
509
  * @param options Any object that contains state for handlebars
542
510
  * @returns parsed program output
543
511
  */
@@ -88,6 +88,14 @@ export class ProjectPaths {
88
88
  return join(this.path, '.cards', 'modules');
89
89
  }
90
90
 
91
+ public moduleResourcePath(
92
+ modulePrefix: string,
93
+ resourceType: ResourceFolderType,
94
+ ) {
95
+ const moduleRoot = join(this.modulesFolder, modulePrefix);
96
+ return join(moduleRoot, resourceType);
97
+ }
98
+
91
99
  public get resourcesFolder(): string {
92
100
  return join(this.path, '.cards', 'local');
93
101
  }
@@ -96,6 +104,19 @@ export class ProjectPaths {
96
104
  return join(this.resourcesFolder, 'reports');
97
105
  }
98
106
 
107
+ /**
108
+ * Return path to a resource type folder.
109
+ * @param resourceType Type of resource
110
+ * @returns path to a resources folder (e.g. '.cards/local/cardTypes')
111
+ */
112
+ public resourcePath(resourceType: ResourceFolderType): string {
113
+ const resourcePath = this.pathMap.get(resourceType);
114
+ if (!resourcePath) {
115
+ throw new Error(`unknown resourceType: ${resourceType}`);
116
+ }
117
+ return resourcePath;
118
+ }
119
+
99
120
  public get tempCardFolder(): string {
100
121
  return join(this.tempFolder, 'cards');
101
122
  }
@@ -111,17 +132,4 @@ export class ProjectPaths {
111
132
  public get workflowsFolder(): string {
112
133
  return join(this.resourcesFolder, 'workflows');
113
134
  }
114
-
115
- /**
116
- * Return path to a resource type folder.
117
- * @param resourceType Type of resource
118
- * @returns path to a resources folder (e.g. '.cards/local/cardTypes')
119
- */
120
- public resourcePath(resourceType: ResourceFolderType): string {
121
- const resourcePath = this.pathMap.get(resourceType);
122
- if (!resourcePath) {
123
- throw new Error(`unknown resourceType: ${resourceType}`);
124
- }
125
- return resourcePath;
126
- }
127
135
  }