@stoar/cli
Version:
CLI tool for STOAR - decentralized file storage on Arweave
49 lines • 1.44 kB
JavaScript
import { homedir } from 'node:os';
import { join } from 'node:path';
import { readFileSync, writeFileSync, mkdirSync, existsSync } from 'node:fs';
const CONFIG_DIR = join(homedir(), '.stoar');
const CONFIG_FILE = join(CONFIG_DIR, 'config.json');
export function loadConfig() {
if (existsSync(CONFIG_FILE)) {
try {
const data = readFileSync(CONFIG_FILE, 'utf-8');
return JSON.parse(data);
}
catch (error) {
console.warn('Warning: Failed to load config file, using defaults');
}
}
return {};
}
export function saveConfig(config) {
if (!existsSync(CONFIG_DIR)) {
mkdirSync(CONFIG_DIR, { recursive: true });
}
writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2));
}
export function getConfig(key) {
const config = loadConfig();
if (key) {
return key.split('.').reduce((obj, k) => obj?.[k], config);
}
return config;
}
export function setConfig(key, value) {
const config = loadConfig();
const keys = key.split('.');
let obj = config;
for (let i = 0; i < keys.length - 1; i++) {
if (!obj[keys[i]]) {
obj[keys[i]] = {};
}
obj = obj[keys[i]];
}
obj[keys[keys.length - 1]] = value;
saveConfig(config);
}
export function resetConfig() {
if (existsSync(CONFIG_FILE)) {
writeFileSync(CONFIG_FILE, '{}');
}
}
//# sourceMappingURL=config.js.map