From 057e8e975961f68ca882cded3ab1fd2c810777f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20B=C3=B6nnemann?= Date: Thu, 18 Jun 2015 16:11:52 -0700 Subject: [PATCH] feat(plugins): normalize plugins and have unified function signature --- package.json | 4 ++-- src/lib/error.js | 3 ++- src/lib/plugin-noop.js | 4 ++++ src/lib/plugins.js | 17 +++++++++++++++++ src/lib/type.js | 20 +++++++++++--------- test/specs/pre.js | 10 +++++----- test/specs/type.js | 18 +++++++++--------- 7 files changed, 50 insertions(+), 26 deletions(-) create mode 100644 src/lib/plugin-noop.js create mode 100644 src/lib/plugins.js diff --git a/package.json b/package.json index 6bb44135..f9a3b1e5 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/src/lib/error.js b/src/lib/error.js index 24d9e2ee..c643c629 100644 --- a/src/lib/error.js +++ b/src/lib/error.js @@ -1,6 +1,7 @@ module.exports = class SemanticReleaseError extends Error { constructor (message, code) { - super(message) + super() + this.message = message this.code = code } } diff --git a/src/lib/plugin-noop.js b/src/lib/plugin-noop.js new file mode 100644 index 00000000..630aa073 --- /dev/null +++ b/src/lib/plugin-noop.js @@ -0,0 +1,4 @@ +/* istanbul ignore next */ +module.exports = function (options, release, cb) { + cb(null) +} diff --git a/src/lib/plugins.js b/src/lib/plugins.js new file mode 100644 index 00000000..24449cef --- /dev/null +++ b/src/lib/plugins.js @@ -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) +} diff --git a/src/lib/type.js b/src/lib/type.js index d46c7fff..10851f24 100644 --- a/src/lib/type.js +++ b/src/lib/type.js @@ -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) + }) } diff --git a/test/specs/pre.js b/test/specs/pre.js index b0ff6278..47815f20 100644 --- a/test/specs/pre.js +++ b/test/specs/pre.js @@ -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() }) }) diff --git a/test/specs/type.js b/test/specs/type.js index a1c8f582..9e151c0e 100644 --- a/test/specs/type.js +++ b/test/specs/type.js @@ -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() }) })