-
Notifications
You must be signed in to change notification settings - Fork 2
/
analyzeCommits.js
67 lines (54 loc) · 2.07 KB
/
analyzeCommits.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const analyzeCommits = require('@semantic-release/commit-analyzer')
const SemanticReleaseError = require('@semantic-release/error')
const execSync = require('child_process').execSync;
const lastTag = require('./lastTag');
const utils = require('./utils');
const until = f => array => {
const first = array[0];
if (!first || f(first)) {
return [];
}
return [ first, ...until(f)(array.slice(1)) ];
};
const lastTaggedRelease = () => {
const tag = lastTag({ branch: '', dev: false });
const args = tag ? `-1 ${tag}` : '--max-parents=0 HEAD';
return execSync(`git rev-list ${args}`, { encoding: 'utf8' }).trim();
};
module.exports = function (pluginConfig, config, cb) {
// run standard commit analysis
return analyzeCommits(pluginConfig, config, function(error, type) {
const branch = config.env.TRAVIS_BRANCH || config.env.GIT_LOCAL_BRANCH || utils.ghActionsBranch(config.env);
const branchTags = config.options.branchTags;
const distTag = branchTags && branchTags[branch];
// use default behavior if not publishing a custom dist-tag
if (!distTag) {
return cb(error, type);
}
let releaseType = type;
if (type) {
// map all types of releases to prereleases
releaseType = {
'major': 'premajor',
'minor': 'preminor',
'patch': 'prepatch'
}[type] || type;
console.log("Publishing a " + releaseType + " release.");
}
// suppress NPM releases of non-feature commits (chore/docs/etc)
const lastReleaseHash = lastTaggedRelease();
const untilLastRelease = until(commit => commit.hash === lastReleaseHash);
const commits = untilLastRelease(config.commits);
const commitSubset = Object.assign({}, config, { commits });
analyzeCommits(pluginConfig, commitSubset, function(_, type) {
if (!type) {
// commits since last dist-tag release are empty, suppress release
return cb(new SemanticReleaseError(
'There are no relevant changes, so no new version is released.',
'ENOCHANGE'
));
}
cb(error, releaseType);
});
});
};