From 141d1601fd1fa3b278db05acbc8f47e2bb239bbf Mon Sep 17 00:00:00 2001 From: Benjamin Petetot Date: Mon, 17 Sep 2018 14:54:57 +0200 Subject: [PATCH] :sparkles: Use handlebars templating to generate markdown (#10) --- packages/gitmoji-changelog-core/src/index.js | 24 +++++++------- .../gitmoji-changelog-core/src/index.spec.js | 29 ++++++++-------- .../gitmoji-changelog-markdown/package.json | 9 +++-- .../gitmoji-changelog-markdown/src/index.js | 16 +++++---- .../src/index.spec.js | 33 ++++++++++--------- .../src/template.md | 10 ++++++ yarn.lock | 25 ++++++++++++-- 7 files changed, 96 insertions(+), 50 deletions(-) create mode 100644 packages/gitmoji-changelog-markdown/src/template.md diff --git a/packages/gitmoji-changelog-core/src/index.js b/packages/gitmoji-changelog-core/src/index.js index 1e54778..cd12122 100644 --- a/packages/gitmoji-changelog-core/src/index.js +++ b/packages/gitmoji-changelog-core/src/index.js @@ -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 } diff --git a/packages/gitmoji-changelog-core/src/index.spec.js b/packages/gitmoji-changelog-core/src/index.spec.js index 9bc521f..aed6a3a 100644 --- a/packages/gitmoji-changelog-core/src/index.spec.js +++ b/packages/gitmoji-changelog-core/src/index.spec.js @@ -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', @@ -50,7 +51,7 @@ Waouh this is awesome 2 }, ], }, - }) + ]) }) it('should generate changelog in json format for all tags', async () => { @@ -58,28 +59,30 @@ Waouh this is awesome 2 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', }, ], }, - }) + ]) }) }) diff --git a/packages/gitmoji-changelog-markdown/package.json b/packages/gitmoji-changelog-markdown/package.json index e8d126b..9d93013 100644 --- a/packages/gitmoji-changelog-markdown/package.json +++ b/packages/gitmoji-changelog-markdown/package.json @@ -6,8 +6,8 @@ "bin": { "gitmoji-changelog": "./src/index.js" }, - "engines" : { - "node" : ">=10" + "engines": { + "node": ">=10" }, "repository": { "type": "git", @@ -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" + } } diff --git a/packages/gitmoji-changelog-markdown/src/index.js b/packages/gitmoji-changelog-markdown/src/index.js index 95a91b2..bb0ef60 100755 --- a/packages/gitmoji-changelog-markdown/src/index.js +++ b/packages/gitmoji-changelog-markdown/src/index.js @@ -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 = { diff --git a/packages/gitmoji-changelog-markdown/src/index.spec.js b/packages/gitmoji-changelog-markdown/src/index.spec.js index 5fc5206..853d97f 100644 --- a/packages/gitmoji-changelog-markdown/src/index.spec.js +++ b/packages/gitmoji-changelog-markdown/src/index.spec.js @@ -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) `) }) }) diff --git a/packages/gitmoji-changelog-markdown/src/template.md b/packages/gitmoji-changelog-markdown/src/template.md new file mode 100644 index 0000000..936e056 --- /dev/null +++ b/packages/gitmoji-changelog-markdown/src/template.md @@ -0,0 +1,10 @@ +# Changelog + +{{#each changelog}} +## {{version}} + +{{#each commits}} +- {{subject}} ({{hash}}) +{{/each}} + +{{/each}} \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 78b263f..2ec4b86 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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: @@ -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" @@ -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" @@ -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" @@ -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"