UNPKG

1.34 kBJavaScriptView Raw
1const {template, escapeRegExp} = require('lodash');
2const semver = require('semver');
3const pReduce = require('p-reduce');
4const debug = require('debug')('semantic-release:get-tags');
5const {getTags, getNote} = require('../../lib/git');
6
7module.exports = async ({cwd, env, options: {tagFormat}}, branches) => {
8 // Generate a regex to parse tags formatted with `tagFormat`
9 // by replacing the `version` variable in the template by `(.+)`.
10 // The `tagFormat` is compiled with space as the `version` as it's an invalid tag character,
11 // so it's guaranteed to no be present in the `tagFormat`.
12 const tagRegexp = `^${escapeRegExp(template(tagFormat)({version: ' '})).replace(' ', '(.+)')}`;
13
14 return pReduce(
15 branches,
16 async (branches, branch) => {
17 const branchTags = await pReduce(
18 await getTags(branch.name, {cwd, env}),
19 async (branchTags, tag) => {
20 const [, version] = tag.match(tagRegexp) || [];
21 return version && semver.valid(semver.clean(version))
22 ? [...branchTags, {gitTag: tag, version, channels: (await getNote(tag, {cwd, env})).channels || [null]}]
23 : branchTags;
24 },
25 []
26 );
27
28 debug('found tags for branch %s: %o', branch.name, branchTags);
29 return [...branches, {...branch, tags: branchTags}];
30 },
31 []
32 );
33};