UNPKG

@ethersphere/swarm-cli

Version:
100 lines (99 loc) 3.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CommandConfig = exports.CONFIG_OPTIONS = void 0; const fs_1 = require("fs"); const os_1 = require("os"); const path_1 = require("path"); const process_1 = require("process"); const config_1 = require("../../config"); /** * Options listed here will be read from the config file * when they are not set explicitly or via `process.env`. * * `optionKey` is the kebab-case variant of the argument used in the parser, a.k.a. the key, * `propertyKey` is the camelCase variant used in TypeScript command classes, a.k.a. the property. */ exports.CONFIG_OPTIONS = [{ optionKey: 'bee-api-url', propertyKey: 'beeApiUrl' }]; class CommandConfig { constructor(appName, console, configFile, configFolder) { Object.defineProperty(this, "config", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "configFilePath", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "configFolderPath", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "console", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.console = console; this.config = { beeApiUrl: config_1.beeApiUrl.default || '', identities: {}, }; this.configFolderPath = this.getConfigFolderPath(appName, configFolder); this.configFilePath = (0, path_1.join)(this.configFolderPath, configFile); this.prepareConfig(); } saveIdentity(name, identity) { if (this.config.identities?.[name]) return false; this.config.identities[name] = identity; this.saveConfig(); return true; } removeIdentity(name) { delete this.config.identities?.[name]; this.saveConfig(); } /** Save configuration object to the CLI's config file */ saveConfig() { (0, fs_1.writeFileSync)(this.configFilePath, JSON.stringify(this.config), { mode: 0o600 }); } /** Load configuration from config path or creates config folder */ prepareConfig() { if (!(0, fs_1.existsSync)(this.configFilePath)) { if (!(0, fs_1.existsSync)(this.configFolderPath)) (0, fs_1.mkdirSync)(this.configFolderPath, { mode: 0o700, recursive: true }); //save config initialized in constructor this.saveConfig(); } else { //load config const configData = (0, fs_1.readFileSync)(this.configFilePath); try { this.config = JSON.parse(configData.toString()); } catch (err) { this.console.error(`There has been an error parsing JSON configuration of CLI from path: '${this.configFilePath}'`); (0, process_1.exit)(1); } } } getConfigFolderPath(appName, configFolder) { if (configFolder) return configFolder; const homePath = (0, os_1.homedir)(); if ((0, os_1.platform)() === 'win32') { return (0, path_1.join)(homePath, 'AppData', appName); } else { return (0, path_1.join)(homePath, `.${appName}`); } } } exports.CommandConfig = CommandConfig;