read-existing-changelog.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. "use strict";
  2. const fs = require("fs-extra");
  3. const path = require("path");
  4. const { BLANK_LINE, COMMIT_GUIDELINE } = require("./constants");
  5. module.exports.readExistingChangelog = readExistingChangelog;
  6. /**
  7. * Read the existing changelog, if it exists.
  8. * @param {import("@lerna/package").Package} pkg
  9. * @returns {Promise<[string, string]>} A tuple of changelog location and contents
  10. */
  11. function readExistingChangelog(pkg) {
  12. const changelogFileLoc = path.join(pkg.location, "CHANGELOG.md");
  13. let chain = Promise.resolve();
  14. // catch allows missing file to pass without breaking chain
  15. chain = chain.then(() => fs.readFile(changelogFileLoc, "utf8").catch(() => ""));
  16. chain = chain.then((changelogContents) => {
  17. // Remove the header if it exists, thus starting at the first entry.
  18. const headerIndex = changelogContents.indexOf(COMMIT_GUIDELINE);
  19. if (headerIndex !== -1) {
  20. return changelogContents.substring(headerIndex + COMMIT_GUIDELINE.length + BLANK_LINE.length);
  21. }
  22. return changelogContents;
  23. });
  24. // consumer expects resolved tuple
  25. chain = chain.then((changelogContents) => [changelogFileLoc, changelogContents]);
  26. return chain;
  27. }