Files
seal-use/src/probe.ts
T

76 lines
2.6 KiB
TypeScript

import { getEnterprisesDirCandidates, loadCorpConfigs, loadGlobalConfig } from "./core/config/loader.js";
import { resolveCorpConfig } from "./core/config/manager.js";
import { createSealClient } from "./core/http/factory.js";
import type { SealEnterpriseConfig } from "./core/auth/types.js";
import { resolveLiveSealEnterpriseConfig } from "./domains/seal/enterprise/source.js";
import { getApprovalContext } from "./domains/seal/api.js";
import { printJson } from "./cli/printer.js";
/** One probe run's payload — the entire stdout of `seal-use probe`. */
export type ProbeResult = {
corp: { id: string; name: string; sourceType: string };
enterprise: SealEnterpriseConfig;
summary: {
rules: number;
hasPendingRuleDeletes: boolean;
documents: number;
documentsTotal?: number;
stylePreferences: string;
};
};
export async function runProbe(options: { corpId?: string } = {}): Promise<ProbeResult> {
const corps = loadCorpConfigs();
const globalConfig = loadGlobalConfig();
const corp = resolveCorpConfig(
corps,
{
stateful: globalConfig.stateful,
defaultCorpId: globalConfig.defaultCorpId,
corpId: options.corpId
},
getEnterprisesDirCandidates()
);
// Progress notes go to stderr: stdout carries the JSON payload alone, so
// `seal-use probe | jq` keeps working (it previously held three prose lines
// interleaved with three JSON blobs).
console.error(`[probe] corp=${corp.id} source=${corp.source.type}`);
const enterprise = await resolveLiveSealEnterpriseConfig(corp);
console.error("[probe] seal enterprise");
const client = await createSealClient(corp);
const context = await getApprovalContext(client, {
documentLimit: 20,
stylePreferencesEndpoint: corp.seal.endpoints.approvalStylePreferences
});
console.error("[probe] approval context summary");
return {
corp: { id: corp.id, name: corp.name, sourceType: corp.source.type },
enterprise,
summary: {
rules: context.rules.rules.length,
hasPendingRuleDeletes: context.rules.hasPendingDeletes,
documents: context.documents.articles.length,
documentsTotal: context.documents.total,
stylePreferences: context.stylePreferences.ok
? "ok"
: `error: ${context.stylePreferences.error}`
}
};
}
if (import.meta.main) {
const args = process.argv.slice(2);
const corpId = args[0] === "--corp" ? args[1] : args[0];
runProbe({ corpId })
.then((result) => printJson(result))
.catch((error) => {
console.error(error instanceof Error ? error.message : error);
process.exit(1);
});
}