-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostinstall.js
More file actions
138 lines (114 loc) · 4.3 KB
/
postinstall.js
File metadata and controls
138 lines (114 loc) · 4.3 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
#!/usr/bin/env node
/**
* postinstall.js - Platform detection and binary installation script for @codeeditorland/rest
*
* This script detects the current platform and architecture, then installs
* the appropriate pre-built Rest binary from the corresponding optional dependency.
*
* Usage:
* node postinstall.js
*
* Environment Variables:
* REST_SKIP_INSTALL - Set to "true" to skip binary installation
* REST_BINARY_PATH - Override the default binary installation path
*/
import { existsSync, mkdirSync } from 'node:fs';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = dirname(fileURLToPath(import.meta.url));
// Check if installation should be skipped
if (process.env.REST_SKIP_INSTALL === 'true') {
console.log('[Rest] Installation skipped via REST_SKIP_INSTALL environment variable');
process.exit(0);
}
/**
* Platform and architecture detection
*/
function getPlatform() {
const platforms = {
'darwin': 'darwin',
'linux': 'linux',
'win32': 'win32',
};
const platform = platforms[process.platform];
if (!platform) {
throw new Error(
`Unsupported platform: ${process.platform}. ` +
`Supported platforms: darwin, linux, win32`
);
}
return platform;
}
function getArchitecture() {
const arch = process.arch;
const archs = {
'x64': 'x64',
'arm64': 'arm64',
};
const architecture = archs[arch];
if (!architecture) {
throw new Error(
`Unsupported architecture: ${arch}. ` +
`Supported architectures: x64, arm64`
);
}
return architecture;
}
/**
* Install the platform-specific binary
*/
async function installBinary() {
const platform = getPlatform();
const architecture = getArchitecture();
const packageName = `@codeeditorland/rest-${platform}-${architecture}`;
console.log(`[Rest] Detected platform: ${platform}, architecture: ${architecture}`);
console.log(`[Rest] Installing binary from: ${packageName}`);
try {
// Try to import the binary package
const binaryPackagePath = await import(packageName).then(m => m.default || m);
// The binary package should export the path to the binary
if (typeof binaryPackagePath === 'string') {
console.log(`[Rest] Binary found at: ${binaryPackagePath}`);
} else {
// If the package doesn't export a path, check if it has a binary in its bin directory
console.log(`[Rest] Binary package loaded: ${packageName}`);
}
// Create the bin directory if it doesn't exist
const binDir = join(__dirname, 'bin');
if (!existsSync(binDir)) {
mkdirSync(binDir, { recursive: true });
}
console.log(`[Rest] Binary installation complete for ${platform}-${architecture}`);
} catch (error) {
// Fallback: Check if the binary exists in the local target directory
// This is useful for local development where binaries are built in Target/release
const localBinaryPath = join(__dirname, '..', 'Target', 'release', 'rest');
const localBinaryPathWin = join(__dirname, '..', 'Target', 'release', 'rest.exe');
let binaryExists = false;
if (platform === 'win32') {
binaryExists = existsSync(localBinaryPathWin);
} else {
binaryExists = existsSync(localBinaryPath);
}
if (binaryExists) {
console.log(`[Rest] Using local binary from Target/release`);
// Create symlink or copy to bin directory
const binaryName = platform === 'win32' ? 'rest.exe' : 'rest';
const binPath = join(binDir, binaryName);
// For now, just log the path - the actual binary handling depends on the build system
console.log(`[Rest] Local binary available at: ${binaryName === 'rest.exe' ? localBinaryPathWin : localBinaryPath}`);
} else {
console.warn(
`[Rest] Warning: Could not find binary for ${platform}-${architecture}. ` +
`Please ensure @codeeditorland/rest-${platform}-${architecture} is installed ` +
`or build the binary locally with: cargo build --release`
);
}
}
}
// Run the installation
installBinary().catch((error) => {
console.error(`[Rest] Error during binary installation:`, error.message);
console.error(`[Rest] Continuing without binary - Rest compiler may not be available`);
process.exit(0); // Don't fail the install, just warn
});