Skip to content

Commit

Permalink
refactor: use conventional-recommended-bump to support --detect-release
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonfncosta committed May 2, 2021
1 parent bf86499 commit 4383381
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 108 deletions.
19 changes: 0 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ const path = require('path')
const printError = require('./lib/print-error')
const tag = require('./lib/lifecycles/tag')
const { resolveUpdaterObjectFromArgument } = require('./lib/updaters')
const getLatestCommits = require('./lib/latest-commits')
const chalk = require('chalk')

module.exports = async function standardVersion (argv) {
const defaults = require('./defaults')
Expand All @@ -35,23 +33,6 @@ module.exports = async function standardVersion (argv) {
}
}

if (argv.detectRelease) {
const latestCommits = await getLatestCommits()

const releaseTypes = argv.types.reduce((acc, current) => (!current.hidden ? [...acc, current.type] : acc), [])

const commitSubjectTypes = latestCommits.map(c => c.subject.split(' ')[0])

const releaseAvailable = commitSubjectTypes.some(c => releaseTypes.some(type => c.includes(type)))

if (!releaseAvailable) {
console.info(
chalk.green(`No commits found for types: [ ${releaseTypes.join(', ')} ], skipping release stage.`)
)
process.exit()
}
}

if (argv.header && argv.header.search(changelog.START_OF_LAST_RELEASE_PATTERN) !== -1) {
throw Error(`custom changelog header must not match ${changelog.START_OF_LAST_RELEASE_PATTERN}`)
}
Expand Down
15 changes: 0 additions & 15 deletions lib/latest-commits.js

This file was deleted.

43 changes: 41 additions & 2 deletions lib/lifecycles/bump.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ async function Bump (args, version) {
if (stdout && stdout.trim().length) args.releaseAs = stdout.trim()
const release = await bumpVersion(args.releaseAs, version, args)
if (!args.firstRelease) {
if (args.detectRelease && !release.releaseType) {
checkpoint(args, release.reason, [])
process.exit()
}
const releaseType = getReleaseType(args.prerelease, release.releaseType, version)
newVersion = semver.valid(releaseType) || semver.inc(version, releaseType, args.prerelease)
updateConfigs(args, newVersion)
Expand Down Expand Up @@ -77,7 +81,12 @@ function isInPrerelease (version) {
return Array.isArray(semver.prerelease(version))
}

const TypeList = ['major', 'minor', 'patch'].reverse()
const MAJOR = 'major'
const MINOR = 'minor'
const PATCH = 'patch'
const VERSIONS = [MAJOR, MINOR, PATCH]

const TypeList = [...VERSIONS].reverse()

/**
* extract the in-pre-release type in target version
Expand Down Expand Up @@ -106,6 +115,8 @@ function getTypePriority (type) {
}

function bumpVersion (releaseAs, currentVersion, args) {
const releaseTypes = args.types.reduce((acc, current) => (!current.hidden ? [...acc, current.type] : acc), [])

return new Promise((resolve, reject) => {
if (releaseAs) {
return resolve({
Expand All @@ -120,7 +131,34 @@ function bumpVersion (releaseAs, currentVersion, args) {
debug: args.verbose && console.info.bind(console, 'conventional-recommended-bump'),
preset: presetOptions,
path: args.path,
tagPrefix: args.tagPrefix
tagPrefix: args.tagPrefix,
whatBump: (commits) => {
let level = PATCH
let breakings = 0
let features = 0

commits.forEach(commit => {
if (commit.notes.length > 0) {
breakings += commit.notes.length
level = MAJOR
} else if (releaseTypes.includes(commit.type)) {
features += 1

level = level === PATCH ? MINOR : level
}
})

if (breakings + features === 0) {
return {
level: null,
reason: `No commits found for types: [${releaseTypes.join(', ')}], skipping release stage.`
}
}

return {
level: VERSIONS.indexOf(level)
}
}
}, function (err, release) {
if (err) return reject(err)
else return resolve(release)
Expand Down Expand Up @@ -169,3 +207,4 @@ function updateConfigs (args, newVersion) {
}

module.exports = Bump
module.exports.bumpVersion = bumpVersion
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^4.2.1",
"get-stream": "^6.0.1",
"git-log-parser": "^1.2.0",
"mocha": "^8.2.1",
"mock-fs": "^4.13.0",
"mockery": "^2.1.0",
Expand Down
86 changes: 86 additions & 0 deletions test/bump.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/* global describe it beforeEach afterEach */

'use strict'

const { bumpVersion } = require('../lib/lifecycles/bump')
const shell = require('shelljs')
const chai = require('chai')
const { expect } = chai

describe('bumpVersion', () => {
const args = {
types: [
{ type: 'feat', section: 'Features' },
{ type: 'test', section: 'Tests', hidden: true }
]
}

beforeEach(() => {
shell.rm('-rf', 'tmp')
shell.config.silent = true
shell.mkdir('tmp')
shell.cd('tmp')
shell.exec('git init')
shell.exec('git config commit.gpgSign false')
shell.exec('git config core.autocrlf false')
})

afterEach(() => {
shell.cd('../')
shell.rm('-rf', 'tmp')
})

describe('when a tag is avaialble', () => {
let result

beforeEach(async () => {
shell.exec('git commit --allow-empty -m "first-commit"')
shell.exec('git tag 1.2.3')
})

describe('and release commits are present', () => {
beforeEach(async () => {
shell.exec('git commit --allow-empty -m "feat: second-commit"')

result = await bumpVersion(null, '1.2.3', args)
})

it('should return a release recommendation', async () => {
expect(result).to.include({ level: 1, releaseType: 'minor' })
})
})

describe('and no release commits are present', () => {
beforeEach(async () => {
shell.exec('git commit --allow-empty -m "test: second-commit"')

result = await bumpVersion(null, '1.2.3', args)
})

it('should return no release', async () => {
expect(result).to.include({
level: null,
reason: 'No commits found for types: [feat], skipping release stage.'
})
})
})
})

describe('when no tag is found', () => {
let result

beforeEach(async () => {
shell.exec('git commit --allow-empty -m "first-commit"')
shell.exec('git commit --allow-empty -m "second-commit"')

result = await bumpVersion(null, '1.2.3', args)
})

it('should return no release', async () => {
expect(result).to.include({
level: null,
reason: 'No commits found for types: [feat], skipping release stage.'
})
})
})
})
12 changes: 0 additions & 12 deletions test/git.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,16 +372,4 @@ describe('git', function () {
stdout.join('').should.not.include('--tag prerelease')
})
})

describe('--detect-release', () => {
it('should not release a new tag for unknown/hidden types', async () => {
mock({
bump: 'minor',
tags: ['v1.0.0']
})
await exec('--detect-release')

getPackageVersion().should.equal('1.0.0')
})
})
})
58 changes: 0 additions & 58 deletions test/utils.spec.js

This file was deleted.

0 comments on commit 4383381

Please sign in to comment.