160 lines
5.4 KiB
TypeScript
160 lines
5.4 KiB
TypeScript
import { existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from "node:fs";
|
|
import { homedir } from "node:os";
|
|
import { dirname, join } from "node:path";
|
|
import { CorpConfig, CorpConfigSchema, GlobalConfig, GlobalConfigSchema } from "./types.js";
|
|
|
|
const ENTERPRISES_DIR = "enterprises";
|
|
const CONFIG_ENV = "HOSE_ENTERPRISES_DIR";
|
|
const USER_DIR_ENV = "HOSE_USER_ENTERPRISES_DIR";
|
|
const GLOBAL_CONFIG_ENV = "HOSE_CONFIG_FILE";
|
|
|
|
export const PRIMARY_USER_ENTERPRISES_DIR = join(homedir(), ".config", "hose-use", "enterprises");
|
|
export const LEGACY_USER_ENTERPRISES_DIR = join(homedir(), ".cache", "ekb-mcp", "enterprises");
|
|
export const USER_ENTERPRISES_DIR = PRIMARY_USER_ENTERPRISES_DIR;
|
|
|
|
export const PRIMARY_GLOBAL_CONFIG_FILE = join(homedir(), ".config", "hose-use", "config.json");
|
|
export const LEGACY_GLOBAL_CONFIG_FILE = join(homedir(), ".cache", "ekb-mcp", "config.json");
|
|
|
|
function ensureDir(dir: string): void {
|
|
mkdirSync(dir, { recursive: true });
|
|
}
|
|
|
|
export function resolveEnterprisesDirs(): string[] {
|
|
const configured = process.env[CONFIG_ENV]?.trim();
|
|
if (configured) {
|
|
return existsSync(configured) ? [configured] : [];
|
|
}
|
|
|
|
const configuredUserDir = process.env[USER_DIR_ENV]?.trim();
|
|
if (configuredUserDir) {
|
|
return [ENTERPRISES_DIR, configuredUserDir].filter((dir) => existsSync(dir));
|
|
}
|
|
|
|
const dirs = [ENTERPRISES_DIR, PRIMARY_USER_ENTERPRISES_DIR, LEGACY_USER_ENTERPRISES_DIR];
|
|
const uniqueExisting: string[] = [];
|
|
for (const d of dirs) {
|
|
if (existsSync(d) && !uniqueExisting.includes(d)) {
|
|
uniqueExisting.push(d);
|
|
}
|
|
}
|
|
return uniqueExisting;
|
|
}
|
|
|
|
export function resolvePrimaryEnterprisesDir(): string | undefined {
|
|
const configured = process.env[CONFIG_ENV]?.trim();
|
|
if (configured) return existsSync(configured) ? configured : undefined;
|
|
if (existsSync(ENTERPRISES_DIR)) return ENTERPRISES_DIR;
|
|
if (existsSync(PRIMARY_USER_ENTERPRISES_DIR)) return PRIMARY_USER_ENTERPRISES_DIR;
|
|
if (existsSync(LEGACY_USER_ENTERPRISES_DIR)) return LEGACY_USER_ENTERPRISES_DIR;
|
|
return undefined;
|
|
}
|
|
|
|
export function resolveWritableEnterprisesDir(): string {
|
|
const configured = process.env[CONFIG_ENV]?.trim();
|
|
if (configured) return configured;
|
|
const configuredUserDir = process.env[USER_DIR_ENV]?.trim();
|
|
if (configuredUserDir) return configuredUserDir;
|
|
if (existsSync(ENTERPRISES_DIR)) return ENTERPRISES_DIR;
|
|
return PRIMARY_USER_ENTERPRISES_DIR;
|
|
}
|
|
|
|
export function getEnterprisesDirCandidates(): string[] {
|
|
return [
|
|
`${CONFIG_ENV}=<path>`,
|
|
ENTERPRISES_DIR,
|
|
PRIMARY_USER_ENTERPRISES_DIR,
|
|
LEGACY_USER_ENTERPRISES_DIR,
|
|
];
|
|
}
|
|
|
|
export function resolveGlobalConfigFile(): string {
|
|
const configured = process.env[GLOBAL_CONFIG_ENV]?.trim();
|
|
if (configured) return configured;
|
|
if (existsSync(PRIMARY_GLOBAL_CONFIG_FILE)) return PRIMARY_GLOBAL_CONFIG_FILE;
|
|
if (existsSync(LEGACY_GLOBAL_CONFIG_FILE)) return LEGACY_GLOBAL_CONFIG_FILE;
|
|
return PRIMARY_GLOBAL_CONFIG_FILE;
|
|
}
|
|
|
|
export function loadGlobalConfig(): GlobalConfig {
|
|
const file = resolveGlobalConfigFile();
|
|
try {
|
|
if (existsSync(file)) {
|
|
const content = readFileSync(file, "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,
|
|
});
|
|
|
|
const configured = process.env[GLOBAL_CONFIG_ENV]?.trim();
|
|
const targetFile = configured || PRIMARY_GLOBAL_CONFIG_FILE;
|
|
ensureDir(dirname(targetFile));
|
|
writeFileSync(targetFile, JSON.stringify(updated, null, 2), "utf-8");
|
|
return updated;
|
|
}
|
|
|
|
export function loadCorpConfigs(overrideDirs?: string[]): CorpConfig[] {
|
|
const dirs = overrideDirs ? overrideDirs.filter(existsSync) : resolveEnterprisesDirs();
|
|
const byId = new Map<string, CorpConfig>();
|
|
|
|
for (const dir of dirs) {
|
|
try {
|
|
const files = readdirSync(dir)
|
|
.filter((item) => item.endsWith(".json"))
|
|
.filter((item) => !item.startsWith("example."));
|
|
|
|
for (const file of files) {
|
|
try {
|
|
const content = readFileSync(join(dir, file), "utf-8");
|
|
const parsed = JSON.parse(content);
|
|
const corp = CorpConfigSchema.parse(parsed);
|
|
// 相同企业 ID 以先出现的为准(工作区优先于全局,全局优先于历史)
|
|
if (!byId.has(corp.id)) {
|
|
byId.set(corp.id, corp);
|
|
}
|
|
} catch (error) {
|
|
console.error(
|
|
`[config] failed to load ${file} from ${dir}: ${error instanceof Error ? error.message : String(error)}`
|
|
);
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error(
|
|
`[config] failed to read enterprises directory ${dir}: ${error instanceof Error ? error.message : String(error)}`
|
|
);
|
|
}
|
|
}
|
|
|
|
return Array.from(byId.values());
|
|
}
|
|
|
|
export function saveCorpConfig(config: CorpConfig, dir?: string): string {
|
|
const targetDir = dir || resolveWritableEnterprisesDir();
|
|
ensureDir(targetDir);
|
|
const validated = CorpConfigSchema.parse(config);
|
|
const filePath = join(targetDir, `${validated.id}.json`);
|
|
writeFileSync(filePath, JSON.stringify(validated, null, 2), "utf-8");
|
|
return filePath;
|
|
}
|
|
|
|
export function saveMultipleCorpConfigs(
|
|
configs: CorpConfig[],
|
|
dir?: string
|
|
): string[] {
|
|
return configs.map((config) => saveCorpConfig(config, dir));
|
|
}
|