forked from changesets/changesets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createChangeset.ts
273 lines (243 loc) 路 7.97 KB
/
createChangeset.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
import chalk from "chalk";
import semver from "semver";
import * as cli from "../../utils/cli-utilities";
import { error, log } from "@changesets/logger";
import { Release, PackageJSON } from "@changesets/types";
import { Package } from "@manypkg/get-packages";
import { ExitError } from "@changesets/errors";
const { green, yellow, red, bold, blue, cyan } = chalk;
async function confirmMajorRelease(pkgJSON: PackageJSON) {
if (semver.lt(pkgJSON.version, "1.0.0")) {
// prettier-ignore
log(yellow(`WARNING: Releasing a major version for ${green(pkgJSON.name)} will be its ${red('first major release')}.`))
log(
yellow(
`If you are unsure if this is correct, contact the package's maintainers ${red(
"before committing this changeset"
)}.`
)
);
let shouldReleaseFirstMajor = await cli.askConfirm(
bold(
`Are you sure you want to release the ${red(
"first major version"
)} of ${pkgJSON.name}?`
)
);
return shouldReleaseFirstMajor;
}
return true;
}
async function getPackagesToRelease(
changedPackages: Array<string>,
allPackages: Array<Package>
) {
function askInitialReleaseQuestion(defaultChoiceList: Array<any>) {
return cli.askCheckboxPlus(
// TODO: Make this wording better
// TODO: take objects and be fancy with matching
`Which packages would you like to include?`,
defaultChoiceList,
(x) => {
// this removes changed packages and unchanged packages from the list
// of packages shown after selection
if (Array.isArray(x)) {
return x
.filter(
(x) => x !== "changed packages" && x !== "unchanged packages"
)
.map((x) => cyan(x))
.join(", ");
}
return x;
}
);
}
if (allPackages.length > 1) {
const unchangedPackagesNames = allPackages
.map(({ packageJson }) => packageJson.name)
.filter((name) => !changedPackages.includes(name));
const defaultChoiceList = [
{
name: "changed packages",
choices: changedPackages,
},
{
name: "unchanged packages",
choices: unchangedPackagesNames,
},
].filter(({ choices }) => choices.length !== 0);
let packagesToRelease = await askInitialReleaseQuestion(defaultChoiceList);
if (packagesToRelease.length === 0) {
do {
error("You must select at least one package to release");
error("(You most likely hit enter instead of space!)");
packagesToRelease = await askInitialReleaseQuestion(defaultChoiceList);
} while (packagesToRelease.length === 0);
}
return packagesToRelease.filter(
(pkgName) =>
pkgName !== "changed packages" && pkgName !== "unchanged packages"
);
}
return [allPackages[0].packageJson.name];
}
function getPkgJsonsByName(packages: Package[]) {
return new Map(
packages.map(({ packageJson }) => [packageJson.name, packageJson])
);
}
function formatPkgNameAndVersion(pkgName: string, version: string) {
return `${bold(pkgName)}@${bold(version)}`;
}
export default async function createChangeset(
changedPackages: Array<string>,
allPackages: Package[]
): Promise<{ confirmed: boolean; summary: string; releases: Array<Release> }> {
const releases: Array<Release> = [];
if (allPackages.length > 1) {
const packagesToRelease = await getPackagesToRelease(
changedPackages,
allPackages
);
let pkgJsonsByName = getPkgJsonsByName(allPackages);
let pkgsLeftToGetBumpTypeFor = new Set(packagesToRelease);
let pkgsThatShouldBeMajorBumped = (
await cli.askCheckboxPlus(
bold(`Which packages should have a ${red("major")} bump?`),
[
{
name: "all packages",
choices: packagesToRelease.map((pkgName) => {
return {
name: pkgName,
message: formatPkgNameAndVersion(
pkgName,
pkgJsonsByName.get(pkgName)!.version
),
};
}),
},
],
(x) => {
// this removes changed packages and unchanged packages from the list
// of packages shown after selection
if (Array.isArray(x)) {
return x
.filter((x) => x !== "all packages")
.map((x) => cyan(x))
.join(", ");
}
return x;
}
)
).filter((x) => x !== "all packages");
for (const pkgName of pkgsThatShouldBeMajorBumped) {
// for packages that are under v1, we want to make sure major releases are intended,
// as some repo-wide sweeping changes have mistakenly release first majors
// of packages.
let pkgJson = pkgJsonsByName.get(pkgName)!;
let shouldReleaseFirstMajor = await confirmMajorRelease(pkgJson);
if (shouldReleaseFirstMajor) {
pkgsLeftToGetBumpTypeFor.delete(pkgName);
releases.push({ name: pkgName, type: "major" });
}
}
if (pkgsLeftToGetBumpTypeFor.size !== 0) {
let pkgsThatShouldBeMinorBumped = (
await cli.askCheckboxPlus(
bold(`Which packages should have a ${green("minor")} bump?`),
[
{
name: "all packages",
choices: [...pkgsLeftToGetBumpTypeFor].map((pkgName) => {
return {
name: pkgName,
message: formatPkgNameAndVersion(
pkgName,
pkgJsonsByName.get(pkgName)!.version
),
};
}),
},
],
(x) => {
// this removes changed packages and unchanged packages from the list
// of packages shown after selection
if (Array.isArray(x)) {
return x
.filter((x) => x !== "all packages")
.map((x) => cyan(x))
.join(", ");
}
return x;
}
)
).filter((x) => x !== "all packages");
for (const pkgName of pkgsThatShouldBeMinorBumped) {
pkgsLeftToGetBumpTypeFor.delete(pkgName);
releases.push({ name: pkgName, type: "minor" });
}
}
if (pkgsLeftToGetBumpTypeFor.size !== 0) {
log(`The following packages will be ${blue("patch")} bumped:`);
pkgsLeftToGetBumpTypeFor.forEach((pkgName) => {
log(
formatPkgNameAndVersion(pkgName, pkgJsonsByName.get(pkgName)!.version)
);
});
for (const pkgName of pkgsLeftToGetBumpTypeFor) {
releases.push({ name: pkgName, type: "patch" });
}
}
} else {
let pkg = allPackages[0];
let type = await cli.askList(
`What kind of change is this for ${green(
pkg.packageJson.name
)}? (current version is ${pkg.packageJson.version})`,
["patch", "minor", "major"]
);
if (type === "major") {
let shouldReleaseAsMajor = await confirmMajorRelease(pkg.packageJson);
if (!shouldReleaseAsMajor) {
throw new ExitError(1);
}
}
releases.push({ name: pkg.packageJson.name, type });
}
log(
"Please enter a summary for this change (this will be in the changelogs)."
);
log(chalk.gray(" (submit empty line to open external editor)"));
let summary = await cli.askQuestion("Summary");
if (summary.length === 0) {
try {
summary = cli.askQuestionWithEditor(
"\n\n# Please enter a summary for your changes.\n# An empty message aborts the editor."
);
if (summary.length > 0) {
return {
confirmed: true,
summary,
releases,
};
}
} catch (err) {
log(
"An error happened using external editor. Please type your summary here:"
);
}
summary = await cli.askQuestion("");
while (summary.length === 0) {
summary = await cli.askQuestion(
"\n\n# A summary is required for the changelog! 馃槳"
);
}
}
return {
confirmed: false,
summary,
releases,
};
}