refactor: use one config object instead of passing arguments to plugins on their own

This commit does a lot but it's all connected and tries to make everything more extensible and future proof.

1. CLI arguments and options from the "package.json" are no longer treated as two different things. You can now pass options either way.

BREAKING CHANGE: cli arguments are now normalized to camelCase, so e.g. `options['github-url']` is now `options.githubUrl`

2. Plugins no longer receive config they need one by one, but in one huge object. This way it's easier to pass more info in the future w/o breaking existing plugins that rely on the position of the callback in the arguments array.

BREAKING CHANGE: Plugins now need to read their passed options from one huge config object.

Old:
```js
module.exports = function (pluginConfig, foo, bar, cb) {…}
```

New:
```js
// ES5
module.exports = function(pluginConfig, config, cb) {
  var foo = config.foo
  var bar = config.bar
  …
}

// ES6
module.exports = function (pluginConfig, {foo, bar}, cb) {…}
```
This commit is contained in:
Stephan Bönnemann
2015-07-19 15:34:30 +02:00
parent 8892ec7f7a
commit d9eeb3fcae
16 changed files with 190 additions and 161 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
const { exec } = require('child_process')
module.exports = function (lastRelease, cb) {
module.exports = function ({lastRelease}, cb) {
const from = lastRelease.gitHead
const range = (from ? from + '..' : '') + 'HEAD'
+7 -7
View File
@@ -3,24 +3,24 @@ const SemanticReleaseError = require('@semantic-release/error')
const npmlog = require('npmlog')
const RegClient = require('npm-registry-client')
module.exports = function (pkg, npmConfig, cb) {
npmlog.level = npmConfig.loglevel || 'error'
module.exports = function ({pkg, npm}, cb) {
npmlog.level = npm.loglevel || 'error'
const client = new RegClient({log: npmlog})
client.get(`${npmConfig.registry}${pkg.name}`, {
auth: npmConfig.auth
client.get(`${npm.registry}${pkg.name}`, {
auth: npm.auth
}, (err, data) => {
if (err && err.statusCode === 404) return cb(null, {})
if (err) return cb(err)
const version = data['dist-tags'][npmConfig.tag]
const version = data['dist-tags'][npm.tag]
if (!version) return cb(new SemanticReleaseError(`There is no release with the dist-tag "${npmConfig.tag}" yet. Tag a version first.`, 'ENODISTTAG'))
if (!version) return cb(new SemanticReleaseError(`There is no release with the dist-tag "${npm.tag}" yet. Tag a version first.`, 'ENODISTTAG'))
cb(null, {
version,
gitHead: data.versions[version].gitHead,
tag: npmConfig.tag
tag: npm.tag
})
})
}
+1 -1
View File
@@ -1,4 +1,4 @@
/* istanbul ignore next */
module.exports = function (options, release, cb) {
module.exports = function (config, options, cb) {
cb(null)
}
+10 -10
View File
@@ -1,20 +1,20 @@
const relative = require('require-relative')
let exports = module.exports = function (source) {
let exports = module.exports = function (options) {
return {
analyzeCommits: exports.normalize(source.analyzeCommits, '@semantic-release/commit-analyzer'),
generateNotes: exports.normalize(source.generateNotes, '@semantic-release/release-notes-generator'),
verifyConditions: exports.normalize(source.verifyConditions, '@semantic-release/condition-travis'),
verifyRelease: exports.normalize(source.verifyRelease, './plugin-noop')
analyzeCommits: exports.normalize(options.analyzeCommits, '@semantic-release/commit-analyzer'),
generateNotes: exports.normalize(options.generateNotes, '@semantic-release/release-notes-generator'),
verifyConditions: exports.normalize(options.verifyConditions, '@semantic-release/condition-travis'),
verifyRelease: exports.normalize(options.verifyRelease, './plugin-noop')
}
}
exports.normalize = function (plugin, fallback) {
if (typeof plugin === 'string') return relative(plugin).bind(null, {})
exports.normalize = function (pluginConfig, fallback) {
if (typeof pluginConfig === 'string') return relative(pluginConfig).bind(null, {})
if (plugin && (typeof plugin.path === 'string')) {
return relative(plugin.path).bind(null, plugin)
if (pluginConfig && (typeof pluginConfig.path === 'string')) {
return relative(pluginConfig.path).bind(null, pluginConfig)
}
return require(fallback).bind(null, plugin)
return require(fallback).bind(null, pluginConfig)
}
+4 -2
View File
@@ -1,7 +1,9 @@
const SemanticReleaseError = require('@semantic-release/error')
module.exports = function (plugins, commits, lastRelease, cb) {
plugins.analyzeCommits(commits, (err, type) => {
module.exports = function (config, cb) {
const { plugins, lastRelease } = config
plugins.analyzeCommits(config, (err, type) => {
if (err) return cb(err)
if (!type) {
+2 -2
View File
@@ -2,7 +2,7 @@ const parseSlug = require('parse-github-repo-url')
const SemanticReleaseError = require('@semantic-release/error')
module.exports = function (pkg, options, env) {
module.exports = function ({pkg, options, env}) {
let errors = []
if (!pkg.name) {
@@ -26,7 +26,7 @@ module.exports = function (pkg, options, env) {
if (options.debug) return errors
if (!options['github-token']) {
if (!options.githubToken) {
errors.push(new SemanticReleaseError(
'No github token specified.',
'ENOGHTOKEN'