UNPKG

3.91 kBJavaScriptView Raw
1const {sortBy, isNil} = require('lodash');
2const semverDiff = require('semver-diff');
3const {FIRST_RELEASE, RELEASE_TYPE} = require('../definitions/constants');
4const {
5 tagsToVersions,
6 isMajorRange,
7 getUpperBound,
8 getLowerBound,
9 highest,
10 lowest,
11 getLatestVersion,
12 getFirstVersion,
13 getRange,
14} = require('../utils');
15
16function maintenance({maintenance, release}) {
17 return sortBy(
18 maintenance.map(({name, range, channel, ...rest}) => ({
19 ...rest,
20 name,
21 range: range || name,
22 channel: isNil(channel) ? name : channel,
23 })),
24 'range'
25 ).map(({name, range, tags, ...rest}, idx, branches) => {
26 const versions = tagsToVersions(tags);
27 // Find the lower bound based on Maintenance branches
28 const maintenanceMin =
29 // If the current branch has a major range (1.x or 1.x.x) and the previous doesn't
30 isMajorRange(range) && branches[idx - 1] && !isMajorRange(branches[idx - 1].range)
31 ? // Then the lowest bound is the upper bound of the previous branch range
32 getUpperBound(branches[idx - 1].range)
33 : // Otherwise the lowest bound is the lowest bound of the current branch range
34 getLowerBound(range);
35 // The actual lower bound is the highest version between the current branch last release and `maintenanceMin`
36 const min = highest(getLatestVersion(versions) || FIRST_RELEASE, maintenanceMin);
37 // Determine the first release of the default branch not present in any maintenance branch
38 const base =
39 (release[0] &&
40 (getFirstVersion(tagsToVersions(release[0].tags), branches) ||
41 getLatestVersion(tagsToVersions(release[0].tags)))) ||
42 FIRST_RELEASE;
43 // The upper bound is the lowest version between the `base` version and the upper bound of the current branch range
44 const max = lowest(base, getUpperBound(range));
45 const diff = semverDiff(min, max);
46 return {
47 ...rest,
48 type: 'maintenance',
49 name,
50 tags,
51 range: getRange(min, max),
52 accept: diff ? RELEASE_TYPE.slice(0, RELEASE_TYPE.indexOf(diff)) : [],
53 mergeRange: getRange(maintenanceMin, getUpperBound(range)),
54 };
55 });
56}
57
58function release({release}) {
59 if (release.length === 0) {
60 return release;
61 }
62
63 // The intial lastVersion is the last release from the base branch of `FIRST_RELEASE` (1.0.0)
64 let lastVersion = getLatestVersion(tagsToVersions(release[0].tags)) || FIRST_RELEASE;
65
66 return release.map(({name, tags, channel, ...rest}, idx) => {
67 const versions = tagsToVersions(tags);
68 // The new lastVersion is the highest version between the current branch last release and the previous branch lastVersion
69 lastVersion = highest(getLatestVersion(versions), lastVersion);
70 // The upper bound is:
71 // - None if the current branch is the last one of the release branches
72 // - Otherwise, The upper bound is the lowest version that is present on the current branch but none of the previous ones
73 const bound =
74 release.length - 1 === idx
75 ? undefined
76 : getFirstVersion(tagsToVersions(release[idx + 1].tags), release.slice(0, idx + 1));
77
78 const diff = bound ? semverDiff(lastVersion, bound) : null;
79 return {
80 ...rest,
81 channel: idx === 0 ? channel : isNil(channel) ? name : channel,
82 tags,
83 type: 'release',
84 name,
85 range: getRange(lastVersion, bound),
86 accept: bound ? RELEASE_TYPE.slice(0, RELEASE_TYPE.indexOf(diff)) : RELEASE_TYPE,
87 main: idx === 0,
88 };
89 });
90}
91
92function prerelease({prerelease}) {
93 return prerelease.map(({name, prerelease, channel, tags, ...rest}) => {
94 const preid = prerelease === true ? name : prerelease;
95 return {
96 ...rest,
97 channel: isNil(channel) ? name : channel,
98 type: 'prerelease',
99 name,
100 prerelease: preid,
101 tags,
102 };
103 });
104}
105
106module.exports = {maintenance, release, prerelease};