is-package-bin.js 824 B

12345678910111213141516171819202122232425
  1. // Function to determine whether a path is in the package.bin set.
  2. // Used to prevent issues when people publish a package from a
  3. // windows machine, and then install with --no-bin-links.
  4. //
  5. // Note: this is not possible in remote or file fetchers, since
  6. // we don't have the manifest until AFTER we've unpacked. But the
  7. // main use case is registry fetching with git a distant second,
  8. // so that's an acceptable edge case to not handle.
  9. const binObj = (name, bin) =>
  10. typeof bin === 'string' ? { [name]: bin } : bin
  11. const hasBin = (pkg, path) => {
  12. const bin = binObj(pkg.name, pkg.bin)
  13. const p = path.replace(/^[^\\/]*\//, '')
  14. for (const kv of Object.entries(bin)) {
  15. if (kv[1] === p) {
  16. return true
  17. }
  18. }
  19. return false
  20. }
  21. module.exports = (pkg, path) =>
  22. pkg && pkg.bin ? hasBin(pkg, path) : false