enhance checkAndDownloadDependenciesForInit - adjust promise logic

This commit is contained in:
geekgeekrun
2024-02-25 09:11:45 +08:00
parent 45bf49c1ba
commit b115f0fbc3
@@ -26,37 +26,50 @@ export const checkAndDownloadDependenciesForInit = async () => {
try { try {
let timeoutTimer = 0 let timeoutTimer = 0
await new Promise((resolve, reject) => { const promiseWithResolver = (() => {
checkAndDownloadPuppeteerExecutable({ const o = {} as unknown as {
downloadProgressCallback(downloadedBytes: number, totalBytes: number) { promise: Promise<unknown>
clearTimeout(timeoutTimer) resolve: (result: unknown) => void
if (downloadedBytes !== totalBytes) { reject: (reason: unknown) => void
timeoutTimer = setTimeout(() => { }
// will encounter this when network disconnected when downloading o.promise = new Promise((resolve, reject) => {
reject(new Error('PROGRESS_NOT_CHANGED_TOO_LONG')) o.resolve = resolve
}, 30 * 1000) o.reject = reject
} })
console.log(downloadedBytes / totalBytes) return o
pipeWriteRegardlessError( })()
pipe,
JSON.stringify({ checkAndDownloadPuppeteerExecutable({
type: 'PUPPETEER_DOWNLOAD_PROGRESS', downloadProgressCallback(downloadedBytes: number, totalBytes: number) {
totalBytes, clearTimeout(timeoutTimer)
downloadedBytes if (downloadedBytes !== totalBytes) {
}) timeoutTimer = setTimeout(() => {
) + '\r\n' // will encounter this when network disconnected when downloading
promiseWithResolver.reject(new Error('PROGRESS_NOT_CHANGED_TOO_LONG'))
}, 5 * 1000)
} }
}).then( console.log(downloadedBytes / totalBytes)
() => { pipeWriteRegardlessError(
resolve(void 0) pipe,
}, JSON.stringify({
(err) => { type: 'PUPPETEER_DOWNLOAD_PROGRESS',
reject(err) totalBytes,
} downloadedBytes
) })
) + '\r\n'
}
}) })
.then(() => {
promiseWithResolver.resolve(void 0)
})
.catch((err) => {
promiseWithResolver.reject(err)
})
await promiseWithResolver.promise
app.exit(DOWNLOAD_ERROR_EXIT_CODE.NO_ERROR) app.exit(DOWNLOAD_ERROR_EXIT_CODE.NO_ERROR)
} catch (err) { } catch (err) {
console.error(err)
app.exit(DOWNLOAD_ERROR_EXIT_CODE.DOWNLOAD_ERROR) app.exit(DOWNLOAD_ERROR_EXIT_CODE.DOWNLOAD_ERROR)
} }
} }