UNPKG

@clipboard-health/ai-rules

Version:

Pre-built AI agent rules for consistent coding standards.

79 lines (78 loc) 3.27 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /* eslint-disable unicorn/no-process-exit, n/no-process-exit */ const promises_1 = require("node:fs/promises"); const node_path_1 = __importDefault(require("node:path")); const constants_1 = require("./constants"); const toErrorMessage_1 = require("./toErrorMessage"); const PATHS = { projectRoot: node_path_1.default.join(__dirname, "../../../.."), rules: node_path_1.default.join(__dirname, ".."), }; async function sync() { try { const profile = getProfileFromArguments(); // Force copy files; rely on `git` if it overwrites files. await (0, promises_1.cp)(node_path_1.default.join(PATHS.rules, profile), PATHS.projectRoot, { force: true, recursive: true }); console.log(`✅ @clipboard-health/ai-rules synced ${profile}`); // Append OVERLAY.md content if it exists await appendOverlayToFiles({ filesToUpdate: ["CLAUDE.md", "AGENTS.md"], projectRoot: PATHS.projectRoot, }); } catch (error) { // Log error but exit gracefully to avoid breaking installs console.error(`⚠️ @clipboard-health/ai-rules sync failed: ${(0, toErrorMessage_1.toErrorMessage)(error)}`); process.exit(0); } } function getProfileFromArguments() { const profile = process.argv[2]; if (!profile || !(profile in constants_1.PROFILES)) { console.error("❌ Error: Invalid profile argument"); console.error(`Usage: npm run sync <profile>`); console.error(`Available profiles: ${Object.keys(constants_1.PROFILES).join(", ")}`); process.exit(1); } return profile; } /** * Appends OVERLAY.md content to specified files if OVERLAY.md exists. */ async function appendOverlayToFiles(params) { const { filesToUpdate, projectRoot } = params; const overlayPath = node_path_1.default.join(projectRoot, "OVERLAY.md"); const overlayContent = await readOverlayContent(overlayPath); if (!overlayContent) { // OVERLAY.md doesn't exist or can't be read, nothing to append return; } // Append to each file await Promise.all(filesToUpdate.map(async (file) => { const filePath = node_path_1.default.join(projectRoot, file); try { const currentContent = await (0, promises_1.readFile)(filePath, "utf8"); const updatedContent = `${currentContent}\n<!-- Source: ./OVERLAY.md -->\n\n${overlayContent}`; await (0, promises_1.writeFile)(filePath, updatedContent, "utf8"); } catch (error) { console.warn(`⚠️ Could not append overlay to ${file}: ${(0, toErrorMessage_1.toErrorMessage)(error)}`); } })); console.log(`📎 Appended OVERLAY.md to ${filesToUpdate.join(", ")}`); } async function readOverlayContent(overlayPath) { try { return await (0, promises_1.readFile)(overlayPath, "utf8"); } catch { return undefined; } } // eslint-disable-next-line unicorn/prefer-top-level-await void sync(); /* eslint-enable unicorn/no-process-exit, n/no-process-exit */