zippycli
Version:
An unofficial Zippyshare CLI
174 lines (133 loc) • 3.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Command = void 0;
var _command = require("@oclif/command");
var _sourceMapSupport = require("source-map-support");
var _util = require("./util");
var _request = require("./request");
/**
* Command constructor.
*/
class Command extends _command.Command {
/**
* Init function.
*
* @returns Returns the parent init.
*/
async init() {
if ((0, _util.envTrue)('ZS_CLI_DEBUG_SOURCE_MAPS')) {
(0, _sourceMapSupport.install)();
}
return super.init();
}
/**
* Check if the shell is interactive.
*
* @returns True if interactive shell, else false.
*/
_isInteractive() {
return process.stdout.isTTY || false;
}
/**
* Init a request factory with the specified timeout.
*
* @param timeout Timeout duraction in milliseconds.
* @returns Request factory.
*/
_initRequest(timeout = 10000) {
return _request.RequestStream.factory({
timeout
});
}
/**
* Read input file to list all URL's line by line.
*
* @param filepath Input file.
* @returns URL list.
*/
async _readInputFile(filepath) {
return (0, _util.readInputFile)(filepath);
}
/**
* Transfer seconds human readable.
*
* @param ms Milliseconds passed or null.
* @returns Formatted string.
*/
_transferSecondsHuman(ms) {
if (ms === null) {
return '-:--:--';
}
const seconds = Math.round(ms / 1000);
const [minutes, s] = (0, _util.divmod)(seconds, 60);
const [h, m] = (0, _util.divmod)(minutes, 60);
const mStr = m < 10 ? `0${m}` : m;
const sStr = s < 10 ? `0${s}` : s;
return `${h}:${mStr}:${sStr}`;
}
/**
* Transfer bytes human readable.
*
* @param size Byte size.
* @returns Formatted string.
*/
_transferBytesHuman(size) {
let based = size;
const base = 1024;
const names = ['B', 'K', 'M', 'G', 'T'];
const il = names.length - 1;
let i = 0;
for (; based > base && i < il; i++) {
based /= base;
}
return `${based.toFixed(2)}${names[i]}`;
}
/**
* Init data transfer progress output function.
*
* @returns Progress update callback function.
*/
_transferProgressOutputInit() {
let messageLongest = 0;
/**
* Progress callback.
*
* @param time Time.
* @param total Total.
*/
const r = (time, total) => {
// Calcaulte the time spent.
const timePast = this._transferSecondsHuman(time.duration); // Calcaulte progress.
const progress = total.current / total.total;
const percent = `${(progress * 100).toFixed(2)}%`; // Calculate amounts.
const amountCurrent = this._transferBytesHuman(total.current);
const amountTotal = this._transferBytesHuman(total.total);
const amount = [`${amountCurrent} (${total.current})`, `${amountTotal} (${total.total})`].join(' / '); // Calculate speed.
const bytesMs = time.delta ? total.delta / time.delta : 0;
const bytesSec = `${this._transferBytesHuman(bytesMs * 1000)}/s`; // Estimate remaining.
const timeLeftMs = bytesMs ? total.remaining / bytesMs : null;
const timeETA = this._transferSecondsHuman(timeLeftMs); // Assemble message.
const message = [timePast, percent, amount, bytesSec, timeETA].join(' '); // Remember the longest message.
messageLongest = Math.max(messageLongest, message.length);
const messagePadded = message.padEnd(messageLongest, ' ');
if (this._isInteractive()) {
process.stdout.write(`\r${messagePadded}\r`);
} else {
this.log(messagePadded);
}
};
return r;
}
/**
* Clear data transfer progress output after.
*/
_transferProgressOutputAfter() {
if (this._isInteractive()) {
this.log('');
}
}
}
exports.Command = Command;
//# sourceMappingURL=command.js.map