skysync-cli
Version:
SkySync Command Line Interface
229 lines (228 loc) • 8.15 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OutputFormatter = void 0;
const cliff = require("clifflite");
const dot = require("dot-object");
const sdk_1 = require("../sdk");
const arrayIndicator = '[]';
class OutputFormatter {
constructor(formatOptions) {
this.formatOptions = formatOptions;
if (!this.formatOptions.tabSize) {
this.formatOptions.tabSize = 2;
}
}
get outputJson() {
return this.formatOptions.outputJson;
}
writeTable(obj, options) {
this.write(obj, options, true);
}
writeAllPages(func, params, options) {
return __awaiter(this, void 0, void 0, function* () {
let isFirst = true;
yield sdk_1.consumePagedResult(item => {
if (this.outputJson) {
if (isFirst) {
console.log('[');
console.log();
}
const json = copyJson(item, options);
console.log(JSON.stringify(json, null, this.formatOptions.tabSize));
}
else {
console.log(this.format([item], options, true, isFirst));
}
isFirst = false;
}, func, params);
if (isFirst) {
this.write([], options, true);
}
else if (this.outputJson) {
console.log(']');
}
});
}
writeItem(obj, options) {
this.write(obj, options, false);
}
writeSuccess(message, force = false) {
if (!this.outputJson || force) {
console.log(message.green);
}
}
writeFailure(message, force = false) {
if (!this.outputJson || force) {
console.error(message.red);
}
}
writeWarning(message, force = false) {
if (!this.outputJson || force) {
console.warn(message.yellow);
}
}
writeText(output) {
console.log(output);
}
write(obj, options, asTable) {
if (!obj) {
if (this.formatOptions.outputJson) {
console.log('null');
return;
}
else {
obj = [];
}
}
if (!Array.isArray(obj)) {
obj = [obj];
}
console.log(this.toString(obj, options, asTable));
}
format(obj, options, asTable = true, includeHeader = true) {
if (!obj) {
return null;
}
if (!asTable && obj.length > 1) {
asTable = true;
}
if (this.formatOptions.outputJson) {
const json = obj.map(x => copyJson(x, options));
return JSON.stringify(json, null, this.formatOptions.tabSize);
}
else if (!asTable) {
const data = options && options.table.map(col => {
const val = formatToString(col, obj[0]);
return [col.header.grey, val];
});
return cliff.stringifyRows(data);
}
else {
const data = options && obj.map(x => {
return options.table.map(col => formatToString(col, x));
}) || [];
if (options && includeHeader) {
data.splice(0, 0, options.table.map(x => x.header));
}
return cliff.stringifyRows(data, ['gray']);
}
}
toString(obj, options, asTable = true) {
return this.format(obj, options, asTable);
}
}
exports.OutputFormatter = OutputFormatter;
function formatToString(col, obj) {
let val = undefined;
if (!Boolean(col.property) && col.transform) {
val = col.transform(obj);
}
else if (col.property && col.property.indexOf('[]') !== -1) {
val = formatArrayToString(col.property, col.transform, obj);
}
else {
val = dot.pick(col.property, obj);
if (col.transform) {
val = col.transform(val);
}
}
if (typeof (val) === 'undefined' || val === null) {
val = '';
}
else if (typeof (val) !== 'string') {
val = val.toString();
}
return val;
}
function formatArrayToString(property, transform, obj) {
let valueArray = [];
if (property) {
let arrayIndicatorIndex = property.indexOf(arrayIndicator);
if (arrayIndicatorIndex !== -1) {
let parentKey = property.substr(0, property.indexOf(arrayIndicator));
let childKey = property.substr(property.indexOf(arrayIndicator + '.') + 3);
let parent = dot.pick(parentKey, obj);
if (Array.isArray(parent)) {
parent.forEach(function (arrayItem) {
if (childKey.indexOf(arrayIndicator) !== -1) {
valueArray.push(formatArrayToString(childKey, transform, arrayItem));
}
else {
let childValue = dot.pick(childKey, arrayItem);
if (childValue && transform) {
childValue = transform(childValue);
}
valueArray.push(childValue);
}
});
}
}
}
return (valueArray.length > 0 ? valueArray.join(', ') : undefined);
}
function copyJson(obj, options) {
if (options) {
const copy = {};
options.table.forEach(x => {
if (x.property) {
copyJsonProperty(x.property, obj, copy);
}
});
if (options.json) {
options.json.forEach(x => {
copyJsonProperty(x, obj, copy);
});
}
obj = copy;
}
return obj;
}
function copyJsonProperty(property, source, target) {
if (property.indexOf(arrayIndicator) !== -1) {
copyJsonArray(property, source, target);
}
else {
dot.copy(property, property, source, target);
}
}
function copyJsonArray(property, source, target) {
let parentKey = property.substr(0, property.indexOf(arrayIndicator));
let childKey = property.substr(property.indexOf(arrayIndicator + '.') + 3);
let parentKeys = [];
let parent = dot.pick(parentKey, source);
if (Array.isArray(parent)) {
if (childKey.indexOf(arrayIndicator) !== -1) {
let childKeys = copyJsonArray(childKey, parent);
childKeys.forEach(function (resolvedChildKey, index) {
let resolvedKey = parentKey + '[' + index + '].' + resolvedChildKey;
if (target) {
dot.copy(resolvedKey, resolvedKey, source, target);
}
else {
parentKeys.push(resolvedKey);
}
});
}
else {
parent.forEach(function (_, index) {
let resolvedKey = parentKey + '[' + index + '].' + childKey;
if (target) {
dot.copy(resolvedKey, resolvedKey, source, target);
}
else {
parentKeys.push(resolvedKey);
}
});
}
}
return parentKeys;
}