agilemonitor-cli
Version:
Command-line interface for AgileMonitor - Monitor your websites from the terminal
81 lines (69 loc) • 1.4 kB
JavaScript
const fs = require('fs');
const path = require('path');
const os = require('os');
const CONFIG_DIR = path.join(os.homedir(), '.agilemonitor');
const CONFIG_FILE = path.join(CONFIG_DIR, 'config.json');
/**
* Get the configuration file path
*/
function getConfigPath() {
return CONFIG_FILE;
}
/**
* Save configuration
*/
function save(data) {
// Create directory if it doesn't exist
if (!fs.existsSync(CONFIG_DIR)) {
fs.mkdirSync(CONFIG_DIR, { recursive: true });
}
// Read existing config
let config = {};
if (fs.existsSync(CONFIG_FILE)) {
try {
config = JSON.parse(fs.readFileSync(CONFIG_FILE, 'utf8'));
} catch (error) {
// Invalid JSON, start fresh
config = {};
}
}
// Merge with new data
config = { ...config, ...data };
// Save
fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2));
}
/**
* Load configuration
*/
function load() {
if (!fs.existsSync(CONFIG_FILE)) {
return {};
}
try {
return JSON.parse(fs.readFileSync(CONFIG_FILE, 'utf8'));
} catch (error) {
return {};
}
}
/**
* Get API key from config
*/
function getApiKey() {
const config = load();
return config.apiKey || null;
}
/**
* Clear configuration
*/
function clear() {
if (fs.existsSync(CONFIG_FILE)) {
fs.unlinkSync(CONFIG_FILE);
}
}
module.exports = {
getConfigPath,
save,
load,
getApiKey,
clear
};