feat: Extract npm and github publish to plugins

- Add a new plugin type: `publish`
- Add support for multi-plugin. A plugin module can now return an object with a property for each plugin type
- Uses by default [npm](https://github.com/semantic-release/npm) and [github](https://github.com/semantic-release/github) in addition of Travis for the verify condition plugin
- Uses by default [npm](https://github.com/semantic-release/npm) and [github](https://github.com/semantic-release/github) for the publish plugin
- `gitTag` if one can be found is passed to `generateNotes` for both `lastRelease` and `nextRelease`
- `semantic-release` now verifies the plugin configuration (in the `release` property of `package.json`) and throws an error if it's invalid
- `semantic-release` now verifies each plugin output and will throw an error if a plugin returns an unexpected value.

BREAKING CHANGE: `githubToken`, `githubUrl` and `githubApiPathPrefix` have to be set at the [github](https://github.com/semantic-release/github) plugin level. They can be set via `GH_TOKEN`, `GH_URL` and `GH_PREFIX` environment variables.

BREAKING CHANGE: the `npm` parameter is not passed to any plugin anymore. Each plugin have to read `.npmrc` if they needs to (with https://github.com/kevva/npm-conf for example).
This commit is contained in:
Pierre Vanduynslager
2017-11-21 16:41:04 -05:00
parent 991a7b5f97
commit d548edcf37
42 changed files with 1111 additions and 1122 deletions
+78
View File
@@ -0,0 +1,78 @@
const {isString, isObject, isFunction, isArray} = require('lodash');
const semver = require('semver');
const conditionTravis = require('@semantic-release/condition-travis');
const commitAnalyzer = require('@semantic-release/commit-analyzer');
const releaseNotesGenerator = require('@semantic-release/release-notes-generator');
const npm = require('@semantic-release/npm');
const github = require('@semantic-release/github');
const RELEASE_TYPE = ['major', 'premajor', 'minor', 'preminor', 'patch', 'prepatch', 'prerelease'];
module.exports = {
verifyConditions: {
default: [npm.verifyConditions, github.verifyConditions, conditionTravis],
config: {
validator: conf => !conf || (isArray(conf) ? conf : [conf]).every(conf => validatePluginConfig(conf)),
message:
'The "verifyConditions" plugin, if defined, must be a single or an array of plugins definition. A plugin definition is either a string or an object with a path property.',
},
},
getLastRelease: {
default: npm.getLastRelease,
config: {
validator: conf => Boolean(conf) && validatePluginConfig(conf),
message:
'The "getLastRelease" plugin is mandatory, and must be a single plugin definition. A plugin definition is either a string or an object with a path property.',
},
output: {
validator: output =>
!output ||
(isObject(output) && !output.version) ||
(isString(output.version) && Boolean(semver.valid(semver.clean(output.version)))),
message:
'The "getLastRelease" plugin output if defined, must be an object with an optionnal valid semver version in the "version" property.',
},
},
analyzeCommits: {
default: commitAnalyzer,
config: {
validator: conf => Boolean(conf) && validatePluginConfig(conf),
message:
'The "analyzeCommits" plugin is mandatory, and must be a single plugin definition. A plugin definition is either a string or an object with a path property.',
},
output: {
validator: output => !output || RELEASE_TYPE.includes(output),
message: 'The "analyzeCommits" plugin output must be either undefined or a valid semver release type.',
},
},
verifyRelease: {
default: false,
config: {
validator: conf => !conf || (isArray(conf) ? conf : [conf]).every(conf => validatePluginConfig(conf)),
message:
'The "verifyRelease" plugin, if defined, must be a single or an array of plugins definition. A plugin definition is either a string or an object with a path property.',
},
},
generateNotes: {
default: releaseNotesGenerator,
config: {
validator: conf => !conf || validatePluginConfig(conf),
message:
'The "generateNotes" plugin, if defined, must be a single plugin definition. A plugin definition is either a string or an object with a path property.',
},
output: {
validator: output => isString(output),
message: 'The "generateNotes" plugin output must be a string.',
},
},
publish: {
default: [npm.publish, github.publish],
config: {
validator: conf => Boolean(conf) && (isArray(conf) ? conf : [conf]).every(conf => validatePluginConfig(conf)),
message:
'The "publish" plugin is mandatory, and must be a single or an array of plugins definition. A plugin definition is either a string or an object with a path property.',
},
},
};
const validatePluginConfig = conf => isString(conf) || isString(conf.path) || isFunction(conf);
+24
View File
@@ -0,0 +1,24 @@
const {isArray} = require('lodash');
const DEFINITIONS = require('./definitions');
const pipeline = require('./pipeline');
const normalize = require('./normalize');
module.exports = (options, logger) =>
Object.keys(DEFINITIONS).reduce((plugins, pluginType) => {
const {config, output, default: def} = DEFINITIONS[pluginType];
let pluginConfs;
if (options[pluginType]) {
if (config && !config.validator(options[pluginType])) {
throw new Error(config.message);
}
pluginConfs = options[pluginType];
} else {
pluginConfs = def;
}
plugins[pluginType] = isArray(pluginConfs)
? pipeline(pluginConfs.map(conf => normalize(pluginType, conf, logger, output)))
: normalize(pluginType, pluginConfs, logger, output);
return plugins;
}, {});
+34
View File
@@ -0,0 +1,34 @@
const {promisify, inspect} = require('util');
const {isString, isObject, isFunction, noop, cloneDeep} = require('lodash');
const importFrom = require('import-from');
module.exports = (pluginType, pluginConfig, logger, validator) => {
if (!pluginConfig) {
return noop;
}
const {path, ...config} = isString(pluginConfig) || isFunction(pluginConfig) ? {path: pluginConfig} : pluginConfig;
if (!isFunction(pluginConfig)) {
logger.log('Load plugin %s', path);
}
const plugin = isFunction(path) ? path : importFrom.silent(__dirname, path) || importFrom(process.cwd(), path);
let func;
if (isFunction(plugin)) {
func = promisify(plugin.bind(null, cloneDeep(config)));
} else if (isObject(plugin) && plugin[pluginType] && isFunction(plugin[pluginType])) {
func = promisify(plugin[pluginType].bind(null, cloneDeep(config)));
} else {
throw new Error(
`The ${pluginType} plugin must be a function, or an object with a function in the property ${pluginType}.`
);
}
return async input => {
const result = await func(cloneDeep(input));
if (validator && !validator.validator(result)) {
throw new Error(`${validator.message}. Received: ${inspect(result)}`);
}
return result;
};
};
+19
View File
@@ -0,0 +1,19 @@
const {identity} = require('lodash');
const pReduce = require('p-reduce');
module.exports = steps => async (input, getNextInput = identity) => {
const results = [];
await pReduce(
steps,
async (prevResult, nextStep) => {
// Call the next step with the input computed at the end of the previous iteration
const result = await nextStep(prevResult);
// Save intermediary result
results.push(result);
// Prepare input for next step, passing the result of the previous iteration and the current one
return getNextInput(prevResult, result);
},
input
);
return results;
};