veendor
Version:
a tool for stroing your npm dependencies in arbitraty storage
153 lines (152 loc) • 5.95 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const logger_1 = require("./logger");
const cli_progress_1 = __importDefault(require("cli-progress"));
const colors_1 = __importDefault(require("colors"));
const stream_1 = require("stream");
let progressBlocked = false;
class ProgressStream extends stream_1.Transform {
constructor(options, title, controlToken, total) {
super(options);
this.controlToken = controlToken;
this.total = total;
this.state = StreamState.preparing;
this.haveTotal = typeof this.total === 'number';
const progressWithTotal = rigthPad(2000, ` ${colors_1.default.green(title)} [{bar}] `
+ `${colors_1.default.gray('{_value} / {_total} Mb')} {percentage}% {duration_formatted}`);
const progressWithoutTotal = rigthPad(2000, ` ${colors_1.default.green(title)} ${colors_1.default.gray(' {_value} Mb')}` +
` {duration_formatted}`);
this.progress = new cli_progress_1.default.Bar({
format: this.haveTotal ? progressWithTotal : progressWithoutTotal,
barsize: 40,
etaBuffer: 50,
hideCursor: false,
clearOnComplete: true,
linewrap: false,
fps: 50,
});
this.completed = 0;
this.once('end', () => {
this.die();
});
this.on('pipe', () => {
if (this.state !== StreamState.bypass && this.state !== StreamState.visible) {
this.state = StreamState.connected;
}
});
this.on('unpipe', () => {
if (this.state !== StreamState.bypass) {
this.toggleVisibility(false);
this.state = StreamState.preparing;
}
});
this.controlToken.toggleVisibility = (shouldBeVisibe) => this.toggleVisibility(shouldBeVisibe);
this.controlToken.toggleBypass = (shouldBeVisibe) => this.toggleBypass(shouldBeVisibe);
this.controlToken.terminate = () => this.die();
}
toggleVisibility(shouldBeVisibe) {
if (shouldBeVisibe && (this.state in [StreamState.connected, StreamState.hidden]) && !progressBlocked) {
this.show();
this.state = StreamState.visible;
return;
}
else if (shouldBeVisibe && this.state === StreamState.preparing) {
setTimeout(() => this.toggleVisibility(true), 1000);
}
else if (!shouldBeVisibe && this.state === StreamState.visible) {
this.hide();
this.state = StreamState.hidden;
return;
}
}
toggleBypass(bypassOn) {
if (bypassOn && (this.state in [StreamState.connected, StreamState.hidden, StreamState.visible])) {
this.state = StreamState.bypass;
return;
}
else if (!bypassOn && this.state === StreamState.bypass) {
this.state = StreamState.hidden;
return;
}
}
_transform(data, _encoding, callback) {
if (this.state !== StreamState.bypass) {
this.completed += data.length;
if (this.state === StreamState.visible && !progressBlocked) {
const total = typeof this.total === 'number' ? this.total : 1000;
this.progress.setTotal(total);
this.progress.update(this.completed, {
_value: formatMb(this.completed),
_total: formatMb(total),
});
}
}
callback(undefined, data);
}
show() {
this.progress.start(typeof this.total === 'number' ? this.total : 1000, this.completed);
}
hide() {
this.progress.stop();
}
die() {
if (this.state === StreamState.terminated) {
return;
}
this.progress.stop();
this.state = StreamState.terminated;
}
}
exports.ProgressStream = ProgressStream;
var StreamState;
(function (StreamState) {
StreamState[StreamState["preparing"] = 0] = "preparing";
StreamState[StreamState["connected"] = 1] = "connected";
StreamState[StreamState["visible"] = 2] = "visible";
StreamState[StreamState["hidden"] = 3] = "hidden";
StreamState[StreamState["bypass"] = 4] = "bypass";
StreamState[StreamState["terminated"] = 5] = "terminated";
})(StreamState || (StreamState = {}));
function roundMb(bytes) {
return Math.floor((bytes / 1024 / 1024) * 100) / 100;
}
function formatMb(bytes) {
return leftPad(7, roundMb(bytes).toFixed(2));
}
function leftPad(width, str) {
// https://stackoverflow.com/questions/5366849/convert-1-to-0001-in-javascript
// @ts-ignore
return Array(width).join(' ').substring(' ', width - str.length) + str;
}
function rigthPad(width, str) {
// https://stackoverflow.com/questions/5366849/convert-1-to-0001-in-javascript
// @ts-ignore
return str + Array(width).join(' ').substring(' ', width - str.length);
}
const allTokens = [];
function provideBackendCallTools(backendConfig, callType) {
const controlToken = {};
allTokens.push(controlToken);
return {
getLogger() {
return logger_1.getLogger();
},
getProgressStream(label, total) {
const resultLabel = label ? `${backendConfig.alias} ${label}` : `${backendConfig.alias} ${callType}`;
return new ProgressStream({}, resultLabel, controlToken, total);
},
};
}
exports.provideBackendCallTools = provideBackendCallTools;
function blockAllProgress(shouldBeBlocked) {
progressBlocked = shouldBeBlocked;
for (const token of allTokens) {
if (token.toggleVisibility) {
token.toggleVisibility(!shouldBeBlocked);
}
}
}
exports.blockAllProgress = blockAllProgress;