open-code-review/scripts/platform.js
kite f76d4266ed feat: add platform-specific npm packages to eliminate postinstall download
Ship Go binaries inside per-platform npm packages (@alibaba-group/ocr-{os}-{arch})
so npm install resolves the correct binary via optionalDependencies + os/cpu fields.
This removes the need for a postinstall download from GitHub Releases, which is
extremely slow for users behind restricted networks (e.g. China mainland).

The postinstall download is retained as a fallback for --no-optional installs.
2026-06-17 14:17:03 +08:00

52 lines
1.4 KiB
JavaScript

"use strict";
const path = require("path");
const fs = require("fs");
const IS_WINDOWS = process.platform === "win32";
const BINARY_FILENAME = IS_WINDOWS ? "opencodereview.exe" : "opencodereview";
const PLATFORM_PKG = {
"darwin-arm64": "@alibaba-group/ocr-darwin-arm64",
"darwin-x64": "@alibaba-group/ocr-darwin-x64",
"linux-arm64": "@alibaba-group/ocr-linux-arm64",
"linux-x64": "@alibaba-group/ocr-linux-x64",
"win32-arm64": "@alibaba-group/ocr-win32-arm64",
"win32-x64": "@alibaba-group/ocr-win32-x64",
};
function getPlatformPackageName() {
return PLATFORM_PKG[`${process.platform}-${process.arch}`] || null;
}
function resolveNativeBinary() {
const pkgName = getPlatformPackageName();
if (pkgName) {
try {
const pkgDir = path.dirname(require.resolve(`${pkgName}/package.json`));
const binPath = path.join(pkgDir, "bin", BINARY_FILENAME);
if (fs.existsSync(binPath)) {
return { path: binPath, fromPlatformPkg: true };
}
} catch (err) {
if (err.code !== "MODULE_NOT_FOUND") {
console.warn(`[WARN] Unexpected error resolving ${pkgName}: ${err.message}`);
}
}
}
const legacyPath = path.join(__dirname, "..", "bin", BINARY_FILENAME);
if (fs.existsSync(legacyPath)) {
return { path: legacyPath, fromPlatformPkg: false };
}
return null;
}
module.exports = {
IS_WINDOWS,
BINARY_FILENAME,
PLATFORM_PKG,
getPlatformPackageName,
resolveNativeBinary,
};