UNPKG

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