@cyberismo/cli 0.0.1

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.
@@ -0,0 +1,223 @@
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
+ // Resource types that have commands affecting them.
14
+ const Resources = [
15
+ 'cardType',
16
+ 'fieldType',
17
+ 'graphModel',
18
+ 'graphView',
19
+ 'linkType',
20
+ 'report',
21
+ 'template',
22
+ 'workflow',
23
+ ];
24
+
25
+ // Lookup table for plural forms for command targets that have plural form commands.
26
+ const pluralLookUpForResources = new Map([
27
+ ['cardType', 'cardTypes'],
28
+ ['fieldType', 'fieldTypes'],
29
+ ['graphModel', 'graphModels'],
30
+ ['graphView', 'graphViews'],
31
+ ['linkType', 'linkTypes'],
32
+ ['report', 'reports'],
33
+ ['template', 'templates'],
34
+ ['workflow', 'workflows'],
35
+ ]);
36
+
37
+ // Default 'sort' puts Uppercase before lowercase.
38
+ // Make sorting case-independent.
39
+ // @todo: This could be in some util file.
40
+ function alphabeticalOrder(a: string, b: string) {
41
+ const aLower = a.toLowerCase();
42
+ const bLower = b.toLowerCase();
43
+ if (aLower < bLower) return -1;
44
+ if (aLower > bLower) return 1;
45
+ return 0;
46
+ }
47
+
48
+ /**
49
+ * Class that helps parsing 'create', 'remove' and 'show' commands.
50
+ */
51
+ abstract class CreateTypes {
52
+ private static ResourceTypes = Resources;
53
+
54
+ // Command specific additions; these can also be affected with 'create'.
55
+ private static ResourceLikeTypes = [
56
+ 'attachment',
57
+ 'card',
58
+ 'label',
59
+ 'link',
60
+ 'project',
61
+ ];
62
+
63
+ private static TargetTypes = [
64
+ ...CreateTypes.ResourceTypes,
65
+ ...CreateTypes.ResourceLikeTypes,
66
+ ].sort(alphabeticalOrder);
67
+
68
+ // For create command, this is only used in cases where resource name is provided to the command.
69
+ // e.g. "cyberismo remove demo/workflows/controlledDocument"
70
+ public static pluralLookupTable = new Map([...pluralLookUpForResources]);
71
+
72
+ // Lists all create-able resource types.
73
+ public static all(): string[] {
74
+ return CreateTypes.TargetTypes;
75
+ }
76
+ }
77
+
78
+ // Helper class for show command types.
79
+ abstract class ShowTypes {
80
+ private static ResourceTypes = Resources;
81
+
82
+ // Command specific additions; these can also be shown with 'show'.
83
+ private static ResourceLikeTypes = [
84
+ 'attachments',
85
+ 'card',
86
+ 'cards',
87
+ 'labels',
88
+ 'module',
89
+ 'project',
90
+ ];
91
+
92
+ private static TargetTypes = [
93
+ ...ShowTypes.ResourceTypes,
94
+ ...ShowTypes.ResourceLikeTypes,
95
+ ];
96
+
97
+ public static pluralLookupTable = new Map([
98
+ ...pluralLookUpForResources,
99
+ ['module', 'modules'],
100
+ ]);
101
+
102
+ // Lists all show-able resource types.
103
+ public static all(): string[] {
104
+ return [
105
+ ...ShowTypes.pluralLookupTable.values(),
106
+ ...ShowTypes.TargetTypes,
107
+ ].sort(alphabeticalOrder);
108
+ }
109
+ }
110
+
111
+ // Helper class for remove command types.
112
+ // Note that remove command is never used with plural names.
113
+ abstract class RemoveTypes {
114
+ private static ResourceTypes = Resources;
115
+
116
+ // Command specific additions; these can also be affected with 'remove'.
117
+ private static ResourceLikeTypes = [
118
+ 'attachment',
119
+ 'card',
120
+ 'label',
121
+ 'link',
122
+ 'module',
123
+ ];
124
+
125
+ private static TargetTypes = [
126
+ ...RemoveTypes.ResourceLikeTypes,
127
+ ...RemoveTypes.ResourceTypes,
128
+ ].sort(alphabeticalOrder);
129
+
130
+ // For remove command, this is only used in cases where resource name is provided to the command.
131
+ // e.g. "cyberismo remove demo/workflows/controlledDocument"
132
+ public static pluralLookupTable = new Map([...pluralLookUpForResources]);
133
+
134
+ // Lists all remove-able resource types.
135
+ public static all(): string[] {
136
+ return RemoveTypes.TargetTypes;
137
+ }
138
+ }
139
+
140
+ // Parser that helps with certain commands that are related to resources (create, show, remove).
141
+ export class ResourceTypeParser {
142
+ private static parseTypes(
143
+ types: string[],
144
+ value: string,
145
+ pluralValues: Map<string, string>,
146
+ ): string {
147
+ // Known type.
148
+ if (types.includes(value)) {
149
+ return value;
150
+ }
151
+
152
+ // If it wasn't a known type, maybe it is a resource name
153
+ const parts = value.split('/');
154
+ if (parts && parts.length === 3) {
155
+ const type = parts.at(1) || '';
156
+ if (types.includes(type)) {
157
+ return value;
158
+ } else {
159
+ for (const plural of pluralValues.values()) {
160
+ if (type === plural) {
161
+ return value;
162
+ }
163
+ }
164
+ }
165
+ }
166
+ throw new Error(`Unknown type: '${value}'.\nSupported types are: '${types.join("', '")}'. Alternatively provide resource name (e.g "cyberismo show <prefix/type/identifier>").
167
+ `);
168
+ }
169
+
170
+ private static command(value: string) {
171
+ let typeValue;
172
+ if (value === 'remove') typeValue = RemoveTypes;
173
+ if (value === 'show') typeValue = ShowTypes;
174
+ if (value === 'create') typeValue = CreateTypes;
175
+ if (!typeValue) throw new Error('Unsupported command: ' + value);
176
+ return typeValue;
177
+ }
178
+
179
+ private static parseCommandTypes(type: string, category: string): string {
180
+ const commandType = ResourceTypeParser.command(category);
181
+ return ResourceTypeParser.parseTypes(
182
+ commandType.all(),
183
+ type,
184
+ commandType.pluralLookupTable,
185
+ );
186
+ }
187
+
188
+ /**
189
+ * Returns type targets related to 'command'.
190
+ * @param command command that targets need to be fetched to ('create', 'show', ...)
191
+ * @returns Array of type targets related to 'command'.
192
+ */
193
+ public static listTargets(command: string): string[] {
194
+ return ResourceTypeParser.command(command).all();
195
+ }
196
+
197
+ /**
198
+ * Parses remove command.
199
+ * @param type Argument 'type' from a command.
200
+ * @returns 'type' if it is a valid value. Throws if not.
201
+ */
202
+ public static parseRemoveTypes(type: string): string {
203
+ return ResourceTypeParser.parseCommandTypes(type, 'remove');
204
+ }
205
+
206
+ /**
207
+ * Parses create command.
208
+ * @param type Argument 'type' from a command.
209
+ * @returns 'type' if it is a valid value. Throws if not.
210
+ */
211
+ public static parseCreateTypes(type: string): string {
212
+ return ResourceTypeParser.parseCommandTypes(type, 'create');
213
+ }
214
+
215
+ /**
216
+ * Parses show command.
217
+ * @param type Argument 'type' from a command.
218
+ * @returns 'type' if it is a valid value. Throws if not.
219
+ */
220
+ public static parseShowTypes(type: string): string {
221
+ return ResourceTypeParser.parseCommandTypes(type, 'show');
222
+ }
223
+ }