Skip to content

Commit

Permalink
🐛 fix duplication with incremental update (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
bpetetot authored Oct 29, 2018
1 parent 2a9904b commit c4ffc59
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
26 changes: 18 additions & 8 deletions packages/gitmoji-changelog-markdown/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function markdownFromScratch({ meta, changes }, options) {

function markdownIncremental({ meta, changes }, options) {
const { lastVersion } = meta
const { output, release } = options
const { output } = options

const tempFile = `${output}.tmp`

Expand All @@ -47,6 +47,7 @@ function markdownIncremental({ meta, changes }, options) {

let previousNextFound = false
let previousVersionFound = false
let nextVersionWritten = false

readStream
.pipe(new Transform({
Expand All @@ -57,23 +58,26 @@ function markdownIncremental({ meta, changes }, options) {
null,
string.split('\n')
.reduce(
(content, nextLine) => {
previousNextFound = previousNextFound || nextLine.startsWith(`<a name="${release}"></a>`)
previousVersionFound = nextLine.startsWith(`<a name="${lastVersion}"></a>`)
(content, nextLine, index, array) => {
previousVersionFound = matchVersionBreakpoint(nextLine, lastVersion)
previousNextFound = previousNextFound || matchVersionBreakpoint(nextLine)

// Remove old release (next version)
if (previousNextFound && !previousVersionFound) {
if (previousNextFound && !previousVersionFound && !nextVersionWritten) {
return content
}

// Rewrite the release (next version)
if (previousVersionFound) {
previousNextFound = false
if (previousVersionFound && !nextVersionWritten) {
nextVersionWritten = true
return `${content}${toMarkdown({ meta, changes })}${nextLine}\n`
}

// Just push the line without changing anything
return `${content}${nextLine}\n`
if (index !== array.length - 1) {
return `${content}${nextLine}\n`
}
return `${content}${nextLine}`
},
'',
)
Expand All @@ -88,6 +92,11 @@ function markdownIncremental({ meta, changes }, options) {
})
}

function matchVersionBreakpoint(tested, version = '.*') {
const regex = new RegExp(`<a name="${version}"></a>`)
return regex.test(tested)
}

function getShortHash(hash, repository) {
if (!hash) return null

Expand All @@ -113,6 +122,7 @@ function autolink(message, repository) {

module.exports = {
buildMarkdownFile,
matchVersionBreakpoint,
getShortHash,
autolink,
}
25 changes: 23 additions & 2 deletions packages/gitmoji-changelog-markdown/src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
const fs = require('fs')
const { Writable, Readable } = require('stream')

const { buildMarkdownFile, autolink, getShortHash } = require('./index')
const {
buildMarkdownFile,
matchVersionBreakpoint,
autolink,
getShortHash,
} = require('./index')

describe('Markdown converter', () => {
it('should generate full changelog into markdown from scratch', async () => {
Expand Down Expand Up @@ -169,7 +174,6 @@ I am the last version
<a name="1.0.0"></a>
## 1.0.0
I am the last version
`)
})
})
Expand Down Expand Up @@ -223,3 +227,20 @@ describe('autolink', () => {
expect(result).toBe(':bug: fix issue [#123](https://github.com/frinyvonnick/gitmoji-changelog/issues/123) and [#456](https://github.com/frinyvonnick/gitmoji-changelog/issues/456)')
})
})

describe('matchVersionBreakpoint', () => {
it('should return true if match with the given version breakpoint', () => {
const result = matchVersionBreakpoint('<a name="1.0.0"></a>', '1.0.0')
expect(result).toBe(true)
})

it('should return true if match with any version breakpoint', () => {
const result = matchVersionBreakpoint('<a name="next"></a>')
expect(result).toBe(true)
})

it('should return false if no match with a version breakpoint', () => {
const result = matchVersionBreakpoint('hello world', '1.0.0')
expect(result).toBe(false)
})
})

0 comments on commit c4ffc59

Please sign in to comment.