pack.ts 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * Pack one release family's whole publish set into a single directory, in
  3. * publish order, and record that order for the publish step.
  4. *
  5. * The pack step is the release boundary: it runs without credentials, produces
  6. * every tarball from one commit, and hands the publish step exactly those bytes
  7. * ([rationale](../../.agents/notes/implemented/process/2026-08-10-npm-release-sequences.md)).
  8. */
  9. import { existsSync, mkdirSync, rmSync, writeFileSync } from 'node:fs'
  10. import { join, resolve } from 'node:path'
  11. import { parseArgs } from 'node:util'
  12. import { releaseFamily, tarballName, type ReleaseFamily, type ReleaseMember } from './families.ts'
  13. import { isEntry, runConcurrent } from './process.ts'
  14. import { PUBLISH_ORDER_FILE, tarballFiles } from './tarball.ts'
  15. /** Where pack output lands when `--out` is omitted. */
  16. const DEFAULT_OUTPUT = 'dist/npm'
  17. /**
  18. * Pack one member and check what its tarball carries.
  19. * @param family - the release family being packed.
  20. * @param member - the member to pack.
  21. * @param destination - absolute output directory.
  22. * @returns The tarball filename.
  23. */
  24. async function packMember(family: ReleaseFamily, member: ReleaseMember, destination: string): Promise<string> {
  25. await runConcurrent('pnpm', ['--dir', member.directory, 'pack', '--pack-destination', destination])
  26. const filename = tarballName(member)
  27. const tarball = join(destination, filename)
  28. if (!existsSync(tarball)) throw new Error(`${member.name} produced no tarball at ${tarball}`)
  29. family.validatePayload(member, tarballFiles(tarball))
  30. return filename
  31. }
  32. /**
  33. * @returns The validated `--concurrency` value; 1 (the default) packs the
  34. * members one at a time, exactly as the credentialed publish workflows run it.
  35. */
  36. function parseConcurrency(raw: string | undefined): number {
  37. if (raw === undefined) return 1
  38. const parsed = Number.parseInt(raw, 10)
  39. if (!Number.isSafeInteger(parsed) || parsed < 1 || String(parsed) !== raw) {
  40. throw new Error(`--concurrency must be a positive integer, got ${JSON.stringify(raw)}`)
  41. }
  42. return parsed
  43. }
  44. /** Pack the family named by `--family` into `--out`. */
  45. async function main(): Promise<void> {
  46. const { values } = parseArgs({
  47. options: { family: { type: 'string' }, out: { type: 'string' }, concurrency: { type: 'string' } },
  48. allowPositionals: false,
  49. })
  50. if (values.family === undefined) throw new Error('usage: pack.ts --family <dsh|vendor> [--out dist/npm] [--concurrency 1]')
  51. const concurrency = parseConcurrency(values.concurrency)
  52. const family = releaseFamily(values.family)
  53. const root = process.cwd()
  54. const destination = resolve(root, values.out ?? DEFAULT_OUTPUT)
  55. const members = family.publishOrder(family.members(root)).order
  56. family.verifyBuildArtifacts(root)
  57. family.verifyVersions(members)
  58. rmSync(destination, { recursive: true, force: true })
  59. mkdirSync(destination, { recursive: true })
  60. // Members pack in a bounded pool; the recorded publish order stays the
  61. // members' order regardless of completion order, because each worker writes
  62. // its result at the member's own position.
  63. const order = new Array<string>(members.length)
  64. let cursor = 0
  65. await Promise.all(Array.from({ length: Math.min(concurrency, members.length) }, async () => {
  66. while (cursor < members.length) {
  67. const index = cursor
  68. cursor += 1
  69. const member = members[index]
  70. if (member === undefined) break
  71. order[index] = await packMember(family, member, destination)
  72. }
  73. }))
  74. writeFileSync(join(destination, PUBLISH_ORDER_FILE), `${order.join('\n')}\n`)
  75. console.log(`release pack: family ${family.id}, ${String(order.length)} tarball(s) in ${values.out ?? DEFAULT_OUTPUT}`)
  76. }
  77. if (isEntry(import.meta.url)) await main()