-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.js
More file actions
111 lines (90 loc) · 2.79 KB
/
install.js
File metadata and controls
111 lines (90 loc) · 2.79 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
#!/usr/bin/env node
const https = require('https');
const fs = require('fs');
const path = require('path');
const os = require('os');
const { execSync } = require('child_process');
const VERSION = 'v0.0.3';
const REPO = 'code-input/cli';
const BINARY_NAME = 'ci';
function getPlatform() {
const platform = os.platform();
const arch = os.arch();
const platformMap = {
'linux': 'linux',
'darwin': 'macos',
'win32': 'windows'
};
const archMap = {
'x64': 'x86_64',
'arm64': 'aarch64'
};
const osName = platformMap[platform];
if (!osName) {
throw new Error(`Unsupported platform: ${platform}`);
}
const archName = archMap[arch];
if (!archName) {
throw new Error(`Unsupported architecture: ${arch}`);
}
return { os: osName, arch: archName };
}
function getBinaryUrl(platform, arch) {
const ext = platform === 'windows' ? '.exe' : '';
return `https://github.com/${REPO}/releases/download/${VERSION}/ci-${platform}-${arch}${ext}`;
}
function getInstallPath() {
// For npm global install, use the configured prefix
try {
const npmPrefix = execSync('npm prefix -g', { encoding: 'utf-8' }).trim();
return path.join(npmPrefix, process.platform === 'win32' ? '' : 'bin');
} catch {
return path.join(os.homedir(), '.npm-global', 'bin');
}
}
function downloadFile(url, dest) {
return new Promise((resolve, reject) => {
https.get(url, (response) => {
if (response.statusCode === 302 || response.statusCode === 301) {
downloadFile(response.headers.location, dest).then(resolve).catch(reject);
return;
}
if (response.statusCode !== 200) {
reject(new Error(`Failed to download: ${response.statusCode}`));
return;
}
const file = fs.createWriteStream(dest);
response.pipe(file);
file.on('finish', () => {
file.close();
// Make executable on Unix
if (process.platform !== 'win32') {
fs.chmodSync(dest, 0o755);
}
resolve();
});
file.on('error', (err) => {
fs.unlink(dest, () => {});
reject(err);
});
}).on('error', reject);
});
}
async function main() {
try {
const { os: platform, arch } = getPlatform();
const url = getBinaryUrl(platform, arch);
const installDir = getInstallPath();
const ext = platform === 'windows' ? '.exe' : '';
const destPath = path.join(installDir, `${BINARY_NAME}${ext}`);
console.log(`Downloading ${BINARY_NAME} ${VERSION} for ${platform}-${arch}...`);
// Ensure install directory exists
fs.mkdirSync(installDir, { recursive: true });
await downloadFile(url, destPath);
console.log(`Installed ${BINARY_NAME} to ${destPath}`);
} catch (error) {
console.error(`Failed to install: ${error.message}`);
process.exit(1);
}
}
main();