-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathUserInteractor.js
544 lines (495 loc) · 24 KB
/
UserInteractor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
// MIT License
//
// Copyright 2018 Electric Imp
//
// SPDX-License-Identifier: MIT
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
// EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
'use strict';
const ImpCentralApi = require('imp-central-api');
const DeviceGroups = ImpCentralApi.DeviceGroups;
const Prompt = require('cli-prompt');
const Util = require('util');
const CliSpinner = require('cli-spinner').Spinner;
const Colors = require('colors/safe');
const Errors = require('./Errors');
const Config = require('./Config');
const Identifier = require('./Identifier');
const JsonFormatter = require('./JsonFormatter');
const SPINNER_TEXT = 'Contacting the impCentral...';
const PRINTED_ENTITIES_LIMIT = 5;
const JSON_VALUE_MAX_LENGTH = 512;
const SUCCESS_COMMAND_STATUS = 'IMPT COMMAND SUCCEEDS';
const FAIL_COMMAND_STATUS = 'IMPT COMMAND FAILS';
const FAIL_EXIT_CODE = 1;
const _PRODUCT = `${Identifier.ENTITY_TYPE.TYPE_PRODUCT}`;
const _DEVICE_GROUP = `${Identifier.ENTITY_TYPE.TYPE_DEVICE_GROUP}`;
const _DEVICE_GROUPS = `${_DEVICE_GROUP}s`;
const _DEVICE = `${Identifier.ENTITY_TYPE.TYPE_DEVICE}`;
const _DEVICES = `${_DEVICE}s`;
const _DEPLOYMENT = `${Identifier.ENTITY_TYPE.TYPE_BUILD}`;
const _DEPLOYMENTS = `${_DEPLOYMENT}s`;
const _MIN_SUPPORTED_DEPLOYMENT = `Min supported ${_DEPLOYMENT}`;
const MESSAGES = {
LOGIN_SUCCESSFUL : '%s login is successful.',
LOGOUT_SUCCESSFUL : '%s logout is successful.',
ENTITY_CREATED : '%s is created successfully.',
ENTITY_UPDATED : '%s is updated successfully.',
ENTITY_DELETED : '%s is deleted successfully.',
ENTITY_REMOVED : '%s is removed successfully.',
ENTITY_INFO : '%s info:',
ENTITY_LIST : '%s list (%d items):',
ENTITY_EMPTY_LIST : 'No %ss are found.',
FILES_DELETED : 'Device/agent source files %s are deleted successfully.',
DIR_DELETED : '%s %s directory is deleted successfully.',
PROJECT_LINKED : 'Project is linked successfully.',
CONFIG_EXISTS_CURR_DIR : 'The current directory already contains %s File. It will be overwritten.',
CONFIG_EXISTS : '%s File %s already exists. It will be overwritten.',
AUTH_FILE_EXISTS : 'The %s Auth File already exists. It will be overwritten.',
CHOICES_CONTINUE_QUESTION : 'Are you sure you want to continue?',
CHOICES_OPERATION_CANCELED : 'Operation is canceled.',
CHOICES_PROMPT : "Enter 'y' (yes) or 'n' (no): ",
CHOICES_INVALID_VALUE : 'Invalid value!',
RELOGIN_MSG : 'Please %s using "%s" command and then try again.',
DELETE_CONFIG_MSG : 'Use "%s" command to delete the outdated %s.',
NO_CONFIG_CURR_DIR_MSG : 'There is no %s File in the current directory.',
NO_CONFIG_MSG : '%s File %s is not found.',
DG_NO_DEVICES : `%s does not have assigned ${_DEVICES}.`,
DG_DEVICES_RESTARTED : `Restart request for ${_DEVICES} assigned to %s is successful:`,
DG_DEVICES_COND_RESTARTED : `Conditional restart request for ${_DEVICES} assigned to %s is successful:`,
DG_DEVICES_UNASSIGNED : `The following ${_DEVICES} are unassigned successfully from %s:`,
DG_DEVICES_REASSIGNED : `The following ${_DEVICES} assigned to %s are reassigned successfully to %s:`,
DG_NO_FLAGGED_BUILDS : `No "flagged" ${_DEPLOYMENTS} are found for %s.`,
DG_NO_BUILDS_TO_DELETE : `No ${_DEPLOYMENTS} that can be deleted are found for %s.`,
DG_MIN_SUPPORTED_DEPLOYMENT_UPDATED : `${_MIN_SUPPORTED_DEPLOYMENT} for %s is updated successfully.`,
DEVICE_ASSIGNED_TO_DG : '%s is assigned successfully to %s.',
DEVICE_ALREADY_ASSIGNED_TO_DG : '%s is already assigned to %s.',
DEVICE_REASSIGN : `%s is assigned to another ${_DEVICE_GROUP}. It will be reassigned to %s.`,
DEVICE_RESTARTED : 'Restart request for %s is successful.',
DEVICE_COND_RESTARTED : 'Conditional restart request for %s is successful.',
DEVICE_UNASSIGNED : '%s is unassigned successfully.',
DEVICE_ALREADY_UNASSIGNED : '%s is already unassigned.',
BUILD_COPIED : '%s is copied successfully to %s.',
BUILD_DOWNLOADED : '%s source file(s) are downloaded successfully.',
BUILD_SOURCE_FILES_EXIST : 'Source file(s) %s already exist. They will be overwritten.',
BUILD_RUN : '%s is run successfully.',
BUILD_DEPLOY_FILE_NOT_SPECIFIED : '--%s option is not specified, empty %s code will be deployed.',
LOG_GET_NEXT : 'Press <Enter> to see more logs or <Ctrl-C> to exit.',
LOG_GET_FINISHED : 'No more logs are available.',
LOG_STREAM_OPENED : `Log stream for ${_DEVICE}(s) %s is opened.`,
LOG_STREAM_EXIT : 'Press <Ctrl-C> to exit.',
LOG_STREAM_CLOSED : 'The log stream is closed.',
LOG_STREAM_RECONNECT : 'The log stream is lost. Trying to reconnect...',
DELETE_ENTITIES : 'The following entities will be deleted:',
DELETE_DEVICES_UNASSIGNED : `The following ${_DEVICES} will be unassigned from ${_DEVICE_GROUPS}:`,
DELETE_DEVICES_REMOVED : `The following ${_DEVICES} will be removed from the account:`,
DELETE_BUILDS_FLAGGED : `The following ${_DEPLOYMENTS} are marked "flagged" to prevent deleting. They will be modified by setting "flagged" attribute to false:`,
DELETE_FILES : 'The following files will be deleted:',
DELETE_DIR : '%s %s directory will be deleted.',
DELETE_CONFIG_FILE_CURR_DIR : '%s File in the current directory will be deleted.',
DELETE_CONFIG_FILE : '%s File %s will be deleted.',
TREE_MORE_ENTITIES : '... %d more %s(s)',
AND_MORE_ENTITIES : 'And %d more %s(s).',
};
const ERRORS = {
CMD_REQUIRED_OPTIONS : 'Either "--%s" or "--%s" option must be specified.',
CMD_MUTUALLY_EXCLUSIVE_OPTIONS : 'Options "--%s" and "--%s" are mutually exclusive.',
CMD_REQUIRED_OPTION : 'Option "--%s" must be specified.',
CMD_COOPERATIVE_OPTIONS : 'Options "--%s" and "--%s" must be specified together.',
CMD_COOPERATIVE_MULT_OPTIONS : 'Options "--%s" or "--%s" must be specified together with "--%s".',
CMD_TARGET_REQUIRED : `Option "--%s" must be specified for ${_DEVICE_GROUP} of type "%s".`,
CMD_UNKNOWN_ARGS : 'Unknown arguments: %s.',
CMD_NUMBER_REQUIRED : 'Option "--%s" must be a number.',
CMD_POSITIVE_INT_REQUIRED : 'Option "--%s" must have a positive integer value.',
CMD_INCORRECT_VALUE : 'Option "--%s" has incorrect value "%s".',
CMD_MULTIPLE_OPTION : 'Option "--%s" must not be specified multiple times.',
CMD_UNKNOWN : 'Please specify a valid command.',
CMD_IMPOSSIBLE_TO_EXECUTE : 'Impossible to execute the command.',
NOT_LOGGED_IN : 'You are not logged in.',
REFRESH_TOKEN_ERR : 'Impossible to refresh access token for the %s Auth File.',
ACCESS_TOKEN_EXPIRED : 'Access token is expired for the %s Auth File. Information required to refresh access token is not saved.',
CONFIG_NOT_FOUND : '%s File is not found.',
CONFIG_CORRUPTED : '%s File is corrupted.',
CONFIG_NOT_FOUND_CURR_DIR : '%s File is not found in the current directory.',
CONFIG_OUTDATED : '%s, saved in %s File, does not exist anymore.',
ACCESS_FAILED : 'Access to impCentral failed or timed out. Please check your network connection.',
PROJECT_LINK_WRONG_DG_TYPE : `Project can not be linked to ${_DEVICE_GROUP} of the type "%s". Only "%s" or "%s" ${_DEVICE_GROUPS} are allowed.`,
TARGET_DG_NOT_FOUND : 'Production target %s is not found.',
WRONG_DG_TYPE_FOR_OPTION : `Invalid option "--%s" for ${_DEVICE_GROUP} of the type "%s". It may be specified for "%s" or "%s" ${_DEVICE_GROUPS} only.`,
TARGET_DG_WRONG_TYPE : 'Production target %s is of the type "%s" but must be of the type "%s".',
TARGET_DG_WRONG_PRODUCT : `Production target ${_DEVICE_GROUP} must be from the same ${_PRODUCT}.`,
DG_WRONG_MIN_SUPPORTED_DEPLOYMENT : `%s does not belong to %s. Impossible to set it as ${_MIN_SUPPORTED_DEPLOYMENT}.`,
DG_OLD_MIN_SUPPORTED_DEPLOYMENT : `${_MIN_SUPPORTED_DEPLOYMENT} cannot be set to an earlier ${_DEPLOYMENT} than the current ${_MIN_SUPPORTED_DEPLOYMENT}.`,
BUILD_DELETE_ERR : `Impossible to delete %s. It is the ${_MIN_SUPPORTED_DEPLOYMENT} or newer for %s.`,
BUILD_DEPLOY_FILE_NOT_FOUND : 'File with IMP %s source code "%s" is not found.',
MULTIPLE_IDENTIFIERS_FOUND : 'Multiple %ss "%s" are found:',
MULTIPLE_IDS_FOUND : 'Multiple %ss are found:',
NO_IDENTIFIER : 'No %s Identifier is specified.',
ENTITY_NOT_FOUND : '%s "%s" is not found.',
ENTITY_CORRUPTED : '%s. %s is related to corrupted %s which does not exist.',
FILE_ERROR : 'File error: %s: %s.',
FILE_NOT_FOUND : 'file is not found',
INTERNAL_ERROR : 'Internal error: %s.',
LOG_STREAM_UNEXPECTED_MSG : 'Unexpected format of log stream message: %s',
UNEXPECTED_STATE : 'Unexpected state'
};
const PRINT_FORMAT_PRETTY = 'PRINT_FORMAT_PRETTY';
const PRINT_FORMAT_JSON = 'PRINT_FORMAT_JSON';
let _PRINT_FORMAT = PRINT_FORMAT_PRETTY
// Helper class for interactions with a user.
class UserInteractor {
static get PRINT_FORMAT() {
return _PRINT_FORMAT
}
static set PRINT_FORMAT(printFormat) {
if(printFormat != PRINT_FORMAT_PRETTY && printFormat != PRINT_FORMAT_JSON){
throw(`ERROR in set UserInteractor.PRINT_FORMAT. printFormat=${printFormat} and is not one of [UserInteractor.PRINT_FORMAT_PRETTY, UserInteractor.PRINT_FORMAT_JSON]`)
}
_PRINT_FORMAT = printFormat
}
static get PRINT_FORMAT_PRETTY() {
return PRINT_FORMAT_PRETTY;
}
static get PRINT_FORMAT_JSON() {
return PRINT_FORMAT_JSON;
}
static get MESSAGES() {
return MESSAGES;
}
static get ERRORS() {
return ERRORS;
}
static get PRINTED_ENTITIES_LIMIT() {
return PRINTED_ENTITIES_LIMIT;
}
static printError(message, ...args) {
UserInteractor.spinnerStop();
console.log('ERROR: ' + message, ...args);
}
static printErrorWithStatus(message, ...args) {
UserInteractor.printError(message, ...args);
UserInteractor.printErrorStatus();
}
static printErrorStatus() {
UserInteractor.printMessage(FAIL_COMMAND_STATUS);
process.exit(FAIL_EXIT_CODE);
}
static printMessage(message, ...args) {
UserInteractor.spinnerStop();
if(UserInteractor.PRINT_FORMAT == PRINT_FORMAT_PRETTY){
console.log(message, ...args);
}
}
static printMessageWithStatus(message, ...args) {
UserInteractor.printMessage(message, ...args);
UserInteractor.printSuccessStatus();
}
static printSuccessStatus() {
UserInteractor.printMessage(SUCCESS_COMMAND_STATUS);
}
static printAlert(message, ...args) {
UserInteractor.spinnerStop();
message = Colors.red(message);
console.log(message, ...args);
}
static jsonDataValueFormatter(value) {
if (typeof(value) === 'string' && value.length > JSON_VALUE_MAX_LENGTH) {
return value.substring(0, JSON_VALUE_MAX_LENGTH) + '\n...';
}
if (Array.isArray(value) && value.length === 0) {
return '';
}
if (value === null || value === undefined) {
return '';
}
return value;
}
static corruptedJsonData(data) {
if (Array.isArray(data)) {
return data.map(elem => UserInteractor.corruptedJsonData(elem));
}
else if (typeof data === 'object') {
let result = {};
for (let key in data) {
result[UserInteractor.corruptedJsonData(key)] = UserInteractor.corruptedJsonData(data[key]);
}
return result;
}
return Colors.red(data);
}
static printJsonData(data) {
UserInteractor.spinnerStop();
if (!data || Array.isArray(data) && data.length === 0 || Object.keys(data).length === 0) {
return;
}
if(UserInteractor.PRINT_FORMAT == PRINT_FORMAT_PRETTY){
const jsonFormatter = new JsonFormatter();
console.log(jsonFormatter.render(data));
}
else { //if(UserInteractor.PRINT_FORMAT == PRINT_FORMAT_JSON){
console.log(JSON.stringify(data,null,'\t'))
}
}
static processError(error) {
UserInteractor.spinnerStop();
if (UserInteractor.debug) {
UserInteractor.printMessage(error);
}
const globalExecutableName = require('./Options').globalExecutableName;
if (error instanceof Errors.InternalLibraryError) {
UserInteractor.printErrorWithStatus(UserInteractor.ERRORS.INTERNAL_ERROR,
error.message || UserInteractor.ERRORS.UNEXPECTED_STATE);
}
else if (error instanceof Errors.RefreshTokenError) {
UserInteractor.printError(UserInteractor.ERRORS.REFRESH_TOKEN_ERR, error._authConfig.location);
UserInteractor.reloginMessage(error._authConfig);
UserInteractor.printErrorStatus();
}
else if (error instanceof Errors.AccessTokenExpiredError) {
UserInteractor.printError(UserInteractor.ERRORS.ACCESS_TOKEN_EXPIRED, error._authConfig.location);
UserInteractor.reloginMessage(error._authConfig);
UserInteractor.printErrorStatus();
}
else if (error instanceof Errors.EntityNotFoundError) {
UserInteractor.printErrorWithStatus(UserInteractor.ERRORS.ENTITY_NOT_FOUND, error._entityType, error._identifier);
}
else if (error instanceof Errors.CorruptedRelatedEntityError) {
UserInteractor.printError(UserInteractor.ERRORS.ENTITY_CORRUPTED,
UserInteractor.ERRORS.UNEXPECTED_STATE, error._relatedIdentifier, error._identifier);
}
else if (error instanceof Errors.OutdatedConfigEntityError) {
UserInteractor.printError(UserInteractor.ERRORS.CONFIG_OUTDATED, error._identifier, error._configType);
UserInteractor.printMessage(UserInteractor.ERRORS.CMD_IMPOSSIBLE_TO_EXECUTE);
UserInteractor.deleteOutdatedConfigMessage(error._configType);
UserInteractor.printErrorStatus();
}
else if (error instanceof Errors.NotUniqueIdentifierError) {
if (error._identifier) {
UserInteractor.printError(UserInteractor.ERRORS.MULTIPLE_IDENTIFIERS_FOUND, error._entityType, error._identifier);
}
else {
UserInteractor.printError(UserInteractor.ERRORS.MULTIPLE_IDS_FOUND, error._entityType);
}
UserInteractor.printJsonData(error._entitiesInfo);
UserInteractor.printMessage(UserInteractor.ERRORS.CMD_IMPOSSIBLE_TO_EXECUTE);
UserInteractor.printErrorStatus();
}
else if (error instanceof Errors.NoIdentifierError) {
UserInteractor.printErrorWithStatus(UserInteractor.ERRORS.NO_IDENTIFIER, error._entityType);
}
else if (error instanceof Errors.NoOptionError) {
UserInteractor.printErrorWithStatus(UserInteractor.ERRORS.CMD_REQUIRED_OPTION, error._option);
}
else if (error instanceof Errors.NoConfigError) {
if (error._config.type === Config.TYPE.AUTH) {
if (error._isLogout) {
UserInteractor.printError(UserInteractor.ERRORS.CONFIG_NOT_FOUND, error._config.info);
}
else {
UserInteractor.printError(UserInteractor.ERRORS.NOT_LOGGED_IN);
UserInteractor.reloginMessage(error._config);
}
}
else {
UserInteractor.printError(UserInteractor.ERRORS.CONFIG_NOT_FOUND_CURR_DIR, error._config.type);
}
UserInteractor.printErrorStatus();
}
else if (error instanceof Errors.CorruptedConfigError) {
UserInteractor.printError(UserInteractor.ERRORS.CONFIG_CORRUPTED, error._config.info);
if (error._config.type === Config.TYPE.AUTH) {
UserInteractor.reloginMessage(error._config);
}
UserInteractor.printErrorStatus();
}
else if (error instanceof Errors.FileError) {
UserInteractor.printErrorWithStatus(UserInteractor.ERRORS.FILE_ERROR, error._fileName, error.message);
}
else if (error instanceof Errors.OperationCanceled) {
UserInteractor.printMessage(UserInteractor.MESSAGES.CHOICES_OPERATION_CANCELED);
UserInteractor.printErrorStatus();
}
else if (error instanceof ImpCentralApi.Errors.ImpCentralApiError) {
UserInteractor.printImpCentralApiError(error);
UserInteractor.printErrorStatus();
}
else if (error instanceof ImpCentralApi.Errors.InvalidDataError) {
const message = error.message;
if (message.indexOf('ENOENT') >= 0 || message.indexOf('ETIMEDOUT') >= 0 ||
message.indexOf('ECONNRESET') >= 0) {
UserInteractor.printErrorWithStatus(UserInteractor.ERRORS.ACCESS_FAILED);
}
else {
UserInteractor.printErrorWithStatus("%s.", message);
}
}
else {
UserInteractor.printErrorWithStatus(error.message);
}
}
static printImpCentralApiError(error) {
if (error.body && error.body.hasOwnProperty('errors')) {
UserInteractor._printErrorsAndWarnings(error.body.errors);
}
else {
UserInteractor.printError(error.message);
}
}
static printResponseMeta(response) {
if (response && response.hasOwnProperty('data') && response.data.hasOwnProperty('meta')) {
UserInteractor._printErrorsAndWarnings(response.data.meta);
}
}
static _printErrorsAndWarnings(entities) {
if (Array.isArray(entities)) {
entities.forEach((entity) => {
if (['title', 'detail', 'status'].every(prop => entity.hasOwnProperty(prop))) {
const message = entity.title + ': ' + entity.detail;
if (UserInteractor._isError(entity)) {
UserInteractor.printError(message);
}
else {
UserInteractor.printMessage(message);
}
}
UserInteractor._printMeta(entity.meta);
});
}
}
static _printMeta(metaData) {
if (Array.isArray(metaData)) {
for (let meta of metaData) {
if (['row', 'column', 'text', 'type', 'file'].every(prop => meta.hasOwnProperty(prop))) {
UserInteractor.printMessage('%s %s (line %d, column %d): %s', meta.file, meta.type, meta.row, meta.column, meta.text);
}
}
}
}
static _isError(entity) {
const status = parseInt(entity.status);
return (status < 200 || status >= 300);
}
static reloginMessage(authConfig) {
const Options = require('./Options');
const AuthConfig = require('./AuthConfig');
const globalExecutableName = Options.globalExecutableName;
let loginCommand = Util.format('%s auth login', globalExecutableName);
if (authConfig.location !== AuthConfig.LOCATION.GLOBAL) {
let localOption = Util.format('--%s', Options.LOCAL);
if (authConfig.location === AuthConfig.LOCATION.ANY) {
localOption = Util.format('[%s]', localOption);
}
loginCommand = Util.format('%s %s', loginCommand, localOption);
}
const action = authConfig.exists() ? 'relogin' : 'login';
UserInteractor.printMessage(UserInteractor.MESSAGES.RELOGIN_MSG, action, loginCommand);
}
static deleteOutdatedConfigMessage(configType) {
const globalExecutableName = require('./Options').globalExecutableName;
const configDeleteCmd = Util.format('%s %s delete',
globalExecutableName, configType === Config.TYPE.PROJECT ? 'project' : 'test');
UserInteractor.printMessage(UserInteractor.MESSAGES.DELETE_CONFIG_MSG,
configDeleteCmd, configType);
}
static printEntities(entities) {
if (entities.length > 0) {
const entitiesToPrint = entities.slice(0, PRINTED_ENTITIES_LIMIT);
UserInteractor.printJsonData(entitiesToPrint.map(entity => entity.displayData));
if (entitiesToPrint.length < entities.length) {
UserInteractor.printMessage(UserInteractor.MESSAGES.AND_MORE_ENTITIES,
entities.length - entitiesToPrint.length, entities[0].entityType);
}
}
}
static _callChoiceHandler(handler, handlersParams) {
if (Array.isArray(handlersParams)) {
return handler(...handlersParams);
}
else {
return handler(handlersParams);
}
}
static _cancelOperation() {
return Promise.reject(new Errors.OperationCanceled());
}
static processCancelContinueChoice(message, continueChoiceHandler, continueChoiceHandlerParams, confirmed, printMsg = true) {
if (confirmed) {
return UserInteractor._callChoiceHandler(continueChoiceHandler, continueChoiceHandlerParams);
}
else {
return new Promise((resolve, reject) => {
if (printMsg) {
if (message) {
UserInteractor.printMessage(message);
}
UserInteractor.printMessage(UserInteractor.MESSAGES.CHOICES_CONTINUE_QUESTION);
}
Prompt(UserInteractor.MESSAGES.CHOICES_PROMPT, function(value) {
UserInteractor.printMessage('');
const val = value.trim().toLowerCase();
if (val === 'y') {
resolve(UserInteractor._callChoiceHandler(continueChoiceHandler, continueChoiceHandlerParams));
}
else if (val === 'n') {
resolve(UserInteractor._cancelOperation());
}
else {
UserInteractor.printMessage(UserInteractor.MESSAGES.CHOICES_INVALID_VALUE);
resolve(UserInteractor.processCancelContinueChoice(
message, continueChoiceHandler, continueChoiceHandlerParams, confirmed, false));
}
});
});
}
}
static get spinner() {
if (!UserInteractor._spinner) {
UserInteractor._spinner = new CliSpinner(SPINNER_TEXT);
UserInteractor._spinner.setSpinnerString('|/-\\');
}
return UserInteractor._spinner;
}
static enableSpinner(value) {
UserInteractor._enableSpinner = value;
}
static spinnerStart() {
if (UserInteractor._enableSpinner && !UserInteractor.spinner.isSpinning()) {
if(UserInteractor.PRINT_FORMAT == PRINT_FORMAT_PRETTY){
UserInteractor.spinner.start();
}
}
}
static spinnerStop() {
if (UserInteractor.spinner.isSpinning()) {
UserInteractor.spinner.stop(true);
}
}
static get debug() {
return UserInteractor._debug;
}
static set debug(value) {
UserInteractor._debug = value;
if (value) {
UserInteractor.enableSpinner(false);
}
}
}
UserInteractor._enableSpinner = true;
UserInteractor._debug = false;
module.exports = UserInteractor;