-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathrelease.config.js
More file actions
164 lines (145 loc) · 5.12 KB
/
Copy pathrelease.config.js
File metadata and controls
164 lines (145 loc) · 5.12 KB
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
const { execFileSync } = require('node:child_process');
const parserOpts = {
// semantic-release's default parser does not treat "feat!:" consistently
// across the release lines this project supports, so keep the breaking-change
// header patterns explicit.
headerPattern: /^(\w*)(?:\(([\w$.\-*/ ]*)\))?!?: (.*)$/,
headerCorrespondence: ['type', 'scope', 'subject'],
breakingHeaderPattern: /^(\w*)(?:\(([\w$.\-*/ ]*)\))?!: (.*)$/,
noteKeywords: ['BREAKING CHANGE', 'BREAKING CHANGES', 'BREAKING-CHANGE'],
};
const expectedStableRelease = '2.0.0';
const initialStableBaseline = '1.0.0';
const syntheticBaselineEnvironment = 'EMULSIFY_SYNTHETIC_STABLE_BASELINE';
const syntheticBaselineShaEnvironment = 'EMULSIFY_SYNTHETIC_STABLE_BASELINE_SHA';
const syntheticBaselineSourceEnvironment = 'EMULSIFY_SYNTHETIC_STABLE_BASELINE_SOURCE';
function parseVersionParts(version) {
return String(version || '')
.split('-')[0]
.split('.')
.map((part) => Number.parseInt(part, 10));
}
function isAtLeastVersion(version, minimumVersion) {
const current = parseVersionParts(version);
const minimum = parseVersionParts(minimumVersion);
if (
current.length !== 3 ||
minimum.length !== 3 ||
current.some(Number.isNaN) ||
minimum.some(Number.isNaN)
) {
return false;
}
for (let index = 0; index < current.length; index += 1) {
if (current[index] > minimum[index]) {
return true;
}
if (current[index] < minimum[index]) {
return false;
}
}
return true;
}
function removeSyntheticBaseline(cwd) {
const baselineTag = process.env[syntheticBaselineEnvironment];
const expectedSha = process.env[syntheticBaselineShaEnvironment];
if (baselineTag !== initialStableBaseline || !expectedSha) {
return;
}
try {
const actualSha = execFileSync('git', ['rev-parse', '--verify', `${baselineTag}^{commit}`], {
cwd: cwd || process.cwd(),
encoding: 'utf8',
}).trim();
if (actualSha !== expectedSha) {
throw new Error(`${baselineTag} points to ${actualSha}, not the temporary baseline commit ${expectedSha}.`);
}
execFileSync('git', ['tag', '--delete', baselineTag], {
cwd: cwd || process.cwd(),
stdio: 'ignore',
});
delete process.env[syntheticBaselineEnvironment];
delete process.env[syntheticBaselineShaEnvironment];
}
catch (error) {
throw new Error(`Unable to remove the temporary ${baselineTag} release baseline tag before publishing.`, {
cause: error,
});
}
}
const expectedStableReleaseGuard = {
verifyRelease(pluginConfig, { branch, cwd, lastRelease = {}, nextRelease }) {
try {
// The 2.x release branch prepares the stable 2.0.0 baseline. After that
// baseline exists, normal semantic-release versioning can continue.
if (branch.name !== 'main' || isAtLeastVersion(lastRelease.version, expectedStableRelease)) {
return;
}
if (nextRelease.version !== expectedStableRelease) {
const message = `Expected semantic-release to prepare ${expectedStableRelease} on main, but computed ${nextRelease.version}. Confirm stable baseline tags and breaking-change commits before publishing.`;
throw new Error(message);
}
}
finally {
// semantic-release pushes every local tag. Remove the temporary baseline
// after version calculation, regardless of whether the guard accepts it.
removeSyntheticBaseline(cwd);
}
},
};
const expectedStableReleaseAnalyzer = {
analyzeCommits(pluginConfig, { branch, lastRelease = {} }) {
// The release-2.x branch is the first stable release line for the rebuilt
// parent theme. The runner temporarily exposes the latest 1.0.0 alpha as a
// local 1.0.0 baseline so semantic-release can calculate 2.0.0.
return branch.name === 'main' && !isAtLeastVersion(lastRelease.version, expectedStableRelease)
? 'major'
: null;
},
};
function finalizeReleaseNotesContext(context) {
if (
context.version === expectedStableRelease
&& context.previousTag === initialStableBaseline
&& process.env[syntheticBaselineSourceEnvironment]?.startsWith(`${initialStableBaseline}-`)
) {
// The temporary baseline is never published, so do not emit a compare URL
// whose left-hand tag would not exist on GitHub.
context.linkCompare = false;
}
return context;
}
module.exports = {
expectedStableRelease,
initialStableBaseline,
tagFormat: '${version}',
branches: ['main'],
repositoryUrl: 'git@github.com:emulsify-ds/emulsify-wordpress.git',
plugins: [
expectedStableReleaseAnalyzer,
['@semantic-release/commit-analyzer', { preset: 'angular', parserOpts }],
[
'@semantic-release/release-notes-generator',
{
preset: 'angular',
parserOpts,
writerOpts: {
commitsSort: ['subject', 'scope'],
finalizeContext: finalizeReleaseNotesContext,
},
},
],
expectedStableReleaseGuard,
[
'@semantic-release/github',
{
assets: [
{
path: 'dist-artifact/emulsify.zip',
label: 'Emulsify WordPress theme (with dependencies)',
},
],
},
],
],
};