caravan-x
Version:
A terminal-based utility for managing Caravan multisig wallets in regtest mode. This tool simplifies development and testing with Caravan by providing an easy-to-use interface
139 lines (138 loc) • 4.79 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigManager = void 0;
const fs = __importStar(require("fs-extra"));
const path = __importStar(require("path"));
const chalk_1 = __importDefault(require("chalk"));
const config_1 = require("../types/config");
/**
* Manages application Configuration
*/
class ConfigManager {
constructor(configPath) {
this.configPath =
configPath ||
path.join(process.env.HOME || "", ".caravan-regtest", "config.json");
this.config = config_1.DEFAULT_CONFIG;
this.initConfig();
}
/**
* Initialize configuration by loading from disk or creating default
*/
initConfig() {
try {
fs.ensureDirSync(path.dirname(this.configPath));
if (fs.existsSync(this.configPath)) {
const savedConfig = fs.readJsonSync(this.configPath);
this.config = { ...config_1.DEFAULT_CONFIG, ...savedConfig };
}
else {
// If no config exists, save the default
this.saveConfig();
}
// Make sure directories exist
fs.ensureDirSync(this.config.appDir);
fs.ensureDirSync(this.config.caravanDir);
fs.ensureDirSync(this.config.keysDir);
}
catch (error) {
console.error("Error initializing configuration:", error);
// Fall back to defaults
this.config = config_1.DEFAULT_CONFIG;
}
}
/**
* Save configuration to disk
*/
saveConfig() {
try {
fs.writeJsonSync(this.configPath, this.config, { spaces: 2 });
}
catch (error) {
console.error("Error saving configuration:", error);
}
}
/**
* Get the current configuration
*/
getConfig() {
return this.config;
}
/**
* Update Bitcoin RPC configuration
*/
updateBitcoinConfig(bitcoinConfig) {
this.config.bitcoin = { ...this.config.bitcoin, ...bitcoinConfig };
this.saveConfig();
}
/**
* Update application directories
*/
updateDirectories(dirs) {
this.config = { ...this.config, ...dirs };
// Make sure the directories exist
if (dirs.appDir)
fs.ensureDirSync(dirs.appDir);
if (dirs.caravanDir)
fs.ensureDirSync(dirs.caravanDir);
if (dirs.keysDir)
fs.ensureDirSync(dirs.keysDir);
this.saveConfig();
}
// Add method to change config location
async changeConfigLocation(newPath) {
if (this.configPath === newPath)
return;
// Save current config to new location
try {
// Ensure directory exists
fs.ensureDirSync(path.dirname(newPath));
// Copy current config to new location
fs.copyFileSync(this.configPath, newPath);
// Update path
this.configPath = newPath;
console.log(chalk_1.default.green(`Configuration moved to: ${newPath}`));
}
catch (error) {
console.error(`Error changing config location: ${error}`);
throw error;
}
}
}
exports.ConfigManager = ConfigManager;