Skip to content

Commit

Permalink
feat(changelog): allow to pass a custom context
Browse files Browse the repository at this point in the history
In repositories that are self-hosted most of the tim the changelog
is genereated with broken links. Allowing to pass a custom context
to conventionalChangelog mitigates this.
  • Loading branch information
dnlup committed Feb 20, 2020
1 parent 2f04ac8 commit 76bb460
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
6 changes: 5 additions & 1 deletion command.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ const defaults = require('./defaults')

const yargs = require('yargs')
.usage('Usage: $0 [options]')
.option('context', {
type: 'string',
describe: 'Pass a custom context to inject during the changelog generation'
})
.option('packageFiles', {
default: defaults.packageFiles,
array: true
Expand All @@ -20,7 +24,7 @@ const yargs = require('yargs')
})
.option('prerelease', {
alias: 'p',
describe: 'make a pre-release with optional option value to specify a tag id',
describe: 'Make a pre-release with optional option value to specify a tag id',
string: true
})
.option('infile', {
Expand Down
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ module.exports = function standardVersion (argv) {
}
}

if (argv.context) {
if (typeof argv.context === 'string') {
try {
argv.context = JSON.parse(argv.context)
} catch (error) {
throw new Error(`Error while parsing context: ${error.message}`)
}
}
}

if (argv.changelogHeader) {
argv.header = argv.changelogHeader
if (!argv.silent) {
Expand Down
3 changes: 2 additions & 1 deletion lib/lifecycles/changelog.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ function outputChangelog (args, newVersion) {
oldContent = oldContent.substring(oldContentStart)
}
let content = ''
const context = { version: newVersion }
const customContext = args.context || {}
const context = { ...customContext, version: newVersion }
const changelogStream = conventionalChangelog({
debug: args.verbose && console.info.bind(console, 'conventional-changelog'),
preset: presetLoader(args),
Expand Down
30 changes: 30 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,22 @@ describe('cli', function () {
beforeEach(initInTempFolder)
afterEach(finishTemp)

it('should take a context', function () {
commit('feat: first commit')
shell.exec('git tag -a v1.0.0 -m "my awesome first release"')
commit('fix: patch release')
execCli('--context \'{ "owner": "owner", "repository": "repository" }\'').code.should.equal(0)
const changelog = fs.readFileSync('CHANGELOG.md', 'utf8')
changelog.should.include('[1.0.1](/owner/repository/compare/v1.0.0...v1.0.1)')
})

it('should error on invalid context', function () {
commit('feat: first commit')
shell.exec('git tag -a v1.0.0 -m "my awesome first release"')
commit('fix: patch release')
execCli('--context \'{ "owner": "owner", "repository": }\'').code.should.equal(1)
})

describe('CHANGELOG.md does not exist', function () {
it('populates changelog with commits since last tag by default', function () {
commit('feat: first commit')
Expand Down Expand Up @@ -860,6 +876,20 @@ describe('standard-version', function () {
})
})

it('should pass a custom context', function (done) {
commit('feat: first commit')
shell.exec('git tag -a v1.0.0 -m "my awesome first release"')
commit('feat: new feature!')
require('./index')({
silent: true,
context: { owner: 'owner', repository: 'respository' }
}).then(() => {
const changelog = fs.readFileSync('CHANGELOG.md', 'utf8')
changelog.should.include('[1.1.0](/owner/respository/compare/v1.0.0...v1.1.0)')
done()
}).catch(done)
})

it('formats the commit and tag messages appropriately', function (done) {
commit('feat: first commit')
shell.exec('git tag -a v1.0.0 -m "my awesome first release"')
Expand Down

0 comments on commit 76bb460

Please sign in to comment.