|
| 1 | +import * as path from "node:path"; |
| 2 | +import fs from "node:fs/promises"; |
| 3 | +import {getPackageJson} from "./version.mjs"; |
| 4 | + |
| 5 | +const CpuToNodeArch = { |
| 6 | + x86_64: "x64", |
| 7 | + aarch64: "arm64", |
| 8 | + i686: "ia32", |
| 9 | + armv7: "arm" |
| 10 | +}; |
| 11 | + |
| 12 | +const NodeArchToCpu = { |
| 13 | + x64: "x86_64", |
| 14 | + arm64: "aarch64", |
| 15 | + ia32: "i686", |
| 16 | + arm: "armv7" |
| 17 | +}; |
| 18 | + |
| 19 | +const SysToNodePlatform = { |
| 20 | + linux: "linux", |
| 21 | + freebsd: "freebsd", |
| 22 | + darwin: "darwin", |
| 23 | + windows: "win32" |
| 24 | +}; |
| 25 | + |
| 26 | +const AbiToNodeLibc = { |
| 27 | + gnu: "glibc", |
| 28 | + musl: "musl" |
| 29 | +}; |
| 30 | + |
| 31 | +const UniArchsByPlatform = { |
| 32 | + darwin: ["x64", "arm64"] |
| 33 | +}; |
| 34 | + |
| 35 | +/** |
| 36 | + * A triple is a specific format for specifying a target architecture. |
| 37 | + * Triples may be referred to as a target triple which is the architecture for the artifact produced, and the host triple which is the architecture that the compiler is running on. |
| 38 | + * The general format of the triple is `<arch><sub>-<vendor>-<sys>-<abi>` where: |
| 39 | + * - `arch` = The base CPU architecture, for example `x86_64`, `i686`, `arm`, `thumb`, `mips`, etc. |
| 40 | + * - `sub` = The CPU sub-architecture, for example `arm` has `v7`, `v7s`, `v5te`, etc. |
| 41 | + * - `vendor` = The vendor, for example `unknown`, `apple`, `pc`, `nvidia`, etc. |
| 42 | + * - `sys` = The system name, for example `linux`, `windows`, `darwin`, etc. none is typically used for bare-metal without an OS. |
| 43 | + * - `abi` = The ABI, for example `gnu`, `android`, `eabi`, etc. |
| 44 | + */ |
| 45 | +function parseTriple(rawTriple) { |
| 46 | + const triple = rawTriple.endsWith("eabi") |
| 47 | + ? `${rawTriple.slice(0, -4)}-eabi` |
| 48 | + : rawTriple; |
| 49 | + const triples = triple.split("-"); |
| 50 | + let cpu; |
| 51 | + let sys; |
| 52 | + let abi = null; |
| 53 | + if (triples.length === 4) { |
| 54 | + [cpu, , sys, abi = null] = triples; |
| 55 | + } else if (triples.length === 3) { |
| 56 | + [cpu, , sys] = triples; |
| 57 | + } else { |
| 58 | + [cpu, sys] = triples; |
| 59 | + } |
| 60 | + const platformName = SysToNodePlatform[sys] ?? sys; |
| 61 | + const arch = CpuToNodeArch[cpu] ?? cpu; |
| 62 | + return { |
| 63 | + platform: platformName, |
| 64 | + arch, |
| 65 | + abi, |
| 66 | + platformArchABI: abi |
| 67 | + ? `${platformName}-${arch}-${abi}` |
| 68 | + : `${platformName}-${arch}`, |
| 69 | + raw: rawTriple |
| 70 | + }; |
| 71 | +} |
| 72 | + |
| 73 | + |
| 74 | +export async function prepublish_handler(options) { |
| 75 | + let root = process.cwd(); |
| 76 | + let json = await getPackageJson(root) |
| 77 | + |
| 78 | + let {napi, version} = json; |
| 79 | + |
| 80 | + let optionalDependencies = {}; |
| 81 | + for (let rawTarget of napi.targets) { |
| 82 | + let target = parseTriple(rawTarget); |
| 83 | + optionalDependencies[`${napi.packageName}-${target.platformArchABI}`] = version; |
| 84 | + } |
| 85 | + |
| 86 | + const packageFile = path.resolve(process.cwd(), "npm/package.json") |
| 87 | + let newPackageJson = { |
| 88 | + ...json, |
| 89 | + optionalDependencies |
| 90 | + } |
| 91 | + |
| 92 | + await fs.writeFile( |
| 93 | + packageFile, |
| 94 | + `${JSON.stringify(newPackageJson, null, 2)}\n`, |
| 95 | + "utf-8" |
| 96 | + ); |
| 97 | + |
| 98 | +} |
0 commit comments