forked from remix-run/remix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelease.js
422 lines (374 loc) · 10.7 KB
/
release.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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
const { execSync } = require("node:child_process");
const chalk = require("chalk");
const path = require("node:path");
const semver = require("semver");
const { default: simpleGit } = require("simple-git");
const git = simpleGit(path.resolve(__dirname, ".."));
const {
ensureCleanWorkingDirectory,
getPackageVersion,
prompt,
incrementRemixVersion,
} = require("./utils");
const releaseTypes = ["patch", "minor", "major"];
run(process.argv.slice(2)).then(
() => {
process.exit(0);
},
(error) => {
console.error(chalk.red(error));
process.exit(1);
}
);
/**
* @param {string[]} args
*/
async function run(args) {
ensureCleanWorkingDirectory();
/** @type {string | undefined} */
let phase;
/** @type {string | undefined} */
let givenVersion;
/** @type {string | undefined} */
let nextVersion;
// Validate args and get the next version number
if (
args.length === 1 &&
(releaseTypes.includes(args[0]) || semver.valid(args[0]))
) {
phase = "start";
givenVersion = args[0];
} else {
phase = args[0];
givenVersion = args[1];
}
let allTags = getAllTags();
let currentBranch = getCurrentBranch();
let gitArgs = { tags: allTags, initialBranch: currentBranch };
switch (phase) {
case "start": {
nextVersion = await initStart(givenVersion, gitArgs);
break;
}
case "bump": {
nextVersion = await initBump(gitArgs);
break;
}
case "finish": {
nextVersion = await initFinish(gitArgs);
break;
}
default:
throw Error(`Invalid argument. Usage:
$ pnpm release [start | bump | finish] [patch | minor | major]`);
}
if (versionExists(allTags, nextVersion)) {
throw Error(`Version ${nextVersion} has already been released.`);
}
let answer = await prompt(
`Are you sure you want to release version ${chalk.bold(nextVersion)}? [Yn] `
);
if (answer === false) return 0;
switch (phase) {
case "start": {
await execStart(nextVersion);
break;
}
case "bump": {
await execBump(nextVersion, gitArgs);
break;
}
case "finish": {
await execFinish(nextVersion, gitArgs);
break;
}
}
let versionTag = getVersionTag(nextVersion);
console.log(
chalk.green(
`Remix version ${nextVersion} is now ready to release. To trigger the release process, create a new release in GitHub from the ${versionTag} tag:
- Navigate to https://github.com/remix-run/remix/releases/new
- Select ${chalk.bold(versionTag)} from the "Choose a tag" menu
- Draft the release notes
- If this is a pre-release, be sure to select the ${chalk.bold(
"This is a pre-release"
)} checkbox
- Click ${chalk.bold("Publish release")}
Once the CI is complete, the new release will be published to npm. 🥳`
)
);
}
/**
* @param {string} givenVersion
* @param {GitAttributes} git
* @returns {Promise<string>}
*/
async function initStart(givenVersion, git) {
ensureDevBranch(git.initialBranch);
if (releaseTypes.includes(givenVersion)) {
givenVersion = `pre${givenVersion}`;
}
/** @type {string | null} */
let nextVersion = semver.valid(givenVersion);
if (nextVersion == null) {
nextVersion = getNextVersion(
await getPackageVersion("remix"),
givenVersion
);
}
return nextVersion;
}
/**
* @param {GitAttributes} git
* @returns {Promise<string>}
*/
async function initBump(git) {
ensureLatestReleaseBranch(git.initialBranch, git);
let versionFromBranch = getVersionFromReleaseBranch(git.initialBranch);
let currentVersion = git.tags
.filter((tag) => tag.startsWith("v" + versionFromBranch))
.sort((a, b) => (a > b ? -1 : a < b ? 1 : 0))[0];
let nextVersion = semver.inc(currentVersion, "prerelease");
if (!nextVersion) {
throw Error(`Invalid semver version: ${currentVersion}`);
}
return nextVersion;
}
/**
* @param {GitAttributes} git
* @returns {Promise<string>}
*/
async function initFinish(git) {
ensureLatestReleaseBranch(git.initialBranch, git);
let nextVersion = getVersionFromReleaseBranch(git.initialBranch);
return nextVersion;
}
/**
* @param {string} nextVersion
*/
async function execStart(nextVersion) {
let releaseBranch = getReleaseBranch(nextVersion);
await gitPull("dev");
try {
checkoutNewBranch(releaseBranch);
} catch {
throw Error(
`Branch ${chalk.bold(
releaseBranch
)} already exists. Delete the branch if you wish to create a new release from the same version, or use a different version number.`
);
}
await gitMerge("main", releaseBranch, { pullFirst: true });
await incrementRemixVersion(nextVersion);
// TODO: After testing a few times, execute git push as a part of the flow and
// remove the silly message
console.log(
chalk.green(`Version ${nextVersion} is ready to roll.`) +
"\n" +
chalk.yellow(`Ryan says since I'm just a 👶 script you probably shouldn't trust me *too* much just yet (he's right, I know!)
Run ${chalk.bold(`git push origin ${releaseBranch} --follow-tags`)}`)
);
// execSync(`git push origin ${releaseBranch} --follow-tags`);
}
/**
* @param {string} nextVersion
* @param {GitAttributes} git
*/
async function execBump(nextVersion, git) {
ensureReleaseBranch(git.initialBranch);
await incrementRemixVersion(nextVersion);
// TODO: After testing a few times, execute git push as a part of the flow and
// remove the silly message
console.log(
chalk.green(`Version ${nextVersion} is ready to roll.`) +
"\n" +
chalk.yellow(`Ryan says since I'm just a 👶 script you probably shouldn't trust me *too* much just yet (he's right, I know!)
Run ${chalk.bold(`git push origin ${git.initialBranch} --follow-tags`)}`)
);
// execSync(`git push origin ${git.initialBranch} --follow-tags`);
}
/**
* @param {string} nextVersion
* @param {GitAttributes} git
*/
async function execFinish(nextVersion, git) {
ensureReleaseBranch(git.initialBranch);
await gitMerge(git.initialBranch, "main");
await incrementRemixVersion(nextVersion);
await gitMerge(git.initialBranch, "dev");
}
/**
* @param {string} from
* @param {string} to
* @param {{ pullFirst?: boolean }} [opts]
*/
async function gitMerge(from, to, opts = {}) {
let initialBranch = getCurrentBranch();
execSync(`git checkout ${from}`);
if (opts.pullFirst) {
await gitPull(from);
}
execSync(`git checkout ${to}`);
let savedError;
/** @type {import('simple-git').MergeResult} */
let summary;
try {
summary = await git.merge([from]);
} catch (error) {
savedError = error;
// @ts-ignore
summary = error.git;
}
if (summary.conflicts.length > 0) {
let answer = await prompt(
`Merge conflicts detected. Resolve all conflicts and commit the changes before resuming the process.
${chalk.bold("Press Y to continue or N to cancel the release.")}`
);
if (answer === false) return 0;
} else if (savedError) {
console.error(chalk.red("Merge failed.\n"));
throw savedError;
}
execSync(`git checkout ${initialBranch}`);
}
/**
* @param {string} branch
* @returns
*/
async function gitPull(branch) {
try {
let resp = execSync(`git pull --rebase origin ${branch}`).toString();
if (hasMergeConflicts(resp)) {
let answer = await prompt(
`Merge conflicts detected. Resolve all conflicts and commit the changes before resuming the process.
${chalk.bold("Press Y to continue or N to cancel the release.")}`
);
if (answer === false) return 0;
} else if (mergeFailed(resp)) {
console.error(chalk.red("Merge failed.\n"));
throw Error(resp);
}
} catch (error) {
console.error(chalk.red(`Error rebasing to origin/${branch}`));
throw error;
}
}
/**
* @param {string | undefined} currentVersion
* @param {string} givenVersion
* @param {string | undefined} [prereleaseId]
*/
function getNextVersion(currentVersion, givenVersion, prereleaseId = "pre") {
if (givenVersion == null) {
throw Error(
"Missing next version. Usage: node scripts/release.js start [nextVersion]"
);
}
// @ts-ignore
let nextVersion = semver.inc(currentVersion, givenVersion, prereleaseId);
if (nextVersion == null) {
throw Error(`Invalid version specifier: ${givenVersion}`);
}
return nextVersion;
}
function getCurrentBranch() {
let output = execSync("git rev-parse --abbrev-ref HEAD").toString().trim();
return output;
}
/**
* @param {string} output
* @returns {boolean}
*/
function hasMergeConflicts(output) {
let lines = output.trim().split("\n");
return lines.some((line) => /^CONFLICT\s/.test(line));
}
/**
* @param {string} output
* @returns {boolean}
*/
function mergeFailed(output) {
let lines = output.trim().split("\n");
return lines.some((line) => /^Automatic merge failed;\s/.test(line));
}
/**
* @param {string} branch
*/
function checkoutNewBranch(branch) {
let output = execSync(`git checkout -b ${branch}`).toString().trim();
if (/^fatal:/.test(output)) {
throw Error(`Branch ${chalk.bold(output)} already exists.`);
}
}
function getAllTags() {
return execSync("git tag --list").toString().trim().split("\n");
}
/**
* @param {string} branch
* @returns {"dev"}
*/
function ensureDevBranch(branch) {
if (branch !== "dev") {
throw Error("Releases should be created from the dev branch.");
}
return branch;
}
/**
* @param {string} branch
*/
function ensureReleaseBranch(branch) {
let version = getVersionFromReleaseBranch(branch);
if (version == null || !semver.valid(version)) {
throw Error(
"You must be on a valid release branch when continuing the release process."
);
}
return version;
}
/**
* @param {string} branch
* @param {GitAttributes} git
*/
function ensureLatestReleaseBranch(branch, git) {
let versionFromBranch = ensureReleaseBranch(branch);
let taggedVersions = git.tags
.filter((tag) => /^v\d/.test(tag))
.sort(semver.compare);
let latestTaggedVersion = taggedVersions[taggedVersions.length - 1];
if (semver.compare(latestTaggedVersion, versionFromBranch) > 0) {
throw Error(
"You must be on the latest release branch when continuing the release process."
);
}
}
/**
* @param {string} branch
* @returns {string}
*/
function getVersionFromReleaseBranch(branch) {
return branch.slice(branch.indexOf("-") + 2);
}
/**
* @param {string} version
*/
function getVersionTag(version) {
return (version.startsWith("v") ? "" : "v") + version;
}
/**
* @param {string} version
*/
function getReleaseBranch(version) {
return `release-${getVersionTag(
version.includes("-") ? version.slice(0, version.indexOf("-")) : version
)}`;
}
/**
* @param {string[]} tags
* @param {string} version
*/
function versionExists(tags, version) {
return tags.includes(getVersionTag(version));
}
/**
* @typedef {{ tags: string[]; initialBranch: string }} GitAttributes
*/