format-changelog.js 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. #!/usr/bin/env node
  2. // Adjusts the format of the changelog that changesets generates.
  3. // This is run automatically when npm version is run.
  4. const fs = require('fs');
  5. const changelog = fs.readFileSync('CHANGELOG.md', 'utf8');
  6. // Groups:
  7. // - 1: Pull Request Number and URL
  8. // - 2: Changeset entry
  9. const RELEASE_LINE_REGEX = /^- (\[#.*?\]\(.*?\))?.*?! - (.*)$/gm;
  10. // Captures vX.Y.Z or vX.Y.Z-rc.W
  11. const VERSION_TITLE_REGEX = /^## (\d+\.\d+\.\d+(-rc\.\d+)?)$/gm;
  12. const isPrerelease = process.env.PRERELEASE === 'true';
  13. const formatted = changelog
  14. // Remove titles
  15. .replace(/^### Major Changes\n\n/gm, '')
  16. .replace(/^### Minor Changes\n\n/gm, '')
  17. .replace(/^### Patch Changes\n\n/gm, '')
  18. // Remove extra whitespace between items
  19. .replace(/^(- \[.*\n)\n(?=-)/gm, '$1')
  20. // Format each release line
  21. .replace(RELEASE_LINE_REGEX, (_, pr, entry) => (pr ? `- ${entry} (${pr})` : `- ${entry}`))
  22. // Add date to new version
  23. .replace(VERSION_TITLE_REGEX, `\n## $1 (${new Date().toISOString().split('T')[0]})`)
  24. // Conditionally allow vX.Y.Z-rc.W sections only in prerelease
  25. .replace(/^## \d\.\d\.\d-rc\S+[^]+?(?=^#)/gm, section => (isPrerelease ? section : ''));
  26. fs.writeFileSync('CHANGELOG.md', formatted);