UNPKG

4.59 kBJavaScriptView Raw
1/**
2 * Copyright (c) 2018, Kinvey, Inc. All rights reserved.
3 *
4 * This software is licensed to you under the Kinvey terms of service located at
5 * http://www.kinvey.com/terms-of-use. By downloading, accessing and/or using this
6 * software, you hereby accept such terms of service (and any agreement referenced
7 * therein) and agree that you have read, understand and agree to be bound by such
8 * terms of service and are of legal age to agree to such terms with Kinvey.
9 *
10 * This software contains valuable confidential and proprietary information of
11 * KINVEY, INC and is subject to applicable licensing agreements.
12 * Unauthorized reproduction, transmission or distribution of this file and its
13 * contents is a violation of applicable laws.
14 */
15
16const consoleTable = require('console.table');
17
18const { EOL } = require('os');
19
20const { OperationMessage, OperationType, OutputFormat } = require('./Constants');
21const { isEmpty, isNullOrUndefined, isStringWhitespace } = require('./Utils');
22
23/**
24 * Defines the result from the successful execution of a command.
25 */
26class CommandResult {
27 /**
28 * Creates CommandResult object.
29 */
30 constructor() {
31 this.rawData = null;
32 this.tableData = null;
33 this.humanReadableFormat = {
34 userFriendlyMsg: ''
35 };
36 }
37
38 /**
39 * Sets raw data that will be used for building formatted result as JSON or to construct a table.
40 * @param data
41 * @returns {CommandResult}
42 */
43 setRawData(data) {
44 this.rawData = data;
45 return this;
46 }
47
48 _getTableData() {
49 return this.tableData || this.rawData;
50 }
51
52 /**
53 * Set table data that will be used to build a table.
54 * @param {Array|Object} data
55 * @returns {CommandResult}
56 */
57 setTableData(data) {
58 this.tableData = data;
59 return this;
60 }
61
62 /**
63 * Get human-readable result: either table or a basic string.
64 * @returns {String}
65 * @private
66 */
67 _getHumanReadableResult() {
68 if (this._humanReadableFormatIsTable()) {
69 const tableData = this._getTableData();
70 const formattedTableData = consoleTable.getTable(tableData);
71 return Array.isArray(tableData) ? `Count: ${tableData.length}${EOL}${EOL}${formattedTableData}` : formattedTableData;
72 }
73
74 return this._getUserFriendlyMsg();
75 }
76
77 /**
78 * Get result in JSON format: { "result": data }
79 * @private
80 */
81 _getJSONResult() {
82 const result = {
83 result: this.rawData
84 };
85
86 const stringifiedResult = JSON.stringify(result, null, 2);
87 return stringifiedResult;
88 }
89
90 /**
91 * Get result in the chosen format.
92 * @param {Constants.OutputFormat} format
93 * @returns {*}
94 */
95 getFormattedResult(format) {
96 let result;
97
98 if (format === OutputFormat.JSON) {
99 result = this._getJSONResult();
100 } else {
101 result = this._getHumanReadableResult();
102 }
103
104 return result;
105 }
106
107 _getUserFriendlyMsg() {
108 return this.humanReadableFormat.userFriendlyMsg;
109 }
110
111 /**
112 * Sets a basic user-friendly message based on the operation type and entity.
113 * @param {Constants.OperationType} operationType
114 * @param {Constants.EntityType} entityType
115 * @param [entityId]
116 */
117 setBasicMsg(operationType, entityType, entityId) {
118 const id = entityId || (this.rawData && this.rawData.id);
119 const skipId = operationType === OperationType.SAVE || isNullOrUndefined(id);
120 const msgLastPart = skipId ? '.' : `: ${id}`;
121 this.humanReadableFormat.userFriendlyMsg = `${OperationMessage[operationType]} ${entityType}${msgLastPart}`;
122 return this;
123 }
124
125 /**
126 * Appends an additional message to the user friendly message.
127 * @param {String} msg
128 * @param {String} [separator]
129 */
130 setAdditionalMsg(msg, separator = EOL) {
131 this.humanReadableFormat.userFriendlyMsg = `${this.humanReadableFormat.userFriendlyMsg}${separator}${msg}`;
132 return this;
133 }
134
135 /**
136 * Sets a custom user-friendly message.
137 * @param {String} msg
138 * @returns {CommandResult}
139 */
140 setCustomMsg(msg) {
141 this.humanReadableFormat.userFriendlyMsg = msg;
142 return this;
143 }
144
145 /**
146 * Returns true if userFriendlyMsg is not available or if there is sufficient data to build a table. Otherwise, false.
147 * @returns {boolean}
148 */
149 _humanReadableFormatIsTable() {
150 const isTable = isStringWhitespace(this.humanReadableFormat.userFriendlyMsg) && (!isNullOrUndefined(this.tableData) || !isNullOrUndefined(this.rawData));
151 return isTable;
152 }
153}
154
155module.exports = CommandResult;