@nzz/q-cli 1.9.1 → 1.9.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. package/.history/bin/commands/qItem/itemService_20220627113036.js +302 -0
  2. package/.history/bin/commands/qItem/itemService_20221105174223.js +304 -0
  3. package/.history/bin/commands/qItem/itemService_20221105174236.js +304 -0
  4. package/.history/bin/commands/qItem/itemService_20221105174311.js +304 -0
  5. package/.history/bin/commands/qItem/itemService_20221105174605.js +306 -0
  6. package/.history/bin/commands/qItem/itemService_20221105174809.js +309 -0
  7. package/.history/bin/commands/qItem/itemService_20221107071956.js +317 -0
  8. package/.history/bin/commands/qItem/itemService_20221107072130.js +310 -0
  9. package/.history/bin/commands/qItem/itemService_20221107072222.js +312 -0
  10. package/.history/bin/commands/qItem/itemService_20221107072621.js +316 -0
  11. package/.history/bin/commands/qItem/itemService_20221107072850.js +315 -0
  12. package/.history/bin/commands/qItem/itemService_20221107073051.js +315 -0
  13. package/.history/bin/commands/qItem/itemService_20221107073244.js +315 -0
  14. package/.history/bin/commands/qItem/itemService_20221107075352.js +323 -0
  15. package/.history/bin/commands/qItem/itemService_20221107075536.js +316 -0
  16. package/.history/bin/commands/qItem/itemService_20221107075700.js +312 -0
  17. package/.history/bin/commands/qItem/itemService_20221107111951.js +312 -0
  18. package/.history/bin/commands/qItem/itemService_20221107113745.js +310 -0
  19. package/.history/bin/commands/qItem/itemService_20221107113901.js +310 -0
  20. package/.history/bin/commands/qItem/updateItem/updateItem_20220627113036.js +64 -0
  21. package/.history/bin/commands/qItem/updateItem/updateItem_20221105173137.js +66 -0
  22. package/.history/bin/commands/qItem/updateItem/updateItem_20221105173239.js +68 -0
  23. package/.history/bin/commands/qItem/updateItem/updateItem_20221105173610.js +69 -0
  24. package/.history/bin/commands/qItem/updateItem/updateItem_20221105173646.js +74 -0
  25. package/.history/bin/commands/qItem/updateItem/updateItem_20221105174010.js +74 -0
  26. package/.history/bin/commands/qItem/updateItem/updateItem_20221105174054.js +75 -0
  27. package/.history/package_20220808135115.json +43 -0
  28. package/.history/package_20221105171707.json +43 -0
  29. package/.history/package_20221105173355.json +43 -0
  30. package/.history/package_20221107112303.json +43 -0
  31. package/bin/commands/qItem/itemService.js +12 -4
  32. package/package.json +2 -2
@@ -0,0 +1,315 @@
1
+ const resourcesService = require("./resourcesService.js");
2
+ const schemaService = require("./schemaService.js");
3
+ const deepmerge = require("deepmerge");
4
+ const fetch = require("node-fetch");
5
+ const chalk = require("chalk");
6
+ const errorColor = chalk.red;
7
+
8
+ async function createItem(item, environment, config) {
9
+ const qServer = config.get(`${environment.name}.qServer`);
10
+ const accessToken = config.get(`${environment.name}.accessToken`);
11
+ const cookie = config.get(`${environment.name}.cookie`);
12
+
13
+ try {
14
+ const response = await fetch(`${qServer}item`, {
15
+ method: "POST",
16
+ body: JSON.stringify(item),
17
+ headers: {
18
+ "user-agent": "Q Command-line Tool",
19
+ Authorization: `Bearer ${accessToken}`,
20
+ "Content-Type": "application/json",
21
+ Cookie: cookie ? cookie : "",
22
+ },
23
+ });
24
+ if (response.ok) {
25
+ return await response.json();
26
+ } else {
27
+ throw new Error(
28
+ `A problem occured while creating item on ${environment.name} environment. Please check your connection and try again.`
29
+ );
30
+ }
31
+ } catch (error) {
32
+ console.error(errorColor(error.message));
33
+ process.exit(1);
34
+ }
35
+ }
36
+
37
+ async function getItem(qServer, environment, accessToken, cookie) {
38
+ try {
39
+ const response = await fetch(`${qServer}item/${environment.id}`, {
40
+ headers: {
41
+ "user-agent": "Q Command-line Tool",
42
+ Authorization: `Bearer ${accessToken}`,
43
+ Cookie: cookie ? cookie : "",
44
+ },
45
+ });
46
+ if (response.ok) {
47
+ return await response.json();
48
+ } else {
49
+ throw new Error(
50
+ `A problem occured while getting item with id ${environment.id} on ${environment.name} environment. Please make sure that the id is correct, you have an internet connection and try again.`
51
+ );
52
+ }
53
+ } catch (error) {
54
+ console.error(errorColor(error.message));
55
+ process.exit(1);
56
+ }
57
+ }
58
+
59
+ function getItems(qConfig, environmentFilter) {
60
+ const items = qConfig.items
61
+ .filter((item) => {
62
+ if (environmentFilter) {
63
+ return item.environments.some(
64
+ (environment) => environment.name === environmentFilter
65
+ );
66
+ }
67
+
68
+ return true;
69
+ })
70
+ .map((item) => {
71
+ if (environmentFilter) {
72
+ item.environments = item.environments.filter(
73
+ (environment) => environment.name === environmentFilter
74
+ );
75
+ }
76
+
77
+ return item;
78
+ });
79
+
80
+ return items;
81
+ }
82
+
83
+ function getDefaultOrNull(schema) {
84
+ if (schema.hasOwnProperty("default")) {
85
+ if (typeof schema.default === "object") {
86
+ return JSON.parse(JSON.stringify(schema.default));
87
+ }
88
+ return schema.default;
89
+ }
90
+ return null;
91
+ }
92
+
93
+ // Returns a default item based on the tool schema
94
+ // The default item is used to derive the file properties of a certain file type
95
+ // These file properties are specified by the tool and are specific to the file type
96
+ // For example an image file has height/width file properties
97
+ function getDefaultItem(schema) {
98
+ schema = JSON.parse(JSON.stringify(schema));
99
+ if (schema.type === "array") {
100
+ let array = [];
101
+ schema.minItems = 1;
102
+ for (let i = 0; i < schema.minItems; i++) {
103
+ let value = getDefaultItem(schema.items);
104
+ if (value) {
105
+ if (
106
+ schema["Q:type"] &&
107
+ schema["Q:type"] === "files" &&
108
+ schema["Q:options"] &&
109
+ schema["Q:options"].fileProperties
110
+ ) {
111
+ array.push(Object.assign(value, schema["Q:options"].fileProperties));
112
+ } else {
113
+ array.push(value);
114
+ }
115
+ }
116
+ }
117
+
118
+ const defaultValue = getDefaultOrNull(schema);
119
+ if (array === null && defaultValue !== null) {
120
+ array = defaultValue;
121
+ }
122
+ return array;
123
+ } else if (schema.type === "object") {
124
+ const defaultValue = getDefaultOrNull(schema);
125
+ if (defaultValue !== null) {
126
+ return defaultValue;
127
+ }
128
+
129
+ if (
130
+ schema["Q:type"] &&
131
+ schema["Q:type"] === "files" &&
132
+ schema["Q:options"] &&
133
+ schema["Q:options"].fileProperties
134
+ ) {
135
+ return schema["Q:options"].fileProperties;
136
+ }
137
+
138
+ if (!schema.hasOwnProperty("properties")) {
139
+ return undefined;
140
+ }
141
+ let object = {};
142
+ Object.keys(schema.properties).forEach((propertyName) => {
143
+ const property = schema.properties[propertyName];
144
+ let value = getDefaultItem(property);
145
+ if (value !== undefined) {
146
+ object[propertyName] = value;
147
+ } else if (
148
+ property["Q:type"] &&
149
+ property["Q:type"] === "files" &&
150
+ property["Q:options"] &&
151
+ property["Q:options"].fileProperties
152
+ ) {
153
+ object[propertyName] = property["Q:options"].fileProperties;
154
+ }
155
+ });
156
+ return object;
157
+ }
158
+
159
+ // if this is not an array or object, we just get the default if any
160
+ const defaultValue = getDefaultOrNull(schema);
161
+ if (defaultValue !== null) {
162
+ return defaultValue;
163
+ }
164
+ return undefined;
165
+ }
166
+
167
+ async function updateItem(item, environment, config, qConfigPath) {
168
+ const qServer = config.get(`${environment.name}.qServer`);
169
+ const accessToken = config.get(`${environment.name}.accessToken`);
170
+ const cookie = config.get(`${environment.name}.cookie`);
171
+ const existingItem = await getItem(qServer, environment, accessToken, cookie);
172
+ const updatedItem = await getUpdatedItem(
173
+ qServer,
174
+ accessToken,
175
+ cookie,
176
+ existingItem,
177
+ item,
178
+ environment,
179
+ qConfigPath
180
+ );
181
+ return await saveItem(qServer, environment, accessToken, cookie, updatedItem);
182
+ }
183
+
184
+ async function getUpdatedItem(
185
+ qServer,
186
+ accessToken,
187
+ cookie,
188
+ existingItem,
189
+ item,
190
+ environment,
191
+ qConfigPath
192
+ ) {
193
+ try {
194
+
195
+
196
+
197
+
198
+ const toolSchema = await schemaService.getToolSchema(
199
+ qServer,
200
+ existingItem.tool
201
+ );
202
+ // Removes additional properties not defined in the schema on the top level object of the item
203
+ toolSchema.additionalProperties = false;
204
+ console.log("-----toolSchema-----")
205
+ console.log(JSON.stringify(toolSchema))
206
+ console.log("-----/toolSchema-----")
207
+
208
+ console.log("-----existingItem-----")
209
+ console.log(JSON.stringify(existingItem.customSchema))
210
+ console.log("-----/existingItem-----")
211
+ // If options object is available additional properties not defined in the schema are removed
212
+ if (toolSchema.properties && toolSchema.properties.options) {
213
+ toolSchema.properties.options.additionalProperties = false;
214
+ }
215
+ const defaultItem = getDefaultItem(toolSchema);
216
+ item = JSON.parse(JSON.stringify(item));
217
+ item = await resourcesService.handleResources(
218
+ qServer,
219
+ accessToken,
220
+ cookie,
221
+ item,
222
+ defaultItem,
223
+ qConfigPath,
224
+ environment
225
+ );
226
+
227
+ // Merge options:
228
+ // File of files property will be updated (if file exists on destination)
229
+ // If it doesn't exist it is appended to the files array
230
+ // All other properties are overwritten from source config
231
+ const options = {
232
+ arrayMerge: (destArr, srcArr) => srcArr,
233
+ customMerge: (key) => {
234
+ if (key === "files") {
235
+ return (destArr, srcArr) => {
236
+ if (destArr.length <= 0) {
237
+ return srcArr;
238
+ }
239
+
240
+ srcArr.forEach((fileObj) => {
241
+ let destIndex = destArr.findIndex(
242
+ (destFileObj) =>
243
+ destFileObj.file.originalName === fileObj.file.originalName
244
+ );
245
+
246
+ if (destIndex !== -1) {
247
+ destArr[destIndex] = fileObj;
248
+ } else {
249
+ destArr.push(fileObj);
250
+ }
251
+ });
252
+ return destArr;
253
+ };
254
+ }
255
+ },
256
+ };
257
+
258
+ // merges existing item with the item defined in q.config.json
259
+ const updatedItem = deepmerge(existingItem, item, options);
260
+ // normalizes the item which removes additional properties not defined in the schema
261
+ // and validates the item against the schema
262
+ const normalizedItem = schemaService.getNormalizedItem(
263
+ toolSchema,
264
+ updatedItem,
265
+ environment
266
+ );
267
+ // the normalized item is merged with the existing item. This is done because properties such as _id and _rev
268
+ // defined in the existing item are removed during normalization, because they are not defined in the schema
269
+ return deepmerge(existingItem, normalizedItem, options);
270
+ } catch (error) {
271
+ console.error(errorColor(error.message));
272
+ process.exit(1);
273
+ }
274
+ }
275
+
276
+ async function saveItem(
277
+ qServer,
278
+ environment,
279
+ accessToken,
280
+ cookie,
281
+ updatedItem
282
+ ) {
283
+ try {
284
+ delete updatedItem.updatedDate;
285
+ console.log("updateItem")
286
+ console.log(JSON.stringify(updateItem))
287
+ const response = await fetch(`${qServer}item`, {
288
+ method: "PUT",
289
+ body: JSON.stringify(updatedItem),
290
+ headers: {
291
+ "user-agent": "Q Command-line Tool",
292
+ Authorization: `Bearer ${accessToken}`,
293
+ "Content-Type": "application/json",
294
+ Cookie: cookie ? cookie : "",
295
+ },
296
+ });
297
+ if (response.ok) {
298
+ return await response.json();
299
+ } else {
300
+ throw new Error(
301
+ `A problem occured while saving item with id ${environment.id} on ${environment.name} environment. Please check your connection and try again.`
302
+ );
303
+ }
304
+ } catch (error) {
305
+ console.error(errorColor(error.message));
306
+ process.exit(1);
307
+ }
308
+ }
309
+
310
+ module.exports = {
311
+ createItem: createItem,
312
+ getItem: getItem,
313
+ getItems: getItems,
314
+ updateItem: updateItem,
315
+ };
@@ -0,0 +1,323 @@
1
+ const resourcesService = require("./resourcesService.js");
2
+ const schemaService = require("./schemaService.js");
3
+ const deepmerge = require("deepmerge");
4
+ const fetch = require("node-fetch");
5
+ const chalk = require("chalk");
6
+ const errorColor = chalk.red;
7
+
8
+ async function createItem(item, environment, config) {
9
+ const qServer = config.get(`${environment.name}.qServer`);
10
+ const accessToken = config.get(`${environment.name}.accessToken`);
11
+ const cookie = config.get(`${environment.name}.cookie`);
12
+
13
+ try {
14
+ const response = await fetch(`${qServer}item`, {
15
+ method: "POST",
16
+ body: JSON.stringify(item),
17
+ headers: {
18
+ "user-agent": "Q Command-line Tool",
19
+ Authorization: `Bearer ${accessToken}`,
20
+ "Content-Type": "application/json",
21
+ Cookie: cookie ? cookie : "",
22
+ },
23
+ });
24
+ if (response.ok) {
25
+ return await response.json();
26
+ } else {
27
+ throw new Error(
28
+ `A problem occured while creating item on ${environment.name} environment. Please check your connection and try again.`
29
+ );
30
+ }
31
+ } catch (error) {
32
+ console.error(errorColor(error.message));
33
+ process.exit(1);
34
+ }
35
+ }
36
+
37
+ async function getItem(qServer, environment, accessToken, cookie) {
38
+ try {
39
+ const response = await fetch(`${qServer}item/${environment.id}`, {
40
+ headers: {
41
+ "user-agent": "Q Command-line Tool",
42
+ Authorization: `Bearer ${accessToken}`,
43
+ Cookie: cookie ? cookie : "",
44
+ },
45
+ });
46
+ if (response.ok) {
47
+ return await response.json();
48
+ } else {
49
+ throw new Error(
50
+ `A problem occured while getting item with id ${environment.id} on ${environment.name} environment. Please make sure that the id is correct, you have an internet connection and try again.`
51
+ );
52
+ }
53
+ } catch (error) {
54
+ console.error(errorColor(error.message));
55
+ process.exit(1);
56
+ }
57
+ }
58
+
59
+ function getItems(qConfig, environmentFilter) {
60
+ const items = qConfig.items
61
+ .filter((item) => {
62
+ if (environmentFilter) {
63
+ return item.environments.some(
64
+ (environment) => environment.name === environmentFilter
65
+ );
66
+ }
67
+
68
+ return true;
69
+ })
70
+ .map((item) => {
71
+ if (environmentFilter) {
72
+ item.environments = item.environments.filter(
73
+ (environment) => environment.name === environmentFilter
74
+ );
75
+ }
76
+
77
+ return item;
78
+ });
79
+
80
+ return items;
81
+ }
82
+
83
+ function getDefaultOrNull(schema) {
84
+ if (schema.hasOwnProperty("default")) {
85
+ if (typeof schema.default === "object") {
86
+ return JSON.parse(JSON.stringify(schema.default));
87
+ }
88
+ return schema.default;
89
+ }
90
+ return null;
91
+ }
92
+
93
+ // Returns a default item based on the tool schema
94
+ // The default item is used to derive the file properties of a certain file type
95
+ // These file properties are specified by the tool and are specific to the file type
96
+ // For example an image file has height/width file properties
97
+ function getDefaultItem(schema) {
98
+ schema = JSON.parse(JSON.stringify(schema));
99
+ if (schema.type === "array") {
100
+ let array = [];
101
+ schema.minItems = 1;
102
+ for (let i = 0; i < schema.minItems; i++) {
103
+ let value = getDefaultItem(schema.items);
104
+ if (value) {
105
+ if (
106
+ schema["Q:type"] &&
107
+ schema["Q:type"] === "files" &&
108
+ schema["Q:options"] &&
109
+ schema["Q:options"].fileProperties
110
+ ) {
111
+ array.push(Object.assign(value, schema["Q:options"].fileProperties));
112
+ } else {
113
+ array.push(value);
114
+ }
115
+ }
116
+ }
117
+
118
+ const defaultValue = getDefaultOrNull(schema);
119
+ if (array === null && defaultValue !== null) {
120
+ array = defaultValue;
121
+ }
122
+ return array;
123
+ } else if (schema.type === "object") {
124
+ const defaultValue = getDefaultOrNull(schema);
125
+ if (defaultValue !== null) {
126
+ return defaultValue;
127
+ }
128
+
129
+ if (
130
+ schema["Q:type"] &&
131
+ schema["Q:type"] === "files" &&
132
+ schema["Q:options"] &&
133
+ schema["Q:options"].fileProperties
134
+ ) {
135
+ return schema["Q:options"].fileProperties;
136
+ }
137
+
138
+ if (!schema.hasOwnProperty("properties")) {
139
+ return undefined;
140
+ }
141
+ let object = {};
142
+ Object.keys(schema.properties).forEach((propertyName) => {
143
+ const property = schema.properties[propertyName];
144
+ let value = getDefaultItem(property);
145
+ if (value !== undefined) {
146
+ object[propertyName] = value;
147
+ } else if (
148
+ property["Q:type"] &&
149
+ property["Q:type"] === "files" &&
150
+ property["Q:options"] &&
151
+ property["Q:options"].fileProperties
152
+ ) {
153
+ object[propertyName] = property["Q:options"].fileProperties;
154
+ }
155
+ });
156
+ return object;
157
+ }
158
+
159
+ // if this is not an array or object, we just get the default if any
160
+ const defaultValue = getDefaultOrNull(schema);
161
+ if (defaultValue !== null) {
162
+ return defaultValue;
163
+ }
164
+ return undefined;
165
+ }
166
+
167
+ async function updateItem(item, environment, config, qConfigPath) {
168
+ const qServer = config.get(`${environment.name}.qServer`);
169
+ const accessToken = config.get(`${environment.name}.accessToken`);
170
+ const cookie = config.get(`${environment.name}.cookie`);
171
+ const existingItem = await getItem(qServer, environment, accessToken, cookie);
172
+ const updatedItem = await getUpdatedItem(
173
+ qServer,
174
+ accessToken,
175
+ cookie,
176
+ existingItem,
177
+ item,
178
+ environment,
179
+ qConfigPath
180
+ );
181
+ return await saveItem(qServer, environment, accessToken, cookie, updatedItem);
182
+ }
183
+
184
+ async function getUpdatedItem(
185
+ qServer,
186
+ accessToken,
187
+ cookie,
188
+ existingItem,
189
+ item,
190
+ environment,
191
+ qConfigPath
192
+ ) {
193
+ try {
194
+
195
+
196
+
197
+
198
+ const toolSchema = await schemaService.getToolSchema(
199
+ qServer,
200
+ existingItem.tool
201
+ );
202
+ // Removes additional properties not defined in the schema on the top level object of the item
203
+ toolSchema.additionalProperties = false;
204
+
205
+ if(existingItem.customSchema) {
206
+ toolSchema.properties = {...toolSchema.properties, ...existingItem.customSchema}
207
+ }
208
+
209
+ if(existingItem.customSchema) {
210
+ toolSchema.definitions = {...toolSchema.definitions, ...existingItem.customSchema}
211
+ }
212
+ console.log("-----toolSchema-----")
213
+ console.log(JSON.stringify(toolSchema))
214
+ console.log("-----/toolSchema-----")
215
+
216
+ console.log("-----existingItem-----")
217
+ console.log(JSON.stringify(existingItem.customSchema))
218
+ console.log("-----/existingItem-----")
219
+ // If options object is available additional properties not defined in the schema are removed
220
+ if (toolSchema.properties && toolSchema.properties.options) {
221
+ toolSchema.properties.options.additionalProperties = false;
222
+ }
223
+ const defaultItem = getDefaultItem(toolSchema);
224
+ item = JSON.parse(JSON.stringify(item));
225
+ item = await resourcesService.handleResources(
226
+ qServer,
227
+ accessToken,
228
+ cookie,
229
+ item,
230
+ defaultItem,
231
+ qConfigPath,
232
+ environment
233
+ );
234
+
235
+ // Merge options:
236
+ // File of files property will be updated (if file exists on destination)
237
+ // If it doesn't exist it is appended to the files array
238
+ // All other properties are overwritten from source config
239
+ const options = {
240
+ arrayMerge: (destArr, srcArr) => srcArr,
241
+ customMerge: (key) => {
242
+ if (key === "files") {
243
+ return (destArr, srcArr) => {
244
+ if (destArr.length <= 0) {
245
+ return srcArr;
246
+ }
247
+
248
+ srcArr.forEach((fileObj) => {
249
+ let destIndex = destArr.findIndex(
250
+ (destFileObj) =>
251
+ destFileObj.file.originalName === fileObj.file.originalName
252
+ );
253
+
254
+ if (destIndex !== -1) {
255
+ destArr[destIndex] = fileObj;
256
+ } else {
257
+ destArr.push(fileObj);
258
+ }
259
+ });
260
+ return destArr;
261
+ };
262
+ }
263
+ },
264
+ };
265
+
266
+ // merges existing item with the item defined in q.config.json
267
+ const updatedItem = deepmerge(existingItem, item, options);
268
+ // normalizes the item which removes additional properties not defined in the schema
269
+ // and validates the item against the schema
270
+ const normalizedItem = schemaService.getNormalizedItem(
271
+ toolSchema,
272
+ updatedItem,
273
+ environment
274
+ );
275
+ // the normalized item is merged with the existing item. This is done because properties such as _id and _rev
276
+ // defined in the existing item are removed during normalization, because they are not defined in the schema
277
+ return deepmerge(existingItem, normalizedItem, options);
278
+ } catch (error) {
279
+ console.error(errorColor(error.message));
280
+ process.exit(1);
281
+ }
282
+ }
283
+
284
+ async function saveItem(
285
+ qServer,
286
+ environment,
287
+ accessToken,
288
+ cookie,
289
+ updatedItem
290
+ ) {
291
+ try {
292
+ delete updatedItem.updatedDate;
293
+ console.log("updateItem")
294
+ console.log(JSON.stringify(updateItem))
295
+ const response = await fetch(`${qServer}item`, {
296
+ method: "PUT",
297
+ body: JSON.stringify(updatedItem),
298
+ headers: {
299
+ "user-agent": "Q Command-line Tool",
300
+ Authorization: `Bearer ${accessToken}`,
301
+ "Content-Type": "application/json",
302
+ Cookie: cookie ? cookie : "",
303
+ },
304
+ });
305
+ if (response.ok) {
306
+ return await response.json();
307
+ } else {
308
+ throw new Error(
309
+ `A problem occured while saving item with id ${environment.id} on ${environment.name} environment. Please check your connection and try again.`
310
+ );
311
+ }
312
+ } catch (error) {
313
+ console.error(errorColor(error.message));
314
+ process.exit(1);
315
+ }
316
+ }
317
+
318
+ module.exports = {
319
+ createItem: createItem,
320
+ getItem: getItem,
321
+ getItems: getItems,
322
+ updateItem: updateItem,
323
+ };