|
| 1 | +#!/usr/bin/env node |
| 2 | +// Postinstall: fetch the platform-matching prebuilt shared library from the |
| 3 | +// GitHub release that matches this package's version. Falls back silently if |
| 4 | +// the fetch fails so that monorepo dev installs (where the binary is built |
| 5 | +// locally via `zig build lib`) aren't blocked. |
| 6 | + |
| 7 | +import { spawnSync } from 'node:child_process' |
| 8 | +import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs' |
| 9 | +import { arch, platform } from 'node:os' |
| 10 | +import { dirname, join } from 'node:path' |
| 11 | +import { fileURLToPath } from 'node:url' |
| 12 | + |
| 13 | +const here = dirname(fileURLToPath(import.meta.url)) |
| 14 | +const pkgDir = join(here, '..') |
| 15 | +const prebuiltDir = join(pkgDir, 'prebuilt') |
| 16 | + |
| 17 | +const { version } = JSON.parse(readFileSync(join(pkgDir, 'package.json'), 'utf8')) |
| 18 | + |
| 19 | +// Map node platform/arch to release-artifact slugs (mirrors release.yml build_target calls) |
| 20 | +const PLATFORM_MAP = { |
| 21 | + 'darwin-arm64': { slug: 'darwin-arm64', libFile: 'libzig-dtsx.dylib' }, |
| 22 | + 'darwin-x64': { slug: 'darwin-x64', libFile: 'libzig-dtsx.dylib' }, |
| 23 | + 'linux-arm64': { slug: 'linux-arm64', libFile: 'libzig-dtsx.so' }, |
| 24 | + 'linux-x64': { slug: 'linux-x64', libFile: 'libzig-dtsx.so' }, |
| 25 | + 'win32-x64': { slug: 'windows-x64', libFile: 'zig-dtsx.dll' }, |
| 26 | + 'freebsd-x64': { slug: 'freebsd-x64', libFile: 'libzig-dtsx.so' }, |
| 27 | +} |
| 28 | + |
| 29 | +const key = `${platform()}-${arch()}` |
| 30 | +const entry = PLATFORM_MAP[key] |
| 31 | + |
| 32 | +if (!entry) { |
| 33 | + console.warn(`[zig-dtsx] no prebuilt for ${key}; run \`zig build lib\` locally if you need the FFI library`) |
| 34 | + process.exit(0) |
| 35 | +} |
| 36 | + |
| 37 | +// Skip if the lib already exists at the dev-build location — monorepo path. |
| 38 | +if (existsSync(join(pkgDir, 'zig-out', 'lib', entry.libFile)) || existsSync(join(pkgDir, 'zig-out', 'bin', entry.libFile))) { |
| 39 | + process.exit(0) |
| 40 | +} |
| 41 | + |
| 42 | +// Skip if prebuilt already in place from a prior install of the same version. |
| 43 | +if (existsSync(join(prebuiltDir, entry.libFile))) { |
| 44 | + process.exit(0) |
| 45 | +} |
| 46 | + |
| 47 | +const archiveName = `zig-dtsx-lib-${entry.slug}.tar.gz` |
| 48 | +const url = `https://github.com/stacksjs/dtsx/releases/download/v${version}/${archiveName}` |
| 49 | + |
| 50 | +try { |
| 51 | + const res = await fetch(url, { redirect: 'follow' }) |
| 52 | + if (!res.ok) |
| 53 | + throw new Error(`HTTP ${res.status}`) |
| 54 | + const buf = Buffer.from(await res.arrayBuffer()) |
| 55 | + |
| 56 | + mkdirSync(prebuiltDir, { recursive: true }) |
| 57 | + const tmpArchive = join(prebuiltDir, `.${archiveName}.tmp`) |
| 58 | + writeFileSync(tmpArchive, buf) |
| 59 | + |
| 60 | + const tarResult = spawnSync('tar', ['-xzf', tmpArchive, '-C', prebuiltDir], { stdio: 'inherit' }) |
| 61 | + rmSync(tmpArchive, { force: true }) |
| 62 | + |
| 63 | + if (tarResult.status !== 0) |
| 64 | + throw new Error(`tar exited with code ${tarResult.status}`) |
| 65 | + |
| 66 | + if (!existsSync(join(prebuiltDir, entry.libFile))) |
| 67 | + throw new Error(`archive did not contain expected file ${entry.libFile}`) |
| 68 | +} |
| 69 | +catch (err) { |
| 70 | + console.warn(`[zig-dtsx] could not fetch prebuilt from ${url}: ${err.message}`) |
| 71 | + console.warn(`[zig-dtsx] dev workflow: run \`zig build lib\` inside this package.`) |
| 72 | + // Exit 0 — never block installs over a missing prebuilt. |
| 73 | + process.exit(0) |
| 74 | +} |
0 commit comments