diff --git a/index.js b/index.js index 0c9e7e47..b2a406bf 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.js'); * @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,36 @@ 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 { + if(fs.existsSync(pluginConfig['header'])) { + const header = fs.readFileSync(headerOpt, { encoding: 'utf8' }); + res = header + '\n' + res; + } + } + } + + if('footer' in pluginConfig) { + var footerOpt = pluginConfig['footer']; + + if(footerOpt === fspath.basename(footerOpt)) { + res = res + '\n' + footerOpt + } else { + if(fs.existsSync(pluginConfig['footer'])) { + const footer = fs.readFileSync(footerOpt, { encoding: 'utf8' }); + res = res + '\n' + footer + } + } + } + + return res; } module.exports = {generateNotes}; diff --git a/test/integration.test.js b/test/integration.test.js index b11cf71f..9e616cec 100644 --- a/test/integration.test.js +++ b/test/integration.test.js @@ -639,3 +639,43 @@ 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/); +}); + +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 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