-
Notifications
You must be signed in to change notification settings - Fork 599
Expand file tree
/
Copy pathplatform.ts
More file actions
165 lines (139 loc) · 4.28 KB
/
platform.ts
File metadata and controls
165 lines (139 loc) · 4.28 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import * as path from 'path';
import * as fs from 'fs';
import { fileURLToPath } from 'url';
function getCurrentDir() {
if (typeof __dirname !== 'undefined') {
return __dirname;
} else {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return path.dirname(fileURLToPath(import.meta.url));
}
}
/**
* Find package root by climbing directory tree until package.json is found.
* @param startDir Starting directory to search from
* @returns Absolute path to package root, or null if not found
*/
export function findPackageRoot(): string | null {
let currentDir = getCurrentDir();
const root = path.parse(currentDir).root;
while (currentDir !== root) {
const packageJsonPath = path.join(currentDir, 'package.json');
if (fs.existsSync(packageJsonPath)) {
// Check if this is the actual package root by verifying it has a 'build' directory
// This ensures we skip intermediate package.json files (e.g., in dest/node-cjs/)
const buildDir = path.join(currentDir, 'build');
if (fs.existsSync(buildDir)) {
return currentDir;
}
}
currentDir = path.dirname(currentDir);
}
return null;
}
/**
* Supported platform/architecture combinations.
*/
export type Platform = 'x86_64-linux' | 'x86_64-darwin' | 'aarch64-linux' | 'aarch64-darwin';
/**
* Map from Platform to build directory name.
*/
const PLATFORM_TO_BUILD_DIR: Record<Platform, string> = {
'x86_64-linux': 'amd64-linux',
'x86_64-darwin': 'amd64-macos',
'aarch64-linux': 'arm64-linux',
'aarch64-darwin': 'arm64-macos',
};
/**
* Detect the current platform and architecture.
* @returns Platform identifier or null if unsupported
*/
export function detectPlatform(): Platform | null {
const arch = process.arch; // 'x64' | 'arm64' | ...
const platform = process.platform; // 'linux' | 'darwin' | 'win32' | ...
if (arch === 'x64' && platform === 'linux') {
return 'x86_64-linux';
}
if (arch === 'x64' && platform === 'darwin') {
return 'x86_64-darwin';
}
if (arch === 'arm64' && platform === 'linux') {
return 'aarch64-linux';
}
if (arch === 'arm64' && platform === 'darwin') {
return 'aarch64-darwin';
}
return null;
}
/**
* Find the bb binary for the native backend.
* @param customPath Optional custom path to bb binary (overrides automatic detection)
* @returns Absolute path to bb binary, or null if not found
*
* Search order:
* 1. If customPath is provided and exists, return it
* 2. If BB_BINARY_PATH is set and exists, return it
* 3. Otherwise search in <package-root>/build/<platform>/bb
*/
export function findBbBinary(customPath?: string): string | null {
// Check custom path first if provided
if (customPath) {
if (fs.existsSync(customPath)) {
return path.resolve(customPath);
}
// Custom path provided but doesn't exist - return null
return null;
}
const envPath = process.env.BB_BINARY_PATH;
if (envPath) {
if (fs.existsSync(envPath)) {
return path.resolve(envPath);
}
return null;
}
// Automatic detection
const platform = detectPlatform();
if (!platform) {
return null;
}
const buildDir = PLATFORM_TO_BUILD_DIR[platform];
// Get package root by climbing directory tree to find package.json
const packageRoot = findPackageRoot();
if (!packageRoot) {
return null;
}
// Check in build/<platform>/bb
const bbPath = path.join(packageRoot, 'build', buildDir, 'bb');
if (fs.existsSync(bbPath)) {
return bbPath;
}
return null;
}
export function findNapiBinary(customPath?: string): string | null {
// Check custom path first if provided
if (customPath) {
if (fs.existsSync(customPath)) {
return path.resolve(customPath);
}
// Custom path provided but doesn't exist - return null
return null;
}
// Automatic detection
const platform = detectPlatform();
if (!platform) {
return null;
}
const buildDir = PLATFORM_TO_BUILD_DIR[platform];
// Get package root by climbing directory tree to find package.json
const packageRoot = findPackageRoot();
if (!packageRoot) {
return null;
}
// Check in build/<platform>/nodejs_module.node
const bbPath = path.join(packageRoot, 'build', buildDir, 'nodejs_module.node');
if (fs.existsSync(bbPath)) {
return bbPath;
}
return null;
}