diff --git a/lib/lifecycles/changelog.js b/lib/lifecycles/changelog.js index f32eabdda..43733225d 100644 --- a/lib/lifecycles/changelog.js +++ b/lib/lifecycles/changelog.js @@ -6,6 +6,7 @@ const fs = require('fs') const presetLoader = require('../preset-loader') const runLifecycleScript = require('../run-lifecycle-script') const writeFile = require('../write-file') +const START_OF_LAST_RELEASE_PATTERN = /(^##? (?!Change Log$)| { createIfMissing(args) - let header = '# Change Log\n\nAll notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.\n' - let oldContent = args.dryRun ? '' : fs.readFileSync(args.infile, 'utf-8') + var header = '# Change Log\n\nAll notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.\n' + var oldContent = args.dryRun ? '' : fs.readFileSync(args.infile, 'utf-8') + var oldContentStart = oldContent.search(START_OF_LAST_RELEASE_PATTERN) // find the position of the last release and remove header: - const changelogSectionRegExp = /\n', 'utf-8') commit('feat: first commit') @@ -164,6 +164,25 @@ describe('cli', function () { content.should.not.match(/legacy header format/) }) + it('appends the new release above the last release, removing the old header (new format)', function () { + commit('feat: first commit') + shell.exec('git tag -a v1.0.0 -m "my awesome first release"') + commit('fix: patch release') + + execCli().code.should.equal(0) + var content = fs.readFileSync('CHANGELOG.md', 'utf-8') + + // remove commit hashes and dates to make testing against a static string easier: + content = content.replace(/patch release [0-9a-f]{6,8}/g, 'patch release ABCDEFXY').replace(/\([0-9]{4}-[0-9]{2}-[0-9]{2}\)/g, '(YYYY-MM-DD)') + content.should.equal('# Change Log\n\nAll notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.\n\n## [1.0.1](/compare/v1.0.0...v1.0.1) (YYYY-MM-DD)\n\n\n### Bug Fixes\n\n* patch release ABCDEFXY\n') + + commit('fix: another patch release') + execCli().code.should.equal(0) + content = fs.readFileSync('CHANGELOG.md', 'utf-8') + content = content.replace(/patch release [0-9a-f]{6,8}/g, 'patch release ABCDEFXY').replace(/\([0-9]{4}-[0-9]{2}-[0-9]{2}\)/g, '(YYYY-MM-DD)') + content.should.equal('# Change Log\n\nAll notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.\n\n## [1.0.2](/compare/v1.0.1...v1.0.2) (YYYY-MM-DD)\n\n\n### Bug Fixes\n\n* another patch release ABCDEFXY\n\n\n\n## [1.0.1](/compare/v1.0.0...v1.0.1) (YYYY-MM-DD)\n\n\n### Bug Fixes\n\n* patch release ABCDEFXY\n') + }) + it('commits all staged files', function () { fs.writeFileSync('CHANGELOG.md', 'legacy header format\n', 'utf-8')