diff --git a/CHANGELOG.md b/CHANGELOG.md
index 091ceb1..66a87ab 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,10 +1,11 @@
# Changelog
-
+
## 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's date on version ([#22](https://github.com/frinyvonnick/gitmoji-changelog/issues/22)) ([ab3d9c6](https://github.com/frinyvonnick/gitmoji-changelog/commit/ab3d9c600e307dd0db16bc7abcbdf8a8a2c83ff5))
diff --git a/packages/gitmoji-changelog-cli/src/cli.js b/packages/gitmoji-changelog-cli/src/cli.js
index d97f80f..7c32b47 100644
--- a/packages/gitmoji-changelog-cli/src/cli.js
+++ b/packages/gitmoji-changelog-cli/src/cli.js
@@ -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) {
diff --git a/packages/gitmoji-changelog-cli/src/index.js b/packages/gitmoji-changelog-cli/src/index.js
index 3706a95..b308de2 100755
--- a/packages/gitmoji-changelog-cli/src/index.js
+++ b/packages/gitmoji-changelog-cli/src/index.js
@@ -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',
diff --git a/packages/gitmoji-changelog-core/src/index.js b/packages/gitmoji-changelog-core/src/index.js
index d61f936..64360c9 100644
--- a/packages/gitmoji-changelog-core/src/index.js
+++ b/packages/gitmoji-changelog-core/src/index.js
@@ -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),
}
@@ -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 = []
@@ -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 {
diff --git a/packages/gitmoji-changelog-markdown/src/index.js b/packages/gitmoji-changelog-markdown/src/index.js
index ba83a0d..dd71359 100755
--- a/packages/gitmoji-changelog-markdown/src/index.js
+++ b/packages/gitmoji-changelog-markdown/src/index.js
@@ -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 })
}
@@ -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(``)) {
- writer.write(line)
+ incrementalWriting = true
+ }
+ if (incrementalWriting) {
+ writer.write(`${line}\n`)
}
}).on('end', () => {
writer.end()
diff --git a/packages/gitmoji-changelog-markdown/src/template.md b/packages/gitmoji-changelog-markdown/src/template.md
index 307c5a4..0b77474 100644
--- a/packages/gitmoji-changelog-markdown/src/template.md
+++ b/packages/gitmoji-changelog-markdown/src/template.md
@@ -1,7 +1,7 @@
# Changelog
{{#each changelog}}
-
+
## {{version}}{{#if date}} - {{date}}{{/if}}
{{#each groups}}