/
index.js
181 lines (155 loc) · 5.86 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { EOL } from 'os';
import fs from 'fs';
import { Plugin } from 'release-it';
import conventionalRecommendedBump from 'conventional-recommended-bump';
import conventionalChangelog from 'conventional-changelog';
import semver from 'semver';
import concat from 'concat-stream';
class ConventionalChangelog extends Plugin {
static disablePlugin(options) {
return options.ignoreRecommendedBump ? null : 'version';
}
getInitialOptions(options, namespace) {
const tagName = options.git ? options.git.tagName : null;
options[namespace].tagPrefix = tagName ? tagName.replace(/v?\$\{version\}$/, '') : '';
return options[namespace];
}
async getChangelog(latestVersion) {
if (!latestVersion) latestVersion = '0.0.0';
if (!this.config.isIncrement) {
this.setContext({ version: latestVersion });
} else {
const { increment, isPreRelease, preReleaseId } = this.config.getContext('version');
const version = await this.getRecommendedVersion({ increment, latestVersion, isPreRelease, preReleaseId });
this.setContext({ version });
}
return this.generateChangelog();
}
async getRecommendedVersion({ increment, latestVersion, isPreRelease, preReleaseId }) {
const { version } = this.getContext();
if (version) return version;
const { options } = this;
this.debug({ increment, latestVersion, isPreRelease, preReleaseId });
this.debug('conventionalRecommendedBump', { options });
try {
const result = await conventionalRecommendedBump(options, options?.parserOpts);
this.debug({ result });
let { releaseType } = result;
if (increment) {
this.log.warn(`The recommended bump is "${releaseType}", but is overridden with "${increment}".`);
releaseType = increment;
}
if (increment && semver.valid(increment)) {
return increment;
} else if (isPreRelease) {
const type =
releaseType && (options.strictSemVer || !semver.prerelease(latestVersion))
? `pre${releaseType}`
: 'prerelease';
return semver.inc(latestVersion, type, preReleaseId);
} else if (releaseType) {
return semver.inc(latestVersion, releaseType, preReleaseId);
} else {
return null;
}
} catch (err) {
this.debug({ err });
throw err;
}
}
getChangelogStream(rawOptions = {}) {
const { version } = this.getContext();
const { isIncrement } = this.config;
const { latestTag, secondLatestTag, tagTemplate } = this.config.getContext();
const currentTag = isIncrement ? (tagTemplate ? tagTemplate.replace('${version}', version) : null) : latestTag;
const previousTag = isIncrement ? latestTag : secondLatestTag;
const releaseCount = rawOptions.releaseCount === 0 ? 0 : isIncrement ? 1 : 2;
const debug = this.config.isDebug ? this.debug : null;
const mergedOptions = Object.assign({}, { releaseCount }, this.options);
const { context, gitRawCommitsOpts, parserOpts, writerOpts, ...options } = mergedOptions;
const mergedContext = Object.assign({ version, previousTag, currentTag }, context);
const mergedGitRawCommitsOpts = Object.assign({ debug, from: previousTag }, gitRawCommitsOpts);
this.debug('conventionalChangelog', {
options,
context: mergedContext,
gitRawCommitsOpts: mergedGitRawCommitsOpts,
parserOpts,
writerOpts
});
return conventionalChangelog(options, mergedContext, mergedGitRawCommitsOpts, parserOpts, writerOpts);
}
generateChangelog(options) {
return new Promise((resolve, reject) => {
const resolver = result => resolve(result.toString().trim());
const changelogStream = this.getChangelogStream(options);
changelogStream.pipe(concat(resolver));
changelogStream.on('error', reject);
});
}
getPreviousChangelog() {
const { infile } = this.options;
return new Promise((resolve, reject) => {
const readStream = fs.createReadStream(infile);
const resolver = result => resolve(result.toString().trim());
readStream.pipe(concat(resolver));
readStream.on('error', reject);
});
}
async writeChangelog() {
const { infile, header: _header = '' } = this.options;
let { changelog } = this.config.getContext();
const header = _header.split(/\r\n|\r|\n/g).join(EOL);
let hasInfile = false;
try {
fs.accessSync(infile);
hasInfile = true;
} catch (err) {
this.debug(err);
}
let previousChangelog = '';
try {
previousChangelog = await this.getPreviousChangelog();
previousChangelog = previousChangelog.replace(header, '');
} catch (err) {
this.debug(err);
}
if (!hasInfile) {
changelog = await this.generateChangelog({ releaseCount: 0 });
this.debug({ changelog });
}
fs.writeFileSync(
infile,
header +
(changelog ? EOL + EOL + changelog.trim() : '') +
(previousChangelog ? EOL + EOL + previousChangelog.trim() : '') +
EOL
);
if (!hasInfile) {
await this.exec(`git add ${infile}`);
}
}
getIncrementedVersion(options) {
const { ignoreRecommendedBump } = this.options;
return ignoreRecommendedBump ? null : this.getRecommendedVersion(options);
}
getIncrementedVersionCI(options) {
return this.getIncrementedVersion(options);
}
async bump(version) {
const recommendedVersion = this.getContext('version');
this.setContext({ version });
if (this.options.ignoreRecommendedBump && recommendedVersion !== version) {
const changelog = await this.generateChangelog();
this.config.setContext({ changelog });
}
}
async beforeRelease() {
const { infile } = this.options;
const { isDryRun } = this.config;
this.log.exec(`Writing changelog to ${infile}`, isDryRun);
if (infile && !isDryRun) {
await this.writeChangelog();
}
}
}
export default ConventionalChangelog;