Skip to content

Commit db2c6e5

Browse files
jthomersonbcoe
authored andcommittedMay 5, 2019
fix: prevent duplicate headers from being added (#305) (#307)
1 parent bf41474 commit db2c6e5

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed
 

‎lib/lifecycles/changelog.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const fs = require('fs')
66
const presetLoader = require('../preset-loader')
77
const runLifecycleScript = require('../run-lifecycle-script')
88
const writeFile = require('../write-file')
9+
const START_OF_LAST_RELEASE_PATTERN = /(^##? (?!Change Log$)|<a name=)/m
910

1011
module.exports = function (args, newVersion) {
1112
if (args.skip.changelog) return Promise.resolve()
@@ -21,12 +22,12 @@ module.exports = function (args, newVersion) {
2122
function outputChangelog (args, newVersion) {
2223
return new Promise((resolve, reject) => {
2324
createIfMissing(args)
24-
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'
25-
let oldContent = args.dryRun ? '' : fs.readFileSync(args.infile, 'utf-8')
25+
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'
26+
var oldContent = args.dryRun ? '' : fs.readFileSync(args.infile, 'utf-8')
27+
var oldContentStart = oldContent.search(START_OF_LAST_RELEASE_PATTERN)
2628
// find the position of the last release and remove header:
27-
const changelogSectionRegExp = /<a name=|##? \[?[0-9]+\.[0-9]+\.[0-9]+\]?/
28-
if (oldContent.search(changelogSectionRegExp) !== -1) {
29-
oldContent = oldContent.substring(oldContent.search(changelogSectionRegExp))
29+
if (oldContentStart !== -1) {
30+
oldContent = oldContent.substring(oldContentStart)
3031
}
3132
let content = ''
3233
let context

‎test.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ describe('cli', function () {
151151
})
152152

153153
describe('CHANGELOG.md exists', function () {
154-
it('appends the new release above the last release, removing the old header', function () {
154+
it('appends the new release above the last release, removing the old header (legacy format)', function () {
155155
fs.writeFileSync('CHANGELOG.md', 'legacy header format<a name="1.0.0">\n', 'utf-8')
156156

157157
commit('feat: first commit')
@@ -164,6 +164,25 @@ describe('cli', function () {
164164
content.should.not.match(/legacy header format/)
165165
})
166166

167+
it('appends the new release above the last release, removing the old header (new format)', function () {
168+
commit('feat: first commit')
169+
shell.exec('git tag -a v1.0.0 -m "my awesome first release"')
170+
commit('fix: patch release')
171+
172+
execCli().code.should.equal(0)
173+
var content = fs.readFileSync('CHANGELOG.md', 'utf-8')
174+
175+
// remove commit hashes and dates to make testing against a static string easier:
176+
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)')
177+
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')
178+
179+
commit('fix: another patch release')
180+
execCli().code.should.equal(0)
181+
content = fs.readFileSync('CHANGELOG.md', 'utf-8')
182+
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)')
183+
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')
184+
})
185+
167186
it('commits all staged files', function () {
168187
fs.writeFileSync('CHANGELOG.md', 'legacy header format<a name="1.0.0">\n', 'utf-8')
169188

0 commit comments

Comments
 (0)
Please sign in to comment.