mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-05-30 03:53:55 +00:00
- contexts.mjs: loadContexts/saveContexts/resolveActiveContext (~/.omniroute/config.json) - commands/contexts.mjs: CRUD completo (add/use/list/show/remove/rename/export/import) - config.mjs: subgroup contexts registrado sob config contexts - program.mjs: flag global --context <name> com env OMNIROUTE_CONTEXT - en.json/pt-BR.json: chaves program.context e config.contexts adicionadas - import usa validação explícita de campos (sem Object.assign cru)
43 lines
1.2 KiB
JavaScript
43 lines
1.2 KiB
JavaScript
import { existsSync, readFileSync, writeFileSync, mkdirSync, chmodSync } from "node:fs";
|
|
import { join, dirname } from "node:path";
|
|
import { resolveDataDir } from "./data-dir.mjs";
|
|
|
|
const CONFIG_VERSION = 1;
|
|
|
|
export function configPath() {
|
|
return join(resolveDataDir(), "config.json");
|
|
}
|
|
|
|
function defaultConfig() {
|
|
return {
|
|
version: CONFIG_VERSION,
|
|
currentContext: "default",
|
|
contexts: {
|
|
default: { baseUrl: `http://localhost:${process.env.PORT || "20128"}`, apiKey: null },
|
|
},
|
|
};
|
|
}
|
|
|
|
export function loadContexts() {
|
|
try {
|
|
if (!existsSync(configPath())) return defaultConfig();
|
|
return JSON.parse(readFileSync(configPath(), "utf8"));
|
|
} catch {
|
|
return defaultConfig();
|
|
}
|
|
}
|
|
|
|
export function saveContexts(cfg) {
|
|
const path = configPath();
|
|
mkdirSync(dirname(path), { recursive: true });
|
|
writeFileSync(path, JSON.stringify(cfg, null, 2));
|
|
try {
|
|
chmodSync(path, 0o600);
|
|
} catch {}
|
|
}
|
|
|
|
export function resolveActiveContext(overrideName) {
|
|
const cfg = loadContexts();
|
|
const name = overrideName || cfg.currentContext || "default";
|
|
return cfg.contexts?.[name] || cfg.contexts?.default || { baseUrl: "http://localhost:20128" };
|
|
}
|