-
Notifications
You must be signed in to change notification settings - Fork 701
Expand file tree
/
Copy pathplatform.js
More file actions
66 lines (56 loc) · 1.76 KB
/
Copy pathplatform.js
File metadata and controls
66 lines (56 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"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() {
const key = `${process.platform}-${process.arch}`;
try {
const parentPkg = JSON.parse(
fs.readFileSync(path.join(__dirname, "..", "package.json"), "utf8")
);
const optDeps = parentPkg.optionalDependencies || {};
for (const name of Object.keys(optDeps)) {
if (name.endsWith(`-${key}`)) {
return name;
}
}
} catch (_) {}
return PLATFORM_PKG[key] || 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,
};