-
Notifications
You must be signed in to change notification settings - Fork 51
/
release.js
190 lines (165 loc) · 4.58 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
"use strict"
const { execSync: exec } = require("child_process")
const { readFileSync: read } = require("fs")
const { request } = require("https")
const { version: currentVersion } = require("../package.json")
const REPO = "BenoitZugmeyer/eslint-plugin-html"
const PACKAGE_FILES = [
"LICENSE",
"src/extract.js",
"src/getFileMode.js",
"src/index.js",
"src/pluginReference.js",
"src/settings.js",
"src/remapMessages.js",
"src/verifyPatch.js",
"src/verifyWithSharedScopes.js",
"src/verifyWithFlatConfigPatch.js",
"src/TransformableString.js",
"src/utils.js",
"package.json",
"CHANGELOG.md",
"README.md",
]
main().catch((error) => {
console.log(error)
process.exitCode = 1
})
async function main() {
const [version, channel] = getVersion()
verifyPackageContent()
runTests()
createVersion(version)
await verifyBuild()
releaseVersion(version, channel)
console.log("Release successful!")
}
function error(message) {
console.error(message)
process.exit(1)
}
function getVersion() {
console.log("Get version...")
// Verify repository status
if (exec("git status --porcelain").length) {
error("Repository should be clean")
}
const matches = read("CHANGELOG.md")
.toString()
.match(
/^(\d{4}-\d{2}-\d{2}) v(\d+\.\d+\.\d+(?:-([a-z]+)\.\d+)?)(?: - .+)?$/m
)
if (!matches) {
error("Invalid changelog format")
}
const [_, date, version, channel = "latest"] = matches
const expected = new Date().toISOString().slice(0, 10)
if (date !== expected) {
error(`Invalid changelog date (should be ${expected})`)
}
return [version, channel]
}
function runTests() {
console.log("Running tests...")
exec("npm run --silent test", { stdio: "inherit" })
exec("npm run --silent lint", { stdio: "inherit" })
}
function verifyPackageContent() {
console.log("Verify package content...")
const packed = exec("npm pack --dry-run 2>&1").toString().split("\n")
const STATE_INIT = 0
const STATE_TARBALL_CONTENTS = 1
const STATE_TARBALL_DETAILS = 2
let state = STATE_INIT
const content = new Set()
for (let line of packed) {
line = line.replace(/^npm notice /, "").trim()
switch (state) {
case STATE_INIT:
if (line === "=== Tarball Contents ===") {
state = STATE_TARBALL_CONTENTS
}
break
case STATE_TARBALL_CONTENTS:
if (line === "=== Tarball Details ===") {
state = STATE_TARBALL_DETAILS
} else {
content.add(line.match(/.*?\s+(.*)$/)[1])
}
break
}
}
const expectedContent = new Set(PACKAGE_FILES)
for (const file of expectedContent) {
if (!content.has(file)) error(`Missing ${file} in package content`)
}
for (const file of content) {
if (!expectedContent.has(file))
error(`Unexpected ${file} in package content`)
}
}
function createVersion(version) {
if (version === currentVersion) {
console.log(`Version ${version} already exists, skipping creation`)
return
}
console.log(`Creating version ${version}`)
exec(`npm version ${version}`, {
stdio: "inherit",
})
exec("git push --no-follow-tags", {
stdio: "inherit",
})
}
async function verifyBuild() {
const sha = exec("git rev-parse HEAD").toString().trim()
while (true) {
const { workflow_runs: runs } = await fetchWorkflowRuns()
const run = runs.find((commit) => commit.head_sha === sha)
if (!run) {
console.log("Workflow run not found yet...")
} else {
if (run.status === "completed") {
if (run.conclusion !== "success") {
error(`Workflow run ${run.html_url} finished as ${run.conclusion}`)
} else {
return
}
} else {
console.log(`Workflow run ${run.html_url} ${run.status}`)
}
}
await new Promise((resolve) => setTimeout(resolve, 3000))
}
}
function fetchWorkflowRuns() {
return new Promise((resolve, reject) => {
const req = request(
`https://api.github.com/repos/${REPO}/actions/runs?event=push&branch=main`,
{
headers: {
"User-Agent": "release-script/1.0.0",
},
}
)
req.on("error", reject)
req.on("response", (response) => {
const datum = []
response.on("error", reject)
response.on("data", (data) => datum.push(data))
response.on("end", () => {
resolve(JSON.parse(Buffer.concat(datum)))
})
})
req.end()
})
}
function releaseVersion(version, channel) {
console.log(`Publishing ${version} to channel ${channel}`)
exec(`npm publish --tag ${channel}`, {
stdio: "inherit",
})
exec("git push --tags", {
stdio: "inherit",
})
}