-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathverify.js
47 lines (41 loc) · 1.92 KB
/
verify.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
47
const {isString, isNil, isArray, isPlainObject, isBoolean} = require('lodash');
const AggregateError = require('aggregate-error');
const getError = require('./get-error.js');
const resolveConfig = require('./resolve-config.js');
const isNonEmptyString = (value) => isString(value) && value.trim();
const isStringOrStringArray = (value) => {
return isNonEmptyString(value) || (isArray(value) && value.every((element) => isNonEmptyString(element)));
};
const isArrayOf = (validator) => (array) => isArray(array) && array.every((value) => validator(value));
const canBeDisabled = (validator) => (value) => value === false || validator(value);
const VALIDATORS = {
assets: canBeDisabled(
isArrayOf((asset) => isStringOrStringArray(asset) || (isPlainObject(asset) && isStringOrStringArray(asset.path)))
),
message: isNonEmptyString,
respectIgnoreFile: isBoolean,
};
/**
* Verify the commit `message` format and the `assets` option configuration:
* - The commit `message`, is defined, must a non empty `String`.
* - The `assets` configuration must be an `Array` of `String` (file path) or `false` (to disable).
* - The `respectIgnoreFile`, if defined, must be a `Boolean`.
*
* @param {Object} pluginConfig The plugin configuration.
* @param {String|Array<String|Object>} [pluginConfig.assets] Files to include in the release commit. Can be files path or globs.
* @param {String} [pluginConfig.message] The commit message for the release.
* @param {Boolean} [pluginConfig.respectIgnoreFile] Whether or not to ignore files in `.gitignore`.
*/
module.exports = (pluginConfig) => {
const options = resolveConfig(pluginConfig);
const errors = Object.entries(options).reduce(
(errors, [option, value]) =>
!isNil(value) && !VALIDATORS[option](value)
? [...errors, getError(`EINVALID${option.toUpperCase()}`, {[option]: value})]
: errors,
[]
);
if (errors.length > 0) {
throw new AggregateError(errors);
}
};