UNPKG

1.68 kBJavaScriptView Raw
1const importFrom = require('import-from');
2const RELEASE_TYPES = require('./default-release-types');
3
4/**
5 * Load and validate the `releaseRules` rules.
6 *
7 * If `releaseRules` parameter is a `string` then load it as an external module with `require`.
8 * Verifies that the loaded/parameter `releaseRules` is an `Array` and each element has a valid `release` attribute.
9 *
10 * @param {Object} pluginConfig The plugin configuration.
11 * @param {String|Array} pluginConfig.releaseRules A `String` to load an external module or an `Array` of rules.
12 * @param {Object} context The semantic-release context.
13 * @param {String} context.cwd The current working directory.
14 *
15 * @return {Array} the loaded and validated `releaseRules`.
16 */
17module.exports = ({releaseRules}, {cwd}) => {
18 let loadedReleaseRules;
19
20 if (releaseRules) {
21 loadedReleaseRules =
22 typeof releaseRules === 'string'
23 ? importFrom.silent(__dirname, releaseRules) || importFrom(cwd, releaseRules)
24 : releaseRules;
25
26 if (!Array.isArray(loadedReleaseRules)) {
27 throw new TypeError('Error in commit-analyzer configuration: "releaseRules" must be an array of rules');
28 }
29
30 loadedReleaseRules.forEach(rule => {
31 if (!rule || !rule.release) {
32 throw new Error('Error in commit-analyzer configuration: rules must be an object with a "release" property');
33 } else if (RELEASE_TYPES.indexOf(rule.release) === -1) {
34 throw new Error(
35 `Error in commit-analyzer configuration: "${
36 rule.release
37 }" is not a valid release type. Valid values are: ${JSON.stringify(RELEASE_TYPES)}`
38 );
39 }
40 });
41 }
42 return loadedReleaseRules;
43};