-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.js
More file actions
115 lines (99 loc) · 3.15 KB
/
install.js
File metadata and controls
115 lines (99 loc) · 3.15 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
const fs = require('fs');
const path = require('path');
const os = require('os');
const { Readable } = require('stream');
const { pipeline } = require('stream/promises');
const VERSION = require('./package.json').version;
const REPO = 'suryavirkapur/clawedcode';
const DOWNLOAD_TIMEOUT_MS = 120000;
function getPlatform() {
const platform = os.platform();
const arch = os.arch();
let osName;
switch (platform) {
case 'linux': osName = 'linux'; break;
case 'darwin': osName = 'darwin'; break;
case 'win32': osName = 'windows'; break;
default:
console.error(`Unsupported OS: ${platform}`);
process.exit(1);
}
let target;
switch (arch) {
case 'x64': target = 'x86_64'; break;
case 'arm64': target = 'aarch64'; break;
default:
console.error(`Unsupported architecture: ${arch}`);
process.exit(1);
}
const ext = osName === 'windows' ? '.exe' : '';
return { osName, target, ext };
}
function download(url, dest) {
const tmpDest = `${dest}.tmp`;
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), DOWNLOAD_TIMEOUT_MS);
return (async () => {
try {
const response = await fetch(url, {
redirect: 'follow',
signal: controller.signal,
headers: {
'user-agent': `clawedcode-installer/${VERSION}`,
},
});
if (!response.ok) {
throw new Error(`Failed to download: ${response.status} ${response.statusText}`);
}
if (!response.body) {
throw new Error('Download response did not include a body');
}
await pipeline(
Readable.fromWeb(response.body),
fs.createWriteStream(tmpDest),
);
fs.renameSync(tmpDest, dest);
fs.chmodSync(dest, 0o755);
} catch (err) {
fs.rmSync(tmpDest, { force: true });
if (err?.name === 'AbortError') {
throw new Error(`Download timed out after ${DOWNLOAD_TIMEOUT_MS / 1000}s`);
}
throw err;
} finally {
clearTimeout(timeout);
}
})();
}
async function main() {
if (process.env.CLAWEDCODE_SKIP_DOWNLOAD === '1') {
console.log('Skipping binary download (CLAWEDCODE_SKIP_DOWNLOAD=1)');
return;
}
const { osName, target, ext } = getPlatform();
const binName = `clawedcode-${target}-${osName}${ext}`;
const binDir = path.join(__dirname, 'bin');
const binPath = path.join(binDir, `clawedcode-bin${ext}`);
if (!fs.existsSync(binDir)) {
fs.mkdirSync(binDir, { recursive: true });
}
const assetDir = process.env.CLAWEDCODE_ASSET_DIR;
if (assetDir) {
const src = path.join(assetDir, binName);
if (!fs.existsSync(src)) {
throw new Error(`Asset not found: ${src}`);
}
fs.copyFileSync(src, binPath);
fs.chmodSync(binPath, 0o755);
console.log(`Installed from local assets: ${src}`);
return;
}
const url = `https://github.com/${REPO}/releases/download/v${VERSION}/${binName}`;
console.log(`Downloading clawedcode ${VERSION} for ${target}-${osName}...`);
await download(url, binPath);
console.log(`Installed to ${binPath}`);
}
main().catch((err) => {
console.error('Failed to install clawedcode:', err.message);
process.exit(1);
});