/* global process, URL, console */ import { existsSync, readFileSync, writeFileSync, unlinkSync } from "node:fs"; import { pathToFileURL } from "node:url"; export function applyPrepackTransform(pkg) { const devDependencies = { ...(pkg.devDependencies || {}) }; delete devDependencies["@fusion/core"]; delete devDependencies["@fusion/dashboard"]; delete devDependencies["@fusion/engine"]; delete devDependencies["@fusion/pi-claude-cli"]; delete devDependencies["@fusion/pi-llama-cpp"]; delete devDependencies["@fusion-plugin-examples/roadmap"]; return { ...pkg, devDependencies, // Inject exports only for the packed manifest so workspace/dev resolution // remains unchanged (postpack restore reverts this file to original state). exports: { ...(pkg.exports || {}), "./plugin-sdk": { types: "./dist/plugin-sdk/index.d.ts", import: "./dist/plugin-sdk/index.js", }, "./package.json": "./package.json", }, }; } function run() { const mode = process.argv[2]; const packageJsonPath = new URL("../package.json", import.meta.url); const backupPath = new URL("../package.json.pack-backup", import.meta.url); if (mode === "prepack") { if (existsSync(backupPath)) { unlinkSync(backupPath); } const original = readFileSync(packageJsonPath, "utf8"); writeFileSync(backupPath, original, "utf8"); const pkg = JSON.parse(original); const transformed = applyPrepackTransform(pkg); writeFileSync(packageJsonPath, `${JSON.stringify(transformed, null, 2)}\n`, "utf8"); process.exit(0); } if (mode === "postpack") { if (!existsSync(backupPath)) { process.exit(0); } const backup = readFileSync(backupPath, "utf8"); writeFileSync(packageJsonPath, backup, "utf8"); unlinkSync(backupPath); process.exit(0); } console.error("Usage: node ./scripts/prepare-publish-manifest.mjs "); process.exit(1); } if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { run(); }