@magnet-ai/cli 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/download.js +85 -0
- package/bin/magnet.js +12 -0
- package/package.json +17 -0
package/bin/download.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Postinstall: download the magnet binary from GitHub Releases for this OS/arch.
|
|
4
|
+
* Requires no Node at runtime; the bin/magnet.js wrapper runs the downloaded binary.
|
|
5
|
+
*/
|
|
6
|
+
const fs = require('fs');
|
|
7
|
+
const path = require('path');
|
|
8
|
+
const https = require('https');
|
|
9
|
+
const { execSync } = require('child_process');
|
|
10
|
+
|
|
11
|
+
const REPO = 'magnet-run/magnet-cli';
|
|
12
|
+
const VERSION = process.env.MAGNET_CLI_VERSION || 'latest';
|
|
13
|
+
const BIN_DIR = path.join(__dirname, '..', 'bin');
|
|
14
|
+
const BINARY = process.platform === 'win32' ? 'magnet.exe' : 'magnet';
|
|
15
|
+
|
|
16
|
+
function getPlatform() {
|
|
17
|
+
const os = process.platform;
|
|
18
|
+
const arch = process.arch;
|
|
19
|
+
if (os === 'darwin' && arch === 'x64') return 'darwin-amd64';
|
|
20
|
+
if (os === 'darwin' && arch === 'arm64') return 'darwin-arm64';
|
|
21
|
+
if (os === 'linux' && arch === 'x64') return 'linux-amd64';
|
|
22
|
+
if (os === 'linux' && arch === 'arm64') return 'linux-arm64';
|
|
23
|
+
if (os === 'win32' && arch === 'x64') return 'windows-amd64';
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function fetch(url) {
|
|
28
|
+
return new Promise((resolve, reject) => {
|
|
29
|
+
https.get(url, { headers: { 'User-Agent': 'magnet-cli-npm' } }, (res) => {
|
|
30
|
+
if (res.statusCode === 302 || res.statusCode === 301) {
|
|
31
|
+
return fetch(res.headers.location).then(resolve).catch(reject);
|
|
32
|
+
}
|
|
33
|
+
const chunks = [];
|
|
34
|
+
res.on('data', (c) => chunks.push(c));
|
|
35
|
+
res.on('end', () => resolve(Buffer.concat(chunks)));
|
|
36
|
+
res.on('error', reject);
|
|
37
|
+
}).on('error', reject);
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async function getLatestTag() {
|
|
42
|
+
const data = await fetch(`https://api.github.com/repos/${REPO}/releases/latest`);
|
|
43
|
+
const j = JSON.parse(data.toString());
|
|
44
|
+
return j.tag_name;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async function main() {
|
|
48
|
+
const platform = getPlatform();
|
|
49
|
+
if (!platform) {
|
|
50
|
+
console.warn('@magnet-ai/cli: No prebuilt binary for ' + process.platform + '/' + process.arch + '. Install from GitHub Releases.');
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const tag = VERSION === 'latest' ? await getLatestTag() : VERSION;
|
|
54
|
+
const archiveName = `magnet-cli-${platform}.tar.gz`;
|
|
55
|
+
const url = `https://github.com/${REPO}/releases/download/${tag}/${archiveName}`;
|
|
56
|
+
fs.mkdirSync(BIN_DIR, { recursive: true });
|
|
57
|
+
const destPath = path.join(BIN_DIR, BINARY);
|
|
58
|
+
try {
|
|
59
|
+
const buf = await fetch(url);
|
|
60
|
+
if (buf.length < 1000) {
|
|
61
|
+
const text = buf.toString();
|
|
62
|
+
if (text.includes('Not Found')) throw new Error('Release not found: ' + tag);
|
|
63
|
+
throw new Error('Download failed: ' + text.slice(0, 200));
|
|
64
|
+
}
|
|
65
|
+
const tmpTar = path.join(BIN_DIR, archiveName);
|
|
66
|
+
fs.writeFileSync(tmpTar, buf);
|
|
67
|
+
try {
|
|
68
|
+
execSync(`tar -xzf "${tmpTar}" -C "${BIN_DIR}"`, { stdio: 'inherit' });
|
|
69
|
+
} finally {
|
|
70
|
+
try { fs.unlinkSync(tmpTar); } catch (_) {}
|
|
71
|
+
}
|
|
72
|
+
const extracted = path.join(BIN_DIR, process.platform === 'win32' ? 'magnet.exe' : 'magnet');
|
|
73
|
+
if (extracted !== destPath && fs.existsSync(extracted)) {
|
|
74
|
+
try { fs.unlinkSync(destPath); } catch (_) {}
|
|
75
|
+
fs.renameSync(extracted, destPath);
|
|
76
|
+
}
|
|
77
|
+
fs.chmodSync(destPath, 0o755);
|
|
78
|
+
} catch (e) {
|
|
79
|
+
console.warn('@magnet-ai/cli: Could not download binary:', e.message);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
main().catch((e) => {
|
|
84
|
+
console.warn('@magnet-ai/cli postinstall:', e.message);
|
|
85
|
+
});
|
package/bin/magnet.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const { spawnSync } = require('child_process');
|
|
4
|
+
|
|
5
|
+
const binDir = path.join(__dirname, '..', 'bin');
|
|
6
|
+
const binary = path.join(binDir, process.platform === 'win32' ? 'magnet.exe' : 'magnet');
|
|
7
|
+
|
|
8
|
+
const result = spawnSync(binary, process.argv.slice(2), {
|
|
9
|
+
stdio: 'inherit',
|
|
10
|
+
windowsHide: true,
|
|
11
|
+
});
|
|
12
|
+
process.exit(result.status !== null ? result.status : 1);
|
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@magnet-ai/cli",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Magnet CLI - download the native binary for your platform",
|
|
5
|
+
"bin": {
|
|
6
|
+
"magnet": "bin/magnet.js"
|
|
7
|
+
},
|
|
8
|
+
"scripts": {
|
|
9
|
+
"postinstall": "node bin/download.js"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "https://github.com/magnet-run/magnet-cli.git"
|
|
14
|
+
},
|
|
15
|
+
"keywords": ["magnet", "cli"],
|
|
16
|
+
"license": "MIT"
|
|
17
|
+
}
|