feat(config): 实现有状态与无状态 (stateful/stateless) 运行模式无缝切换与企业解析
This commit is contained in:
1 parent
e7aa8a86cc
commit
189415cdac
6 files changed
+293
-16
No files matched your search
@@ -1,13 +1,43 @@
|
||||
import { readFileSync, readdirSync, writeFileSync, mkdirSync } from "fs";
|
||||
import { join } from "path";
|
||||
import { CorpConfig, CorpConfigSchema } from "./types.js";
|
||||
import { join, dirname } from "path";
|
||||
import { homedir } from "os";
|
||||
import { CorpConfig, CorpConfigSchema, GlobalConfig, GlobalConfigSchema } from "./types.js";
|
||||
|
||||
const ENTERPRISES_DIR = "./enterprises";
|
||||
const GLOBAL_CONFIG_PATH = join(homedir(), ".cache", "ekb-mcp", "config.json");
|
||||
|
||||
function ensureDir(dir: string): void {
|
||||
mkdirSync(dir, { recursive: true });
|
||||
}
|
||||
|
||||
export function resolveGlobalConfigFile(): string {
|
||||
return GLOBAL_CONFIG_PATH;
|
||||
}
|
||||
|
||||
export function loadGlobalConfig(): GlobalConfig {
|
||||
try {
|
||||
const content = readFileSync(GLOBAL_CONFIG_PATH, "utf-8");
|
||||
return GlobalConfigSchema.parse(JSON.parse(content));
|
||||
} catch {
|
||||
return {
|
||||
stateful: true,
|
||||
defaultCorpId: undefined,
|
||||
defaultAccountId: undefined,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export function saveGlobalConfig(config: Partial<GlobalConfig>): GlobalConfig {
|
||||
const current = loadGlobalConfig();
|
||||
const updated = GlobalConfigSchema.parse({
|
||||
...current,
|
||||
...config,
|
||||
});
|
||||
ensureDir(dirname(GLOBAL_CONFIG_PATH));
|
||||
writeFileSync(GLOBAL_CONFIG_PATH, JSON.stringify(updated, null, 2), "utf-8");
|
||||
return updated;
|
||||
}
|
||||
|
||||
export function loadCorpConfigs(dir: string = ENTERPRISES_DIR): CorpConfig[] {
|
||||
let files: string[];
|
||||
try {
|
||||
|
||||
Reference in new issue
Block a user