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 pkg = require('../../package.json');
  7. const suffix = process.env.PRERELEASE_SUFFIX || 'rc';
  8. if (pkg.version.indexOf('-' + suffix) !== -1) {
  9. process.exit(0);
  10. }
  11. const version = pkg.version.replace(/-.*/, ''); // Remove the rc suffix
  12. const changelog = fs.readFileSync('CHANGELOG.md', 'utf8');
  13. // The changelog entry to be updated looks like this:
  14. // ## 2.5.3 (unreleased)
  15. // We need to add the date in a YYYY-MM-DD format, so that it looks like this:
  16. // ## 2.5.3 (2019-04-25)
  17. if (changelog.indexOf(`## ${version} (unreleased)`) === -1) {
  18. throw Error(`Found no changelog entry for version ${version}`);
  19. }
  20. fs.writeFileSync('CHANGELOG.md', changelog.replace(
  21. `## ${version} (unreleased)`,
  22. `## ${version} (${new Date().toISOString().split('T')[0]})`)
  23. );
  24. cp.execSync('git add CHANGELOG.md', { stdio: 'inherit' });