codebuff-cli 1.0.12 → 1.0.14

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.
@@ -3,7 +3,6 @@ const fs = require('fs');
3
3
  const path = require('path');
4
4
  const https = require('https');
5
5
  const http = require('http');
6
- const os = require('os');
7
6
 
8
7
  const REPO = 'Marcus-Mok-GH/codebuff-cli';
9
8
  const BINARY_NAME = 'codebuff';
@@ -12,22 +11,29 @@ const REQUEST_TIMEOUT_MS = 120000;
12
11
  const MAX_REDIRECTS = 10;
13
12
 
14
13
  function getVersion() {
15
- const candidates = [
16
- path.join(__dirname, '..', '..', 'package.json'),
17
- path.join(__dirname, '..', 'package.json'),
18
- ];
19
- for (const p of candidates) {
20
- try {
21
- const pkg = JSON.parse(fs.readFileSync(p, 'utf8'));
22
- if (pkg.version) return pkg.version;
23
- } catch {}
24
- }
25
- return '1.0.9';
14
+ const pkgPath = path.join(__dirname, '..', 'package.json');
15
+ try {
16
+ const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
17
+ if (pkg.version) return pkg.version;
18
+ } catch {}
19
+ throw new Error('Could not determine version from ' + pkgPath);
20
+ }
21
+
22
+ function getPlatform() {
23
+ const platform = process.platform;
24
+ const arch = process.arch;
25
+ const mappings = {
26
+ darwin: 'darwin',
27
+ linux: 'linux',
28
+ win32: 'win32',
29
+ };
30
+ const osName = mappings[platform] || platform;
31
+ return osName + '-' + arch;
26
32
  }
27
33
 
28
34
  function getBinaryPath() {
29
- const binDir = path.join(os.homedir(), '.codebuff', 'bin');
30
- const name = process.platform === 'win32' ? `${BINARY_NAME}.exe` : BINARY_NAME;
35
+ const binDir = path.join(__dirname, '..', 'bin');
36
+ const name = process.platform === 'win32' ? BINARY_NAME + '.exe' : BINARY_NAME;
31
37
  return path.join(binDir, name);
32
38
  }
33
39
 
@@ -44,7 +50,7 @@ function cleanup(dest) {
44
50
  function download(url, dest, redirectCount = 0) {
45
51
  return new Promise((resolve, reject) => {
46
52
  if (redirectCount > MAX_REDIRECTS) {
47
- reject(new Error(`Too many redirects (max ${MAX_REDIRECTS}) while downloading from ${url}`));
53
+ reject(new Error('Too many redirects (max ' + MAX_REDIRECTS + ') while downloading from ' + url));
48
54
  return;
49
55
  }
50
56
 
@@ -67,7 +73,7 @@ function download(url, dest, redirectCount = 0) {
67
73
  if (res.statusCode !== 200) {
68
74
  file.close();
69
75
  cleanup(dest);
70
- reject(new Error(`Failed to download from ${url}: HTTP ${res.statusCode}`));
76
+ reject(new Error('Download failed: HTTP ' + res.statusCode + ' from ' + url));
71
77
  return;
72
78
  }
73
79
 
@@ -82,76 +88,77 @@ function download(url, dest, redirectCount = 0) {
82
88
  req.on('error', (err) => {
83
89
  file.close();
84
90
  cleanup(dest);
85
- reject(new Error(`Request failed for ${url}: ${err.message}`));
91
+ reject(new Error('Network error downloading from ' + url + ': ' + err.message));
86
92
  });
87
93
 
88
94
  req.setTimeout(REQUEST_TIMEOUT_MS, () => {
89
95
  req.destroy();
90
96
  file.close();
91
97
  cleanup(dest);
92
- reject(new Error(`Request timeout after ${REQUEST_TIMEOUT_MS}ms for ${url}`));
98
+ reject(new Error('Download timeout (' + REQUEST_TIMEOUT_MS + 'ms) for ' + url));
93
99
  });
94
100
  });
95
101
  }
96
102
 
97
- async function downloadWithRetry(url, dest, retries = MAX_RETRIES) {
103
+ async function downloadWithRetry(url, dest) {
98
104
  let lastError;
99
- for (let attempt = 0; attempt <= retries; attempt++) {
105
+ for (let attempt = 1; attempt <= MAX_RETRIES; attempt++) {
100
106
  try {
107
+ console.log('Downloading binary (attempt ' + attempt + '/' + MAX_RETRIES + ')...');
101
108
  await download(url, dest);
102
109
  return true;
103
110
  } catch (err) {
104
111
  lastError = err;
105
- if (attempt < retries) {
112
+ console.error('Attempt ' + attempt + ' failed: ' + err.message);
113
+ if (attempt < MAX_RETRIES) {
106
114
  const delay = Math.pow(2, attempt) * 1000;
107
- console.warn(`Download attempt ${attempt + 1} failed for ${url}, retrying in ${delay}ms...`);
115
+ console.log('Retrying in ' + delay + 'ms...');
108
116
  await new Promise((resolve) => setTimeout(resolve, delay));
109
117
  }
110
118
  }
111
119
  }
112
- console.error(`Download failed after ${retries + 1} attempts for ${url}: ${lastError.message}`);
120
+ console.error('Failed to download binary after ' + MAX_RETRIES + ' attempts.');
121
+ console.error('Last error: ' + lastError.message);
113
122
  return false;
114
123
  }
115
124
 
116
125
  async function main() {
126
+ const version = getVersion();
127
+ const platform = getPlatform();
117
128
  const binaryPath = getBinaryPath();
118
129
 
119
- if (fs.existsSync(binaryPath)) {
120
- console.log('Binary already exists at', binaryPath);
121
- return;
122
- }
123
-
124
- const version = getVersion();
125
- const baseUrl = `https://github.com/${REPO}/releases/download/v${version}`;
130
+ const url = 'https://github.com/' + REPO + '/releases/download/v' + version + '/codebuff-' + platform;
126
131
 
127
- // Try platform-specific binary first
128
- const platformName = `${BINARY_NAME}-${process.platform}-${process.arch}${
129
- process.platform === 'win32' ? '.exe' : ''
130
- }`;
131
- const platformUrl = `${baseUrl}/${platformName}`;
132
- const genericUrl = `${baseUrl}/${BINARY_NAME}`;
132
+ console.log('Platform: ' + platform);
133
+ console.log('Version: ' + version);
134
+ console.log('Binary path: ' + binaryPath);
135
+ console.log('Download URL: ' + url);
133
136
 
134
137
  fs.mkdirSync(path.dirname(binaryPath), { recursive: true });
135
138
 
136
- if (await downloadWithRetry(platformUrl, binaryPath)) {
137
- console.log(`Downloaded platform-specific binary: ${platformName}`);
138
- } else if (await downloadWithRetry(genericUrl, binaryPath)) {
139
- console.log(`Downloaded generic binary: ${BINARY_NAME}`);
139
+ if (fs.existsSync(binaryPath)) {
140
+ console.log('Binary already exists. Skipping download.');
141
+ process.exit(0);
142
+ }
143
+
144
+ if (await downloadWithRetry(url, binaryPath)) {
145
+ console.log('Download complete.');
140
146
  } else {
141
- console.error(
142
- `Failed to download binary from:\n ${platformUrl}\n ${genericUrl}`
143
- );
147
+ console.error('\nUnable to download the codebuff binary.');
148
+ console.error('You can try installing manually from:');
149
+ console.error(' ' + url);
144
150
  process.exit(1);
145
151
  }
146
152
 
147
153
  if (process.platform !== 'win32') {
148
154
  fs.chmodSync(binaryPath, 0o755);
155
+ console.log('Made binary executable.');
149
156
  }
150
157
 
151
- console.log(`Binary installed at: ${binaryPath}`);
158
+ console.log('Binary installed at: ' + binaryPath);
152
159
  }
153
160
 
154
161
  main().catch((err) => {
155
- console.error('Error:', err.message);
162
+ console.error('Fatal error:', err.message);
156
163
  process.exit(1);
157
164
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codebuff-cli",
3
- "version": "1.0.12",
3
+ "version": "1.0.14",
4
4
  "license": "Apache-2.0",
5
5
  "bin": {
6
6
  "codebuff": "cli/bin/codebuff.cjs",