codebuff 1.0.337 → 1.0.338
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 +52 -10
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
const fs = require('fs')
|
|
4
4
|
const path = require('path')
|
|
5
5
|
const os = require('os')
|
|
6
|
-
const { spawn
|
|
6
|
+
const { spawn } = require('child_process')
|
|
7
7
|
const https = require('https')
|
|
8
8
|
const zlib = require('zlib')
|
|
9
9
|
const tar = require('tar')
|
|
@@ -121,15 +121,49 @@ function getCurrentVersion() {
|
|
|
121
121
|
if (!fs.existsSync(CONFIG.binaryPath)) return null
|
|
122
122
|
|
|
123
123
|
try {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
124
|
+
return new Promise((resolve, reject) => {
|
|
125
|
+
const child = spawn(CONFIG.binaryPath, ['--version'], {
|
|
126
|
+
cwd: os.homedir(),
|
|
127
|
+
stdio: 'pipe',
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
let output = ''
|
|
131
|
+
let errorOutput = ''
|
|
132
|
+
|
|
133
|
+
child.stdout.on('data', (data) => {
|
|
134
|
+
output += data.toString()
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
child.stderr.on('data', (data) => {
|
|
138
|
+
errorOutput += data.toString()
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
const timeout = setTimeout(() => {
|
|
142
|
+
child.kill('SIGTERM')
|
|
143
|
+
setTimeout(() => {
|
|
144
|
+
if (!child.killed) {
|
|
145
|
+
child.kill('SIGKILL')
|
|
146
|
+
}
|
|
147
|
+
}, 1000)
|
|
148
|
+
resolve('error')
|
|
149
|
+
}, 1000)
|
|
150
|
+
|
|
151
|
+
child.on('exit', (code) => {
|
|
152
|
+
clearTimeout(timeout)
|
|
153
|
+
if (code === 0) {
|
|
154
|
+
resolve(output.trim())
|
|
155
|
+
} else {
|
|
156
|
+
resolve('error')
|
|
157
|
+
}
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
child.on('error', () => {
|
|
161
|
+
clearTimeout(timeout)
|
|
162
|
+
resolve('error')
|
|
163
|
+
})
|
|
129
164
|
})
|
|
130
|
-
return result.trim()
|
|
131
165
|
} catch (error) {
|
|
132
|
-
return
|
|
166
|
+
return 'error'
|
|
133
167
|
}
|
|
134
168
|
}
|
|
135
169
|
|
|
@@ -177,6 +211,10 @@ async function downloadBinary(version) {
|
|
|
177
211
|
// Ensure config directory exists
|
|
178
212
|
fs.mkdirSync(CONFIG.configDir, { recursive: true })
|
|
179
213
|
|
|
214
|
+
if (fs.existsSync(CONFIG.binaryPath)) {
|
|
215
|
+
fs.unlinkSync(CONFIG.binaryPath)
|
|
216
|
+
}
|
|
217
|
+
|
|
180
218
|
term.write('Downloading...')
|
|
181
219
|
|
|
182
220
|
const res = await httpGet(downloadUrl)
|
|
@@ -261,13 +299,17 @@ async function ensureBinaryExists() {
|
|
|
261
299
|
|
|
262
300
|
async function checkForUpdates(runningProcess, exitListener) {
|
|
263
301
|
try {
|
|
264
|
-
const currentVersion = getCurrentVersion()
|
|
302
|
+
const currentVersion = await getCurrentVersion()
|
|
265
303
|
if (!currentVersion) return
|
|
266
304
|
|
|
267
305
|
const latestVersion = await getLatestVersion()
|
|
268
306
|
if (!latestVersion) return
|
|
269
307
|
|
|
270
|
-
if (
|
|
308
|
+
if (
|
|
309
|
+
// Download new version if current binary errors.
|
|
310
|
+
currentVersion === 'error' ||
|
|
311
|
+
compareVersions(currentVersion, latestVersion) < 0
|
|
312
|
+
) {
|
|
271
313
|
term.clearLine()
|
|
272
314
|
|
|
273
315
|
// Remove the specific exit listener to prevent it from interfering with the update
|