UNPKG

1.62 kBJavaScriptView Raw
1const {isUndefined} = require('lodash');
2const semver = require('semver');
3const {makeTag, isSameChannel} = require('./utils');
4
5/**
6 * Last release.
7 *
8 * @typedef {Object} LastRelease
9 * @property {string} version The version number of the last release.
10 * @property {string} gitHead The Git reference used to make the last release.
11 * @property {string} gitTag The git tag associated with the last release.
12 * @property {string} channel The channel on which of the last release was published.
13 * @property {string} name The name of the last release.
14 */
15
16/**
17 * Determine the Git tag and version of the last tagged release.
18 *
19 * - Filter out the branch tags that are not valid semantic version
20 * - Sort the versions
21 * - Retrive the highest version
22 *
23 * @param {Object} context semantic-release context.
24 * @param {Object} params Function parameters.
25 * @param {Object} params.before Find only releases with version number lower than this version.
26 *
27 * @return {LastRelease} The last tagged release or empty object if none is found.
28 */
29module.exports = ({branch, options: {tagFormat}}, {before} = {}) => {
30 const [{version, gitTag, channels} = {}] = branch.tags
31 .filter(
32 (tag) =>
33 ((branch.type === 'prerelease' && tag.channels.some((channel) => isSameChannel(branch.channel, channel))) ||
34 !semver.prerelease(tag.version)) &&
35 (isUndefined(before) || semver.lt(tag.version, before))
36 )
37 .sort((a, b) => semver.rcompare(a.version, b.version));
38
39 if (gitTag) {
40 return {version, gitTag, channels, gitHead: gitTag, name: makeTag(tagFormat, version)};
41 }
42
43 return {};
44};