|
| 1 | +#!/usr/bin/env bun |
| 2 | + |
| 3 | +import * as NodeFS from 'node:fs/promises' |
| 4 | +import * as NodeOS from 'node:os' |
| 5 | +import * as NodePath from 'node:path' |
| 6 | +import * as NodeUtil from 'node:util' |
| 7 | + |
| 8 | +import { colors } from '#const.mjs' |
| 9 | + |
| 10 | +/** |
| 11 | + * @typedef {import('#const.mjs').Tool} Tool |
| 12 | + * @typedef {import('#const.mjs').Platform} Platform |
| 13 | + * @typedef {import('#const.mjs').Arch} Arch |
| 14 | + */ |
| 15 | + |
| 16 | +const RELEASE_ARTIFACT_PREFIX = 'foundry' |
| 17 | + |
| 18 | +main().catch(error => { |
| 19 | + console.error(colors.red, error) |
| 20 | + console.error(colors.reset) |
| 21 | + process.exit(1) |
| 22 | +}) |
| 23 | + |
| 24 | +/** |
| 25 | + * Entry point: locate the platform-specific artifact, extract the binary, |
| 26 | + * and delegate to prepublish to stage it into the package directory. |
| 27 | + * @returns {Promise<void>} |
| 28 | + */ |
| 29 | +async function main() { |
| 30 | + const { tool, platform, arch, releaseVersion, artifactDir } = resolveArgs() |
| 31 | + |
| 32 | + const artifactPrefix = NodePath.join( |
| 33 | + artifactDir, |
| 34 | + `${RELEASE_ARTIFACT_PREFIX}_${releaseVersion}_${platform}_${arch}` |
| 35 | + ) |
| 36 | + |
| 37 | + const archivePath = await chooseArchive(artifactPrefix) |
| 38 | + const extractionDir = await extractArchive(archivePath) |
| 39 | + |
| 40 | + try { |
| 41 | + const binaryPath = await resolveExtractedBinary({ tool, platform, extractionDir }) |
| 42 | + await stagePackage({ tool, platform, arch, binaryPath }) |
| 43 | + } finally { |
| 44 | + await NodeFS.rm(extractionDir, { recursive: true, force: true }) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Parse CLI arguments/environment. |
| 50 | + * @returns {{ tool: Tool; platform: Platform; arch: Arch; releaseVersion: string; artifactDir: string }} |
| 51 | + */ |
| 52 | +function resolveArgs() { |
| 53 | + const { values } = NodeUtil.parseArgs({ |
| 54 | + args: Bun.argv.slice(2), |
| 55 | + options: { |
| 56 | + tool: { type: 'string' }, |
| 57 | + platform: { type: 'string' }, |
| 58 | + arch: { type: 'string' }, |
| 59 | + release: { type: 'string' }, |
| 60 | + 'release-version': { type: 'string' }, |
| 61 | + artifacts: { type: 'string' }, |
| 62 | + 'artifact-dir': { type: 'string' } |
| 63 | + }, |
| 64 | + strict: true |
| 65 | + }) |
| 66 | + |
| 67 | + const tool = requireValue(values.tool || process.env.TARGET_TOOL, 'tool') |
| 68 | + const platform = requireValue(values.platform || process.env.PLATFORM_NAME, 'platform') |
| 69 | + const arch = requireValue(values.arch || process.env.ARCH, 'arch') |
| 70 | + const releaseVersion = requireValue( |
| 71 | + values.release || values['release-version'] || process.env.RELEASE_VERSION, |
| 72 | + 'release version' |
| 73 | + ) |
| 74 | + const artifactDir = requireValue( |
| 75 | + values.artifacts || values['artifact-dir'] || process.env.ARTIFACT_DIR, |
| 76 | + 'artifact directory' |
| 77 | + ) |
| 78 | + |
| 79 | + return /** @type {{ tool: Tool; platform: Platform; arch: Arch; releaseVersion: string; artifactDir: string }} */ ({ |
| 80 | + tool: /** @type {Tool} */ (tool), |
| 81 | + platform: /** @type {Platform} */ (platform), |
| 82 | + arch: /** @type {Arch} */ (arch), |
| 83 | + releaseVersion, |
| 84 | + artifactDir: NodePath.resolve(artifactDir) |
| 85 | + }) |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * @param {string | undefined} value |
| 90 | + * @param {string} name |
| 91 | + * @returns {string} |
| 92 | + */ |
| 93 | +function requireValue(value, name) { |
| 94 | + if (typeof value === 'string' && value.trim()) return value.trim() |
| 95 | + throw new Error(`Missing required ${name}`) |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * Determine which archive variant exists for the given artifact prefix. |
| 100 | + * @param {string} prefix |
| 101 | + * @returns {Promise<string>} |
| 102 | + */ |
| 103 | +async function chooseArchive(prefix) { |
| 104 | + const tarPath = `${prefix}.tar.gz` |
| 105 | + const zipPath = `${prefix}.zip` |
| 106 | + |
| 107 | + if (await pathExists(tarPath)) return tarPath |
| 108 | + if (await pathExists(zipPath)) return zipPath |
| 109 | + |
| 110 | + throw new Error(`No release artifact found for prefix ${prefix}`) |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * @param {string} filePath |
| 115 | + * @returns {Promise<boolean>} |
| 116 | + */ |
| 117 | +async function pathExists(filePath) { |
| 118 | + try { |
| 119 | + await NodeFS.access(filePath) |
| 120 | + return true |
| 121 | + } catch { |
| 122 | + return false |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +/** |
| 127 | + * Extract the archive into a temporary directory. |
| 128 | + * @param {string} archivePath |
| 129 | + * @returns {Promise<string>} |
| 130 | + */ |
| 131 | +async function extractArchive(archivePath) { |
| 132 | + const tempDir = await NodeFS.mkdtemp(NodePath.join(NodeOS.tmpdir(), 'foundry-npm-')) |
| 133 | + const command = archivePath.endsWith('.zip') |
| 134 | + ? Bun.$`unzip -o ${archivePath} -d ${tempDir}` |
| 135 | + : Bun.$`tar -xzf ${archivePath} -C ${tempDir}` |
| 136 | + |
| 137 | + const result = await command |
| 138 | + .env(process.env) |
| 139 | + .nothrow() |
| 140 | + |
| 141 | + if (result.exitCode !== 0) { |
| 142 | + const stderr = typeof result.stderr === 'string' |
| 143 | + ? result.stderr |
| 144 | + : result.stderr?.toString('utf8') |
| 145 | + const stdout = typeof result.stdout === 'string' |
| 146 | + ? result.stdout |
| 147 | + : result.stdout?.toString('utf8') |
| 148 | + throw new Error(`Failed to extract ${archivePath}: ${stderr || stdout || 'unknown error'}`) |
| 149 | + } |
| 150 | + |
| 151 | + return tempDir |
| 152 | +} |
| 153 | + |
| 154 | +/** |
| 155 | + * Locate the expected binary within the extraction directory. |
| 156 | + * @param {{ tool: Tool; platform: Platform; extractionDir: string }} options |
| 157 | + * @returns {Promise<string>} |
| 158 | + */ |
| 159 | +async function resolveExtractedBinary({ tool, platform, extractionDir }) { |
| 160 | + const binaryName = platform === 'win32' ? `${tool}.exe` : tool |
| 161 | + const candidate = NodePath.join(extractionDir, binaryName) |
| 162 | + |
| 163 | + if (await pathExists(candidate)) return candidate |
| 164 | + |
| 165 | + throw new Error(`Binary ${binaryName} not found in ${extractionDir}`) |
| 166 | +} |
| 167 | + |
| 168 | +/** |
| 169 | + * Delegate to prepublish to stage the extracted binary into the package dir. |
| 170 | + * @param {{ tool: Tool; platform: Platform; arch: Arch; binaryPath: string }} options |
| 171 | + * @returns {Promise<void>} |
| 172 | + */ |
| 173 | +async function stagePackage({ tool, platform, arch, binaryPath }) { |
| 174 | + console.info(colors.green, `Staging ${tool} (${platform}/${arch}) from ${binaryPath}`, colors.reset) |
| 175 | + |
| 176 | + const subprocess = Bun |
| 177 | + .$`bun ./scripts/prepublish.mjs --tool ${tool} --binary ${binaryPath}` |
| 178 | + .cwd(NodePath.resolve(import.meta.dir, '..')) |
| 179 | + .env({ |
| 180 | + ...process.env, |
| 181 | + TARGET_TOOL: tool, |
| 182 | + TOOL: tool, |
| 183 | + PLATFORM_NAME: platform, |
| 184 | + ARCH: arch |
| 185 | + }) |
| 186 | + |
| 187 | + const result = await subprocess.nothrow() |
| 188 | + if (result.exitCode !== 0) { |
| 189 | + const stderr = typeof result.stderr === 'string' |
| 190 | + ? result.stderr |
| 191 | + : result.stderr?.toString('utf8') |
| 192 | + throw new Error(stderr || `Failed to stage package for ${tool}`) |
| 193 | + } |
| 194 | +} |
0 commit comments