@cyberismo/data-handler 0.0.12 → 0.0.13

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 (78) hide show
  1. package/dist/command-handler.d.ts +24 -42
  2. package/dist/command-handler.js +30 -25
  3. package/dist/command-handler.js.map +1 -1
  4. package/dist/commands/import.js +2 -2
  5. package/dist/commands/import.js.map +1 -1
  6. package/dist/commands/show.d.ts +4 -0
  7. package/dist/commands/show.js +4 -0
  8. package/dist/commands/show.js.map +1 -1
  9. package/dist/commands/update.d.ts +11 -1
  10. package/dist/commands/update.js +14 -2
  11. package/dist/commands/update.js.map +1 -1
  12. package/dist/commands/validate.d.ts +2 -1
  13. package/dist/commands/validate.js +3 -2
  14. package/dist/commands/validate.js.map +1 -1
  15. package/dist/containers/card-container.js +1 -1
  16. package/dist/containers/card-container.js.map +1 -1
  17. package/dist/containers/project/calculation-engine.js +1 -1
  18. package/dist/containers/project/calculation-engine.js.map +1 -1
  19. package/dist/index.d.ts +4 -2
  20. package/dist/index.js.map +1 -1
  21. package/dist/interfaces/command-options.d.ts +81 -0
  22. package/dist/interfaces/command-options.js +14 -0
  23. package/dist/interfaces/command-options.js.map +1 -0
  24. package/dist/interfaces/folder-content-interfaces.d.ts +50 -0
  25. package/dist/interfaces/folder-content-interfaces.js +45 -0
  26. package/dist/interfaces/folder-content-interfaces.js.map +1 -0
  27. package/dist/interfaces/resource-interfaces.d.ts +28 -10
  28. package/dist/interfaces/resource-interfaces.js.map +1 -1
  29. package/dist/macros/report/index.js +4 -4
  30. package/dist/macros/report/index.js.map +1 -1
  31. package/dist/resources/card-type-resource.js +11 -5
  32. package/dist/resources/card-type-resource.js.map +1 -1
  33. package/dist/resources/field-type-resource.js +1 -1
  34. package/dist/resources/field-type-resource.js.map +1 -1
  35. package/dist/resources/folder-resource.d.ts +37 -9
  36. package/dist/resources/folder-resource.js +108 -12
  37. package/dist/resources/folder-resource.js.map +1 -1
  38. package/dist/resources/graph-model-resource.d.ts +7 -4
  39. package/dist/resources/graph-model-resource.js +12 -25
  40. package/dist/resources/graph-model-resource.js.map +1 -1
  41. package/dist/resources/graph-view-resource.d.ts +7 -4
  42. package/dist/resources/graph-view-resource.js +12 -26
  43. package/dist/resources/graph-view-resource.js.map +1 -1
  44. package/dist/resources/link-type-resource.js +1 -1
  45. package/dist/resources/link-type-resource.js.map +1 -1
  46. package/dist/resources/report-resource.d.ts +14 -10
  47. package/dist/resources/report-resource.js +41 -45
  48. package/dist/resources/report-resource.js.map +1 -1
  49. package/dist/resources/resource-object.d.ts +7 -0
  50. package/dist/resources/resource-object.js.map +1 -1
  51. package/dist/resources/template-resource.d.ts +5 -1
  52. package/dist/resources/template-resource.js +12 -7
  53. package/dist/resources/template-resource.js.map +1 -1
  54. package/dist/resources/workflow-resource.js +6 -0
  55. package/dist/resources/workflow-resource.js.map +1 -1
  56. package/package.json +9 -7
  57. package/src/command-handler.ts +71 -58
  58. package/src/commands/import.ts +2 -0
  59. package/src/commands/show.ts +4 -0
  60. package/src/commands/update.ts +20 -2
  61. package/src/commands/validate.ts +6 -2
  62. package/src/containers/card-container.ts +1 -1
  63. package/src/containers/project/calculation-engine.ts +1 -3
  64. package/src/index.ts +36 -2
  65. package/src/interfaces/command-options.ts +144 -0
  66. package/src/interfaces/folder-content-interfaces.ts +69 -0
  67. package/src/interfaces/resource-interfaces.ts +41 -12
  68. package/src/macros/report/index.ts +4 -4
  69. package/src/resources/card-type-resource.ts +12 -6
  70. package/src/resources/field-type-resource.ts +1 -1
  71. package/src/resources/folder-resource.ts +149 -19
  72. package/src/resources/graph-model-resource.ts +16 -27
  73. package/src/resources/graph-view-resource.ts +16 -28
  74. package/src/resources/link-type-resource.ts +1 -1
  75. package/src/resources/report-resource.ts +60 -62
  76. package/src/resources/resource-object.ts +11 -0
  77. package/src/resources/template-resource.ts +12 -7
  78. package/src/resources/workflow-resource.ts +4 -0
@@ -1,43 +1,46 @@
1
1
  /**
2
2
  Cyberismo
3
3
  Copyright © Cyberismo Ltd and contributors 2024
4
+
4
5
  This program is free software: you can redistribute it and/or modify it under
5
6
  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
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
11
12
  License along with this program. If not, see <https://www.gnu.org/licenses/>.
12
13
  */
13
14
 
14
- import { readdir, readFile } from 'node:fs/promises';
15
+ import { readdir } from 'node:fs/promises';
15
16
  import { readFileSync } from 'node:fs';
16
17
  import { extname, join } from 'node:path';
17
18
 
18
19
  import { copyDir } from '../utils/file-utils.js';
19
- import type {
20
- Card,
21
- Operation,
22
- Project,
23
- ResourceName,
24
- } from './folder-resource.js';
25
20
  import {
26
21
  DefaultContent,
27
22
  FolderResource,
28
23
  resourceNameToString,
29
24
  sortCards,
30
25
  } from './folder-resource.js';
26
+ import { getStaticDirectoryPath } from '@cyberismo/assets';
27
+ import { filename } from '../interfaces/folder-content-interfaces.js';
28
+ import { Validate } from '../commands/validate.js';
29
+
30
+ import type {
31
+ Card,
32
+ Operation,
33
+ Project,
34
+ ResourceName,
35
+ } from './folder-resource.js';
31
36
  import type {
32
37
  Report,
33
38
  ReportMetadata,
39
+ ReportUpdateKey,
34
40
  } from '../interfaces/resource-interfaces.js';
41
+ import type { ReportContent } from '../interfaces/folder-content-interfaces.js';
35
42
  import type { Schema } from 'jsonschema';
36
- import { getStaticDirectoryPath } from '@cyberismo/assets';
37
- import { Validate } from '../commands/validate.js';
38
43
 
39
- const CARD_CONTENT_HANDLEBAR_FILE = 'index.adoc.hbs';
40
- const QUERY_HANDLEBAR_FILE = 'query.lp.hbs';
41
44
  const REPORT_SCHEMA_FILE = 'parameterSchema.json';
42
45
  const PARAMETER_SCHEMA_ID = 'jsonSchema';
43
46
 
@@ -67,8 +70,21 @@ export class ReportResource extends FolderResource {
67
70
  'defaultReport',
68
71
  );
69
72
 
70
- // When resource name changes.
71
- private async handleNameChange(existingName: string) {
73
+ // Try to read schema file content
74
+ private readSchemaFile(path: string) {
75
+ try {
76
+ const schema = readFileSync(path);
77
+ return JSON.parse(schema.toString());
78
+ } catch {
79
+ return undefined;
80
+ }
81
+ }
82
+
83
+ /**
84
+ * Handle name changes for reports
85
+ * @param existingName The previous name before the change
86
+ */
87
+ protected async onNameChange(existingName: string): Promise<void> {
72
88
  await Promise.all([
73
89
  super.updateHandleBars(
74
90
  existingName,
@@ -81,16 +97,6 @@ export class ReportResource extends FolderResource {
81
97
  await this.write();
82
98
  }
83
99
 
84
- // Try to read schema file content
85
- private readSchemaFile(path: string) {
86
- try {
87
- const schema = readFileSync(path);
88
- return JSON.parse(schema.toString());
89
- } catch {
90
- return undefined;
91
- }
92
- }
93
-
94
100
  /**
95
101
  * Sets new metadata into the report object.
96
102
  * @param newContent metadata content for the report.
@@ -143,7 +149,7 @@ export class ReportResource extends FolderResource {
143
149
  public async rename(newName: ResourceName) {
144
150
  const existingName = this.content.name;
145
151
  await super.rename(newName);
146
- return this.handleNameChange(existingName);
152
+ return this.onNameChange(existingName);
147
153
  }
148
154
 
149
155
  /**
@@ -151,19 +157,16 @@ export class ReportResource extends FolderResource {
151
157
  * @returns report metadata.
152
158
  */
153
159
  public async show(): Promise<Report> {
154
- const reportMetadata = (await super.show()) as ReportMetadata;
160
+ const baseData = (await super.show()) as ReportMetadata;
161
+ const fileContents = await super.contentData();
162
+ const content: ReportContent = {
163
+ contentTemplate: fileContents.contentTemplate as string,
164
+ queryTemplate: fileContents.queryTemplate as string,
165
+ schema: fileContents.schema ? (fileContents.schema as Schema) : undefined,
166
+ };
155
167
  return {
156
- name: resourceNameToString(this.resourceName),
157
- displayName: reportMetadata.displayName,
158
- description: reportMetadata.description,
159
- metadata: reportMetadata,
160
- contentTemplate: (
161
- await readFile(join(this.internalFolder, CARD_CONTENT_HANDLEBAR_FILE))
162
- ).toString(),
163
- queryTemplate: (
164
- await readFile(join(this.internalFolder, QUERY_HANDLEBAR_FILE))
165
- ).toString(),
166
- schema: this.reportSchema,
168
+ ...baseData,
169
+ content: content,
167
170
  };
168
171
  }
169
172
 
@@ -171,32 +174,27 @@ export class ReportResource extends FolderResource {
171
174
  * Updates report resource.
172
175
  * @param key Key to modify
173
176
  * @param op Operation to perform on 'key'
174
- * @throws if key is unknown.
175
177
  */
176
- public async update<Type>(key: string, op: Operation<Type>) {
177
- const nameChange = key === 'name';
178
- const existingName = this.content.name;
179
-
180
- await super.update(key, op);
181
-
182
- const content = structuredClone(this.content) as ReportMetadata;
183
-
184
- if (key === 'name') {
185
- content.name = super.handleScalar(op) as string;
186
- } else if (key === 'displayName') {
187
- content.displayName = super.handleScalar(op) as string;
188
- } else if (key === 'description') {
189
- content.description = super.handleScalar(op) as string;
190
- } else if (key === 'category') {
191
- content.category = super.handleScalar(op) as string;
178
+ public async update<Type>(key: ReportUpdateKey, op: Operation<Type>) {
179
+ if (
180
+ typeof key === 'object' &&
181
+ key.key === 'content' &&
182
+ key.subKey === 'schema'
183
+ ) {
184
+ const fileContent = JSON.stringify(super.handleScalar(op), null, 2);
185
+ await this.updateFile(filename(key.subKey)!, fileContent);
186
+ return;
192
187
  }
193
188
 
194
- await super.postUpdate(content, key, op);
189
+ if (key === 'category') {
190
+ const content = structuredClone(this.content) as ReportMetadata;
191
+ content.category = super.handleScalar(op) as string;
195
192
 
196
- // Renaming this report causes that references to its name must be updated.
197
- if (nameChange) {
198
- await this.handleNameChange(existingName);
193
+ await super.postUpdate(content, key, op);
194
+ return;
199
195
  }
196
+
197
+ await super.update(key, op);
200
198
  }
201
199
 
202
200
  /**
@@ -67,6 +67,17 @@ export type Operation<T> =
67
67
  | RankOperation<T>
68
68
  | RemoveOperation<T>;
69
69
 
70
+ // Utility mapping from operation name to its concrete operation type
71
+ export type OperationMap<T> = {
72
+ add: AddOperation<T>;
73
+ change: ChangeOperation<T>;
74
+ rank: RankOperation<T>;
75
+ remove: RemoveOperation<T>;
76
+ };
77
+
78
+ // Given an operation name, get the corresponding operation type
79
+ export type OperationFor<T, N extends UpdateOperations> = OperationMap<T>[N];
80
+
70
81
  /**
71
82
  * Abstract class for resources.
72
83
  */
@@ -57,13 +57,15 @@ export class TemplateResource extends FolderResource {
57
57
  });
58
58
  }
59
59
 
60
- // When resource name changes.
61
- private async handleNameChange(existingName: string) {
60
+ /**
61
+ * Handle name changes for templates
62
+ * @param existingName The previous name before the change
63
+ */
64
+ protected async onNameChange(existingName: string): Promise<void> {
62
65
  await Promise.all([
63
66
  super.updateHandleBars(existingName, this.content.name),
64
67
  super.updateCalculations(existingName, this.content.name),
65
68
  ]);
66
- // Finally, write updated content.
67
69
  await this.write();
68
70
  }
69
71
 
@@ -105,7 +107,7 @@ export class TemplateResource extends FolderResource {
105
107
  public async rename(newName: ResourceName) {
106
108
  const existingName = this.content.name;
107
109
  await super.rename(newName);
108
- return this.handleNameChange(existingName);
110
+ return this.onNameChange(existingName);
109
111
  }
110
112
 
111
113
  /**
@@ -117,8 +119,8 @@ export class TemplateResource extends FolderResource {
117
119
  const container = this.templateObject();
118
120
 
119
121
  return {
120
- metadata: templateMetadata,
121
122
  name: resourceNameToString(this.resourceName),
123
+ category: templateMetadata.category,
122
124
  displayName: templateMetadata.displayName,
123
125
  description: templateMetadata.description,
124
126
  path: this.fileName,
@@ -144,7 +146,10 @@ export class TemplateResource extends FolderResource {
144
146
  const nameChange = key === 'name';
145
147
  const existingName = this.content.name;
146
148
 
147
- await super.update(key, op);
149
+ // Only call super.update for keys that base class supports
150
+ if (key === 'name' || key === 'displayName' || key === 'description') {
151
+ await super.update(key, op);
152
+ }
148
153
 
149
154
  const content = structuredClone(this.content) as TemplateMetadata;
150
155
 
@@ -164,7 +169,7 @@ export class TemplateResource extends FolderResource {
164
169
 
165
170
  // Renaming this template causes that references to its name must be updated.
166
171
  if (nameChange) {
167
- await this.handleNameChange(existingName);
172
+ await this.onNameChange(existingName);
168
173
  }
169
174
  }
170
175
 
@@ -283,6 +283,10 @@ export class WorkflowResource extends FileResource {
283
283
 
284
284
  if (key === 'name') {
285
285
  content.name = super.handleScalar(op) as string;
286
+ } else if (key === 'displayName') {
287
+ content.displayName = super.handleScalar(op) as string;
288
+ } else if (key === 'description') {
289
+ content.description = super.handleScalar(op) as string;
286
290
  } else if (key === 'states') {
287
291
  content.states = super.handleArray(
288
292
  op,