1 | const {template, isString, isPlainObject} = require('lodash');
|
2 | const AggregateError = require('aggregate-error');
|
3 | const {isGitRepo, verifyTagName} = require('./git');
|
4 | const getError = require('./get-error');
|
5 |
|
6 | module.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 |
|
21 | if (!(await verifyTagName(template(tagFormat)({version: '0.0.0'})))) {
|
22 | errors.push(getError('EINVALIDTAGFORMAT', context));
|
23 | }
|
24 |
|
25 |
|
26 |
|
27 |
|
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 | };
|