Skip to content

Commit

Permalink
✨ Use handlebars templating to generate markdown (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
bpetetot authored and frinyvonnick committed Sep 17, 2018
1 parent 2d783e4 commit 141d160
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 50 deletions.
24 changes: 13 additions & 11 deletions packages/gitmoji-changelog-core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,21 @@ function getCommits(from, to) {

async function generateChangelog() {
let previousTag = ''
const result = {}
const tags = await gitSemverTagsAsync()

if (tags.length > 0) {
await Promise.all(tags.map(async tag => {
const commits = await getCommits(previousTag, tag)

result[tag] = { commits }
previousTag = tag
}))
}

result.next = { commits: await getCommits(previousTag) }
const result = await Promise.all(tags.map(async tag => {
const commits = await getCommits(previousTag, tag)
previousTag = tag
return {
version: tag,
commits,
}
}))

result.push({
version: 'next',
commits: await getCommits(previousTag),
})

return result
}
Expand Down
29 changes: 16 additions & 13 deletions packages/gitmoji-changelog-core/src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ Waouh this is awesome 2

const result = await generateChangelog()

expect(result).toEqual({
next: {
expect(result).toEqual([
{
version: 'next',
commits: [
{
hash: 'c40ee8669ba7ea5151adc2942fa8a7fc98d9e23c',
Expand All @@ -50,36 +51,38 @@ Waouh this is awesome 2
},
],
},
})
])
})

it('should generate changelog in json format for all tags', async () => {
gitSemverTags.mockImplementation((cb) => cb(null, ['v1.0.0']))

const result = await generateChangelog()

expect(result).toEqual({
next: {
expect(result).toEqual([
{
version: 'v1.0.0',
commits: [
{
hash: 'c40ee8669ba7ea5151adc2942fa8a7fc98d9e23c',
date: '2018-08-28T10:07:00+02:00',
hash: 'c40ee8669ba7ea5151adc2942fa8a7fc98d9e23f',
date: '2018-08-28T10:06:00+02:00',
subject: ':sparkles: Upgrade brand new feature',
body: 'Waouh this is awesome 3',
body: 'Waouh this is awesome 2',
},
],
},
'v1.0.0': {
{
version: 'next',
commits: [
{
hash: 'c40ee8669ba7ea5151adc2942fa8a7fc98d9e23f',
date: '2018-08-28T10:06:00+02:00',
hash: 'c40ee8669ba7ea5151adc2942fa8a7fc98d9e23c',
date: '2018-08-28T10:07:00+02:00',
subject: ':sparkles: Upgrade brand new feature',
body: 'Waouh this is awesome 2',
body: 'Waouh this is awesome 3',
},
],
},
})
])
})
})

Expand Down
9 changes: 6 additions & 3 deletions packages/gitmoji-changelog-markdown/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"bin": {
"gitmoji-changelog": "./src/index.js"
},
"engines" : {
"node" : ">=10"
"engines": {
"node": ">=10"
},
"repository": {
"type": "git",
Expand All @@ -18,5 +18,8 @@
"bugs": {
"url": "https://github.com/frinyvonnick/gitmoji-changelog/issues"
},
"homepage": "https://github.com/frinyvonnick/gitmoji-changelog#readme"
"homepage": "https://github.com/frinyvonnick/gitmoji-changelog#readme",
"dependencies": {
"handlebars": "^4.0.12"
}
}
16 changes: 10 additions & 6 deletions packages/gitmoji-changelog-markdown/src/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
const fs = require('fs')
const path = require('path')
const handlebars = require('handlebars')

const MARKDOWN_TEMPLATE = path.join(__dirname, 'template.md')

function convert(changelog) {
const versions = Object.keys(changelog)
const template = fs.readFileSync(MARKDOWN_TEMPLATE, 'utf-8')

const generateMarkdown = handlebars.compile(template)

return versions.reduce((markdown, version) => {
return changelog[version].commits.reduce((commitMarkdown, commit) => {
return `${commitMarkdown}- ${commit.subject} (${commit.hash})\n`
}, `${markdown}\n## ${version}\n\n`)
}, '# Changelog\n')
return generateMarkdown({ changelog })
}

module.exports = {
Expand Down
33 changes: 18 additions & 15 deletions packages/gitmoji-changelog-markdown/src/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,42 @@
const { convert } = require('./index')

describe('Markdown converter', () => {
it('should convert json changelog into markdown', () => {
const json = {
next: {
it('should convert changelog into markdown', () => {
const changelog = [
{
version: 'v1.0.0',
commits: [
{
hash: 'c40ee8669ba7ea5151adc2942fa8a7fc98d9e23c',
date: '2018-08-28T10:07:00+02:00',
hash: 'c40ee8669ba7ea5151adc2942fa8a7fc98d9e23f',
date: '2018-08-28T10:06:00+02:00',
subject: ':sparkles: Upgrade brand new feature',
body: 'Waouh this is awesome 3',
body: 'Waouh this is awesome 2',
},
],
},
'v1.0.0': {
{
version: 'next',
commits: [
{
hash: 'c40ee8669ba7ea5151adc2942fa8a7fc98d9e23f',
date: '2018-08-28T10:06:00+02:00',
hash: 'c40ee8669ba7ea5151adc2942fa8a7fc98d9e23c',
date: '2018-08-28T10:07:00+02:00',
subject: ':sparkles: Upgrade brand new feature',
body: 'Waouh this is awesome 2',
body: 'Waouh this is awesome 3',
},
],
},
}
]

expect(convert(json)).toEqual(`# Changelog
expect(convert(changelog)).toEqual(`# Changelog
## v1.0.0
- :sparkles: Upgrade brand new feature (c40ee8669ba7ea5151adc2942fa8a7fc98d9e23f)
## next
- :sparkles: Upgrade brand new feature (c40ee8669ba7ea5151adc2942fa8a7fc98d9e23c)
## v1.0.0
- :sparkles: Upgrade brand new feature (c40ee8669ba7ea5151adc2942fa8a7fc98d9e23f)
`)
})
})
10 changes: 10 additions & 0 deletions packages/gitmoji-changelog-markdown/src/template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Changelog

{{#each changelog}}
## {{version}}

{{#each commits}}
- {{subject}} ({{hash}})
{{/each}}

{{/each}}
25 changes: 23 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ async@^1.4.0, async@^1.5.0:
version "1.5.2"
resolved "https://registry.yarnpkg.com/async/-/async-1.5.2.tgz#ec6a61ae56480c0c3cb241c95618e20892f9672a"

async@^2.1.4:
async@^2.1.4, async@^2.5.0:
version "2.6.1"
resolved "https://registry.yarnpkg.com/async/-/async-2.6.1.tgz#b245a23ca71930044ec53fa46aa00a3e87c6a610"
dependencies:
Expand Down Expand Up @@ -668,6 +668,10 @@ command-join@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/command-join/-/command-join-2.0.0.tgz#52e8b984f4872d952ff1bdc8b98397d27c7144cf"

commander@~2.17.1:
version "2.17.1"
resolved "https://registry.yarnpkg.com/commander/-/commander-2.17.1.tgz#bd77ab7de6de94205ceacc72f1716d29f20a77bf"

compare-func@^1.3.1:
version "1.3.2"
resolved "https://registry.yarnpkg.com/compare-func/-/compare-func-1.3.2.tgz#99dd0ba457e1f9bc722b12c08ec33eeab31fa648"
Expand Down Expand Up @@ -1686,6 +1690,16 @@ growly@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/growly/-/growly-1.3.0.tgz#f10748cbe76af964b7c96c93c6bcc28af120c081"

handlebars@^4.0.12:
version "4.0.12"
resolved "https://registry.yarnpkg.com/handlebars/-/handlebars-4.0.12.tgz#2c15c8a96d46da5e266700518ba8cb8d919d5bc5"
dependencies:
async "^2.5.0"
optimist "^0.6.1"
source-map "^0.6.1"
optionalDependencies:
uglify-js "^3.1.4"

handlebars@^4.0.2, handlebars@^4.0.3:
version "4.0.11"
resolved "https://registry.yarnpkg.com/handlebars/-/handlebars-4.0.11.tgz#630a35dfe0294bc281edae6ffc5d329fc7982dcc"
Expand Down Expand Up @@ -3895,7 +3909,7 @@ source-map@^0.5.3, source-map@^0.5.6, source-map@^0.5.7, source-map@~0.5.1:
version "0.5.7"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.5.7.tgz#8a039d2d1021d22d1ea14c80d8ea468ba2ef3fcc"

source-map@^0.6.0, source-map@~0.6.1:
source-map@^0.6.0, source-map@^0.6.1, source-map@~0.6.1:
version "0.6.1"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"

Expand Down Expand Up @@ -4255,6 +4269,13 @@ uglify-js@^2.6:
optionalDependencies:
uglify-to-browserify "~1.0.0"

uglify-js@^3.1.4:
version "3.4.9"
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.4.9.tgz#af02f180c1207d76432e473ed24a28f4a782bae3"
dependencies:
commander "~2.17.1"
source-map "~0.6.1"

uglify-to-browserify@~1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/uglify-to-browserify/-/uglify-to-browserify-1.0.2.tgz#6e0924d6bda6b5afe349e39a6d632850a0f882b7"
Expand Down

0 comments on commit 141d160

Please sign in to comment.