111 lines
3.2 KiB
TypeScript
111 lines
3.2 KiB
TypeScript
import { CorpConfig, GlobalConfig } from "./types.js";
|
|
|
|
export function resolveCorpConfig(
|
|
corps: CorpConfig[],
|
|
options: Pick<GlobalConfig, "stateful" | "defaultCorpId"> & { corpId?: string },
|
|
candidates: string[] = []
|
|
): CorpConfig {
|
|
const requestedCorpId = options.corpId?.trim() || undefined;
|
|
const configuredCorpId = options.stateful
|
|
? options.defaultCorpId?.trim() || undefined
|
|
: undefined;
|
|
const selectedCorpId = requestedCorpId ?? configuredCorpId;
|
|
|
|
if (selectedCorpId) {
|
|
const corp = corps.find((item) => item.id === selectedCorpId);
|
|
if (corp) return corp;
|
|
throw new Error(`No enterprise config found for ${selectedCorpId}`);
|
|
}
|
|
|
|
if (!options.stateful) {
|
|
throw new Error(
|
|
"Stateless mode is enabled. Every enterprise command requires --corp <corpId>. " +
|
|
"Use `seal-home config mode stateful` to enable a default enterprise."
|
|
);
|
|
}
|
|
|
|
const firstCorp = corps[0];
|
|
if (firstCorp) return firstCorp;
|
|
|
|
throw new Error(
|
|
"No enterprise config found. Add a non-example JSON file under one of: " +
|
|
(candidates.length ? candidates.join(", ") : "the configured enterprise directories")
|
|
);
|
|
}
|
|
|
|
export type CorpContextOptions = Pick<GlobalConfig, "stateful" | "defaultCorpId">;
|
|
|
|
export class CorpContext {
|
|
private current: CorpConfig | null = null;
|
|
private corps = new Map<string, CorpConfig>();
|
|
private stateful = false;
|
|
private defaultCorpId: string | undefined;
|
|
|
|
constructor(corps: CorpConfig[], options: CorpContextOptions = { stateful: false }) {
|
|
this.stateful = options.stateful;
|
|
this.defaultCorpId = options.defaultCorpId;
|
|
this.replaceCorps(corps);
|
|
this.selectCurrent(corps[0]);
|
|
}
|
|
|
|
refresh(corps: CorpConfig[], options: CorpContextOptions = {
|
|
stateful: this.stateful,
|
|
defaultCorpId: this.defaultCorpId
|
|
}): void {
|
|
const currentId = this.current?.id;
|
|
this.stateful = options.stateful;
|
|
this.defaultCorpId = options.defaultCorpId;
|
|
this.replaceCorps(corps);
|
|
this.selectCurrent(currentId ? this.corps.get(currentId) ?? corps[0] : corps[0]);
|
|
}
|
|
|
|
private replaceCorps(corps: CorpConfig[]): void {
|
|
this.corps = new Map();
|
|
for (const corp of corps) {
|
|
this.corps.set(corp.id, corp);
|
|
}
|
|
}
|
|
|
|
getCurrent(): CorpConfig | null {
|
|
return this.current;
|
|
}
|
|
|
|
getCorp(corpId: string): CorpConfig | null {
|
|
return this.corps.get(corpId) ?? null;
|
|
}
|
|
|
|
resolve(corpId?: string): CorpConfig | null {
|
|
if (corpId) return this.getCorp(corpId);
|
|
return this.current;
|
|
}
|
|
|
|
switchCorp(corpId: string): boolean {
|
|
if (!this.stateful) return false;
|
|
const corp = this.corps.get(corpId);
|
|
if (!corp) return false;
|
|
this.current = corp;
|
|
this.defaultCorpId = corp.id;
|
|
return true;
|
|
}
|
|
|
|
listCorps(): Array<{ id: string; name: string; sourceType: string; current: boolean }> {
|
|
return Array.from(this.corps.values()).map((corp) => ({
|
|
id: corp.id,
|
|
name: corp.name,
|
|
sourceType: corp.source.type,
|
|
current: corp.id === this.current?.id
|
|
}));
|
|
}
|
|
|
|
private selectCurrent(fallback?: CorpConfig): void {
|
|
if (!this.stateful) {
|
|
this.current = null;
|
|
return;
|
|
}
|
|
|
|
this.current = this.defaultCorpId !== undefined
|
|
? this.corps.get(this.defaultCorpId) ?? null
|
|
: fallback ?? null;
|
|
}
|
|
}
|