1 | const {sortBy, isNil} = require('lodash');
|
2 | const semverDiff = require('semver-diff');
|
3 | const {FIRST_RELEASE, RELEASE_TYPE} = require('../definitions/constants');
|
4 | const {
|
5 | tagsToVersions,
|
6 | isMajorRange,
|
7 | getUpperBound,
|
8 | getLowerBound,
|
9 | highest,
|
10 | lowest,
|
11 | getLatestVersion,
|
12 | getFirstVersion,
|
13 | getRange,
|
14 | } = require('../utils');
|
15 |
|
16 | function 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 |
|
28 | const maintenanceMin =
|
29 |
|
30 | isMajorRange(range) && branches[idx - 1] && !isMajorRange(branches[idx - 1].range)
|
31 | ?
|
32 | getUpperBound(branches[idx - 1].range)
|
33 | :
|
34 | getLowerBound(range);
|
35 |
|
36 | const min = highest(getLatestVersion(versions) || FIRST_RELEASE, maintenanceMin);
|
37 |
|
38 | const base =
|
39 | (release[0] &&
|
40 | (getFirstVersion(tagsToVersions(release[0].tags), branches) ||
|
41 | getLatestVersion(tagsToVersions(release[0].tags)))) ||
|
42 | FIRST_RELEASE;
|
43 |
|
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 |
|
58 | function release({release}) {
|
59 | if (release.length === 0) {
|
60 | return release;
|
61 | }
|
62 |
|
63 |
|
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 |
|
69 | lastVersion = highest(getLatestVersion(versions), lastVersion);
|
70 |
|
71 |
|
72 |
|
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 |
|
92 | function 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 |
|
106 | module.exports = {maintenance, release, prerelease};
|