check-working-tree.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. "use strict";
  2. const { describeRef } = require("@lerna/describe-ref");
  3. const { ValidationError } = require("@lerna/validation-error");
  4. const { collectUncommitted } = require("@lerna/collect-uncommitted");
  5. module.exports.checkWorkingTree = checkWorkingTree;
  6. module.exports.mkThrowIfUncommitted = mkThrowIfUncommitted;
  7. module.exports.throwIfReleased = throwIfReleased;
  8. module.exports.throwIfUncommitted = mkThrowIfUncommitted();
  9. function checkWorkingTree({ cwd } = {}) {
  10. let chain = Promise.resolve();
  11. chain = chain.then(() => describeRef({ cwd }));
  12. // wrap each test separately to allow all applicable errors to be reported
  13. const tests = [
  14. // prevent duplicate versioning
  15. chain.then(throwIfReleased),
  16. // prevent publish of uncommitted changes
  17. chain.then(mkThrowIfUncommitted({ cwd })),
  18. ];
  19. // passes through result of describeRef() to aid composability
  20. return chain.then((result) => Promise.all(tests).then(() => result));
  21. }
  22. function throwIfReleased({ refCount }) {
  23. if (refCount === "0") {
  24. throw new ValidationError(
  25. "ERELEASED",
  26. "The current commit has already been released. Please make new commits before continuing."
  27. );
  28. }
  29. }
  30. const EUNCOMMIT_MSG =
  31. "Working tree has uncommitted changes, please commit or remove the following changes before continuing:\n";
  32. function mkThrowIfUncommitted(options = {}) {
  33. return function throwIfUncommitted({ isDirty }) {
  34. if (isDirty) {
  35. return collectUncommitted(options).then((uncommitted) => {
  36. throw new ValidationError("EUNCOMMIT", `${EUNCOMMIT_MSG}${uncommitted.join("\n")}`);
  37. });
  38. }
  39. };
  40. }