-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathindex.js
82 lines (71 loc) · 3.35 KB
/
index.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const {isUndefined} = require('lodash');
const parser = require('conventional-commits-parser').sync;
const filter = require('conventional-commits-filter');
const debug = require('debug')('semantic-release:commit-analyzer');
const loadParserConfig = require('./lib/load-parser-config.js');
const loadReleaseRules = require('./lib/load-release-rules.js');
const analyzeCommit = require('./lib/analyze-commit.js');
const compareReleaseTypes = require('./lib/compare-release-types.js');
const RELEASE_TYPES = require('./lib/default-release-types.js');
const DEFAULT_RELEASE_RULES = require('./lib/default-release-rules.js');
/**
* Determine the type of release to create based on a list of commits.
*
* @param {Object} pluginConfig The plugin configuration.
* @param {String} pluginConfig.preset conventional-changelog preset ('angular', 'atom', 'codemirror', 'ember', 'eslint', 'express', 'jquery', 'jscs', 'jshint')
* @param {String} pluginConfig.config Requirable npm package with a custom conventional-changelog preset
* @param {String|Array} pluginConfig.releaseRules A `String` to load an external module or an `Array` of rules.
* @param {Object} pluginConfig.parserOpts Additional `conventional-changelog-parser` options that will overwrite ones loaded by `preset` or `config`.
* @param {Object} context The semantic-release context.
* @param {Array<Object>} context.commits The commits to analyze.
* @param {String} context.cwd The current working directory.
*
* @returns {String|null} the type of release to create based on the list of commits or `null` if no release has to be done.
*/
async function analyzeCommits(pluginConfig, context) {
const {commits, logger} = context;
const releaseRules = loadReleaseRules(pluginConfig, context);
const config = await loadParserConfig(pluginConfig, context);
let releaseType = null;
filter(
commits
.filter(({message, hash}) => {
if (!message.trim()) {
debug('Skip commit %s with empty message', hash);
return false;
}
return true;
})
.map(({message, ...commitProps}) => ({rawMsg: message, message, ...commitProps, ...parser(message, config)}))
).every(({rawMsg, ...commit}) => {
logger.log(`Analyzing commit: %s`, rawMsg);
let commitReleaseType;
// Determine release type based on custom releaseRules
if (releaseRules) {
debug('Analyzing with custom rules');
commitReleaseType = analyzeCommit(releaseRules, commit);
}
// If no custom releaseRules or none matched the commit, try with default releaseRules
if (isUndefined(commitReleaseType)) {
debug('Analyzing with default rules');
commitReleaseType = analyzeCommit(DEFAULT_RELEASE_RULES, commit);
}
if (commitReleaseType) {
logger.log('The release type for the commit is %s', commitReleaseType);
} else {
logger.log('The commit should not trigger a release');
}
// Set releaseType if commit's release type is higher
if (commitReleaseType && compareReleaseTypes(releaseType, commitReleaseType)) {
releaseType = commitReleaseType;
}
// Break loop if releaseType is the highest
if (releaseType === RELEASE_TYPES[0]) {
return false;
}
return true;
});
logger.log('Analysis of %s commits complete: %s release', commits.length, releaseType || 'no');
return releaseType;
}
module.exports = {analyzeCommits};