The ekb-mcp -> hose-use rename migration (LEGACY_* constants, syncLegacyEnterprisesDir/syncLegacyGlobalConfig with 7-day grace cleanup, dual-source enterprises resolution) has completed its job: legacy data self-deleted on active machines years of grace ago. Config resolution now returns the single primary ~/.config/hose-use source; startup no longer scans the legacy dir. - core/config/loader.ts: drop legacy constants, sync functions, and LEGACY path from diagnostics candidates; loader.test.ts drops the three legacy-migration cases - domains/approval/db.ts deleted: pure pass-through over core/db whose consumers (repository.ts) already call core/db directly; the four test files now import getDatabase/closeDatabase from core/db - scripts/dev-hygiene.ts: drop the config.legacy finding and legacyConfig snapshot field - docs/daily/* removed: Python-era (ekb-cli/ekb-mcp) notes describing commands and architecture that no longer exist (preserved in git history) No behavior change on machines without legacy data: tsc clean, 313 tests pass (316 minus the 3 removed legacy cases).
180 lines
5.9 KiB
TypeScript
180 lines
5.9 KiB
TypeScript
import { chmodSync, existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from "node:fs";
|
|
import { homedir } from "node:os";
|
|
import { dirname, join } from "node:path";
|
|
import { CorpConfig, CorpConfigSchema, GlobalConfig, GlobalConfigSchema, normalizeCorpConfig } from "./types.js";
|
|
|
|
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 USER_ENTERPRISES_DIR = PRIMARY_USER_ENTERPRISES_DIR;
|
|
|
|
export const PRIMARY_GLOBAL_CONFIG_FILE = join(homedir(), ".config", "hose-use", "config.json");
|
|
|
|
function ensureDir(dir: string): void {
|
|
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
try {
|
|
chmodSync(dir, 0o700);
|
|
} catch {}
|
|
}
|
|
|
|
function writeSecureFile(filePath: string, content: string): void {
|
|
ensureDir(dirname(filePath));
|
|
writeFileSync(filePath, content, { encoding: "utf-8", mode: 0o600 });
|
|
try {
|
|
chmodSync(filePath, 0o600);
|
|
} catch {}
|
|
}
|
|
|
|
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();
|
|
const userDirs = configuredUserDir
|
|
? (existsSync(configuredUserDir) ? [configuredUserDir] : [])
|
|
: [PRIMARY_USER_ENTERPRISES_DIR];
|
|
|
|
const globalConfig = loadGlobalConfig();
|
|
const testDir = globalConfig.testEnterprisesDir?.trim();
|
|
|
|
const candidates = testDir ? [testDir, ...userDirs] : userDirs;
|
|
const uniqueExisting: string[] = [];
|
|
for (const d of candidates) {
|
|
if (existsSync(d) && !uniqueExisting.includes(d)) {
|
|
uniqueExisting.push(d);
|
|
}
|
|
}
|
|
return uniqueExisting;
|
|
}
|
|
|
|
export function resolvePrimaryEnterprisesDir(): string | undefined {
|
|
return resolveEnterprisesDirs()[0];
|
|
}
|
|
|
|
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;
|
|
const testDir = loadGlobalConfig().testEnterprisesDir?.trim();
|
|
if (testDir && existsSync(testDir)) return testDir;
|
|
return PRIMARY_USER_ENTERPRISES_DIR;
|
|
}
|
|
|
|
export function getEnterprisesDirCandidates(): string[] {
|
|
const testDir = loadGlobalConfig().testEnterprisesDir?.trim();
|
|
return [
|
|
`${CONFIG_ENV}=<path>`,
|
|
...(testDir ? [`testEnterprisesDir: ${testDir}`] : []),
|
|
PRIMARY_USER_ENTERPRISES_DIR,
|
|
];
|
|
}
|
|
|
|
export function resolveGlobalConfigFile(): string {
|
|
const configured = process.env[GLOBAL_CONFIG_ENV]?.trim();
|
|
if (configured) return configured;
|
|
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 GlobalConfigSchema.parse({});
|
|
}
|
|
|
|
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;
|
|
writeSecureFile(targetFile, JSON.stringify(updated, null, 2));
|
|
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 = normalizeCorpConfig(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 candidatePath = join(targetDir, `${config.id}.json`);
|
|
if (!config.credentials?.appKey && existsSync(candidatePath)) {
|
|
try {
|
|
const existing = JSON.parse(readFileSync(candidatePath, "utf-8"));
|
|
const existingKey = existing?.credentials?.appKey || existing?.auth?.appKey;
|
|
const existingSec = existing?.credentials?.appSecurity || existing?.auth?.appSecurity;
|
|
if (existingKey && existingSec) {
|
|
config = {
|
|
...config,
|
|
credentials: {
|
|
...config.credentials,
|
|
appKey: existingKey,
|
|
appSecurity: existingSec,
|
|
},
|
|
};
|
|
}
|
|
} catch {
|
|
// ignore malformed existing file
|
|
}
|
|
}
|
|
const validated = normalizeCorpConfig(CorpConfigSchema.parse(config));
|
|
const filePath = join(targetDir, `${validated.id}.json`);
|
|
writeSecureFile(filePath, JSON.stringify(validated, null, 2));
|
|
return filePath;
|
|
}
|
|
|
|
export function saveMultipleCorpConfigs(
|
|
configs: CorpConfig[],
|
|
dir?: string
|
|
): string[] {
|
|
return configs.map((config) => saveCorpConfig(config, dir));
|
|
}
|