UNPKG

1.97 kBJavaScriptView Raw
1const {uniqBy, intersection} = require('lodash');
2const semver = require('semver');
3const semverDiff = require('semver-diff');
4const getLastRelease = require('./get-last-release');
5const {makeTag, getLowerBound} = require('./utils');
6
7/**
8 * Find releases that have been merged from from a higher branch but not added on the channel of the current branch.
9 *
10 * @param {Object} context semantic-release context.
11 *
12 * @return {Array<Object>} Last release and next release to be added on the channel of the current branch.
13 */
14module.exports = (context) => {
15 const {
16 branch,
17 branches,
18 options: {tagFormat},
19 } = context;
20
21 const higherChannels = branches
22 // Consider only releases of higher branches
23 .slice(branches.findIndex(({name}) => name === branch.name) + 1)
24 // Exclude prerelease branches
25 .filter(({type}) => type !== 'prerelease')
26 .map(({channel}) => channel || null);
27
28 const versiontoAdd = uniqBy(
29 branch.tags.filter(
30 ({channels, version}) =>
31 !channels.includes(branch.channel || null) &&
32 intersection(channels, higherChannels).length > 0 &&
33 (branch.type !== 'maintenance' || semver.gte(version, getLowerBound(branch.mergeRange)))
34 ),
35 'version'
36 ).sort((a, b) => semver.compare(b.version, a.version))[0];
37
38 if (versiontoAdd) {
39 const {version, gitTag, channels} = versiontoAdd;
40 const lastRelease = getLastRelease(context, {before: version});
41 if (semver.gt(getLastRelease(context).version, version)) {
42 return;
43 }
44
45 const type = lastRelease.version ? semverDiff(lastRelease.version, version) : 'major';
46 const name = makeTag(tagFormat, version);
47 return {
48 lastRelease,
49 currentRelease: {type, version, channels, gitTag, name, gitHead: gitTag},
50 nextRelease: {
51 type,
52 version,
53 channel: branch.channel || null,
54 gitTag: makeTag(tagFormat, version),
55 name,
56 gitHead: gitTag,
57 },
58 };
59 }
60};