-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathprepare-release.mjs
107 lines (94 loc) · 2.56 KB
/
prepare-release.mjs
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
import { exec } from "@actions/exec"
import readChangesets from "@changesets/read"
import {
checkout,
commitAll,
forcePush,
resetBranch,
setupUser,
} from "./git-utils.mjs"
import fs from "fs"
import { getChangelogEntry } from "./md-utils.mjs"
import { Octokit } from "@octokit/action"
import github from "@actions/github"
import {
BASE_BRANCH,
PACKAGE_DIR,
PACKAGE_NAME,
RELEASE_BRANCH,
VERSION_COMMAND,
} from "./params.mjs"
const cwd = process.cwd()
const octokit = new Octokit({})
console.log("Reading changesets")
const changesets = await readChangesets(cwd)
if (changesets.length === 0) {
console.log("No changesets found")
process.exit(0)
}
console.log("Setting up user")
await setupUser()
console.log("Checking out release branch")
await checkout(RELEASE_BRANCH)
await resetBranch()
console.log("Running version command")
const [versionCommand, ...versionArgs] = VERSION_COMMAND.split(/\s+/)
await exec(versionCommand, versionArgs, { cwd })
console.log("Reading package files")
const pkg = JSON.parse(
await fs.promises.readFile(`${PACKAGE_DIR}/package.json`, "utf8"),
)
const changelog = await fs.promises.readFile(
`${PACKAGE_DIR}/CHANGELOG.md`,
"utf8",
)
const canary = JSON.parse(await fs.promises.readFile("canary.json", "utf8"))
console.log("Committing changes")
await commitAll(`${PACKAGE_NAME}@${pkg.version}`)
console.log("Pushing changes")
await forcePush(RELEASE_BRANCH)
console.log("Find existing release PR")
const { data: prs } = await octokit.pulls.list({
...github.context.repo,
state: "open",
base: BASE_BRANCH,
head: `${github.context.repo.owner}:${RELEASE_BRANCH}`,
})
console.log("Existing PRs", prs)
const entry = getChangelogEntry(changelog, pkg.version)
const title = `🚀 Release ${"`" + pkg.name}@${pkg.version + "`"} 🚀`
const canaryUrl = canary.packages[0].url
const body = `${entry.content}
---
You can try ${
"`" + PACKAGE_NAME + "@" + pkg.version + "`"
} in your project before it's released with:
${"```"}
npm i ${canaryUrl}
${"```"}
`
if (prs.length === 0) {
console.log("Creating new release PR")
const { data: pr } = await octokit.rest.pulls.create({
...github.context.repo,
base: BASE_BRANCH,
head: RELEASE_BRANCH,
title,
body,
})
console.log("Adding `release` label")
await octokit.rest.issues.addLabels({
...github.context.repo,
issue_number: pr.number,
labels: ["release"],
})
} else {
console.log("Updating existing release PR")
const { number } = prs[0]
await octokit.rest.pulls.update({
...github.context.repo,
pull_number: number,
title,
body,
})
}