codecane 1.0.411 → 1.0.413
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/index.js +50 -47
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -16,7 +16,6 @@ const CONFIG = {
|
|
|
16
16
|
githubRepo: 'CodebuffAI/codebuff-community',
|
|
17
17
|
userAgent: 'codebuff-cli',
|
|
18
18
|
requestTimeout: 10000,
|
|
19
|
-
updateCheckTimeout: 5000,
|
|
20
19
|
}
|
|
21
20
|
|
|
22
21
|
CONFIG.binaryPath = path.join(CONFIG.configDir, CONFIG.binaryName)
|
|
@@ -60,9 +59,16 @@ function httpGet(url, options = {}) {
|
|
|
60
59
|
},
|
|
61
60
|
}
|
|
62
61
|
|
|
62
|
+
// Add GitHub token if available
|
|
63
|
+
const token = process.env.GITHUB_TOKEN
|
|
64
|
+
if (token) {
|
|
65
|
+
console.log('Using your GITHUB_TOKEN to download the latest version.')
|
|
66
|
+
reqOptions.headers.Authorization = `Bearer ${token}`
|
|
67
|
+
}
|
|
68
|
+
|
|
63
69
|
const req = https.get(reqOptions, (res) => {
|
|
64
70
|
if (res.statusCode === 302 || res.statusCode === 301) {
|
|
65
|
-
return httpGet(res.headers.location, options)
|
|
71
|
+
return httpGet(new URL(res.headers.location, url).href, options)
|
|
66
72
|
.then(resolve)
|
|
67
73
|
.catch(reject)
|
|
68
74
|
}
|
|
@@ -80,21 +86,34 @@ function httpGet(url, options = {}) {
|
|
|
80
86
|
}
|
|
81
87
|
|
|
82
88
|
async function getLatestVersion() {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
89
|
+
const res = await httpGet(
|
|
90
|
+
`https://api.github.com/repos/${CONFIG.githubRepo}/releases/latest`
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
/* ── simple rate-limit fallback ─────────────────────────── */
|
|
94
|
+
if (
|
|
95
|
+
res.statusCode === 403 &&
|
|
96
|
+
res.headers['x-ratelimit-remaining'] === '0'
|
|
97
|
+
) {
|
|
98
|
+
term.writeLine(
|
|
99
|
+
'GitHub API rate-limit reached. Skipping version check – either wait an hour or set GITHUB_TOKEN and try again.'
|
|
86
100
|
)
|
|
87
|
-
|
|
88
|
-
let data = ''
|
|
89
|
-
for await (const chunk of res) {
|
|
90
|
-
data += chunk
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
const release = JSON.parse(data)
|
|
94
|
-
return release.tag_name?.replace(/^v/, '') || null
|
|
95
|
-
} catch (error) {
|
|
96
101
|
return null
|
|
97
102
|
}
|
|
103
|
+
|
|
104
|
+
if (res.statusCode !== 200) return null // other errors
|
|
105
|
+
|
|
106
|
+
const body = await streamToString(res)
|
|
107
|
+
return JSON.parse(body).tag_name?.replace(/^v/, '') || null
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function streamToString(stream) {
|
|
111
|
+
return new Promise((resolve, reject) => {
|
|
112
|
+
let data = ''
|
|
113
|
+
stream.on('data', chunk => (data += chunk))
|
|
114
|
+
stream.on('end', () => resolve(data))
|
|
115
|
+
stream.on('error', reject)
|
|
116
|
+
})
|
|
98
117
|
}
|
|
99
118
|
|
|
100
119
|
function getCurrentVersion() {
|
|
@@ -102,6 +121,7 @@ function getCurrentVersion() {
|
|
|
102
121
|
|
|
103
122
|
try {
|
|
104
123
|
const result = execSync(`"${CONFIG.binaryPath}" --version`, {
|
|
124
|
+
cwd: os.homedir(),
|
|
105
125
|
encoding: 'utf-8',
|
|
106
126
|
stdio: 'pipe',
|
|
107
127
|
timeout: 1000,
|
|
@@ -137,10 +157,6 @@ function formatBytes(bytes) {
|
|
|
137
157
|
return parseFloat((bytes / Math.pow(k, i)).toFixed(1)) + ' ' + sizes[i]
|
|
138
158
|
}
|
|
139
159
|
|
|
140
|
-
function formatSpeed(bytesPerSecond) {
|
|
141
|
-
return formatBytes(bytesPerSecond) + '/s'
|
|
142
|
-
}
|
|
143
|
-
|
|
144
160
|
function createProgressBar(percentage, width = 30) {
|
|
145
161
|
const filled = Math.round((width * percentage) / 100)
|
|
146
162
|
const empty = width - filled
|
|
@@ -160,7 +176,7 @@ async function downloadBinary(version) {
|
|
|
160
176
|
// Ensure config directory exists
|
|
161
177
|
fs.mkdirSync(CONFIG.configDir, { recursive: true })
|
|
162
178
|
|
|
163
|
-
term.write(
|
|
179
|
+
term.write('Downloading...')
|
|
164
180
|
|
|
165
181
|
const res = await httpGet(downloadUrl)
|
|
166
182
|
|
|
@@ -172,46 +188,35 @@ async function downloadBinary(version) {
|
|
|
172
188
|
let downloadedSize = 0
|
|
173
189
|
let lastProgressTime = Date.now()
|
|
174
190
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
for await (const chunk of res) {
|
|
178
|
-
chunks.push(chunk)
|
|
191
|
+
res.on('data', (chunk) => {
|
|
179
192
|
downloadedSize += chunk.length
|
|
180
|
-
|
|
181
193
|
const now = Date.now()
|
|
182
194
|
if (now - lastProgressTime >= 100 || downloadedSize === totalSize) {
|
|
183
195
|
lastProgressTime = now
|
|
184
|
-
|
|
185
196
|
if (totalSize > 0) {
|
|
186
|
-
const
|
|
187
|
-
const progressBar = createProgressBar(percentage)
|
|
188
|
-
|
|
197
|
+
const pct = Math.round((downloadedSize / totalSize) * 100)
|
|
189
198
|
term.write(
|
|
190
|
-
`Downloading... ${
|
|
199
|
+
`Downloading... ${createProgressBar(pct)} ${pct}% of ${formatBytes(
|
|
200
|
+
totalSize
|
|
201
|
+
)}`
|
|
191
202
|
)
|
|
192
203
|
} else {
|
|
193
204
|
term.write(`Downloading... ${formatBytes(downloadedSize)}`)
|
|
194
205
|
}
|
|
195
206
|
}
|
|
196
|
-
}
|
|
197
|
-
term.clearLine()
|
|
198
|
-
console.log('Download complete!')
|
|
199
|
-
|
|
200
|
-
term.write('Extracting...')
|
|
207
|
+
})
|
|
201
208
|
|
|
202
|
-
|
|
209
|
+
await new Promise((resolve, reject) => {
|
|
210
|
+
res
|
|
211
|
+
.pipe(zlib.createGunzip())
|
|
212
|
+
.pipe(tar.x({ cwd: CONFIG.configDir }))
|
|
213
|
+
.on('finish', resolve)
|
|
214
|
+
.on('error', reject)
|
|
215
|
+
})
|
|
216
|
+
term.clearLine()
|
|
217
|
+
console.log('Download and extract complete!')
|
|
203
218
|
|
|
204
219
|
try {
|
|
205
|
-
// Unix tar.gz extraction for all platforms
|
|
206
|
-
await new Promise((resolve, reject) => {
|
|
207
|
-
const gunzip = zlib.createGunzip()
|
|
208
|
-
const extract = tar.extract({ cwd: CONFIG.configDir })
|
|
209
|
-
|
|
210
|
-
gunzip.pipe(extract).on('finish', resolve).on('error', reject)
|
|
211
|
-
|
|
212
|
-
gunzip.end(buffer)
|
|
213
|
-
})
|
|
214
|
-
|
|
215
220
|
// Find the extracted binary - it should be named "codebuff" or "codebuff.exe"
|
|
216
221
|
const files = fs.readdirSync(CONFIG.configDir)
|
|
217
222
|
const extractedPath = path.join(CONFIG.configDir, CONFIG.binaryName)
|
|
@@ -290,7 +295,6 @@ async function checkForUpdates(runningProcess, exitListener) {
|
|
|
290
295
|
// Restart with new binary - this replaces the current process
|
|
291
296
|
const newChild = spawn(CONFIG.binaryPath, process.argv.slice(2), {
|
|
292
297
|
stdio: 'inherit',
|
|
293
|
-
cwd: process.cwd(),
|
|
294
298
|
detached: false,
|
|
295
299
|
})
|
|
296
300
|
|
|
@@ -313,7 +317,6 @@ async function main() {
|
|
|
313
317
|
// Start codebuff
|
|
314
318
|
const child = spawn(CONFIG.binaryPath, process.argv.slice(2), {
|
|
315
319
|
stdio: 'inherit',
|
|
316
|
-
cwd: process.cwd(),
|
|
317
320
|
})
|
|
318
321
|
|
|
319
322
|
// Store reference to the exit listener so we can remove it during updates
|