Skip to content

Commit 9f94093

Browse files
committed
fix: do not push dist-tag releases on every change
1 parent 36378ee commit 9f94093

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

analyzeCommits.js

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,33 @@
11
const analyzeCommits = require('@semantic-release/commit-analyzer')
2+
const SemanticReleaseError = require('@semantic-release/error')
3+
const execSync = require('child_process').execSync;
4+
5+
const until = f => array => {
6+
const first = array[0];
7+
8+
if (!first || f(first)) {
9+
return [];
10+
}
11+
12+
return [ first, ...until(f)(array.slice(1)) ];
13+
};
14+
15+
const lastTaggedRelease = () => execSync('git rev-list -1 `git describe --tags --abbrev=0 --match "v[0-9]*"`', { encoding: 'utf8' }).trim();
216

317
module.exports = function (pluginConfig, config, cb) {
418
// run standard commit analysis
519
return analyzeCommits(pluginConfig, config, function(error, type) {
620
const branch = config.env.TRAVIS_BRANCH || config.env.GIT_LOCAL_BRANCH;
721
const branchTags = config.options.branchTags;
822
const distTag = branchTags && branchTags[branch];
9-
let releaseType = type;
1023

11-
// if branch publishes a dist-tag
12-
if (type && distTag) {
24+
// use default behavior if not publishing a custom dist-tag
25+
if (!distTag) {
26+
return cb(error, type);
27+
}
28+
29+
let releaseType = type;
30+
if (type) {
1331
// map all types of releases to prereleases
1432
releaseType = {
1533
'major': 'premajor',
@@ -20,7 +38,23 @@ module.exports = function (pluginConfig, config, cb) {
2038
console.log("Publishing a " + releaseType + " release.");
2139
}
2240

23-
cb(error, releaseType);
41+
// suppress NPM releases of non-feature commits (chore/docs/etc)
42+
const lastReleaseHash = lastTaggedRelease();
43+
const untilLastRelease = until(commit => commit.hash === lastReleaseHash);
44+
const commits = untilLastRelease(config.commits);
45+
const commitSubset = Object.assign({}, config, { commits });
46+
47+
analyzeCommits(pluginConfig, commitSubset, function(_, type) {
48+
if (!type) {
49+
// commits since last dist-tag release are empty, suppress release
50+
return cb(new SemanticReleaseError(
51+
'There are no relevant changes, so no new version is released.',
52+
'ENOCHANGE'
53+
));
54+
}
55+
56+
cb(error, releaseType);
57+
});
2458
});
2559
};
2660

0 commit comments

Comments
 (0)