Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 fix duplication with incremental update #60

Merged
merged 3 commits into from
Oct 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
bpetetot marked this conversation as resolved.
Show resolved Hide resolved
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)
})
})