Skip to content

Commit

Permalink
feat: conventionalcommits preset, preMajor config option (#434)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe committed Apr 10, 2019
1 parent 4eb1f55 commit dde12fe
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 36 deletions.
Expand Up @@ -14,7 +14,11 @@ module.exports = function (config) {
commits.forEach(commit => {
if (commit.notes.length > 0) {
breakings += commit.notes.length
level = 0
if (config.preMajor) {
level = 1
} else {
level = 0
}
} else if (commit.type === `feat`) {
features += 1
if (level === 2) {
Expand Down
30 changes: 27 additions & 3 deletions packages/conventional-changelog-preset-loader/index.js
Expand Up @@ -4,15 +4,39 @@ module.exports.presetLoader = presetLoader

function presetLoader (requireMethod) {
return path => {
let scope = ``
let name = path.toLowerCase()
let name = ''
let scope = ''
if (typeof path === 'string') {
name = path.toLowerCase()
} else if (typeof path === 'object' && path.name) {
// Rather than a string preset name, options.preset can be an object
// with a "name" key indicating the preset to load; additinoal key/value
// pairs are assumed to be configuration for the preset. See the documentation
// for a given preset for configuration available.
name = path.name.toLowerCase()
} else {
throw Error('preset must be string or object with key name')
}

if (name[0] === `@`) {
const parts = name.split(`/`)
scope = parts.shift() + `/`
name = parts.join(`/`)
}

return requireMethod(`${scope}conventional-changelog-${name}`)
try {
const config = requireMethod(`${scope}conventional-changelog-${name}`)
// rather than returning a promise, presets can return a builder function
// which accepts a config object (allowing for customization) and returns
// a promise.
if (config && !config.then && typeof path === 'object') {
return config(path)
} else {
// require returned a promise that resolves to a config object.
return config
}
} catch (_) {
throw Error('does not exist')
}
}
}
24 changes: 7 additions & 17 deletions packages/conventional-changelog/index.js
Expand Up @@ -8,25 +8,15 @@ function conventionalChangelog (options, context, gitRawCommitsOpts, parserOpts,

if (options.preset) {
try {
let presetConfig = null
if (typeof options.preset === 'object' && options.preset.name) {
// Rather than a string preset name, options.preset can be an object
// with a "name" key indicating the preset to load; additinoal key/value
// pairs are assumed to be configuration for the preset. See the documentation
// for a given preset for configuration available.
presetConfig = options.preset
options.config = conventionalChangelogPresetLoader(options.preset.name.toLowerCase())
options.config = conventionalChangelogPresetLoader(options.preset)
} catch (err) {
if (typeof options.preset === 'object') {
options.warn(`Preset: "${options.preset.name}" ${err.message}`)
} else if (typeof options.preset === 'string') {
options.warn(`Preset: "${options.preset}" ${err.message}`)
} else {
options.config = conventionalChangelogPresetLoader(options.preset.toLowerCase())
}
// rather than returning a promise, presets can return a builder function
// which accepts a config object (allowing for customization) and returns
// a promise.
if (!options.config.then && presetConfig) {
options.config = options.config(presetConfig)
options.warn(`Preset: ${err.message}`)
}
} catch (err) {
options.warn('Preset: "' + options.preset + '" does not exist')
}
}

Expand Down
6 changes: 5 additions & 1 deletion packages/conventional-recommended-bump/index.js
Expand Up @@ -29,7 +29,11 @@ function conventionalRecommendedBump (optionsArgument, parserOptsArgument, cbArg
try {
presetPackage = conventionalChangelogPresetLoader(options.preset)
} catch (err) {
return cb(new Error(`Unable to load the "${options.preset}" preset package. Please make sure it's installed.`))
if (err.message === 'does not exist') {
return cb(new Error(`Unable to load the "${options.preset}" preset package. Please make sure it's installed.`))
} else {
return cb(err)
}
}
}

Expand Down
57 changes: 43 additions & 14 deletions packages/conventional-recommended-bump/test/index.spec.js
Expand Up @@ -72,20 +72,6 @@ describe(`conventional-recommended-bump API`, () => {
})
})

describe(`loading a preset package`, () => {
it(`throw an error if unable to load a preset package`, done => {
preparing(1)

conventionalRecommendedBump({
preset: `does-not-exist`
}, {}, err => {
assert.ok(err)
assert.strictEqual(err.message, `Unable to load the "does-not-exist" preset package. Please make sure it's installed.`)
done()
})
})
})

it(`should return an error if there are no commits in the repository`, done => {
preparing(1)

Expand Down Expand Up @@ -224,6 +210,49 @@ describe(`conventional-recommended-bump API`, () => {
})
})

describe(`loading a preset package`, () => {
it(`throws an error if unable to load a preset package`, done => {
preparing(5)

conventionalRecommendedBump({
preset: `does-not-exist`
}, {}, err => {
assert.ok(err)
assert.strictEqual(err.message, `Unable to load the "does-not-exist" preset package. Please make sure it's installed.`)
done()
})
})

it('recommends a minor release when preMajor=true', done => {
preparing(5)

conventionalRecommendedBump({
preset: {
name: 'conventionalcommits',
preMajor: true
}
}, {}, (_, recommendation) => {
assert.notStrictEqual(recommendation.reason.indexOf('1 BREAKING'), -1)
assert.strictEqual(recommendation.releaseType, 'minor')
done()
})
})

it('recommends a major release when preMajor=false', done => {
preparing(5)

conventionalRecommendedBump({
preset: {
name: 'conventionalcommits'
}
}, {}, (_, recommendation) => {
assert.notStrictEqual(recommendation.reason.indexOf('1 BREAKING'), -1)
assert.strictEqual(recommendation.releaseType, 'major')
done()
})
})
})

describe(`repository with custom tag prefix`, () => {
it(`should recommends a minor release if appropriate`, done => {
preparing(5)
Expand Down

0 comments on commit dde12fe

Please sign in to comment.