From 93e90451dc6cd5f989870420bd8760fca0baeefc Mon Sep 17 00:00:00 2001 From: SuperEwald Date: Tue, 7 Jun 2022 16:17:00 +0200 Subject: [PATCH 1/3] feat: add option for custom release note header --- index.js | 18 +++++++++++++++++- test/integration.test.js | 20 ++++++++++++++++++++ test/testHeader.md | 1 + 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 test/testHeader.md diff --git a/index.js b/index.js index 30e630ff..829d8ce6 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,5 @@ +const fs = require('fs') +const fspath = require('path') const {format} = require('url'); const {find, merge} = require('lodash'); const getStream = require('get-stream'); @@ -18,6 +20,7 @@ const HOSTS_CONFIG = require('./lib/hosts-config'); * @param {String} pluginConfig.config Requierable npm package with a custom conventional-changelog preset * @param {Object} pluginConfig.parserOpts Additional `conventional-changelog-parser` options that will overwrite ones loaded by `preset` or `config`. * @param {Object} pluginConfig.writerOpts Additional `conventional-changelog-writer` options that will overwrite ones loaded by `preset` or `config`. + * @param {String} pluginConfig.header Path to a text file appended to the head of the release notes. * @param {Object} context The semantic-release context. * @param {Array} context.commits The commits to analyze. * @param {Object} context.lastRelease The last release with `gitHead` corresponding to the commit hash used to make the last release and `gitTag` corresponding to the git tag associated with `gitHead`. @@ -85,8 +88,21 @@ async function generateNotes(pluginConfig, context) { debug('linkReferences: %o', changelogContext.linkReferences); debug('issue: %o', changelogContext.issue); debug('commit: %o', changelogContext.commit); + + var res = await getStream(intoStream.object(parsedCommits).pipe(writer(changelogContext, writerOpts))); + + if('header' in pluginConfig) { + var headerOpt = pluginConfig['header']; - return getStream(intoStream.object(parsedCommits).pipe(writer(changelogContext, writerOpts))); + if(headerOpt === fspath.basename(headerOpt)) { + res = headerOpt + "\n" + res; + } else { + const header = fs.readFileSync(headerOpt, { encoding: 'utf8' }); + res = header + "\n" + res; + } + } + + return res; } module.exports = {generateNotes}; diff --git a/test/integration.test.js b/test/integration.test.js index b11cf71f..91522558 100644 --- a/test/integration.test.js +++ b/test/integration.test.js @@ -639,3 +639,23 @@ test('ReThrow error from "conventional-changelog"', async (t) => { {message: 'Test error'} ); }); + +test('Accept "header" file path option', async(t) => { + const commits = [ + {hash: '111', message: 'fix(scope1): First fix'}, + {hash: '222', message: 'feat(scope2): First feature'}, + ]; + + const releaseNotes = await generateNotes({header: './test/testHeader.md'}, {cwd, options: {repositoryUrl}, lastRelease, nextRelease, commits}); + t.regex(releaseNotes, /### Test Header/); +}) + +test('Accept "header" string option', async(t) => { + const commits = [ + {hash: '111', message: 'fix(scope1): First fix'}, + {hash: '222', message: 'feat(scope2): First feature'}, + ]; + + const releaseNotes = await generateNotes({header: "### Test Header"}, {cwd, options: {repositoryUrl}, lastRelease, nextRelease, commits}); + t.regex(releaseNotes, /### Test Header/); +}) \ No newline at end of file diff --git a/test/testHeader.md b/test/testHeader.md new file mode 100644 index 00000000..2cd757b8 --- /dev/null +++ b/test/testHeader.md @@ -0,0 +1 @@ +### Test Header \ No newline at end of file From 8bbfbc10bea66ec29f75c67c50afbdb4498eef07 Mon Sep 17 00:00:00 2001 From: SuperEwald Date: Wed, 8 Jun 2022 09:36:17 +0200 Subject: [PATCH 2/3] feat: add option for custom release notes footer --- index.js | 15 +++++++++++++-- test/integration.test.js | 24 ++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 829d8ce6..8911ddfb 100644 --- a/index.js +++ b/index.js @@ -95,10 +95,21 @@ async function generateNotes(pluginConfig, context) { var headerOpt = pluginConfig['header']; if(headerOpt === fspath.basename(headerOpt)) { - res = headerOpt + "\n" + res; + res = headerOpt + '\n' + res; } else { const header = fs.readFileSync(headerOpt, { encoding: 'utf8' }); - res = header + "\n" + res; + res = header + '\n' + res; + } + } + + if('footer' in pluginConfig) { + var footerOpt = pluginConfig['footer']; + + if(footerOpt === fspath.basename(footerOpt)) { + res = res + '\n' + footerOpt + } else { + const footer = fs.readFileSync(footerOpt, { encoding: 'utf8' }); + res = res + '\n' + footer } } diff --git a/test/integration.test.js b/test/integration.test.js index 91522558..9e616cec 100644 --- a/test/integration.test.js +++ b/test/integration.test.js @@ -648,7 +648,7 @@ test('Accept "header" file path option', async(t) => { const releaseNotes = await generateNotes({header: './test/testHeader.md'}, {cwd, options: {repositoryUrl}, lastRelease, nextRelease, commits}); t.regex(releaseNotes, /### Test Header/); -}) +}); test('Accept "header" string option', async(t) => { const commits = [ @@ -658,4 +658,24 @@ test('Accept "header" string option', async(t) => { const releaseNotes = await generateNotes({header: "### Test Header"}, {cwd, options: {repositoryUrl}, lastRelease, nextRelease, commits}); t.regex(releaseNotes, /### Test Header/); -}) \ No newline at end of file +}); + +test('Accept "footer" file path option', async(t) => { + const commits = [ + {hash: '111', message: 'fix(scope1): First fix'}, + {hash: '222', message: 'feat(scope2): First feature'}, + ]; + + const releaseNotes = await generateNotes({footer: './test/testHeader.md'}, {cwd, options: {repositoryUrl}, lastRelease, nextRelease, commits}) + t.regex(releaseNotes, /### Test Header/); +}); + +test('Accept "footer" string option', async(t) => { + const commits = [ + {hash: '111', message: 'fix(scope1): First fix'}, + {hash: '222', message: 'feat(scope2): First feature'}, + ]; + + const releaseNotes = await generateNotes({footer: "### Test Footer"}, {cwd, options: {repositoryUrl}, lastRelease, nextRelease, commits}); + t.regex(releaseNotes, /### Test Footer/); +}); \ No newline at end of file From b98331f7d3bf21e7e812fb12075faefa0742cc8e Mon Sep 17 00:00:00 2001 From: Super Ewald Date: Wed, 15 Jun 2022 11:32:03 +0200 Subject: [PATCH 3/3] fix(custom-notes): add header/footer filecheck --- index.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 8911ddfb..762d21ba 100644 --- a/index.js +++ b/index.js @@ -97,8 +97,10 @@ async function generateNotes(pluginConfig, context) { if(headerOpt === fspath.basename(headerOpt)) { res = headerOpt + '\n' + res; } else { - const header = fs.readFileSync(headerOpt, { encoding: 'utf8' }); - res = header + '\n' + res; + if(fs.existsSync(pluginConfig['header'])) { + const header = fs.readFileSync(headerOpt, { encoding: 'utf8' }); + res = header + '\n' + res; + } } } @@ -108,8 +110,10 @@ async function generateNotes(pluginConfig, context) { if(footerOpt === fspath.basename(footerOpt)) { res = res + '\n' + footerOpt } else { - const footer = fs.readFileSync(footerOpt, { encoding: 'utf8' }); - res = res + '\n' + footer + if(fs.existsSync(pluginConfig['footer'])) { + const footer = fs.readFileSync(footerOpt, { encoding: 'utf8' }); + res = res + '\n' + footer + } } }