update-changelog-release-date.js 1.0 KB

123456789101112131415161718192021222324252627282930313233
  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 header = new RegExp(`^## (Unreleased|${version})$`, 'm');
  15. if (!header.test(changelog)) {
  16. console.error('Missing changelog entry');
  17. process.exit(1);
  18. }
  19. const newHeader =
  20. pkg.version.indexOf(suffix) === -1 ? `## ${version} (${new Date().toISOString().split('T')[0]})` : `## ${version}`;
  21. fs.writeFileSync('CHANGELOG.md', changelog.replace(header, newHeader));
  22. cp.execSync('git add CHANGELOG.md', { stdio: 'inherit' });