Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(changelog): allow to pass a custom context #552

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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