Skip to content

Commit 21a45f5

Browse files
committed
cli downloader: Store version in a metadata file for faster access
1 parent 4d14a45 commit 21a45f5

File tree

1 file changed

+21
-48
lines changed

1 file changed

+21
-48
lines changed

cli/release/index.js

Lines changed: 21 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ function createConfig(packageName) {
2222
configDir,
2323
binaryName,
2424
binaryPath: path.join(configDir, binaryName),
25+
metadataPath: path.join(configDir, 'codebuff-metadata.json'),
2526
userAgent: `${packageName}-cli`,
2627
requestTimeout: 20000,
2728
}
@@ -111,51 +112,18 @@ function streamToString(stream) {
111112
}
112113

113114
function getCurrentVersion() {
114-
if (!fs.existsSync(CONFIG.binaryPath)) return null
115-
116115
try {
117-
return new Promise((resolve) => {
118-
const child = spawn(CONFIG.binaryPath, ['--version'], {
119-
cwd: os.homedir(),
120-
stdio: 'pipe',
121-
})
122-
123-
let output = ''
124-
125-
child.stdout.on('data', (data) => {
126-
output += data.toString()
127-
})
128-
129-
child.stderr.on('data', () => {
130-
// Ignore stderr output
131-
})
132-
133-
const timeout = setTimeout(() => {
134-
child.kill('SIGTERM')
135-
setTimeout(() => {
136-
if (!child.killed) {
137-
child.kill('SIGKILL')
138-
}
139-
}, 4000)
140-
resolve('error')
141-
}, 4000)
142-
143-
child.on('exit', (code) => {
144-
clearTimeout(timeout)
145-
if (code === 0) {
146-
resolve(output.trim())
147-
} else {
148-
resolve('error')
149-
}
150-
})
151-
152-
child.on('error', () => {
153-
clearTimeout(timeout)
154-
resolve('error')
155-
})
156-
})
116+
if (!fs.existsSync(CONFIG.metadataPath)) {
117+
return null
118+
}
119+
const metadata = JSON.parse(fs.readFileSync(CONFIG.metadataPath, 'utf8'))
120+
// Also verify the binary still exists
121+
if (!fs.existsSync(CONFIG.binaryPath)) {
122+
return null
123+
}
124+
return metadata.version || null
157125
} catch (error) {
158-
return 'error'
126+
return null
159127
}
160128
}
161129

@@ -317,6 +285,11 @@ async function downloadBinary(version) {
317285
if (process.platform !== 'win32') {
318286
fs.chmodSync(extractedPath, 0o755)
319287
}
288+
// Save version metadata for fast version checking
289+
fs.writeFileSync(
290+
CONFIG.metadataPath,
291+
JSON.stringify({ version }, null, 2),
292+
)
320293
} else {
321294
throw new Error(
322295
`Binary not found after extraction. Expected: ${extractedPath}, Available files: ${files.join(', ')}`,
@@ -333,8 +306,8 @@ async function downloadBinary(version) {
333306
}
334307

335308
async function ensureBinaryExists() {
336-
const currentVersion = await getCurrentVersion()
337-
if (currentVersion !== null && currentVersion !== 'error') {
309+
const currentVersion = getCurrentVersion()
310+
if (currentVersion !== null) {
338311
return
339312
}
340313

@@ -357,14 +330,14 @@ async function ensureBinaryExists() {
357330

358331
async function checkForUpdates(runningProcess, exitListener) {
359332
try {
360-
const currentVersion = await getCurrentVersion()
361-
if (!currentVersion) return
333+
const currentVersion = getCurrentVersion()
362334

363335
const latestVersion = await getLatestVersion()
364336
if (!latestVersion) return
365337

366338
if (
367-
currentVersion === 'error' ||
339+
// Download new version if current version is unknown or outdated.
340+
currentVersion === null ||
368341
compareVersions(currentVersion, latestVersion) < 0
369342
) {
370343
term.clearLine()

0 commit comments

Comments
 (0)