-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.js
141 lines (122 loc) · 4.32 KB
/
install.js
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
const fs = require('fs');
const path = require('path');
const os = require('os');
const https = require('https');
const {pipeline} = require('stream/promises');
const tar = require('tar');
function getBinaryName() {
return os.platform() === 'win32' ? 'mew.exe' : 'mew';
}
function getPlatformString() {
const platform = os.platform();
const arch = os.arch();
switch (platform) {
case 'win32':
return `win32-${arch}`;
case 'darwin':
return `darwin-${arch}`;
case 'linux':
return `linux-${arch}`;
default:
throw new Error(`Unsupported platform: ${platform}-${arch}`);
}
}
function downloadFile(url) {
return new Promise((resolve, reject) => {
https.get(url, response => {
if (response.statusCode === 302) {
// Handle redirect
https.get(response.headers.location, resolve).on('error', reject);
} else {
resolve(response);
}
}).on('error', reject);
});
}
async function downloadAndInstallBinary() {
try {
const tempDir = path.join(os.tmpdir(), 'mew-install');
if (!fs.existsSync(tempDir)) {
fs.mkdirSync(tempDir, {recursive: true});
}
const version = require('./package.json').version;
const platform = getPlatformString();
const url = `https://github.com/antharuu/Mew/releases/download/v${version}/mew-${platform}.tar.gz`;
const tarPath = path.join(tempDir, 'mew.tar.gz');
const binaryName = getBinaryName();
const finalPath = path.join(__dirname, binaryName);
console.log('Downloading from:', url);
console.log('Temporary tar file:', tarPath);
console.log('Final binary location:', finalPath);
// Download the file
const response = await downloadFile(url);
// Save to temporary file
const writeStream = fs.createWriteStream(tarPath);
await pipeline(response, writeStream);
// Extract
await tar.x({
file: tarPath, cwd: __dirname
});
// Check if binary exists
if (!fs.existsSync(finalPath)) {
throw new Error(`Binary not found after extraction: ${finalPath}`);
}
// Set permissions
try {
fs.chmodSync(finalPath, 0o755);
} catch (error) {
console.warn('Warning: Could not set executable permissions:', error.message);
}
// Cleanup
try {
fs.unlinkSync(tarPath);
fs.rmdirSync(tempDir, {recursive: true});
} catch (error) {
console.warn('Warning: Could not cleanup temporary files:', error.message);
}
return finalPath;
} catch (error) {
console.error('Error during installation:', error);
throw error;
}
}
async function copyLocalBinary() {
const binaryName = getBinaryName();
const possiblePaths = [path.join(__dirname, 'target', 'release', binaryName), path.join(__dirname, '..', 'target', 'release', binaryName), path.join(process.cwd(), 'target', 'release', binaryName)];
for (const sourcePath of possiblePaths) {
console.log('Checking for local binary at:', sourcePath);
if (fs.existsSync(sourcePath)) {
const destPath = path.join(__dirname, binaryName);
console.log('Found local binary, copying to:', destPath);
fs.copyFileSync(sourcePath, destPath);
try {
fs.chmodSync(destPath, 0o755);
} catch (error) {
console.warn('Warning: Could not set executable permissions:', error.message);
}
return true;
}
}
return false;
}
async function install() {
try {
// Try local binary first
if (await copyLocalBinary()) {
console.log('Local binary installed successfully');
return;
}
// Try download
console.log('No local binary found, attempting download...');
await downloadAndInstallBinary();
console.log('Binary downloaded and installed successfully');
} catch (error) {
console.error('Installation failed:', error);
process.exit(1);
}
}
// Run installation
install().catch(error => {
console.error('Installation failed:', error);
process.exit(1);
});