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 * Uploads use latest under the maintainer's RC policy (CONTRIBUTING.md),
61 * independently of the exact-version metadata selector. Existing versions
62 * are skipped; promoting their tags requires a separate npm dist-tag command.
63 */
64export async function publishDuringRc(
65 rootDirectory = root,
66 publishCommand = publishChangesets,
67) {
68 const { packages } = await getPackages(rootDirectory)
69 const publicPackages = packages.filter(
70 ({ packageJson }) => !packageJson.private,
71 )
72 const candidates = publicPackages.filter(({ packageJson }) =>
73 candidateVersion.test(packageJson.version),
74 )
75 const stable = publicPackages.filter(({ packageJson }) =>
76 stableVersion.test(packageJson.version),
77 )
78 if (
79 candidates.length === 0 ||
80 candidates.length + stable.length !== publicPackages.length
81 ) {
82 throw new Error(
83 'RC publishing found a public package with an unexpected version.',
84 )
85 }
86
87 const versions = new Set(
88 [...stable, ...candidates].map(({ packageJson }) => packageJson.version),
89 )
90 for (const version of versions) {
91 await publishWithHiddenPackages(
92 rootDirectory,
93 publicPackages.filter(
94 ({ packageJson }) => packageJson.version !== version,
95 ),
96 { version, tag: 'latest' },
97 publishCommand,
98 )
99 }
100}
101
102async function main() {
103 const preStatePath = join(root, '.changeset', 'pre.json')
104 const preState = existsSync(preStatePath)
105 ? JSON.parse(readFileSync(preStatePath, 'utf8'))
106 : undefined
107 if (preState?.mode === 'pre' && preState.tag === 'rc') {
108 await publishDuringRc()
109 } else {
110 publishChangesets(root)
111 }
112}
113
114if (
115 process.argv[1] &&
116 resolve(process.argv[1]) === fileURLToPath(import.meta.url)
117)
118 await main()