forked from changesets/changesets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
303 lines (271 loc) 路 8.87 KB
/
index.ts
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import {
ReleasePlan,
Config,
NewChangeset,
PreState,
PackageGroup
} from "@changesets/types";
import determineDependents from "./determine-dependents";
import flattenReleases from "./flatten-releases";
import matchFixedConstraint from "./match-fixed-constraint";
import applyLinks from "./apply-links";
import { incrementVersion } from "./increment";
import * as semver from "semver";
import { InternalError } from "@changesets/errors";
import { Packages, Package } from "@manypkg/get-packages";
import { getDependentsGraph } from "@changesets/get-dependents-graph";
import { PreInfo, InternalRelease } from "./types";
function getPreVersion(version: string) {
let parsed = semver.parse(version)!;
let preVersion =
parsed.prerelease[1] === undefined ? -1 : parsed.prerelease[1];
if (typeof preVersion !== "number") {
throw new InternalError("preVersion is not a number");
}
preVersion++;
return preVersion;
}
function getSnapshotSuffix(
snapshot: string | boolean | undefined,
timestampSeparator: "-" | ".",
timestampPosition: "start" | "end"
): string | undefined {
if (snapshot === undefined) {
return;
}
const parts: string[] = [];
const timestamp = new Date()
.toISOString()
.replace(/\.\d{3}Z$/, "")
.replace(/[^\d]/g, "");
if (timestampPosition === "start") {
parts.push(timestamp);
if (typeof snapshot === "string") {
parts.push(snapshot);
}
} else {
if (typeof snapshot === "string") {
parts.push(snapshot);
}
parts.push(timestamp);
}
return `-${parts.join(timestampSeparator)}`;
}
function getNewVersion(
release: InternalRelease,
preInfo: PreInfo | undefined,
snapshotSuffix: string | undefined,
useCalculatedVersionForSnapshots: boolean
): string {
if (release.type === "none") {
return release.oldVersion;
}
/**
* Using version as 0.0.0 so that it does not hinder with other version release
* For example;
* if user has a regular pre-release at 1.0.0-beta.0 and then you had a snapshot pre-release at 1.0.0-canary-git-hash
* and a consumer is using the range ^1.0.0-beta, most people would expect that range to resolve to 1.0.0-beta.0
* but it'll actually resolve to 1.0.0-canary-hash. Using 0.0.0 solves this problem because it won't conflict with other versions.
*
* You can set `useCalculatedVersionForSnapshots` flag to true to use calculated versions if you don't care about the above problem.
*/
if (snapshotSuffix && !useCalculatedVersionForSnapshots) {
return `0.0.0${snapshotSuffix}`;
}
const calculatedVersion = incrementVersion(release, preInfo);
if (snapshotSuffix && useCalculatedVersionForSnapshots) {
return `${calculatedVersion}${snapshotSuffix}`;
}
return calculatedVersion;
}
function assembleReleasePlan(
changesets: NewChangeset[],
packages: Packages,
config: Config,
// intentionally not using an optional parameter here so the result of `readPreState` has to be passed in here
preState: PreState | undefined,
snapshot?: string | boolean
): ReleasePlan {
let packagesByName = new Map(
packages.packages.map(x => [x.packageJson.name, x])
);
const relevantChangesets = getRelevantChangesets(
changesets,
config.ignore,
preState
);
const preInfo = getPreInfo(changesets, packagesByName, config, preState);
// Caching the snapshot version here and use this if it is snapshot release
const snapshotSuffix = getSnapshotSuffix(
snapshot,
config.snapshotTimestampSeparator,
config.snapshotTimestampPosition
);
// releases is, at this point a list of all packages we are going to releases,
// flattened down to one release per package, having a reference back to their
// changesets, and with a calculated new versions
let releases = flattenReleases(
relevantChangesets,
packagesByName,
config.ignore
);
let dependencyGraph = getDependentsGraph(packages, {
bumpVersionsWithWorkspaceProtocolOnly:
config.bumpVersionsWithWorkspaceProtocolOnly
});
let releasesValidated = false;
while (releasesValidated === false) {
// The map passed in to determineDependents will be mutated
let dependentAdded = determineDependents({
releases,
packagesByName,
dependencyGraph,
preInfo,
config
});
// `releases` might get mutated here
let fixedConstraintUpdated = matchFixedConstraint(
releases,
packagesByName,
config
);
let linksUpdated = applyLinks(releases, packagesByName, config.linked);
releasesValidated =
!linksUpdated && !dependentAdded && !fixedConstraintUpdated;
}
if (preInfo?.state.mode === "exit") {
for (let pkg of packages.packages) {
// If a package had a prerelease, but didn't trigger a version bump in the regular release,
// we want to give it a patch release.
// Detailed explanation at https://github.com/changesets/changesets/pull/382#discussion_r434434182
if (preInfo.preVersions.get(pkg.packageJson.name) !== 0) {
const existingRelease = releases.get(pkg.packageJson.name);
if (!existingRelease) {
releases.set(pkg.packageJson.name, {
name: pkg.packageJson.name,
type: "patch",
oldVersion: pkg.packageJson.version,
changesets: []
});
} else if (
existingRelease.type === "none" &&
!config.ignore.includes(pkg.packageJson.name)
) {
existingRelease.type = "patch";
}
}
}
}
return {
changesets: relevantChangesets,
releases: [...releases.values()].map(incompleteRelease => {
return {
...incompleteRelease,
newVersion: getNewVersion(
incompleteRelease,
preInfo,
snapshotSuffix,
config.___experimentalUnsafeOptions_WILL_CHANGE_IN_PATCH
.useCalculatedVersionForSnapshots
)
};
}),
preState: preInfo?.state
};
}
function getRelevantChangesets(
changesets: NewChangeset[],
ignored: Readonly<string[]>,
preState: PreState | undefined
): NewChangeset[] {
for (const changeset of changesets) {
// Using the following 2 arrays to decide whether a changeset
// contains both ignored and not ignored packages
const ignoredPackages = [];
const notIgnoredPackages = [];
for (const release of changeset.releases) {
if (
ignored.find(ignoredPackageName => ignoredPackageName === release.name)
) {
ignoredPackages.push(release.name);
} else {
notIgnoredPackages.push(release.name);
}
}
if (ignoredPackages.length > 0 && notIgnoredPackages.length > 0) {
throw new Error(
`Found mixed changeset ${changeset.id}\n` +
`Found ignored packages: ${ignoredPackages.join(" ")}\n` +
`Found not ignored packages: ${notIgnoredPackages.join(" ")}\n` +
"Mixed changesets that contain both ignored and not ignored packages are not allowed"
);
}
}
if (preState && preState.mode !== "exit") {
let usedChangesetIds = new Set(preState.changesets);
return changesets.filter(changeset => !usedChangesetIds.has(changeset.id));
}
return changesets;
}
function getHighestPreVersion(
packageGroup: PackageGroup,
packagesByName: Map<string, Package>
): number {
let highestPreVersion = 0;
for (let pkg of packageGroup) {
highestPreVersion = Math.max(
getPreVersion(packagesByName.get(pkg)!.packageJson.version),
highestPreVersion
);
}
return highestPreVersion;
}
function getPreInfo(
changesets: NewChangeset[],
packagesByName: Map<string, Package>,
config: Config,
preState: PreState | undefined
): PreInfo | undefined {
if (preState === undefined) {
return;
}
let updatedPreState = {
...preState,
changesets: changesets.map(changeset => changeset.id),
initialVersions: {
...preState.initialVersions
}
};
for (const [, pkg] of packagesByName) {
if (updatedPreState.initialVersions[pkg.packageJson.name] === undefined) {
updatedPreState.initialVersions[pkg.packageJson.name] =
pkg.packageJson.version;
}
}
// Populate preVersion
// preVersion is the map between package name and its next pre version number.
let preVersions = new Map<string, number>();
for (const [, pkg] of packagesByName) {
preVersions.set(
pkg.packageJson.name,
getPreVersion(pkg.packageJson.version)
);
}
for (let fixedGroup of config.fixed) {
let highestPreVersion = getHighestPreVersion(fixedGroup, packagesByName);
for (let fixedPackage of fixedGroup) {
preVersions.set(fixedPackage, highestPreVersion);
}
}
for (let linkedGroup of config.linked) {
let highestPreVersion = getHighestPreVersion(linkedGroup, packagesByName);
for (let linkedPackage of linkedGroup) {
preVersions.set(linkedPackage, highestPreVersion);
}
}
return {
state: updatedPreState,
preVersions
};
}
export default assembleReleasePlan;