Add stateful and stateless enterprise selection
This commit is contained in:
1 parent
d383b3aad8
commit
0959cef1cc
22 files changed
+609
-187
No files matched your search
@@ -1,11 +1,13 @@
|
||||
import { existsSync, readFileSync, readdirSync } from "node:fs";
|
||||
import { existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from "node:fs";
|
||||
import { homedir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { CorpConfig, CorpConfigSchema } from "./types.js";
|
||||
import { dirname, join } from "node:path";
|
||||
import { CorpConfig, CorpConfigSchema, GlobalConfig, GlobalConfigSchema } from "./types.js";
|
||||
|
||||
const ENTERPRISES_DIR = "enterprises";
|
||||
const CONFIG_ENV = "SEAL_HOME_ENTERPRISES_DIR";
|
||||
const USER_DIR_ENV = "SEAL_HOME_USER_ENTERPRISES_DIR";
|
||||
const GLOBAL_CONFIG_ENV = "SEAL_HOME_CONFIG_FILE";
|
||||
const GLOBAL_CONFIG_FILE = join(homedir(), ".config", "seal-home", "config.json");
|
||||
export const USER_ENTERPRISES_DIR = join(homedir(), ".config", "seal-home", "enterprises");
|
||||
|
||||
export function loadCorpConfigs(): CorpConfig[] {
|
||||
@@ -56,3 +58,24 @@ export function getEnterprisesDirCandidates(): string[] {
|
||||
USER_ENTERPRISES_DIR
|
||||
];
|
||||
}
|
||||
|
||||
export function resolveGlobalConfigFile(): string {
|
||||
return process.env[GLOBAL_CONFIG_ENV]?.trim() || GLOBAL_CONFIG_FILE;
|
||||
}
|
||||
|
||||
export function loadGlobalConfig(): GlobalConfig {
|
||||
const file = resolveGlobalConfigFile();
|
||||
if (!existsSync(file)) {
|
||||
return GlobalConfigSchema.parse({});
|
||||
}
|
||||
|
||||
return GlobalConfigSchema.parse(JSON.parse(readFileSync(file, "utf-8")));
|
||||
}
|
||||
|
||||
export function saveGlobalConfig(config: GlobalConfig): GlobalConfig {
|
||||
const normalized = GlobalConfigSchema.parse(config);
|
||||
const file = resolveGlobalConfigFile();
|
||||
mkdirSync(dirname(file), { recursive: true, mode: 0o700 });
|
||||
writeFileSync(file, `${JSON.stringify(normalized, null, 2)}\n`, { mode: 0o600 });
|
||||
return normalized;
|
||||
}
|
||||
Reference in new issue
Block a user