restore-package-json.mjs 927 B

12345678910111213141516171819202122232425262728293031
  1. /**
  2. * postpack hook — restores original package.json from backup.
  3. *
  4. * Shared by all publishable packages. Invoked as:
  5. * "postpack": "node ../../scripts/restore-package-json.mjs"
  6. */
  7. import { readFile, rm, writeFile, rename } from "node:fs/promises";
  8. import { join } from "node:path";
  9. const packageDir = process.cwd();
  10. const packageJsonPath = join(packageDir, "package.json");
  11. const backupPath = join(packageDir, ".package.json.publish-backup");
  12. async function writeAtomic(path, content) {
  13. const tempPath = `${path}.tmp-${process.pid}-${Date.now()}`;
  14. await writeFile(tempPath, content, "utf-8");
  15. await rename(tempPath, path);
  16. }
  17. async function main() {
  18. try {
  19. const original = await readFile(backupPath, "utf-8");
  20. await writeAtomic(packageJsonPath, original);
  21. await rm(backupPath, { force: true });
  22. } catch {
  23. // No backup means prepack found nothing to replace — fine.
  24. }
  25. }
  26. await main();