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

View File

@ -1,6 +1,7 @@
module.exports = class SemanticReleaseError extends Error { module.exports = class SemanticReleaseError extends Error {
constructor (message, code) { constructor (message, code) {
super(message) super()
this.message = message
this.code = code 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') const SemanticReleaseError = require('./error')
module.exports = function (plugins, commits, lastRelease, cb) { module.exports = function (plugins, commits, lastRelease, cb) {
const type = plugins.analyze(commits) plugins.analyze(commits, (err, type) => {
if (err) return cb(err)
if (!type) { if (!type) {
return cb(new SemanticReleaseError( return cb(new SemanticReleaseError(
'There are no relevant changes, so no new version is released', 'There are no relevant changes, so no new version is released',
'ENOCHANGE' '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 = { const plugins = {
verify: (release, cb) => cb(null, release), verify: (release, cb) => cb(null, release),
analyze: () => 'major' analyze: (commits, cb) => cb(null, 'major')
} }
test('full pre run', (t) => { test('full pre run', (t) => {
t.test('increase version', (tt) => { t.test('increase version', (tt) => {
tt.plan(3)
pre({ pre({
name: 'available' name: 'available'
}, { }, {
@ -23,12 +25,12 @@ test('full pre run', (t) => {
tt.error(err) tt.error(err)
tt.is(release.type, 'major') tt.is(release.type, 'major')
tt.is(release.version, '2.0.0') tt.is(release.version, '2.0.0')
tt.end()
}) })
}) })
t.test('increase version', (tt) => { t.test('increase version', (tt) => {
tt.plan(3)
pre({ pre({
name: 'unavailable' name: 'unavailable'
}, { }, {
@ -39,8 +41,6 @@ test('full pre run', (t) => {
tt.error(err) tt.error(err)
tt.is(release.type, 'initial') tt.is(release.type, 'initial')
tt.is(release.version, '1.0.0') 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) => { test('get type from commits', (t) => {
t.test('get type from plugin', (tt) => { t.test('get type from plugin', (tt) => {
tt.plan(2)
type({ type({
analyze: () => 'major' analyze: (commits, cb) => cb(null, 'major')
}, [{ }, [{
hash: '0', hash: '0',
message: 'a' message: 'a'
@ -14,31 +16,29 @@ test('get type from commits', (t) => {
}, (err, type) => { }, (err, type) => {
tt.error(err) tt.error(err)
tt.is(type, 'major') tt.is(type, 'major')
tt.end()
}) })
}) })
t.test('error when no changes', (tt) => { t.test('error when no changes', (tt) => {
tt.plan(1)
type({ type({
analyze: () => null analyze: (commits, cb) => cb(null, null)
}, [], {}, }, [], {},
(err) => { (err) => {
tt.is(err.code, 'ENOCHANGE') tt.is(err.code, 'ENOCHANGE')
tt.end()
}) })
}) })
t.test('initial version', (tt) => { t.test('initial version', (tt) => {
tt.plan(2)
type({ type({
analyze: () => 'major' analyze: (commits, cb) => cb(null, 'major')
}, [], {}, }, [], {},
(err, type) => { (err, type) => {
tt.error(err) tt.error(err)
tt.is(type, 'initial') tt.is(type, 'initial')
tt.end()
}) })
}) })