feat(plugins): normalize plugins and have unified function signature

This commit is contained in:
Stephan Bönnemann 2015-06-18 16:11:52 -07:00
parent da23c15ea5
commit 057e8e9759
7 changed files with 50 additions and 26 deletions

View File

@ -18,7 +18,8 @@
"npm-registry-client": "^6.4.0",
"npmconf": "^2.1.2",
"npmlog": "^1.2.1",
"run-auto": "^1.1.2"
"run-auto": "^1.1.2",
"semver": "^4.3.6"
},
"devDependencies": {
"babel": "^5.5.8",
@ -29,7 +30,6 @@
"nyc": "^2.3.0",
"proxyquire": "^1.5.0",
"rimraf": "^2.4.0",
"semver": "^4.3.6",
"standard": "^4.2.1",
"tap": "^1.2.0"
},

View File

@ -1,6 +1,7 @@
module.exports = class SemanticReleaseError extends Error {
constructor (message, code) {
super(message)
super()
this.message = message
this.code = code
}
}

4
src/lib/plugin-noop.js Normal file
View File

@ -0,0 +1,4 @@
/* istanbul ignore next */
module.exports = function (options, release, cb) {
cb(null)
}

17
src/lib/plugins.js Normal file
View File

@ -0,0 +1,17 @@
let exports = module.exports = function (source) {
return {
analyze: exports.normalize(source.analyze, '@semantic-release/commit-analyzer'),
generateNotes: exports.normalize(source.analyze, '@semantic-release/release-notes-generator'),
verify: exports.normalize(source.verify, './plugin-noop')
}
}
exports.normalize = function (plugin, fallback) {
if (typeof plugin === 'string') return require(plugin).bind(null, {})
if (plugin && (typeof plugin.path === 'string')) {
return require(plugin.path).bind(null, plugin)
}
return require(fallback).bind(null, plugin)
}

View File

@ -1,16 +1,18 @@
const SemanticReleaseError = require('./error')
module.exports = function (plugins, commits, lastRelease, cb) {
const type = plugins.analyze(commits)
plugins.analyze(commits, (err, type) => {
if (err) return cb(err)
if (!type) {
return cb(new SemanticReleaseError(
'There are no relevant changes, so no new version is released',
'ENOCHANGE'
))
}
if (!type) {
return cb(new SemanticReleaseError(
'There are no relevant changes, so no new version is released',
'ENOCHANGE'
))
}
if (!lastRelease.version) return cb(null, 'initial')
if (!lastRelease.version) return cb(null, 'initial')
cb(null, type)
cb(null, type)
})
}

View File

@ -8,11 +8,13 @@ const pre = proxyquire('../../dist/pre', {
const plugins = {
verify: (release, cb) => cb(null, release),
analyze: () => 'major'
analyze: (commits, cb) => cb(null, 'major')
}
test('full pre run', (t) => {
t.test('increase version', (tt) => {
tt.plan(3)
pre({
name: 'available'
}, {
@ -23,12 +25,12 @@ test('full pre run', (t) => {
tt.error(err)
tt.is(release.type, 'major')
tt.is(release.version, '2.0.0')
tt.end()
})
})
t.test('increase version', (tt) => {
tt.plan(3)
pre({
name: 'unavailable'
}, {
@ -39,8 +41,6 @@ test('full pre run', (t) => {
tt.error(err)
tt.is(release.type, 'initial')
tt.is(release.version, '1.0.0')
tt.end()
})
})

View File

@ -4,8 +4,10 @@ const type = require('../../dist/lib/type')
test('get type from commits', (t) => {
t.test('get type from plugin', (tt) => {
tt.plan(2)
type({
analyze: () => 'major'
analyze: (commits, cb) => cb(null, 'major')
}, [{
hash: '0',
message: 'a'
@ -14,31 +16,29 @@ test('get type from commits', (t) => {
}, (err, type) => {
tt.error(err)
tt.is(type, 'major')
tt.end()
})
})
t.test('error when no changes', (tt) => {
tt.plan(1)
type({
analyze: () => null
analyze: (commits, cb) => cb(null, null)
}, [], {},
(err) => {
tt.is(err.code, 'ENOCHANGE')
tt.end()
})
})
t.test('initial version', (tt) => {
tt.plan(2)
type({
analyze: () => 'major'
analyze: (commits, cb) => cb(null, 'major')
}, [], {},
(err, type) => {
tt.error(err)
tt.is(type, 'initial')
tt.end()
})
})