Skip to content

Commit

Permalink
✨ add incremental markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Petetot committed Oct 5, 2018
1 parent 2c148a6 commit 11b71e6
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 14 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Changelog

<a name="next"></a>
<a name="vnext"></a>
## next

### Added

- :sparkles: write incremental markdown ([2c148a6](https://github.com/frinyvonnick/gitmoji-changelog/commit/2c148a696eede21e2a35740bb25ac34db87e2371))
- :sparkles: write process to generate incremental markdown file ([f86030c](https://github.com/frinyvonnick/gitmoji-changelog/commit/f86030c2b43f71d709eae9920be53dcfa91d34a4))
- :sparkles: Add logger ([#21](https://github.com/frinyvonnick/gitmoji-changelog/issues/21)) ([93887ca](https://github.com/frinyvonnick/gitmoji-changelog/commit/93887ca1f2e59263a31b3b700507aa1e9853118c))
- :sparkles: Add tag&#x27;s date on version ([#22](https://github.com/frinyvonnick/gitmoji-changelog/issues/22)) ([ab3d9c6](https://github.com/frinyvonnick/gitmoji-changelog/commit/ab3d9c600e307dd0db16bc7abcbdf8a8a2c83ff5))
Expand Down
14 changes: 9 additions & 5 deletions packages/gitmoji-changelog-cli/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ const { generateChangelog, logger } = require('@gitmoji-changelog/core')
const { buildMarkdownFile } = require('@gitmoji-changelog/markdown')
const fs = require('fs')

async function main({ format, mode } = {}) {
async function main({ format, mode, release } = {}) {
try {
const changelog = await generateChangelog({ mode })
const changelog = await generateChangelog({ mode, release })

logger.info(changelog.meta.package.name)
logger.info('v%s', changelog.meta.package.version)
logger.info(changelog.meta.repository.url)
if (changelog.meta.package) {
const { name, version } = changelog.meta.package
logger.info(`${name} v${version}`)
}
if (changelog.meta.repository) {
logger.info(changelog.meta.repository.url)
}

let output
switch (format) {
Expand Down
6 changes: 6 additions & 0 deletions packages/gitmoji-changelog-cli/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ const args = yargs
description: 'build from scratch or incremental changelog',
choices: ['init', 'incremental'],
})
.option('release', {
alias: 'r',
default: 'from-package',
description: 'next release version for the changelog from package.json or next release',
choices: ['from-package', 'next'],
})
.option('format', {
alias: 'f',
default: 'markdown',
Expand Down
19 changes: 14 additions & 5 deletions packages/gitmoji-changelog-core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,19 @@ function makeGroups(commits) {
.filter(group => group.commits.length)
}

function getVersionFromTagName(tagName) {
if (tagName && tagName.startsWith('v')) {
return tagName.slice(1, tagName.length)
}
return tagName
}

async function generateChanges(from, to) {
const commits = await getCommits(from, to)
const lastCommitDate = getLastCommitDate(commits)

return {
version: to,
version: getVersionFromTagName(to),
date: to && lastCommitDate,
groups: makeGroups(commits),
}
Expand All @@ -60,7 +67,9 @@ async function generateTagsChanges(tags) {
}

async function generateChangelog(options = {}) {
const { mode = 'init', version = 'next' } = options
const { mode = 'init', release } = options

const packageInfo = await getPackageInfo()

let changes = []

Expand All @@ -71,13 +80,13 @@ async function generateChangelog(options = {}) {
changes = await generateTagsChanges(tags)
}

if (version) {
const nextVersion = release === 'from-package' ? packageInfo.version : release
if (mode !== 'init' && nextVersion) {
const lastChanges = await generateChanges(lastTag)
lastChanges.version = version
lastChanges.version = nextVersion
changes.push(lastChanges)
}

const packageInfo = await getPackageInfo()
const repository = await getRepoInfo(packageInfo)

return {
Expand Down
9 changes: 7 additions & 2 deletions packages/gitmoji-changelog-markdown/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function toMarkdown({ meta, changes }) {
message: autolink(commit.message, meta.repository),
body: autolink(commit.body, meta.repository),
}))
.sort((r1, r2) => r1.version < r2.version) // TODO sort must be done in core

return compileTemplate({ changelog })
}
Expand All @@ -51,11 +52,15 @@ function markdownIncremental({ meta, changes }, options) {
writer.write(nextVersionOuput)

// read original file until last tags and add it to the end
let incrementalWriting = false
return new Promise(resolve => {
const stream = readline(currentFile)
const stream = readline(currentFile) // TODO find a better lib than 'readline'
stream.on('line', (line) => {
if (line.startsWith(`<a name="${lastTag}"></a>`)) {
writer.write(line)
incrementalWriting = true
}
if (incrementalWriting) {
writer.write(`${line}\n`)
}
}).on('end', () => {
writer.end()
Expand Down
2 changes: 1 addition & 1 deletion packages/gitmoji-changelog-markdown/src/template.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Changelog

{{#each changelog}}
<a name="{{version}}"></a>
<a name="v{{version}}"></a>
## {{version}}{{#if date}} - {{date}}{{/if}}

{{#each groups}}
Expand Down

0 comments on commit 11b71e6

Please sign in to comment.