Open History to see the last commit
1import { execFileSync } from 'node:child_process'
2import { existsSync, readFileSync, unlinkSync, writeFileSync } from 'node:fs'
3import { dirname, join, resolve } from 'node:path'
4import { fileURLToPath } from 'node:url'
5import { getPackages } from '@manypkg/get-packages'
6
7const root = resolve(dirname(fileURLToPath(import.meta.url)), '..')
8const candidateVersion = /^1\.0\.0-rc\.\d+(?:\.\d+)?$/
9const stableVersion = /^\d+\.\d+\.\d+$/
10
11function publishChangesets(rootDirectory, publication) {
12 const flags = publication ? ['--tag', publication.tag] : []
13 execFileSync('pnpm', ['exec', 'changeset', 'publish', ...flags], {
14 cwd: rootDirectory,
15 stdio: 'inherit',
16 ...(publication
17 ? {
18 env: {
19 ...process.env,
20 npm_config_tag: publication.version,
21 NPM_CONFIG_TAG: publication.version,
22 },
23 }
24 : {}),
25 })
26}
27
28async function publishWithHiddenPackages(
29 rootDirectory,
30 hiddenPackages,
31 publication,
32 publishCommand,
33) {
34 const originals = hiddenPackages.map(({ dir }) => {
35 const path = join(dir, 'package.json')
36 return { path, contents: readFileSync(path) }
37 })
38 const preStatePath = join(rootDirectory, '.changeset', 'pre.json')
39 const preState = readFileSync(preStatePath)
40
41 try {
42 for (const { path, contents } of originals) {
43 const manifest = JSON.parse(contents.toString())
44 writeFileSync(
45 path,
46 `${JSON.stringify({ ...manifest, private: true }, null, 2)}\n`,
47 )
48 }
49 unlinkSync(preStatePath)
50 await publishCommand(rootDirectory, publication)
51 } finally {
52 for (const { path, contents } of originals) writeFileSync(path, contents)
53 writeFileSync(preStatePath, preState)
54 }
55}
56
57/**
58 * Publish stable versions first, then candidates, grouped by exact version.
59 * npm reads that exact version even when dist-tags are missing or stale.
60 * Explicit upload tags are independent of the metadata selector.
61 */
62export async function publishDuringRc(
63 rootDirectory = root,
64 publishCommand = publishChangesets,
65) {
66 const { packages } = await getPackages(rootDirectory)
67 const publicPackages = packages.filter(
68 ({ packageJson }) => !packageJson.private,
69 )
70 const candidates = publicPackages.filter(({ packageJson }) =>
71 candidateVersion.test(packageJson.version),
72 )
73 const stable = publicPackages.filter(({ packageJson }) =>
74 stableVersion.test(packageJson.version),
75 )
76 if (
77 candidates.length === 0 ||
78 candidates.length + stable.length !== publicPackages.length
79 ) {
80 throw new Error(
81 'RC publishing found a public package with an unexpected version.',
82 )
83 }
84
85 const versions = new Set(
86 [...stable, ...candidates].map(({ packageJson }) => packageJson.version),
87 )
88 for (const version of versions) {
89 await publishWithHiddenPackages(
90 rootDirectory,
91 publicPackages.filter(
92 ({ packageJson }) => packageJson.version !== version,
93 ),
94 { version, tag: stableVersion.test(version) ? 'latest' : 'rc' },
95 publishCommand,
96 )
97 }
98}
99
100async function main() {
101 const preStatePath = join(root, '.changeset', 'pre.json')
102 const preState = existsSync(preStatePath)
103 ? JSON.parse(readFileSync(preStatePath, 'utf8'))
104 : undefined
105 if (preState?.mode === 'pre' && preState.tag === 'rc') {
106 await publishDuringRc()
107 } else {
108 publishChangesets(root)
109 }
110}
111
112if (
113 process.argv[1] &&
114 resolve(process.argv[1]) === fileURLToPath(import.meta.url)
115)
116 await main()