UNPKG

1.7 kBJavaScriptView Raw
1const {isString, isNil, isArray, isPlainObject} = require('lodash');
2const AggregateError = require('aggregate-error');
3const getError = require('./get-error');
4const resolveConfig = require('./resolve-config');
5
6const isNonEmptyString = value => isString(value) && value.trim();
7const isStringOrStringArray = value => isNonEmptyString(value) || (isArray(value) && value.every(isNonEmptyString));
8const isArrayOf = validator => array => isArray(array) && array.every(value => validator(value));
9const canBeDisabled = validator => value => value === false || validator(value);
10
11const VALIDATORS = {
12 assets: canBeDisabled(
13 isArrayOf(asset => isStringOrStringArray(asset) || (isPlainObject(asset) && isStringOrStringArray(asset.path)))
14 ),
15 message: isNonEmptyString,
16};
17
18/**
19 * Verify the commit `message` format and the `assets` option configuration:
20 * - The commit `message`, is defined, must a non empty `String`.
21 * - The `assets` configuration must be an `Array` of `String` (file path) or `false` (to disable).
22 *
23 * @param {Object} pluginConfig The plugin configuration.
24 * @param {String|Array<String|Object>} [pluginConfig.assets] Files to include in the release commit. Can be files path or globs.
25 * @param {String} [pluginConfig.message] The commit message for the release.
26 */
27module.exports = pluginConfig => {
28 const options = resolveConfig(pluginConfig);
29
30 const errors = Object.entries(options).reduce(
31 (errors, [option, value]) =>
32 !isNil(value) && !VALIDATORS[option](value)
33 ? [...errors, getError(`EINVALID${option.toUpperCase()}`, {[option]: value})]
34 : errors,
35 []
36 );
37
38 if (errors.length > 0) {
39 throw new AggregateError(errors);
40 }
41};