1 | const {escapeRegExp, template} = require('lodash');
|
2 | const semver = require('semver');
|
3 | const pLocate = require('p-locate');
|
4 | const debug = require('debug')('semantic-release:get-last-release');
|
5 | const {getTags, isRefInHistory, getTagHead} = require('./git');
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | module.exports = async ({cwd, env, options: {tagFormat}, logger}) => {
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | const tagRegexp = `^${escapeRegExp(template(tagFormat)({version: ' '})).replace(' ', '(.+)')}`;
|
33 | const tags = (await getTags({cwd, env}))
|
34 | .map(tag => ({gitTag: tag, version: (tag.match(tagRegexp) || new Array(2))[1]}))
|
35 | .filter(
|
36 | tag => tag.version && semver.valid(semver.clean(tag.version)) && !semver.prerelease(semver.clean(tag.version))
|
37 | )
|
38 | .sort((a, b) => semver.rcompare(a.version, b.version));
|
39 |
|
40 | debug('found tags: %o', tags);
|
41 |
|
42 | const tag = await pLocate(tags, tag => isRefInHistory(tag.gitTag, {cwd, env}), {preserveOrder: true});
|
43 |
|
44 | if (tag) {
|
45 | logger.log(`Found git tag ${tag.gitTag} associated with version ${tag.version}`);
|
46 | return {gitHead: await getTagHead(tag.gitTag, {cwd, env}), ...tag};
|
47 | }
|
48 |
|
49 | logger.log('No git tag version found');
|
50 | return {};
|
51 | };
|