-
Notifications
You must be signed in to change notification settings - Fork 31.9k
Expand file tree
/
Copy pathpublish-release.js
More file actions
executable file
·371 lines (337 loc) · 11 KB
/
Copy pathpublish-release.js
File metadata and controls
executable file
·371 lines (337 loc) · 11 KB
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
#!/usr/bin/env node
// @ts-check
const path = require('path')
const execa = require('execa')
const semver = require('semver')
const { Sema } = require('async-sema')
const fs = require('fs/promises')
const {
getGitHubToken,
getGitHubTokenMissingMessage,
} = require('./release-github-auth')
const cwd = process.cwd()
const dryRun = process.argv.includes('--dry-run')
const maxPublishAttempts = 4
const publishRetryDelaySeconds = 15
;(async function () {
if (dryRun) {
console.log('Dry run: not publishing to npm')
}
const publishSema = new Sema(2)
const { version } = JSON.parse(
await fs.readFile(path.join(cwd, 'lerna.json'), 'utf-8')
)
const parsedVersion = semver.parse(version)
if (parsedVersion === null) {
throw new Error(`Invalid version in lerna.json: ${version}`)
}
const prereleaseChannel = parsedVersion.prerelease[0]
const isPrerelease = prereleaseChannel != null
console.log(`Publishing ${version}`)
let npmDistTag = isPrerelease ? String(prereleaseChannel) : 'latest'
try {
if (!isPrerelease) {
const res = await fetch(
`https://registry.npmjs.org/-/package/next/dist-tags`
)
const tags = await res.json()
if (semver.lt(version, tags.latest)) {
// If the current version is less than the latest, it means this
// is a backport release. Since NPM sets the 'latest' tag by default
// during publishing, when users install `next@latest`, they might
// get the backported version instead of the actual "latest" version.
// Therefore, we explicitly set the tag as 'backport' for backports.
// But force @latest tag if we accidentally tagged a prerelase as latest
if (!semver.prerelease(tags.latest)) {
npmDistTag = 'backport'
}
}
}
} catch (error) {
console.log('Failed to fetch Next.js dist tags from the NPM registry.')
throw error
}
console.log(`Publishing as "${npmDistTag}" dist tag...`)
const publish = async (label, args, attempt = 1) => {
let output = ''
try {
await publishSema.acquire()
const child = execa('pnpm', args, { stdio: 'pipe' })
const handleData = (type) => (chunk) => {
process[type].write(chunk)
output += chunk.toString()
}
child.stdout?.on('data', handleData('stdout'))
child.stderr?.on('data', handleData('stderr'))
// Return here to avoid retry logic
return await child
} catch (err) {
console.error(
`Failed to publish ${label} (attempt ${attempt} of ${maxPublishAttempts})`,
err
)
if (
output.includes('cannot publish over the previously published versions')
) {
console.error('Ignoring already published error', label)
return
}
if (attempt >= maxPublishAttempts) {
throw err
}
} finally {
publishSema.release()
}
// Recursive call need to be outside of the publishSema
console.log(`retrying ${label} in ${publishRetryDelaySeconds}s`)
await new Promise((resolve) =>
setTimeout(resolve, publishRetryDelaySeconds * 1000)
)
await publish(label, args, attempt + 1)
}
// Copy binaries to package folders, update version, and publish
const nativePackagesDir = path.join(cwd, 'crates/next-napi-bindings/npm')
const platforms = (await fs.readdir(nativePackagesDir)).filter(
(name) => !name.startsWith('.')
)
const nativeResults = await Promise.allSettled(
platforms.map(async (platform) => {
const binaryName = `next-swc.${platform}.node`
try {
await fs.cp(
path.join(cwd, 'packages/next-swc/native', binaryName),
path.join(nativePackagesDir, platform, binaryName)
)
} catch (error) {
if (dryRun) {
console.warn(
`Binary ${binaryName} not found, but ignoring due to dry run`
)
return
}
throw error
}
const pkgDir = path.join(nativePackagesDir, platform)
const pkg = JSON.parse(
await fs.readFile(path.join(pkgDir, 'package.json'), {
encoding: 'utf-8',
})
)
pkg.version = version
await fs.writeFile(
path.join(pkgDir, 'package.json'),
JSON.stringify(pkg, null, 2)
)
await publish(platform, [
'publish',
pkgDir,
'--access',
'public',
'--no-git-checks',
'--ignore-scripts',
'--tag',
npmDistTag,
...(dryRun ? ['--dry-run'] : []),
])
})
)
// Update name/version of wasm packages and publish
const pkgDirectory = 'crates/wasm'
const wasmDir = path.join(cwd, pkgDirectory)
const wasmResults = await Promise.allSettled(
['web', 'nodejs'].map(async (wasmTarget) => {
const pkgDir = path.join(wasmDir, `pkg-${wasmTarget}`)
const wasmPkg = JSON.parse(
await fs.readFile(path.join(pkgDir, 'package.json'), {
encoding: 'utf-8',
})
)
wasmPkg.name = `@next/swc-wasm-${wasmTarget}`
wasmPkg.version = version
wasmPkg.repository = {
type: 'git',
url: 'https://github.com/vercel/next.js',
directory: pkgDirectory,
}
await fs.writeFile(
path.join(pkgDir, 'package.json'),
JSON.stringify(wasmPkg, null, 2)
)
await publish(`wasm-${wasmTarget}`, [
'publish',
pkgDir,
'--access',
'public',
'--no-git-checks',
'--ignore-scripts',
'--tag',
npmDistTag,
...(dryRun ? ['--dry-run'] : []),
])
})
)
if (nativeResults.some((item) => item.status === 'rejected')) {
console.error(
`Not all native packages published successfully`,
nativeResults
)
process.exit(1)
}
if (wasmResults.some((item) => item.status === 'rejected')) {
console.error(`Not all wasm packages published successfully`, wasmResults)
if (process.env.BAIL_ON_NATIVE_WASM_PUBLISH_FAILURE === 'true') {
process.exit(1)
} else {
console.warn(
'Continuing with release even though some wasm packages failed to publish because BAIL_ON_NATIVE_WASM_PUBLISH_FAILURE is not set to true'
)
}
}
// Update optional dependencies versions
const nextPkg = JSON.parse(
await fs.readFile(path.join(cwd, 'packages/next/package.json'), {
encoding: 'utf-8',
})
)
for (const platform of platforms) {
const optionalDependencies = nextPkg.optionalDependencies || {}
optionalDependencies['@next/swc-' + platform] = version
nextPkg.optionalDependencies = optionalDependencies
}
// These props are only needed for development and build so we strip
// before publishing to reduce the total metadata size. The max is
// 100 MB for all versions of a pkg and we are currently at 30MB. Run:
// curl -w '%{size_download}' -so /dev/null https://registry.npmjs.org/next | awk '{print $1/1000000 " MB"}'
for (const field of ['devDependencies', 'taskr', 'scripts']) {
delete nextPkg[field]
}
await fs.writeFile(
path.join(cwd, 'packages/next/package.json'),
JSON.stringify(nextPkg, null, 2)
)
if (isPrerelease) {
// Lerna does not update peerDependencies at version time, and a static
// range like "^16.0.0" never satisfies a fresh prerelease version.
// Append the release version to any peer dependency on packages published
// from this repo so the published prerelease accepts its own version while
// keeping the stable ranges.
// Stable releases keep their wide ranges untouched so a published package
// keeps working with newer Next.js minors.
// TODO: Use `|| workspace:*` once pnpm supports that. Only a lone `workspace:*`
// works at the moment
const pnpmListJson = await execa('pnpm', [
'--silent',
'--recursive',
'--filter',
'./packages/**',
'list',
'--depth',
'-1',
'--json',
])
const workspacePackages = JSON.parse(pnpmListJson.stdout)
const publishedPackageNames = new Set(
workspacePackages
.filter((workspacePackage) => !workspacePackage.private)
.map((workspacePackage) => workspacePackage.name)
)
for (const workspacePackage of workspacePackages) {
if (workspacePackage.private) {
continue
}
const packageJsonPath = path.join(workspacePackage.path, 'package.json')
const manifest = JSON.parse(await fs.readFile(packageJsonPath, 'utf-8'))
const peerDependencies = manifest.peerDependencies ?? {}
let updated = false
for (const dependencyName of Object.keys(peerDependencies)) {
if (publishedPackageNames.has(dependencyName)) {
peerDependencies[dependencyName] =
`${peerDependencies[dependencyName]} || ${version}`
updated = true
}
}
if (updated) {
await fs.writeFile(packageJsonPath, JSON.stringify(manifest, null, 2))
console.log(
`Appended ${version} to peer dependencies in ${workspacePackage.name}`
)
}
}
}
await publish('workspace', [
'--filter',
'./packages/**',
'publish',
'--recursive',
'--access',
'public',
'--no-git-checks',
'--ignore-scripts',
'--report-summary',
'--tag',
npmDistTag,
...(dryRun ? ['--dry-run'] : []),
])
if (dryRun) {
console.log('Dry run: skipping GitHub release un-draft')
return
}
const githubToken = getGitHubToken()
if (!githubToken) {
throw new Error(getGitHubTokenMissingMessage())
}
if (isPrerelease) {
try {
const ghHeaders = {
Accept: 'application/vnd.github+json',
Authorization: `Bearer ${githubToken}`,
'X-GitHub-Api-Version': '2022-11-28',
}
const tag = `v${version}`
let release
let releasesData
// The release might take a minute to show up in
// the list so retry a bit
for (let i = 0; i < 6; i++) {
try {
const releaseUrlRes = await fetch(
`https://api.github.com/repos/vercel/next.js/releases`,
{
headers: ghHeaders,
}
)
releasesData = await releaseUrlRes.json()
release = releasesData.find((release) => release.tag_name === tag)
} catch (err) {
console.log(`Fetching release failed`, err)
}
if (!release) {
console.log(`Retrying in 10s...`)
await new Promise((resolve) => setTimeout(resolve, 10 * 1000))
}
}
if (!release) {
console.log(`Failed to find release`, releasesData)
return
}
const undraftRes = await fetch(release.url, {
headers: ghHeaders,
method: 'PATCH',
body: JSON.stringify({
draft: false,
name: tag,
}),
})
if (undraftRes.ok) {
console.log(`un-drafted ${prereleaseChannel} release successfully`)
} else {
console.log(`Failed to undraft`, await undraftRes.text())
}
} catch (err) {
console.error(`Failed to undraft release`, err)
}
}
})().catch((err) => {
console.error(err)
process.exit(1)
})