-
Notifications
You must be signed in to change notification settings - Fork 74
/
load-release-rules.js
46 lines (41 loc) · 1.8 KB
/
load-release-rules.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import lodash from 'lodash';
const {isUndefined} = lodash;
import {esmImport} from './esm-import.js';
import RELEASE_TYPES from './default-release-types.js';
import {resolve} from 'path';
import {pathToFileURL} from 'url';
/**
* Load and validate the `releaseRules` rules.
*
* If `releaseRules` parameter is a `string` then load it as an external module with `require`.
* Verifies that the loaded/parameter `releaseRules` is an `Array` and each element has a valid `release` attribute.
*
* @param {Object} pluginConfig The plugin configuration.
* @param {String|Array} pluginConfig.releaseRules A `String` to load an external module or an `Array` of rules.
* @param {Object} context The semantic-release context.
* @param {String} context.cwd The current working directory.
*
* @return {Array} the loaded and validated `releaseRules`.
*/
export default async ({releaseRules}, {cwd}) => {
let loadedReleaseRules;
if (releaseRules) {
loadedReleaseRules =
typeof releaseRules === 'string' ? await esmImport(pathToFileURL(resolve(cwd, releaseRules)).href) : releaseRules;
if (!Array.isArray(loadedReleaseRules)) {
throw new TypeError('Error in commit-analyzer configuration: "releaseRules" must be an array of rules');
}
loadedReleaseRules.forEach(rule => {
if (!rule || isUndefined(rule.release)) {
throw new Error('Error in commit-analyzer configuration: rules must be an object with a "release" property');
} else if (!RELEASE_TYPES.includes(rule.release) && rule.release !== null && rule.release !== false) {
throw new Error(
`Error in commit-analyzer configuration: "${
rule.release
}" is not a valid release type. Valid values are: ${JSON.stringify(RELEASE_TYPES)}`
);
}
});
}
return loadedReleaseRules;
};