|
| 1 | +import * as fs from "node:fs"; |
| 2 | +import { resolve } from "node:path"; |
| 3 | +import { fileURLToPath } from "node:url"; |
| 4 | +import { format } from "node:util"; |
| 5 | + |
| 6 | +const CLI_ROOT = resolve(fileURLToPath(import.meta.url), "../.."); |
| 7 | +const PACKAGES_ROOT = resolve(CLI_ROOT, ".."); |
| 8 | +const REPO_ROOT = resolve(PACKAGES_ROOT, "../.."); |
| 9 | +const MANIFEST_PATH = resolve(CLI_ROOT, "package.json"); |
| 10 | + |
| 11 | +const rootManifest = JSON.parse(fs.readFileSync(MANIFEST_PATH, "utf-8")); |
| 12 | + |
| 13 | +function getName(platform, arch, prefix = "cli") { |
| 14 | + return format(`${prefix}-${platform}`, arch); |
| 15 | +} |
| 16 | + |
| 17 | +function copyBinaryToNativePackage(platform, arch) { |
| 18 | + const os = platform.split("-")[0]; |
| 19 | + const buildName = getName(platform, arch); |
| 20 | + const packageRoot = resolve(PACKAGES_ROOT, buildName); |
| 21 | + const packageName = `@biomejs/${buildName}`; |
| 22 | + |
| 23 | + // Update the package.json manifest |
| 24 | + const { version, license, repository, engines, homepage } = rootManifest; |
| 25 | + |
| 26 | + const manifest = JSON.stringify( |
| 27 | + { |
| 28 | + name: packageName, |
| 29 | + version, |
| 30 | + license, |
| 31 | + repository, |
| 32 | + engines, |
| 33 | + homepage, |
| 34 | + os: [os], |
| 35 | + cpu: [arch], |
| 36 | + libc: |
| 37 | + os === "linux" |
| 38 | + ? packageName.endsWith("musl") |
| 39 | + ? ["musl"] |
| 40 | + : ["glibc"] |
| 41 | + : undefined, |
| 42 | + }, |
| 43 | + null, |
| 44 | + 2, |
| 45 | + ); |
| 46 | + |
| 47 | + const manifestPath = resolve(packageRoot, "package.json"); |
| 48 | + console.info(`Update manifest ${manifestPath}`); |
| 49 | + fs.writeFileSync(manifestPath, manifest); |
| 50 | + |
| 51 | + // Copy the CLI binary |
| 52 | + const ext = os === "win32" ? ".exe" : ""; |
| 53 | + const binarySource = resolve( |
| 54 | + REPO_ROOT, |
| 55 | + `${getName(platform, arch, "biome")}${ext}`, |
| 56 | + ); |
| 57 | + const binaryTarget = resolve(packageRoot, `biome${ext}`); |
| 58 | + |
| 59 | + if (!fs.existsSync(binarySource)) { |
| 60 | + console.error( |
| 61 | + `Source for binary for ${buildName} not found at: ${binarySource}`, |
| 62 | + ); |
| 63 | + process.exit(1); |
| 64 | + } |
| 65 | + |
| 66 | + console.info(`Copy binary ${binaryTarget}`); |
| 67 | + fs.copyFileSync(binarySource, binaryTarget); |
| 68 | + fs.chmodSync(binaryTarget, 0o755); |
| 69 | +} |
| 70 | + |
| 71 | +const PLATFORMS = ["win32-%s", "darwin-%s", "linux-%s", "linux-%s-musl"]; |
| 72 | +const ARCHITECTURES = ["x64", "arm64"]; |
| 73 | + |
| 74 | +for (const platform of PLATFORMS) { |
| 75 | + for (const arch of ARCHITECTURES) { |
| 76 | + copyBinaryToNativePackage(platform, arch); |
| 77 | + } |
| 78 | +} |
0 commit comments