Open History to see the last commit
1import fs from 'node:fs'
2import path from 'node:path'
3β
4const releaseBranch = process.env.RELEASE_BRANCH
5const prereleaseBranches = new Set(['alpha', 'beta', 'rc'])
6const preStatePath = path.resolve('.changeset/pre.json')
7β
8if (!releaseBranch) {
9 throw new Error('Expected the RELEASE_BRANCH environment variable')
10}
11β
12if (releaseBranch === 'main') {
13 if (fs.existsSync(preStatePath)) {
14 throw new Error(
15 'Refusing to publish main while .changeset/pre.json exists. Exit prerelease mode before releasing from main.',
16 )
17 }
18β
19 console.log('Verified stable release mode for main')
20 process.exit(0)
21}
22β
23if (!prereleaseBranches.has(releaseBranch)) {
24 throw new Error(`Unsupported release branch: ${releaseBranch}`)
25}
26β
27if (!fs.existsSync(preStatePath)) {
28 throw new Error(
29 `Refusing to publish ${releaseBranch} without .changeset/pre.json`,
30 )
31}
32β
33const preState = JSON.parse(fs.readFileSync(preStatePath, 'utf8')) as {
34 mode?: string
35 tag?: string
36}
37β
38if (preState.mode !== 'pre') {
39 throw new Error(
40 `Refusing to publish ${releaseBranch} while prerelease mode is ${String(preState.mode)}`,
41 )
42}
43β
44if (preState.tag !== releaseBranch) {
45 throw new Error(
46 `Refusing to publish ${releaseBranch} with the ${String(preState.tag)} npm dist-tag`,
47 )
48}
49β
50console.log(
51 `Verified prerelease mode for ${releaseBranch} with the ${preState.tag} npm dist-tag`,
52)