fix(cli): harden setup and destructive workflows
This commit is contained in:
@@ -4,26 +4,43 @@ import writeFileAtomic from "write-file-atomic";
|
||||
|
||||
import { PRODUCT_IDENTITY } from "../identity.js";
|
||||
|
||||
const canonicalSkillDestination = (home: string): string =>
|
||||
join(home, ".agents/skills", PRODUCT_IDENTITY.skillName, "SKILL.md");
|
||||
|
||||
const readCanonicalSkill = (): Promise<string> =>
|
||||
readFile(
|
||||
new URL(
|
||||
`../../skills/${PRODUCT_IDENTITY.skillName}/SKILL.md`,
|
||||
import.meta.url,
|
||||
),
|
||||
"utf8",
|
||||
);
|
||||
|
||||
/** Determine whether setup would need to install or update the canonical skill. */
|
||||
export const canonicalSkillInstallationNeeded = async (
|
||||
home: string,
|
||||
): Promise<boolean> => {
|
||||
try {
|
||||
const [canonical, installed] = await Promise.all([
|
||||
readCanonicalSkill(),
|
||||
readFile(canonicalSkillDestination(home), "utf8").catch(() => undefined),
|
||||
]);
|
||||
return installed !== canonical;
|
||||
} catch {
|
||||
// Disclose the action when setup cannot prove the installed copy is aligned.
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
/** Transactionally install or upgrade the versioned canonical REA skill. */
|
||||
export const installCanonicalSkill = async (
|
||||
home: string,
|
||||
): Promise<"installed" | "unchanged" | "failed"> => {
|
||||
const destination = join(
|
||||
home,
|
||||
".agents/skills",
|
||||
PRODUCT_IDENTITY.skillName,
|
||||
"SKILL.md",
|
||||
);
|
||||
const destination = canonicalSkillDestination(home);
|
||||
const backup = `${destination}.rea.backup`;
|
||||
let original: string | undefined;
|
||||
try {
|
||||
const content = await readFile(
|
||||
new URL(
|
||||
`../../skills/${PRODUCT_IDENTITY.skillName}/SKILL.md`,
|
||||
import.meta.url,
|
||||
),
|
||||
"utf8",
|
||||
);
|
||||
const content = await readCanonicalSkill();
|
||||
original = await readFile(destination, "utf8").catch(() => undefined);
|
||||
if (original === content) return "unchanged";
|
||||
await mkdir(dirname(destination), { recursive: true });
|
||||
|
||||
Reference in New Issue
Block a user