@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,197 @@
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
+ // Resource types that have commands affecting them.
13
+ const Resources = [
14
+ 'cardType',
15
+ 'fieldType',
16
+ 'graphModel',
17
+ 'graphView',
18
+ 'linkType',
19
+ 'report',
20
+ 'template',
21
+ 'workflow',
22
+ ];
23
+ // Lookup table for plural forms for command targets that have plural form commands.
24
+ const pluralLookUpForResources = new Map([
25
+ ['cardType', 'cardTypes'],
26
+ ['fieldType', 'fieldTypes'],
27
+ ['graphModel', 'graphModels'],
28
+ ['graphView', 'graphViews'],
29
+ ['linkType', 'linkTypes'],
30
+ ['report', 'reports'],
31
+ ['template', 'templates'],
32
+ ['workflow', 'workflows'],
33
+ ]);
34
+ // Default 'sort' puts Uppercase before lowercase.
35
+ // Make sorting case-independent.
36
+ // @todo: This could be in some util file.
37
+ function alphabeticalOrder(a, b) {
38
+ const aLower = a.toLowerCase();
39
+ const bLower = b.toLowerCase();
40
+ if (aLower < bLower)
41
+ return -1;
42
+ if (aLower > bLower)
43
+ return 1;
44
+ return 0;
45
+ }
46
+ /**
47
+ * Class that helps parsing 'create', 'remove' and 'show' commands.
48
+ */
49
+ class CreateTypes {
50
+ static ResourceTypes = Resources;
51
+ // Command specific additions; these can also be affected with 'create'.
52
+ static ResourceLikeTypes = [
53
+ 'attachment',
54
+ 'card',
55
+ 'label',
56
+ 'link',
57
+ 'project',
58
+ ];
59
+ static TargetTypes = [
60
+ ...CreateTypes.ResourceTypes,
61
+ ...CreateTypes.ResourceLikeTypes,
62
+ ].sort(alphabeticalOrder);
63
+ // For create command, this is only used in cases where resource name is provided to the command.
64
+ // e.g. "cyberismo remove demo/workflows/controlledDocument"
65
+ static pluralLookupTable = new Map([...pluralLookUpForResources]);
66
+ // Lists all create-able resource types.
67
+ static all() {
68
+ return CreateTypes.TargetTypes;
69
+ }
70
+ }
71
+ // Helper class for show command types.
72
+ class ShowTypes {
73
+ static ResourceTypes = Resources;
74
+ // Command specific additions; these can also be shown with 'show'.
75
+ static ResourceLikeTypes = [
76
+ 'attachments',
77
+ 'card',
78
+ 'cards',
79
+ 'labels',
80
+ 'module',
81
+ 'project',
82
+ ];
83
+ static TargetTypes = [
84
+ ...ShowTypes.ResourceTypes,
85
+ ...ShowTypes.ResourceLikeTypes,
86
+ ];
87
+ static pluralLookupTable = new Map([
88
+ ...pluralLookUpForResources,
89
+ ['module', 'modules'],
90
+ ]);
91
+ // Lists all show-able resource types.
92
+ static all() {
93
+ return [
94
+ ...ShowTypes.pluralLookupTable.values(),
95
+ ...ShowTypes.TargetTypes,
96
+ ].sort(alphabeticalOrder);
97
+ }
98
+ }
99
+ // Helper class for remove command types.
100
+ // Note that remove command is never used with plural names.
101
+ class RemoveTypes {
102
+ static ResourceTypes = Resources;
103
+ // Command specific additions; these can also be affected with 'remove'.
104
+ static ResourceLikeTypes = [
105
+ 'attachment',
106
+ 'card',
107
+ 'label',
108
+ 'link',
109
+ 'module',
110
+ ];
111
+ static TargetTypes = [
112
+ ...RemoveTypes.ResourceLikeTypes,
113
+ ...RemoveTypes.ResourceTypes,
114
+ ].sort(alphabeticalOrder);
115
+ // For remove command, this is only used in cases where resource name is provided to the command.
116
+ // e.g. "cyberismo remove demo/workflows/controlledDocument"
117
+ static pluralLookupTable = new Map([...pluralLookUpForResources]);
118
+ // Lists all remove-able resource types.
119
+ static all() {
120
+ return RemoveTypes.TargetTypes;
121
+ }
122
+ }
123
+ // Parser that helps with certain commands that are related to resources (create, show, remove).
124
+ export class ResourceTypeParser {
125
+ static parseTypes(types, value, pluralValues) {
126
+ // Known type.
127
+ if (types.includes(value)) {
128
+ return value;
129
+ }
130
+ // If it wasn't a known type, maybe it is a resource name
131
+ const parts = value.split('/');
132
+ if (parts && parts.length === 3) {
133
+ const type = parts.at(1) || '';
134
+ if (types.includes(type)) {
135
+ return value;
136
+ }
137
+ else {
138
+ for (const plural of pluralValues.values()) {
139
+ if (type === plural) {
140
+ return value;
141
+ }
142
+ }
143
+ }
144
+ }
145
+ throw new Error(`Unknown type: '${value}'.\nSupported types are: '${types.join("', '")}'. Alternatively provide resource name (e.g "cyberismo show <prefix/type/identifier>").
146
+ `);
147
+ }
148
+ static command(value) {
149
+ let typeValue;
150
+ if (value === 'remove')
151
+ typeValue = RemoveTypes;
152
+ if (value === 'show')
153
+ typeValue = ShowTypes;
154
+ if (value === 'create')
155
+ typeValue = CreateTypes;
156
+ if (!typeValue)
157
+ throw new Error('Unsupported command: ' + value);
158
+ return typeValue;
159
+ }
160
+ static parseCommandTypes(type, category) {
161
+ const commandType = ResourceTypeParser.command(category);
162
+ return ResourceTypeParser.parseTypes(commandType.all(), type, commandType.pluralLookupTable);
163
+ }
164
+ /**
165
+ * Returns type targets related to 'command'.
166
+ * @param command command that targets need to be fetched to ('create', 'show', ...)
167
+ * @returns Array of type targets related to 'command'.
168
+ */
169
+ static listTargets(command) {
170
+ return ResourceTypeParser.command(command).all();
171
+ }
172
+ /**
173
+ * Parses remove command.
174
+ * @param type Argument 'type' from a command.
175
+ * @returns 'type' if it is a valid value. Throws if not.
176
+ */
177
+ static parseRemoveTypes(type) {
178
+ return ResourceTypeParser.parseCommandTypes(type, 'remove');
179
+ }
180
+ /**
181
+ * Parses create command.
182
+ * @param type Argument 'type' from a command.
183
+ * @returns 'type' if it is a valid value. Throws if not.
184
+ */
185
+ static parseCreateTypes(type) {
186
+ return ResourceTypeParser.parseCommandTypes(type, 'create');
187
+ }
188
+ /**
189
+ * Parses show command.
190
+ * @param type Argument 'type' from a command.
191
+ * @returns 'type' if it is a valid value. Throws if not.
192
+ */
193
+ static parseShowTypes(type) {
194
+ return ResourceTypeParser.parseCommandTypes(type, 'show');
195
+ }
196
+ }
197
+ //# sourceMappingURL=resource-type-parser.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resource-type-parser.js","sourceRoot":"","sources":["../src/resource-type-parser.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;EAUE;AAEF,oDAAoD;AACpD,MAAM,SAAS,GAAG;IAChB,UAAU;IACV,WAAW;IACX,YAAY;IACZ,WAAW;IACX,UAAU;IACV,QAAQ;IACR,UAAU;IACV,UAAU;CACX,CAAC;AAEF,oFAAoF;AACpF,MAAM,wBAAwB,GAAG,IAAI,GAAG,CAAC;IACvC,CAAC,UAAU,EAAE,WAAW,CAAC;IACzB,CAAC,WAAW,EAAE,YAAY,CAAC;IAC3B,CAAC,YAAY,EAAE,aAAa,CAAC;IAC7B,CAAC,WAAW,EAAE,YAAY,CAAC;IAC3B,CAAC,UAAU,EAAE,WAAW,CAAC;IACzB,CAAC,QAAQ,EAAE,SAAS,CAAC;IACrB,CAAC,UAAU,EAAE,WAAW,CAAC;IACzB,CAAC,UAAU,EAAE,WAAW,CAAC;CAC1B,CAAC,CAAC;AAEH,kDAAkD;AAClD,iCAAiC;AACjC,0CAA0C;AAC1C,SAAS,iBAAiB,CAAC,CAAS,EAAE,CAAS;IAC7C,MAAM,MAAM,GAAG,CAAC,CAAC,WAAW,EAAE,CAAC;IAC/B,MAAM,MAAM,GAAG,CAAC,CAAC,WAAW,EAAE,CAAC;IAC/B,IAAI,MAAM,GAAG,MAAM;QAAE,OAAO,CAAC,CAAC,CAAC;IAC/B,IAAI,MAAM,GAAG,MAAM;QAAE,OAAO,CAAC,CAAC;IAC9B,OAAO,CAAC,CAAC;AACX,CAAC;AAED;;GAEG;AACH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AAC;CACF"}
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@cyberismo/cli",
3
+ "version": "0.0.1",
4
+ "description": "CLI tool to handle tasks.",
5
+ "main": "dist/index.js",
6
+ "bin": {
7
+ "cyberismo": "bin/run"
8
+ },
9
+ "files": [
10
+ "bin",
11
+ "dist",
12
+ "src"
13
+ ],
14
+ "keywords": [],
15
+ "author": "",
16
+ "license": "AGPL-3.0",
17
+ "dependencies": {
18
+ "@inquirer/confirm": "^5.1.9",
19
+ "commander": "^13.1.0"
20
+ },
21
+ "devDependencies": {
22
+ "@cyberismo/backend": "0.0.2",
23
+ "@cyberismo/data-handler": "0.0.2"
24
+ },
25
+ "types": "dist/index.d.ts",
26
+ "type": "module",
27
+ "exports": {
28
+ ".": "./dist/index.js"
29
+ },
30
+ "scripts": {
31
+ "preinstall": "npx only-allow pnpm",
32
+ "build": "tsc -p tsconfig.build.json",
33
+ "clean": "rm -rf node_modules",
34
+ "lint": "eslint .",
35
+ "test": "mocha --require mocha-suppress-logs --disable-warning=ExperimentalWarning --forbid-only \"./test/**/*.test.ts\""
36
+ }
37
+ }