Repository navigation
Expand file tree
/
Copy pathcheck-release.mjs
More file actions
183 lines (167 loc) · 6.26 KB
/
Copy pathcheck-release.mjs
File metadata and controls
183 lines (167 loc) · 6.26 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
#!/usr/bin/env node
/**
* The release gate. `pnpm run preflight` checks the code. This script checks
* the release: is a publish of this version, now, to this dist-tag, what
* the maintainer wants? Both release workflows run it before preflight.
*
* Usage:
* node scripts/check-release.mjs --version 0.0.0-alpha.1 --dist-tag latest
*/
import { execFileSync } from 'node:child_process'
import { readFileSync } from 'node:fs'
import { dirname, join, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), '..')
const PACKAGE = '@rootnative/devhub'
// Trusted publishing needs npm 11.5.1, and `npm stage publish` needs
// 11.15.0. Both publishers on npmjs.com are stage-only.
const MIN_NPM = '11.15.0'
function arg(name) {
const at = process.argv.indexOf(`--${name}`)
return at === -1 ? undefined : process.argv[at + 1]
}
const problems = []
const warnings = []
const fail = (message) => problems.push(message)
const warn = (message) => warnings.push(message)
const requestedVersion = arg('version')
const distTag = arg('dist-tag')
if (!requestedVersion || !distTag) {
console.error(
'Usage: node scripts/check-release.mjs --version <version> --dist-tag <tag>',
)
process.exit(2)
}
/** A negative number when `a` is older than `b`. Ignores a prerelease part. */
function compareVersions(a, b) {
const parse = (version) => version.split('-')[0].split('.').map(Number)
const left = parse(a)
const right = parse(b)
for (let i = 0; i < Math.max(left.length, right.length); i++) {
const difference = (left[i] ?? 0) - (right[i] ?? 0)
if (difference !== 0) {
return difference
}
}
return 0
}
// An older npm does not try trusted publishing. It looks for a token, finds
// none, and fails with a 404 that names neither problem.
const npmVersion = execFileSync('npm', ['--version'], {
encoding: 'utf8',
}).trim()
if (compareVersions(npmVersion, MIN_NPM) < 0) {
fail(
`npm ${npmVersion} cannot do a staged publish with a trusted publisher. Use npm ${MIN_NPM} or later. In CI, change the npm version in both release workflows.`,
)
}
const manifest = JSON.parse(
readFileSync(join(repoRoot, 'packages', 'devhub', 'package.json'), 'utf8'),
)
const version = manifest.version
if (version !== requestedVersion) {
fail(
`You asked for ${requestedVersion}, but packages/devhub/package.json has ${version}. The release workflows set the version. Run one of them, and do not change the version by hand.`,
)
}
if (manifest.private === true) {
fail(
'packages/devhub/package.json has "private": true, so npm refuses the publish. Remove it on main, and run the release again.',
)
}
// The release workflows do not write the CHANGELOG. A person commits the
// section of the version before the run.
const changelog = readFileSync(
join(repoRoot, 'packages', 'devhub', 'CHANGELOG.md'),
'utf8',
).replace(/\r\n/g, '\n')
const escaped = version.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
if (
!new RegExp(`^## \\[${escaped}\\] - \\d{4}-\\d{2}-\\d{2}$`, 'm').test(
changelog,
)
) {
fail(
`packages/devhub/CHANGELOG.md has no "## [${version}] - YYYY-MM-DD" section. Move the entries under "## [Unreleased]" to it, commit it to main, and run the release again.`,
)
}
const releaseLink = `[${version}]: https://github.com/rootnative/devhub/releases/tag/devhub@${version}`
if (!changelog.split('\n').includes(releaseLink)) {
fail(
`packages/devhub/CHANGELOG.md has no link line "${releaseLink}". Add it at the end, and set "[unreleased]" to compare from devhub@${version}.`,
)
}
// The checkout must have the tags, so a CI checkout uses `fetch-depth: 0`.
const tag = `devhub@${version}`
let tagExists = true
try {
execFileSync('git', ['rev-parse', '-q', '--verify', `refs/tags/${tag}`], {
cwd: repoRoot,
stdio: 'ignore',
})
} catch {
tagExists = false
}
if (tagExists) {
fail(
`The tag ${tag} exists.\n` +
` If ${version} is on npm, npm does not accept it again. Release the next version.\n` +
` If it is not on npm, an earlier run pushed the tag and then failed. Delete the tag, and run the workflow again:\n` +
` git push --delete origin ${tag} && git tag -d ${tag}`,
)
}
/**
* The version that `latest` names on npm now. `null` when the package has
* no version on npm. `undefined` when the lookup failed.
*/
function latestOnNpm() {
try {
const out = execFileSync('npm', ['view', PACKAGE, 'dist-tags.latest'], {
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'pipe'],
}).trim()
return out === '' ? null : out
} catch (error) {
const text = `${error.stderr ?? ''}${error.message ?? ''}`
return /E404|404 Not Found/.test(text) ? null : undefined
}
}
// A prerelease on `latest` is correct while the alpha line is the only
// line: `npx @rootnative/devhub` resolves `latest`. It is wrong only when
// it replaces a stable version as the default install.
const isPrerelease = version.includes('-')
if (isPrerelease && distTag === 'latest') {
const current = latestOnNpm()
if (current === undefined) {
fail(
`${version} is a prerelease for "latest", and the npm lookup failed, so the check cannot tell if it replaces a stable version. Check the network, or use the "alpha" dist-tag.`,
)
} else if (current !== null && !current.includes('-')) {
fail(
`${version} is a prerelease, and it would replace the stable version ${current} on "latest". Use "alpha", "beta", or "rc".`,
)
} else {
warn(
`${version} is a prerelease for "latest". This is correct while no stable version is on npm. After the first stable version, use "alpha".`,
)
}
}
if (!isPrerelease && distTag !== 'latest') {
warn(
`${version} is a stable version for the "${distTag}" dist-tag, so "npm install ${PACKAGE}" does not install it.`,
)
}
const inActions = process.env.GITHUB_ACTIONS === 'true'
for (const message of warnings) {
console.error(`${inActions ? '::warning::' : 'WARN '}${message}`)
}
for (const message of problems) {
console.error(`${inActions ? '::error::' : 'ERROR '}${message}`)
}
if (problems.length > 0) {
console.error(`\n[check-release] ${problems.length} problem(s).`)
process.exit(1)
}
console.log(
`[check-release] ok: ${version} as ${tag} on the dist-tag ${distTag}, with npm ${npmVersion}.`,
)