|
| 1 | +/** |
| 2 | + * @file Entry point for socket-bin's cross-org tail publishes. Driven by the |
| 3 | + * `publish-cross-org.yml` workflow's `workflow_dispatch` inputs (mapped |
| 4 | + * into env vars: `SOURCE_REPO`, `RELEASE_TAG`, `DRY_RUN`). Reads |
| 5 | + * `scripts/source-allowlist.mts` for the trust boundary, delegates the |
| 6 | + * download → verify → extract → stage pipeline to the fleet |
| 7 | + * `stageMultiPackagePublish` runner, and on non-dry-run iterates the |
| 8 | + * staged tails and `npm publish --provenance` each one. |
| 9 | + * |
| 10 | + * Mirrors `socket-addon/scripts/publish-cross-org.mts` byte-for-byte |
| 11 | + * except for the tail-directory convention (`packages/<prefix><triplet>` |
| 12 | + * is identical in both repos) and the `kind` field on allowlist rows |
| 13 | + * (socket-addon families are NAPI; socket-bin families are CLI). The |
| 14 | + * `kind` distinction flows through `buildBinaryPathInTail()`, which |
| 15 | + * places `bin/<name>[.exe]` instead of `<name>.node`. |
| 16 | + * |
| 17 | + * The allowlist is empty until the first binsuite family is scaffolded |
| 18 | + * — the runner refuses on `allowlist-miss`, which is the intended state |
| 19 | + * for a freshly-scaffolded publisher with no authorized sources yet. |
| 20 | + * |
| 21 | + * Exit codes: |
| 22 | + * 0 — every requested tail staged + (unless dry-run) published. |
| 23 | + * 1 — any stage or publish failure (the first one; fail-fast). |
| 24 | + */ |
| 25 | + |
| 26 | +import path from 'node:path' |
| 27 | +import { fileURLToPath } from 'node:url' |
| 28 | + |
| 29 | +import { getDefaultLogger } from '@socketsecurity/lib-stable/logger/default' |
| 30 | + |
| 31 | +import { runCommand } from './fleet/util/run-command.mts' |
| 32 | +import { |
| 33 | + MultiPackageStageError, |
| 34 | + stageMultiPackagePublish, |
| 35 | + type TailStageOutcome, |
| 36 | +} from './fleet/util/multi-package-publish.mts' |
| 37 | +import type { GitHubRepoSlug } from './fleet/util/source-allowlist.mts' |
| 38 | +import { |
| 39 | + buildBinaryPathInTail, |
| 40 | + findAllowlistEntry, |
| 41 | +} from './fleet/util/source-allowlist.mts' |
| 42 | +import { SOURCE_ALLOWLIST } from './source-allowlist.mts' |
| 43 | + |
| 44 | +const logger = getDefaultLogger() |
| 45 | +const REPO_ROOT = path.dirname(path.dirname(fileURLToPath(import.meta.url))) |
| 46 | +const STAGING_DIR = path.join(REPO_ROOT, '.cross-org-stage') |
| 47 | + |
| 48 | +function readRequiredEnv(name: string): string { |
| 49 | + const value = process.env[name] |
| 50 | + if (!value || value.trim() === '') { |
| 51 | + throw new Error( |
| 52 | + `Missing required env: ${name}. Set it via workflow_dispatch input or the calling shell.`, |
| 53 | + ) |
| 54 | + } |
| 55 | + return value.trim() |
| 56 | +} |
| 57 | + |
| 58 | +function isGitHubRepoSlug(value: string): value is GitHubRepoSlug { |
| 59 | + const parts = value.split('/') |
| 60 | + return parts.length === 2 && parts[0]!.length > 0 && parts[1]!.length > 0 |
| 61 | +} |
| 62 | + |
| 63 | +async function publishTail(tail: TailStageOutcome): Promise<void> { |
| 64 | + logger.log(`Publishing ${tail.tailName}@${tail.version}…`) |
| 65 | + const exitCode = await runCommand( |
| 66 | + 'npm', |
| 67 | + ['publish', '--access', 'public', '--provenance'], |
| 68 | + { cwd: tail.tailDir }, |
| 69 | + ) |
| 70 | + if (exitCode !== 0) { |
| 71 | + throw new Error( |
| 72 | + `npm publish failed for ${tail.tailName}@${tail.version} (exit ${exitCode}). See above stderr from npm.`, |
| 73 | + ) |
| 74 | + } |
| 75 | + logger.success(`Published ${tail.tailName}@${tail.version}`) |
| 76 | +} |
| 77 | + |
| 78 | +async function main(): Promise<void> { |
| 79 | + const sourceRepoRaw = readRequiredEnv('SOURCE_REPO') |
| 80 | + if (!isGitHubRepoSlug(sourceRepoRaw)) { |
| 81 | + throw new Error( |
| 82 | + `SOURCE_REPO must be <owner>/<repo>; got ${sourceRepoRaw}.`, |
| 83 | + ) |
| 84 | + } |
| 85 | + const sourceRepo: GitHubRepoSlug = sourceRepoRaw |
| 86 | + const releaseTag = readRequiredEnv('RELEASE_TAG') |
| 87 | + const dryRun = process.env['DRY_RUN'] !== 'false' |
| 88 | + |
| 89 | + const entry = findAllowlistEntry(SOURCE_ALLOWLIST, sourceRepo, releaseTag) |
| 90 | + if (!entry) { |
| 91 | + throw new MultiPackageStageError( |
| 92 | + `No socket-bin allowlist row matches ${sourceRepo} tag ${releaseTag}. Add a SourceAllowlistEntry in scripts/source-allowlist.mts or correct the inputs.`, |
| 93 | + 'allowlist-miss', |
| 94 | + ) |
| 95 | + } |
| 96 | + |
| 97 | + logger.log(`socket-bin cross-org publish`) |
| 98 | + logger.log(` source: ${sourceRepo} @ ${releaseTag}`) |
| 99 | + logger.log(` family: ${entry.familyId} (${entry.kind} → ${entry.binaryName})`) |
| 100 | + logger.log(` dry-run: ${dryRun}`) |
| 101 | + |
| 102 | + const result = await stageMultiPackagePublish({ |
| 103 | + allowlist: SOURCE_ALLOWLIST, |
| 104 | + sourceRepo, |
| 105 | + releaseTag, |
| 106 | + tailDirFor: triplet => |
| 107 | + path.join(REPO_ROOT, 'packages', `${entry.namePrefix}${triplet}`), |
| 108 | + binaryPathInTail: triplet => buildBinaryPathInTail(entry, triplet), |
| 109 | + stagingDir: STAGING_DIR, |
| 110 | + dryRun, |
| 111 | + }) |
| 112 | + |
| 113 | + logger.log( |
| 114 | + `Staged ${result.tails.length} tail(s) for ${result.entry.familyId}@${result.version}`, |
| 115 | + ) |
| 116 | + |
| 117 | + if (dryRun) { |
| 118 | + logger.log('Dry run — skipping npm publish step. Done.') |
| 119 | + return |
| 120 | + } |
| 121 | + |
| 122 | + for (let i = 0, { length } = result.tails; i < length; i += 1) { |
| 123 | + // eslint-disable-next-line no-await-in-loop |
| 124 | + await publishTail(result.tails[i]!) |
| 125 | + } |
| 126 | + |
| 127 | + logger.success( |
| 128 | + `socket-bin cross-org publish complete: ${result.tails.length} tail(s) → ${entry.targetScope}/${entry.namePrefix}*@${result.version}`, |
| 129 | + ) |
| 130 | +} |
| 131 | + |
| 132 | +if (process.argv[1] === fileURLToPath(import.meta.url)) { |
| 133 | + main().catch((err: unknown) => { |
| 134 | + logger.error(err instanceof Error ? err.message : String(err)) |
| 135 | + process.exitCode = 1 |
| 136 | + }) |
| 137 | +} |
| 138 | + |
| 139 | +export { main } |
0 commit comments