update-changelog-release-date.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/usr/bin/env node
  2. // Sets the release date of the current release in the changelog.
  3. // This is run automatically when npm version is run.
  4. const fs = require('fs');
  5. const cp = require('child_process');
  6. const suffix = process.env.PRERELEASE_SUFFIX || 'rc';
  7. const changelog = fs.readFileSync('CHANGELOG.md', 'utf8');
  8. // The changelog entry to be updated looks like this:
  9. // ## Unreleased
  10. // We need to add the version and release date in a YYYY-MM-DD format, so that it looks like this:
  11. // ## 2.5.3 (2019-04-25)
  12. const pkg = require('../../package.json');
  13. const version = pkg.version.replace(new RegExp('-' + suffix + '\\..*'), '');
  14. const unreleased = /^## Unreleased$/im;
  15. const released = new RegExp(`^## ${version} \\([-\\d]*\\)$`, 'm');
  16. if (released.test(changelog)) {
  17. process.exit(0);
  18. }
  19. if (!unreleased.test(changelog)) {
  20. console.error('Missing changelog entry');
  21. process.exit(1);
  22. }
  23. fs.writeFileSync('CHANGELOG.md', changelog.replace(
  24. unreleased,
  25. `## ${version} (${new Date().toISOString().split('T')[0]})`),
  26. );
  27. cp.execSync('git add CHANGELOG.md', { stdio: 'inherit' });