@dugongjs/cli
Version:
66 lines (65 loc) • 1.88 kB
JavaScript
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
import { homedir } from "os";
import { join } from "path";
const CONFIG_DIR = join(homedir(), ".dugong");
const CONFIG_FILE = join(CONFIG_DIR, "config.json");
function ensureConfigDir() {
if (!existsSync(CONFIG_DIR)) {
mkdirSync(CONFIG_DIR);
}
}
function loadConfig() {
ensureConfigDir();
if (!existsSync(CONFIG_FILE)) {
const defaultConfig = {
currentContext: "default",
contexts: {}
};
return defaultConfig;
}
const configRaw = readFileSync(CONFIG_FILE, "utf-8");
return JSON.parse(configRaw);
}
function saveConfig(config) {
ensureConfigDir();
const configRaw = JSON.stringify(config, null, 2);
writeFileSync(CONFIG_FILE, configRaw);
}
function setConfig(name, context, makeCurrent = false) {
const config = loadConfig();
config.contexts[name] = context;
if (makeCurrent) {
config.currentContext = name;
}
saveConfig(config);
}
function getCurrentContext() {
const config = loadConfig();
return config.contexts[config.currentContext] ?? null;
}
function switchContext(name) {
const config = loadConfig();
if (!config.contexts[name]) {
return false;
}
config.currentContext = name;
saveConfig(config);
return true;
}
function deleteContext(name) {
const config = loadConfig();
if (!config.contexts[name]) {
return false;
}
delete config.contexts[name];
if (config.currentContext === name) {
config.currentContext = Object.keys(config.contexts)[0] ?? null;
}
saveConfig(config);
return true;
}
function listContexts() {
const config = loadConfig();
return Object.keys(config.contexts);
}
export { deleteContext, getCurrentContext, listContexts, loadConfig, setConfig, switchContext };