Open History to see the last commit
1/**
2 * Pack the public workspace packages for npm.
3 *
4 * Bun's packer rewrites `workspace:*` to the concrete versions npm
5 * consumers need. This script only writes tarballs. Staging is
6 * `npm stage publish` from `.github/workflows/publish.yml` (OIDC, env
7 * `npm`, workflow filename `publish.yml`) so the trusted-publisher
8 * match holds. Future Actions stages get provenance; laptop `0.1.0`
9 * did not.
10 *
11 * bun scripts/npm-pack.ts # dist/npm, then stop
12 * bun scripts/npm-pack.ts --pack-dir /tmp/npm
13 *
14 * If `--tag` or `RELEASE_TAG` is set, it must be `vMAJOR.MINOR.PATCH`
15 * (optional prerelease) and match every public package's version.
16 *
17 * A package with a `build` script is built first. A package whose
18 * `publishConfig` names entry points (rate-card: `dist/` JS and .d.ts
19 * instead of `src/` TypeScript) is packed from a staged copy with those
20 * fields lifted onto the manifest, because `bun pm pack` does not apply
21 * them. Every tarball must contain each file its manifest points at.
22 */
23import {
24 cpSync,
25 existsSync,
26 mkdirSync,
27 mkdtempSync,
28 readdirSync,
29 readFileSync,
30 rmSync,
31 writeFileSync,
32} from 'node:fs'
33import { tmpdir } from 'node:os'
34import { basename, join, resolve } from 'node:path'
35import { spawnSync } from 'node:child_process'
36
37export type PublicPackage = {
38 name: string
39 version: string
40 dir: string
41 dependencies: Record<string, string>
42}
43
44const WORKSPACE_OR_CATALOG = /^(workspace|catalog):/
45
46export function repoRoot(): string {
47 return join(import.meta.dirname, '..')
48}
49
50export function tarballFileName(name: string, version: string): string {
51 const unscoped = name.startsWith('@') ? name.slice(1).replace('/', '-') : name
52 return `${unscoped}-${version}.tgz`
53}
54
55/** `v0.1.0`, `refs/tags/v0.1.0`, or `v1.2.3-rc.1` → version. Else undefined. */
56export function versionFromTag(tag: string): string | undefined {
57 const trimmed = tag.replace(/^refs\/tags\//, '')
58 if (!/^v\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/.test(trimmed)) return undefined
59 return trimmed.slice(1)
60}
61
62export function listPublicPackages(
63 packagesDir: string = join(repoRoot(), 'packages'),
64): Array<PublicPackage> {
65 const pkgs: Array<PublicPackage> = []
66 for (const entry of readdirSync(packagesDir, { withFileTypes: true })) {
67 if (!entry.isDirectory()) continue
68 const pkgPath = join(packagesDir, entry.name, 'package.json')
69 let raw: string
70 try {
71 raw = readFileSync(pkgPath, 'utf8')
72 } catch {
73 continue
74 }
75 const parsed: unknown = JSON.parse(raw)
76 const pkg = asPackageJson(parsed, pkgPath)
77 if (pkg.private) continue
78 pkgs.push({
79 name: pkg.name,
80 version: pkg.version,
81 dir: join(packagesDir, entry.name),
82 dependencies: pkg.dependencies,
83 })
84 }
85 pkgs.sort((a, b) => a.name.localeCompare(b.name))
86 return pkgs
87}
88
89/** Dependents last (client/codegen, then vite, CLI) — npmjs.com approve order. */
90export function publishOrder(
91 packages: Array<PublicPackage>,
92): Array<PublicPackage> {
93 const byName = new Map(packages.map((pkg) => [pkg.name, pkg]))
94 const remaining = new Set(packages.map((pkg) => pkg.name))
95 const ordered: Array<PublicPackage> = []
96 while (remaining.size > 0) {
97 const ready = [...remaining]
98 .filter((name) => {
99 const pkg = byName.get(name)
100 if (!pkg) return false
101 return Object.keys(pkg.dependencies).every((dep) => !remaining.has(dep))
102 })
103 .sort()
104 if (ready.length === 0) {
105 throw new Error(
106 `workspace dependency cycle among ${[...remaining].join(', ')}`,
107 )
108 }
109 for (const name of ready) {
110 remaining.delete(name)
111 const pkg = byName.get(name)
112 if (!pkg) throw new Error(`missing package ${name}`)
113 ordered.push(pkg)
114 }
115 }
116 return ordered
117}
118
119export function assertVersionsMatchTag(
120 packages: Array<PublicPackage>,
121 tag: string,
122): void {
123 const expected = versionFromTag(tag)
124 if (expected === undefined) {
125 throw new Error(
126 `release tag ${JSON.stringify(tag)} is not vMAJOR.MINOR.PATCH`,
127 )
128 }
129 const mismatches = packages.filter((pkg) => pkg.version !== expected)
130 if (mismatches.length > 0) {
131 const detail = mismatches
132 .map((pkg) => `${pkg.name}@${pkg.version}`)
133 .join(', ')
134 throw new Error(
135 `tag v${expected} does not match package versions: ${detail}`,
136 )
137 }
138}
139
140export function assertPublishableManifest(
141 manifest: unknown,
142 source: string,
143): void {
144 const pkg = asPackageJson(manifest, source)
145 if (pkg.private) {
146 throw new Error(`${source}: packed manifest is private`)
147 }
148 for (const [name, spec] of Object.entries(pkg.dependencies)) {
149 if (WORKSPACE_OR_CATALOG.test(spec)) {
150 throw new Error(`${source}: ${name} still uses ${spec}`)
151 }
152 }
153}
154
155export function readPackedManifest(tarball: string): unknown {
156 return JSON.parse(
157 run('tar', ['-xOf', tarball, 'package/package.json'], process.cwd()),
158 ) as unknown
159}
160
161export function packedEntryNames(tarball: string): Array<string> {
162 return run('tar', ['-tzf', tarball], process.cwd())
163 .split('\n')
164 .filter((line) => line.length > 0)
165}
166
167/** Absolute dest so `bun pm pack` (cwd = package dir) writes where we look. */
168export function resolvePackDir(packDir: string): string {
169 return resolve(packDir)
170}
171
172/** `publishConfig` keys that configure npm, not the packed manifest. */
173const NPM_CONFIG_KEYS = new Set(['access', 'registry', 'tag', 'provenance'])
174
175/**
176 * The manifest npm should see: `publishConfig` manifest fields (`main`,
177 * `types`, `exports`) lifted to the top level, as pnpm does on publish.
178 * `undefined` when there is nothing to lift.
179 */
180export function publishManifest(
181 manifest: Record<string, unknown>,
182): Record<string, unknown> | undefined {
183 const config = manifest.publishConfig
184 if (!isRecord(config)) return undefined
185 const lifted = Object.entries(config).filter(
186 ([key]) => !NPM_CONFIG_KEYS.has(key),
187 )
188 if (lifted.length === 0) return undefined
189 return {
190 ...manifest,
191 ...Object.fromEntries(lifted),
192 publishConfig: Object.fromEntries(
193 Object.entries(config).filter(([key]) => NPM_CONFIG_KEYS.has(key)),
194 ),
195 }
196}
197
198/** Relative file paths a manifest's `main`, `types`, `bin` and `exports` name. */
199export function manifestEntryPoints(manifest: unknown): Array<string> {
200 if (!isRecord(manifest)) return []
201 const paths = new Set<string>()
202 const collect = (value: unknown) => {
203 if (typeof value === 'string') {
204 if (value.startsWith('./')) paths.add(value.slice(2))
205 } else if (isRecord(value)) {
206 Object.values(value).forEach(collect)
207 }
208 }
209 for (const key of ['main', 'types', 'module', 'bin', 'exports']) {
210 collect(manifest[key])
211 }
212 return [...paths].sort()
213}
214
215export function assertShipsEntryPoints(
216 manifest: unknown,
217 entries: Array<string>,
218 source: string,
219): void {
220 const missing = manifestEntryPoints(manifest).filter(
221 (path) => !entries.includes(`package/${path}`),
222 )
223 if (missing.length > 0) {
224 throw new Error(`${source}: does not ship ${missing.join(', ')}`)
225 }
226}
227
228export function run(command: string, args: Array<string>, cwd: string): string {
229 const result = spawnSync(command, args, { cwd, encoding: 'utf8' })
230 if (result.status !== 0) {
231 throw new Error(
232 `${command} ${args.join(' ')} in ${cwd} failed: ${result.stderr || result.stdout}`,
233 )
234 }
235 return result.stdout
236}
237
238export function packPackage(pkg: PublicPackage, packDir: string): string {
239 const dest = resolvePackDir(packDir)
240 mkdirSync(dest, { recursive: true })
241 const manifest: unknown = JSON.parse(
242 readFileSync(join(pkg.dir, 'package.json'), 'utf8'),
243 )
244 if (!isRecord(manifest)) {
245 throw new Error(`${pkg.dir}: package.json is not an object`)
246 }
247 if (
248 isRecord(manifest.scripts) &&
249 typeof manifest.scripts.build === 'string'
250 ) {
251 run('bun', ['run', 'build'], pkg.dir)
252 }
253 const published = publishManifest(manifest)
254 let cwd = pkg.dir
255 if (published) {
256 // Outside the workspace, so bun cannot rewrite `workspace:*` here;
257 // assertPublishableManifest refuses the tarball if one is left.
258 cwd = mkdtempSync(join(tmpdir(), 'npm-pack-'))
259 cpSync(pkg.dir, cwd, {
260 recursive: true,
261 filter: (path) => basename(path) !== 'node_modules',
262 })
263 writeFileSync(
264 join(cwd, 'package.json'),
265 `${JSON.stringify(published, null, 2)}\n`,
266 )
267 }
268 let stdout: string
269 try {
270 stdout = run('bun', ['pm', 'pack', '--destination', dest, '--quiet'], cwd)
271 } finally {
272 if (cwd !== pkg.dir) rmSync(cwd, { recursive: true, force: true })
273 }
274 const tarball = join(dest, tarballFileName(pkg.name, pkg.version))
275 if (!existsSync(tarball)) {
276 throw new Error(
277 `expected ${tarball} after bun pm pack; stdout=${stdout.trim()}`,
278 )
279 }
280 return tarball
281}
282
283export function packAll(
284 packages: Array<PublicPackage>,
285 packDir: string,
286): Array<{ pkg: PublicPackage; tarball: string }> {
287 const dest = resolvePackDir(packDir)
288 rmSync(dest, { recursive: true, force: true })
289 mkdirSync(dest, { recursive: true })
290 const packed: Array<{ pkg: PublicPackage; tarball: string }> = []
291 for (const pkg of packages) {
292 const tarball = packPackage(pkg, dest)
293 const manifest = readPackedManifest(tarball)
294 assertPublishableManifest(manifest, tarball)
295 const entries = packedEntryNames(tarball)
296 assertShipsEntryPoints(manifest, entries, tarball)
297 const tests = entries.filter((entry) => entry.endsWith('.test.ts'))
298 if (tests.length > 0) {
299 throw new Error(`${tarball} includes test files: ${tests.join(', ')}`)
300 }
301 packed.push({ pkg, tarball })
302 }
303 return packed
304}
305
306function asPackageJson(
307 value: unknown,
308 source: string,
309): {
310 name: string
311 version: string
312 private: boolean
313 dependencies: Record<string, string>
314} {
315 if (!isRecord(value)) {
316 throw new Error(`${source}: package.json is not an object`)
317 }
318 if (typeof value.name !== 'string' || typeof value.version !== 'string') {
319 throw new Error(`${source}: name and version must be strings`)
320 }
321 return {
322 name: value.name,
323 version: value.version,
324 private: value.private === true,
325 dependencies: {
326 ...stringRecord(value.dependencies, `${source} dependencies`),
327 ...stringRecord(
328 value.optionalDependencies,
329 `${source} optionalDependencies`,
330 ),
331 ...stringRecord(value.peerDependencies, `${source} peerDependencies`),
332 },
333 }
334}
335
336function isRecord(value: unknown): value is Record<string, unknown> {
337 return typeof value === 'object' && value !== null && !Array.isArray(value)
338}
339
340function stringRecord(value: unknown, source: string): Record<string, string> {
341 if (value === undefined) return {}
342 if (!isRecord(value)) throw new Error(`${source}: expected an object`)
343 const out: Record<string, string> = {}
344 for (const [key, spec] of Object.entries(value)) {
345 if (typeof spec !== 'string') {
346 throw new Error(`${source}: ${key} is not a string`)
347 }
348 out[key] = spec
349 }
350 return out
351}
352
353function parseCli(argv: Array<string>): {
354 packDir: string | undefined
355 tag: string | undefined
356} {
357 let packDir: string | undefined
358 let tag: string | undefined
359 for (let i = 0; i < argv.length; i += 1) {
360 const arg = argv[i]
361 if (arg === '--pack-dir') {
362 const value = argv[i + 1]
363 if (value === undefined) throw new Error('--pack-dir requires a path')
364 packDir = value
365 i += 1
366 continue
367 }
368 if (arg === '--tag') {
369 const value = argv[i + 1]
370 if (value === undefined) throw new Error('--tag requires a value')
371 tag = value
372 i += 1
373 continue
374 }
375 throw new Error(`unknown argument: ${arg}`)
376 }
377 return { packDir, tag }
378}
379
380export function main(argv: Array<string> = process.argv.slice(2)): void {
381 const flags = parseCli(argv)
382 const packages = publishOrder(listPublicPackages())
383 if (packages.length === 0) {
384 throw new Error('no public packages under packages/')
385 }
386 const tag = flags.tag ?? process.env.RELEASE_TAG
387 if (tag !== undefined && tag !== '') {
388 assertVersionsMatchTag(packages, tag)
389 }
390 const packDir = resolvePackDir(
391 flags.packDir ??
392 process.env.NPM_PACK_DIR ??
393 join(repoRoot(), 'dist', 'npm'),
394 )
395 const packed = packAll(packages, packDir)
396 console.log(`packed ${packed.length} packages → ${packDir}\n`)
397 for (const { pkg, tarball } of packed) {
398 console.log(` ${pkg.name}@${pkg.version}`)
399 console.log(` ${tarball}`)
400 }
401 console.log(
402 `\nNext: npm stage publish happens in .github/workflows/publish.yml (OIDC).`,
403 )
404}
405
406if (import.meta.main) {
407 try {
408 main()
409 } catch (error) {
410 const message = error instanceof Error ? error.message : String(error)
411 console.error(message)
412 process.exit(1)
413 }
414}