symlink-binary.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. "use strict";
  2. const fs = require("fs-extra");
  3. const path = require("path");
  4. const pMap = require("p-map");
  5. const { Package } = require("@lerna/package");
  6. const { createSymlink } = require("@lerna/create-symlink");
  7. module.exports.symlinkBinary = symlinkBinary;
  8. /**
  9. * Symlink bins of srcPackage to node_modules/.bin in destPackage
  10. * @param {Object|string} srcPackageRef
  11. * @param {Object|string} destPackageRef
  12. * @returns {Promise}
  13. */
  14. function symlinkBinary(srcPackageRef, destPackageRef) {
  15. return Promise.all([Package.lazy(srcPackageRef), Package.lazy(destPackageRef)]).then(
  16. ([srcPackage, destPackage]) => {
  17. const actions = Object.keys(srcPackage.bin).map((name) => {
  18. const srcLocation = srcPackage.contents
  19. ? path.resolve(srcPackage.location, srcPackage.contents)
  20. : srcPackage.location;
  21. const src = path.join(srcLocation, srcPackage.bin[name]);
  22. const dst = path.join(destPackage.binLocation, name);
  23. // Symlink all declared binaries, even if they don't exist (yet). We will
  24. // assume the package author knows what they're doing and that the binaries
  25. // will be generated during a later build phase (potentially source compiled from
  26. // another language).
  27. return { src, dst };
  28. });
  29. if (actions.length === 0) {
  30. return Promise.resolve();
  31. }
  32. return fs.mkdirp(destPackage.binLocation).then(() =>
  33. pMap(actions, (meta) => {
  34. if (meta) {
  35. return createSymlink(meta.src, meta.dst, "exec");
  36. }
  37. })
  38. );
  39. }
  40. );
  41. }