@ethersphere/swarm-cli
Version:
CLI tool for Bee
129 lines (128 loc) • 5.08 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.List = void 0;
const furious_commander_1 = require("furious-commander");
const process_1 = require("process");
const stamp_1 = require("../../service/stamp");
const text_1 = require("../../utils/text");
const stamp_command_1 = require("./stamp-command");
class List extends stamp_command_1.StampCommand {
constructor() {
super(...arguments);
Object.defineProperty(this, "name", {
enumerable: true,
configurable: true,
writable: true,
value: 'list'
});
Object.defineProperty(this, "alias", {
enumerable: true,
configurable: true,
writable: true,
value: 'ls'
});
Object.defineProperty(this, "description", {
enumerable: true,
configurable: true,
writable: true,
value: 'List postage stamps'
});
Object.defineProperty(this, "leastUsed", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "limit", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "hideUsage", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "maxUsage", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "minUsage", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
}
async run() {
await super.init();
this.console.verbose(`Listing postage stamps...`);
const stamps = (await this.bee.getAllPostageBatch()) || [];
if (stamps.length === 0) {
this.console.error('You do not have any stamps.');
(0, process_1.exit)(1);
}
const filteredStamps = stamps.filter(x => x.usage * 100 >= this.minUsage && x.usage * 100 <= this.maxUsage);
if (filteredStamps.length === 0) {
(0, process_1.exit)(1);
}
const limitedStamps = filteredStamps.slice(0, this.limit);
const orderedStamps = this.leastUsed
? limitedStamps.sort((a, b) => a.usage - b.usage)
: limitedStamps.sort((a, b) => b.duration.toSeconds() - a.duration.toSeconds());
(0, text_1.printDivided)(orderedStamps, (items, console) => {
(0, stamp_1.printStamp)(items, console, { printUsageInQuiet: !this.hideUsage, showTtl: true });
}, this.console);
if (!this.quiet && !this.verbose) {
this.console.info('Run with --verbose for more details.');
}
}
}
__decorate([
(0, furious_commander_1.Option)({ key: 'least-used', type: 'boolean', description: 'Sort stamps so least used comes first' }),
__metadata("design:type", Boolean)
], List.prototype, "leastUsed", void 0);
__decorate([
(0, furious_commander_1.Option)({ key: 'limit', type: 'number', minimum: 1, description: 'Limit the amount of printed stamps' }),
__metadata("design:type", Number)
], List.prototype, "limit", void 0);
__decorate([
(0, furious_commander_1.Option)({ key: 'hide-usage', type: 'boolean', description: 'Do not print usage percentage' }),
__metadata("design:type", Boolean)
], List.prototype, "hideUsage", void 0);
__decorate([
(0, furious_commander_1.Option)({
key: 'max-usage',
type: 'number',
minimum: 0,
maximum: 100,
description: 'Only list stamps at most this usage percentage',
default: 100,
}),
__metadata("design:type", Number)
], List.prototype, "maxUsage", void 0);
__decorate([
(0, furious_commander_1.Option)({
key: 'min-usage',
type: 'number',
minimum: 0,
maximum: 100,
description: 'Only list stamps at least this usage percentage',
default: 0,
}),
__metadata("design:type", Number)
], List.prototype, "minUsage", void 0);
exports.List = List;