verify-entry-lib.mjs 1.1 KB

12345678910111213141516171819202122232425262728
  1. #!/usr/bin/env node
  2. /**
  3. * Prepack gate for entry packages: refuse to pack a tarball whose built
  4. * `lib/` is missing. Entry `files` lists use globs, and a glob matching
  5. * nothing packs a silently JS-less tarball instead of failing — this gate
  6. * turns that into a loud refusal on a checkout that never ran
  7. * `pnpm build:ts`.
  8. *
  9. * Runs from each entry package's `prepack` hook (pnpm sets the script cwd
  10. * to the package directory).
  11. */
  12. import fs from 'node:fs';
  13. import path from 'node:path';
  14. const packageDir = process.cwd();
  15. const manifest = JSON.parse(fs.readFileSync(path.join(packageDir, 'package.json'), 'utf8'));
  16. const exportedFiles = Object.values(manifest.exports)
  17. .flatMap((entry) => typeof entry === 'string' ? [entry] : Object.values(entry))
  18. .filter((file) => typeof file === 'string' && file.startsWith('./lib/'));
  19. for (const file of exportedFiles) {
  20. if (!fs.existsSync(path.join(packageDir, file))) {
  21. console.error(`verify-entry-lib: ${manifest.name} has no ${file} — run \`pnpm build:ts\` before packing.`);
  22. process.exit(1);
  23. }
  24. }
  25. console.log(`verify-entry-lib: ${manifest.name} built lib/ present.`);