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.
33 lines
1.1 KiB
TypeScript
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 };
|
|
}
|