jovo-cli
Version:
jovo command line tool (beta)
117 lines • 4.67 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const chalk_1 = __importDefault(require("chalk"));
const indent_string_1 = __importDefault(require("indent-string"));
const log_update_1 = __importDefault(require("log-update"));
const _1 = require("./");
const intdentText = (text, level, indentText = ' ', color, firstLinePrefix) => {
if (typeof text !== 'string') {
text = 'Message can not be displayed!';
}
const indentLength = level * indent_string_1.default.length;
const maxLength = 120;
const maxTextLength = maxLength - indentLength;
let textLeft = text;
let tempText = '';
const returnArray = [];
let lastSpacePosition;
let fullIdentText;
let itteration = 0;
do {
tempText = textLeft.substring(0, maxTextLength);
textLeft = textLeft.substring(maxTextLength);
if (textLeft && tempText.charAt(maxTextLength - 1) !== ' ' && textLeft.charAt(0) !== ' ') {
lastSpacePosition = tempText.lastIndexOf(' ');
if (lastSpacePosition !== -1) {
textLeft = (tempText.substring(lastSpacePosition) + textLeft).trim();
tempText = tempText.substring(0, lastSpacePosition).trim();
}
}
else {
tempText = tempText.trim();
textLeft = textLeft.trim();
}
fullIdentText = indentText.repeat(level + 1);
if (firstLinePrefix) {
if (itteration === 0) {
fullIdentText = fullIdentText + firstLinePrefix;
}
else {
fullIdentText = fullIdentText + ' '.repeat(firstLinePrefix.length);
}
}
returnArray.push(`${chalk_1.default[color](fullIdentText + tempText)}`);
itteration++;
} while (textLeft);
return returnArray;
};
const renderHelper = (tasks, options, level = 1) => {
let output = [];
for (const task of tasks) {
if (task.isEnabled()) {
if (options.seperateTopTasks === true && level === 1) {
output.push('');
}
output.push(indent_string_1.default(` ${_1.getSymbol(task, options)} ${task.title}`, level, { indent: ' ' }));
if ((task.isPending() || task.isSkipped() || task.hasFailed()) && _1.isDefined(task.output)) {
const data = task.output;
if (_1.isDefined(data)) {
if (data.substr(0, 5) === 'Info:') {
const arr = data.substr(6).split('\n');
for (const item of arr) {
output.push.apply(output, intdentText(item, level, ' ', 'grey', ' -> '));
}
}
else if (data.substr(0, 6) === 'Error:') {
const arr = data.substr(7).split('\n');
for (const item of arr) {
output.push.apply(output, intdentText(item, level, ' ', 'red', ' '));
}
}
else {
output.push.apply(output, intdentText(data, level, ' ', 'grey', ' -> '));
}
}
}
if ((task.isPending() || task.hasFailed() || options.collapse === false) &&
(task.hasFailed() || options.showSubtasks !== false) &&
task.subtasks.length > 0) {
output = output.concat(renderHelper(task.subtasks, options, level + 1));
}
}
}
return output.join('\n');
};
class JovoCliRenderer {
constructor(tasks = [], options = {}) {
this.nonTTY = false;
this._tasks = tasks;
this._options = Object.assign({ showSubtasks: true, collapse: true, clearOutput: false, separateTopTasks: false }, options);
}
render() {
if (this._id) {
return;
}
this._id = setInterval(() => {
log_update_1.default(renderHelper(this._tasks, this._options));
}, 100);
}
end(err) {
if (this._id) {
clearInterval(this._id);
this._id = undefined;
}
log_update_1.default(renderHelper(this._tasks, this._options));
if (this._options.clearOutput && err === undefined) {
log_update_1.default.clear();
}
else {
log_update_1.default.done();
}
}
}
exports.JovoCliRenderer = JovoCliRenderer;
//# sourceMappingURL=JovoCliRenderer.js.map