Files
seal-use/src/core/config/repository.ts
T
gitea a189f49223 Group core utility modules into util, skill, and update
Move duration, redact, runtime, secure-file, and semver under core/util; embedded-skill and skill-version under core/skill; update-cache under core/update. Move the dev-override test next to the script it covers, and update the embedded-skill generator output path and regenerated manifest. No behavior change.
2026-11-19 16:31:50 +08:00

33 lines
1.1 KiB
TypeScript

import { existsSync } from "node:fs";
import { writeSecureFile } from "../util/secure-file.js";
import { join } from "node:path";
import { resolveWritableEnterprisesDir } from "./loader.js";
import { CorpConfigSchema, type CorpConfig } from "./types.js";
export type SavedCorpConfig = {
config: CorpConfig;
path: string;
};
function safeCorpFileName(value: string): string {
const normalized = value.trim().replace(/[^a-zA-Z0-9._-]+/g, "-");
return normalized.replace(/^-+|-+$/g, "") || "enterprise";
}
/** Persist one validated enterprise config without coupling callers to CLI code. */
export function saveCorpConfig(
input: CorpConfig,
options: { overwrite?: boolean } = {}
): SavedCorpConfig {
const config = CorpConfigSchema.parse(input);
const enterprisesDir = resolveWritableEnterprisesDir();
const path = join(enterprisesDir, `${safeCorpFileName(config.id)}.json`);
if (existsSync(path) && options.overwrite !== true) {
throw new Error(`Enterprise config already exists: ${path}. Pass "overwrite": true to replace it.`);
}
writeSecureFile(path, `${JSON.stringify(config, null, 2)}\n`);
return { config, path };
}