UNPKG

@ethersphere/swarm-cli

Version:
49 lines (48 loc) 1.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.History = void 0; const fs_1 = require("fs"); const process_1 = require("process"); class History { constructor(commandConfig, console) { Object.defineProperty(this, "commandConfig", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "console", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.commandConfig = commandConfig; this.console = console; } getItems() { const historyFilePath = this.commandConfig.getHistoryFilePath(); if (!(0, fs_1.existsSync)(historyFilePath)) { return []; } const historyData = (0, fs_1.readFileSync)(historyFilePath); try { const historyList = JSON.parse(historyData.toString()); return historyList; } catch (err) { this.console.error(`There has been an error parsing history JSON from path: '${historyFilePath}'`); (0, process_1.exit)(1); } } getItemByIndex(index) { return this.getItems().find((item) => item.index === index); } addItem(item) { const history = this.getItems(); item.index = history.length + 1; history.push(item); (0, fs_1.writeFileSync)(this.commandConfig.getHistoryFilePath(), JSON.stringify(history)); } } exports.History = History;