npm-exists.js 558 B

1234567891011121314151617181920212223
  1. "use strict";
  2. const got = require("got");
  3. const constant = value => _ => value;
  4. /**
  5. *
  6. * Checks if the given dependency/module is registered on npm
  7. *
  8. * @param {String} moduleName - The dependency to be checked
  9. * @returns {Promise} constant - Returns either true or false,
  10. * based on if it exists or not
  11. */
  12. module.exports = function npmExists(moduleName) {
  13. const hostname = "https://www.npmjs.org";
  14. const pkgUrl = `${hostname}/package/${moduleName}`;
  15. return got(pkgUrl, {
  16. method: "HEAD"
  17. })
  18. .then(constant(true))
  19. .catch(constant(false));
  20. };