-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathinstall.js
More file actions
98 lines (78 loc) · 3.04 KB
/
install.js
File metadata and controls
98 lines (78 loc) · 3.04 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
#!/usr/bin/env node
const https = require('https');
const fs = require('fs');
const path = require('path');
const { getPlatform, getBinaryName } = require('./binary');
const VERSION = require('./package.json').version;
const GITHUB_REPO = 'newcore-network/opencore-cli';
async function download(url, dest) {
return new Promise((resolve, reject) => {
const file = fs.createWriteStream(dest);
https.get(url, (response) => {
if (response.statusCode === 302 || response.statusCode === 301) {
// Follow redirect
return download(response.headers.location, dest).then(resolve).catch(reject);
}
if (response.statusCode !== 200) {
reject(new Error(`Failed to download: ${response.statusCode} ${response.statusMessage}`));
return;
}
const totalBytes = parseInt(response.headers['content-length'], 10);
let downloadedBytes = 0;
response.on('data', (chunk) => {
downloadedBytes += chunk.length;
const percent = ((downloadedBytes / totalBytes) * 100).toFixed(1);
process.stdout.write(`\rDownloading OpenCore CLI... ${percent}%`);
});
response.pipe(file);
file.on('finish', () => {
file.close();
console.log('\nDownload complete!');
resolve();
});
}).on('error', (err) => {
fs.unlink(dest, () => {});
reject(err);
});
});
}
async function install() {
try {
console.log('Installing OpenCore CLI...');
const platform = getPlatform();
if (!platform) {
throw new Error(`Unsupported platform: ${process.platform} ${process.arch}`);
}
const binaryName = getBinaryName();
const binDir = path.join(__dirname, 'bin');
// Create bin directory
if (!fs.existsSync(binDir)) {
fs.mkdirSync(binDir, { recursive: true });
}
const binaryPath = path.join(binDir, binaryName);
// Skip download if binary already exists (e.g. built locally via `go build` for development)
if (fs.existsSync(binaryPath)) {
if (process.platform !== 'win32') {
fs.chmodSync(binaryPath, 0o755);
}
console.log('OpenCore CLI binary already exists, skipping download.');
return;
}
// Download binary from GitHub releases
const downloadUrl = `https://github.com/${GITHUB_REPO}/releases/download/v${VERSION}/opencore-${platform}${platform.startsWith('windows') ? '.exe' : ''}`;
console.log(`Downloading from: ${downloadUrl}`);
await download(downloadUrl, binaryPath);
// Make binary executable on Unix systems
if (process.platform !== 'win32') {
fs.chmodSync(binaryPath, 0o755);
}
console.log('✓ OpenCore CLI installed successfully!');
console.log(`Run 'opencore --version' to verify installation.`);
} catch (error) {
console.error('Failed to install OpenCore CLI:', error.message);
console.error('\nYou can manually download the binary from:');
console.error(`https://github.com/${GITHUB_REPO}/releases/tag/v${VERSION}`);
process.exit(1);
}
}
install();