Skip to content
This repository has been archived by the owner on Dec 21, 2024. It is now read-only.

Commit

Permalink
优化在下载失败或者中途退出的处理
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel-luo committed Jan 3, 2023
1 parent 590ddd5 commit 227dbba
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const axios = require("axios");
const {spawn} = require('child_process');

let subprocesses = [];
let isLoading = false;
let logFn = (...args) => {
console.log(...args);
}
Expand Down Expand Up @@ -42,6 +43,11 @@ app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit()
})

app.on('before-quit', () => {
const resourcePath = path.parse(app.getAppPath()).dir;
if (isLoading) _removeFile(`${resourcePath}/app.asar-new`);
})

app.on('quit', () => {
const resourcePath = path.parse(app.getAppPath()).dir;
if (fs.existsSync(`${resourcePath}/updater.exe`) && fs.existsSync(`${resourcePath}/app.asar-new`)) {
Expand Down Expand Up @@ -75,17 +81,20 @@ async function handleStart(e, account, password, platform, isFillAP, chromeUrl)
}

async function updateApp() {
if (!app.isPackaged) return;
if (!app.isPackaged || isLoading) return;
logFn("开始更新...");
const resourcePath = path.parse(app.getAppPath()).dir;
try {
logFn("创建更新脚本...");
if (!fs.existsSync(`${resourcePath}/updater.exe`)) fs.copyFileSync(app.getAppPath() + '/updater.exe', `${resourcePath}/updater.exe`);
logFn("开始下载最新版本资源...")
logFn("开始下载最新版本资源...");
isLoading = true;
if (await _downloadFile('https://github.com/Samuel-luo/network_class_Ganker/releases/latest/download/app.asar', `${resourcePath}/app.asar-new`).then(() => 1).catch(() => 0)) {
app.exit(0)
} else {
logFn("下载失败!");
_removeFile(`${resourcePath}/app.asar-new`);
isLoading = false;
}
} catch (err) {
logFn(err);
Expand Down Expand Up @@ -144,15 +153,16 @@ function _closeChildProcess() {


async function _downloadFile(url, filepath) {
const writer = fs.createWriteStream(filepath);
const response = await axios({
url,
method: "GET",
responseType: "stream",
});
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on("finish", resolve);
writer.on("error", reject);
responseType: "arraybuffer",
});
fs.writeFileSync(filepath, response.data, {encoding: "binary"})
}

function _removeFile(filePath) {
while (fs.existsSync(filePath)) {
fs.rmSync(filePath);
}
}

0 comments on commit 227dbba

Please sign in to comment.