UNPKG

zksync-cli

Version:

CLI tool that simplifies the process of developing applications and interacting with the ZKsync network

56 lines 1.91 kB
import { readFileSync } from "fs"; import { fileOrDirExists, getLocalPath, writeFile } from "../utils/files.js"; import Logger from "../utils/logger.js"; class ConfigHandlerClass { constructor() { this.configPath = getLocalPath("config.json"); this.internalConfig = {}; this.loadConfig(); } loadConfig() { if (this.configExists) { try { this.internalConfig = JSON.parse(readFileSync(this.configPath, "utf-8")); } catch (error) { Logger.error(`Error while reading config file: ${error}`); } } } saveConfig() { writeFile(this.configPath, JSON.stringify(this.internalConfig, null, 2)); Logger.debug(`Saved config to ${this.configPath}`); } get configExists() { return fileOrDirExists(this.configPath); } accessNestedProperty(path, createIfNotExist = false) { const keys = path.split("."); let current = this.internalConfig; for (let i = 0; i < keys.length - 1; i++) { if (current[keys[i]] === undefined) { if (createIfNotExist) { current[keys[i]] = {}; } else { return undefined; } } current = current[keys[i]]; } return { parent: current, lastKey: keys[keys.length - 1] }; } getConfigValue(path) { const result = this.accessNestedProperty(path); return result ? result.parent[result.lastKey] : undefined; } setConfigValue(path, value) { const result = this.accessNestedProperty(path, true); if (result) { result.parent[result.lastKey] = value; this.saveConfig(); } } } export const configHandler = new ConfigHandlerClass(); //# sourceMappingURL=ConfigHandler.js.map