get-unpublished-packages.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. "use strict";
  2. const log = require("npmlog");
  3. const pMap = require("p-map");
  4. const pacote = require("pacote");
  5. module.exports.getUnpublishedPackages = getUnpublishedPackages;
  6. /**
  7. * Retrieve a list of graph nodes for packages that need to be published.
  8. * @param {import("@lerna/package-graph").PackageGraph} packageGraph
  9. * @param {import("./fetch-config").FetchConfig} opts
  10. * @returns {Promise<import("@lerna/package-graph").PackageGraphNode[]>}
  11. */
  12. function getUnpublishedPackages(packageGraph, opts) {
  13. log.silly("getUnpublishedPackages");
  14. let chain = Promise.resolve();
  15. // don't bother attempting to get the packument for private packages
  16. const graphNodesToCheck = Array.from(packageGraph.values()).filter(({ pkg }) => !pkg.private);
  17. const mapper = (pkg) =>
  18. pacote.packument(pkg.name, opts).then(
  19. (packument) => {
  20. if (packument.versions === undefined || packument.versions[pkg.version] === undefined) {
  21. return pkg;
  22. }
  23. },
  24. () => {
  25. log.warn("", "Unable to determine published version, assuming %j unpublished.", pkg.name);
  26. return pkg;
  27. }
  28. );
  29. chain = chain.then(() => pMap(graphNodesToCheck, mapper, { concurrency: 4 }));
  30. return chain.then((results) => results.filter(Boolean));
  31. }