@magnet-ai/cli 0.1.0 → 0.1.2

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/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # @magnet-ai/cli
2
+
3
+ Install the **Magnet CLI** via npm. This package downloads the native binary for your platform from [GitHub Releases](https://github.com/toolkit-ai/magnet-cli/releases) at install time. You do not need Node.js to run the CLI after installation.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install -g @magnet-ai/cli
9
+ ```
10
+
11
+ Then run:
12
+
13
+ ```bash
14
+ magnet --help
15
+ magnet issues list
16
+ magnet search "query"
17
+ ```
18
+
19
+ ## Requirements
20
+
21
+ Set your Magnet API key before using the CLI:
22
+
23
+ ```bash
24
+ export MAGNET_API_KEY="your-uuid-api-key"
25
+ ```
26
+
27
+ Get an API key from your [Magnet](https://www.magnet.run) organization settings.
28
+
29
+ ## Test the download (before releasing)
30
+
31
+ From the `npm` folder, run the download script to fetch the binary from the latest GitHub Release:
32
+
33
+ ```bash
34
+ cd npm
35
+ npm run download
36
+ # or: node bin/download.js
37
+ ```
38
+
39
+ To test against a specific release tag: `MAGNET_CLI_VERSION=v0.1.0 node bin/download.js`. Then run `./bin/magnet.js --help` to confirm the binary works.
40
+
41
+ ## Full documentation
42
+
43
+ See the [main repository](https://github.com/toolkit-ai/magnet-cli) for commands, pagination, and other install options (direct download, build from source).
44
+
45
+ ## License
46
+
47
+ MIT
package/bin/download.js CHANGED
@@ -8,7 +8,18 @@ const path = require('path');
8
8
  const https = require('https');
9
9
  const { execSync } = require('child_process');
10
10
 
11
- const REPO = 'magnet-run/magnet-cli';
11
+ // Use repo from package.json so it works when published from any org (e.g. toolkit-ai/magnet-cli)
12
+ function getRepo() {
13
+ try {
14
+ const pkg = require(path.join(__dirname, '..', 'package.json'));
15
+ const url = (pkg.repository && pkg.repository.url) || pkg.repository || '';
16
+ const m = url.match(/github\.com[:/]([^/]+\/[^/]+?)(?:\.git)?$/);
17
+ return m ? m[1] : 'toolkit-ai/magnet-cli';
18
+ } catch (_) {
19
+ return 'toolkit-ai/magnet-cli';
20
+ }
21
+ }
22
+ const REPO = process.env.MAGNET_CLI_REPO || getRepo();
12
23
  const VERSION = process.env.MAGNET_CLI_VERSION || 'latest';
13
24
  const BIN_DIR = path.join(__dirname, '..', 'bin');
14
25
  const BINARY = process.platform === 'win32' ? 'magnet.exe' : 'magnet';
@@ -24,23 +35,36 @@ function getPlatform() {
24
35
  return null;
25
36
  }
26
37
 
27
- function fetch(url) {
38
+ const FETCH_TIMEOUT_MS = 60000; // 60s for API / metadata
39
+ const DOWNLOAD_TIMEOUT_MS = 120000; // 120s for the binary tarball
40
+
41
+ function fetch(url, redirects = 0, timeoutMs = FETCH_TIMEOUT_MS) {
42
+ if (redirects > 5) return Promise.reject(new Error('Too many redirects'));
28
43
  return new Promise((resolve, reject) => {
29
- https.get(url, { headers: { 'User-Agent': 'magnet-cli-npm' } }, (res) => {
44
+ const req = https.get(url, { headers: { 'User-Agent': 'magnet-cli-npm' } }, (res) => {
30
45
  if (res.statusCode === 302 || res.statusCode === 301) {
31
- return fetch(res.headers.location).then(resolve).catch(reject);
46
+ const loc = res.headers.location;
47
+ if (loc) return fetch(loc, redirects + 1, timeoutMs).then(resolve).catch(reject);
32
48
  }
33
49
  const chunks = [];
34
50
  res.on('data', (c) => chunks.push(c));
35
51
  res.on('end', () => resolve(Buffer.concat(chunks)));
36
52
  res.on('error', reject);
37
- }).on('error', reject);
53
+ });
54
+ req.on('error', reject);
55
+ req.setTimeout(timeoutMs, () => {
56
+ req.destroy();
57
+ reject(new Error('Request timed out after ' + (timeoutMs / 1000) + 's'));
58
+ });
38
59
  });
39
60
  }
40
61
 
41
62
  async function getLatestTag() {
42
63
  const data = await fetch(`https://api.github.com/repos/${REPO}/releases/latest`);
43
- const j = JSON.parse(data.toString());
64
+ const text = data.toString();
65
+ if (text.length < 10) throw new Error('Empty response from GitHub');
66
+ const j = JSON.parse(text);
67
+ if (!j.tag_name) throw new Error('No releases found. Push a tag (e.g. v0.1.0) to trigger a release.');
44
68
  return j.tag_name;
45
69
  }
46
70
 
@@ -50,13 +74,14 @@ async function main() {
50
74
  console.warn('@magnet-ai/cli: No prebuilt binary for ' + process.platform + '/' + process.arch + '. Install from GitHub Releases.');
51
75
  return;
52
76
  }
77
+ console.log('@magnet-ai/cli: Downloading binary for ' + platform + '...');
53
78
  const tag = VERSION === 'latest' ? await getLatestTag() : VERSION;
54
79
  const archiveName = `magnet-cli-${platform}.tar.gz`;
55
80
  const url = `https://github.com/${REPO}/releases/download/${tag}/${archiveName}`;
56
81
  fs.mkdirSync(BIN_DIR, { recursive: true });
57
82
  const destPath = path.join(BIN_DIR, BINARY);
58
83
  try {
59
- const buf = await fetch(url);
84
+ const buf = await fetch(url, 0, DOWNLOAD_TIMEOUT_MS);
60
85
  if (buf.length < 1000) {
61
86
  const text = buf.toString();
62
87
  if (text.includes('Not Found')) throw new Error('Release not found: ' + tag);
@@ -75,6 +100,7 @@ async function main() {
75
100
  fs.renameSync(extracted, destPath);
76
101
  }
77
102
  fs.chmodSync(destPath, 0o755);
103
+ console.log('@magnet-ai/cli: Done.');
78
104
  } catch (e) {
79
105
  console.warn('@magnet-ai/cli: Could not download binary:', e.message);
80
106
  }
package/bin/magnet ADDED
Binary file
package/bin/magnet.js CHANGED
@@ -1,10 +1,18 @@
1
1
  #!/usr/bin/env node
2
2
  const path = require('path');
3
+ const fs = require('fs');
3
4
  const { spawnSync } = require('child_process');
4
5
 
5
6
  const binDir = path.join(__dirname, '..', 'bin');
6
7
  const binary = path.join(binDir, process.platform === 'win32' ? 'magnet.exe' : 'magnet');
7
8
 
9
+ if (!fs.existsSync(binary)) {
10
+ console.error('magnet: binary not found at', binary);
11
+ console.error('Run "npm install -g @magnet-ai/cli" again. If that fails, the postinstall could not download the binary.');
12
+ console.error('Ensure a GitHub Release exists (push a tag like v0.1.0) and the repo has prebuilt binaries.');
13
+ process.exit(1);
14
+ }
15
+
8
16
  const result = spawnSync(binary, process.argv.slice(2), {
9
17
  stdio: 'inherit',
10
18
  windowsHide: true,
package/package.json CHANGED
@@ -1,17 +1,21 @@
1
1
  {
2
2
  "name": "@magnet-ai/cli",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Magnet CLI - download the native binary for your platform",
5
5
  "bin": {
6
6
  "magnet": "bin/magnet.js"
7
7
  },
8
8
  "scripts": {
9
- "postinstall": "node bin/download.js"
9
+ "postinstall": "node bin/download.js",
10
+ "download": "node bin/download.js"
10
11
  },
11
12
  "repository": {
12
13
  "type": "git",
13
- "url": "https://github.com/magnet-run/magnet-cli.git"
14
+ "url": "https://github.com/toolkit-ai/magnet-cli.git"
14
15
  },
15
- "keywords": ["magnet", "cli"],
16
+ "keywords": [
17
+ "magnet",
18
+ "cli"
19
+ ],
16
20
  "license": "MIT"
17
- }
21
+ }