UNPKG

1.47 kBJavaScriptView Raw
1const {template, isString, isPlainObject} = require('lodash');
2const AggregateError = require('aggregate-error');
3const {isGitRepo, verifyTagName} = require('./git');
4const getError = require('./get-error');
5
6module.exports = async (context) => {
7 const {
8 cwd,
9 env,
10 options: {repositoryUrl, tagFormat, branches},
11 } = context;
12 const errors = [];
13
14 if (!(await isGitRepo({cwd, env}))) {
15 errors.push(getError('ENOGITREPO', {cwd}));
16 } else if (!repositoryUrl) {
17 errors.push(getError('ENOREPOURL'));
18 }
19
20 // Verify that compiling the `tagFormat` produce a valid Git tag
21 if (!(await verifyTagName(template(tagFormat)({version: '0.0.0'})))) {
22 errors.push(getError('EINVALIDTAGFORMAT', context));
23 }
24
25 // Verify the `tagFormat` contains the variable `version` by compiling the `tagFormat` template
26 // with a space as the `version` value and verify the result contains the space.
27 // The space is used as it's an invalid tag character, so it's guaranteed to no be present in the `tagFormat`.
28 if ((template(tagFormat)({version: ' '}).match(/ /g) || []).length !== 1) {
29 errors.push(getError('ETAGNOVERSION', context));
30 }
31
32 branches.forEach((branch) => {
33 if (
34 !((isString(branch) && branch.trim()) || (isPlainObject(branch) && isString(branch.name) && branch.name.trim()))
35 ) {
36 errors.push(getError('EINVALIDBRANCH', {branch}));
37 }
38 });
39
40 if (errors.length > 0) {
41 throw new AggregateError(errors);
42 }
43};