diff --git a/forge.config.js b/forge.config.js index d8ff3ea..48ce300 100644 --- a/forge.config.js +++ b/forge.config.js @@ -1,7 +1,7 @@ module.exports = { packagerConfig: { asar: true, - ignore: /updater\.cpp/, + ignore: /(updater\.cpp|forge\.config\.js|\.gitignore)/, }, rebuildConfig: {}, makers: [ diff --git a/main.js b/main.js index 8093c68..a8694e6 100644 --- a/main.js +++ b/main.js @@ -1,12 +1,13 @@ const {app, BrowserWindow, ipcMain} = require('electron'); -const {fork} = require('child_process'); +const {spawn, fork, exec} = require('child_process'); const path = require("path"); const fs = require("fs"); const axios = require("axios"); -const {spawn} = require('child_process'); +const asar = require('asar'); let subprocesses = []; let isLoading = false; +const resourcePath = path.parse(app.getAppPath()).dir; let logFn = (...args) => { console.log(...args); } @@ -27,6 +28,7 @@ const createWindow = () => { } app.whenReady().then(() => { + if (app.isPackaged && !fs.existsSync(`${resourcePath}/release`)) asar.extractAll(`${resourcePath}/app.asar`, `${resourcePath}/release`); ipcMain.handle('start', handleStart) ipcMain.handle('updateApp', updateApp) ipcMain.handle('get-rec-info', getRecInfo) @@ -44,12 +46,10 @@ app.on('window-all-closed', () => { }) 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`)) { logFn("开始替换资源..."); const child = spawn(`"${resourcePath}/updater.exe"`, { @@ -80,16 +80,25 @@ async function handleStart(e, account, password, platform, isFillAP, chromeUrl) return 1; } +function getRecInfo() { + try { + return fs.readFileSync(app.isPackaged ? app.getAppPath() + '/data.rec' : './data.rec', {encoding: 'utf8'}); + } catch (err) { + return "{}"; + } +} + async function updateApp() { if (!app.isPackaged || isLoading) return; + _closeChildProcess(); 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`); + if (!fs.existsSync(`${resourcePath}/updater.exe`)) _copyFile(app.getAppPath() + '/updater.exe', `${resourcePath}/updater.exe`); 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)) { + _removeDir(`${resourcePath}/release`); app.exit(0) } else { logFn("下载失败!"); @@ -101,17 +110,9 @@ async function updateApp() { } } -function getRecInfo() { - try { - return fs.readFileSync(app.isPackaged ? app.getAppPath() + '/data.rec' : './data.rec', {encoding: 'utf8'}); - } catch (err) { - return "{}"; - } -} - function createChildProcess(data, index) { - let subprocess = fork(app.isPackaged ? app.getAppPath() + '/index.js' : './index.js', { - cwd: app.isPackaged ? app.getAppPath() : __dirname, stdio: ['pipe', 'pipe', 'pipe', 'ipc'], env: { + let subprocess = fork(app.isPackaged ? path.join(resourcePath, 'release', 'index.js') : './index.js', { + cwd: app.isPackaged ? `${resourcePath}/release` : __dirname, stdio: ['pipe', 'pipe', 'pipe', 'ipc'], env: { account: data.account, password: data.password, platform: data.platform, isFillAP: data.isFillAP, chromeUrl: data.chromeUrl } }); @@ -142,9 +143,7 @@ function _closeChildProcess() { let subprocess; while (subprocess = subprocesses.shift()) { try { - if (subprocess) { - subprocess.send('exit'); - } + subprocess.send('exit'); } catch (err) { // 静默处理 err } @@ -161,8 +160,31 @@ async function _downloadFile(url, filepath) { fs.writeFileSync(filepath, response.data, {encoding: "binary"}) } +function _copyFile(from, to) { + fs.copyFileSync(from, to); +} + function _removeFile(filePath) { while (fs.existsSync(filePath)) { fs.rmSync(filePath); } } + +function _removeDir(filePath) { + const files = [] + if (fs.existsSync(filePath)) { + const files = fs.readdirSync(filePath) + files.forEach((file) => { + const nextFilePath = `${filePath}/${file}` + const states = fs.statSync(nextFilePath) + if (states.isDirectory()) { + // recurse + _removeDir(nextFilePath) + } else { + // delete file + fs.unlinkSync(nextFilePath) + } + }) + fs.rmdirSync(filePath) + } +} diff --git a/package-lock.json b/package-lock.json index ee9fe55..54a2f03 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,14 +1,15 @@ { "name": "network_class", - "version": "0.1.0", + "version": "0.2.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "network_class", - "version": "0.1.0", + "version": "0.2.0", "license": "MIT", "dependencies": { + "asar": "^3.2.0", "axios": "^1.2.2", "electron-squirrel-startup": "^1.0.0", "selenium-webdriver": "^4.5.0" @@ -1716,7 +1717,6 @@ "version": "7.2.0", "resolved": "https://registry.npmjs.org/@types/glob/-/glob-7.2.0.tgz", "integrity": "sha512-ZUxbzKl0IfJILTS6t7ip5fQQM/J3TJYubDm3nMbgubNNYS62eXeUpoLUC8/7fJNiFYHTrGPQn7hspDUzIHX3UA==", - "dev": true, "optional": true, "dependencies": { "@types/minimatch": "*", @@ -1743,14 +1743,13 @@ "version": "5.1.2", "resolved": "https://registry.npmjs.org/@types/minimatch/-/minimatch-5.1.2.tgz", "integrity": "sha512-K0VQKziLUWkVKiRVrx4a40iPaxTUefQmjtkQofBkYRcoaaL/8rhwDWww9qWbrgicNOgnpIsMxyNIUM4+n6dUIA==", - "dev": true, "optional": true }, "node_modules/@types/node": { "version": "16.18.3", "resolved": "https://registry.npmjs.org/@types/node/-/node-16.18.3.tgz", "integrity": "sha512-jh6m0QUhIRcZpNv7Z/rpN+ZWXOicUUQbSoWks7Htkbb9IjFQj4kzcX/xFCkjstCj5flMsN8FiSvt+q+Tcs4Llg==", - "dev": true + "devOptional": true }, "node_modules/@types/responselike": { "version": "1.0.0", @@ -1905,8 +1904,6 @@ "resolved": "https://registry.npmjs.org/asar/-/asar-3.2.0.tgz", "integrity": "sha512-COdw2ZQvKdFGFxXwX3oYh2/sOsJWJegrdJCGxnN4MZ7IULgRBp9P6665aqj9z1v9VwP4oP1hRBojRDQ//IGgAg==", "deprecated": "Please use @electron/asar moving forward. There is no API change, just a package name change", - "dev": true, - "optional": true, "dependencies": { "chromium-pickle-js": "^0.2.0", "commander": "^5.0.0", @@ -1927,8 +1924,6 @@ "version": "5.1.0", "resolved": "https://registry.npmjs.org/commander/-/commander-5.1.0.tgz", "integrity": "sha512-P0CysNDQ7rtVw4QIQtm+MRxV66vKFSvlsQvGYXZWR3qFU0jlMKHZZZgw8e+8DSah4UDKMqnknRDQz+xuQXQ/Zg==", - "dev": true, - "optional": true, "engines": { "node": ">= 6" } @@ -2265,8 +2260,7 @@ "node_modules/chromium-pickle-js": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/chromium-pickle-js/-/chromium-pickle-js-0.2.0.tgz", - "integrity": "sha512-1R5Fho+jBq0DDydt+/vHWj5KJNJCKdARKOCwZUen84I5BreWoLqRLANH1U87eJy1tiASPtMnGqJJq0ZsLoRPOw==", - "dev": true + "integrity": "sha512-1R5Fho+jBq0DDydt+/vHWj5KJNJCKdARKOCwZUen84I5BreWoLqRLANH1U87eJy1tiASPtMnGqJJq0ZsLoRPOw==" }, "node_modules/clean-stack": { "version": "2.2.0", @@ -8395,7 +8389,6 @@ "version": "7.2.0", "resolved": "https://registry.npmjs.org/@types/glob/-/glob-7.2.0.tgz", "integrity": "sha512-ZUxbzKl0IfJILTS6t7ip5fQQM/J3TJYubDm3nMbgubNNYS62eXeUpoLUC8/7fJNiFYHTrGPQn7hspDUzIHX3UA==", - "dev": true, "optional": true, "requires": { "@types/minimatch": "*", @@ -8421,14 +8414,13 @@ "version": "5.1.2", "resolved": "https://registry.npmjs.org/@types/minimatch/-/minimatch-5.1.2.tgz", "integrity": "sha512-K0VQKziLUWkVKiRVrx4a40iPaxTUefQmjtkQofBkYRcoaaL/8rhwDWww9qWbrgicNOgnpIsMxyNIUM4+n6dUIA==", - "dev": true, "optional": true }, "@types/node": { "version": "16.18.3", "resolved": "https://registry.npmjs.org/@types/node/-/node-16.18.3.tgz", "integrity": "sha512-jh6m0QUhIRcZpNv7Z/rpN+ZWXOicUUQbSoWks7Htkbb9IjFQj4kzcX/xFCkjstCj5flMsN8FiSvt+q+Tcs4Llg==", - "dev": true + "devOptional": true }, "@types/responselike": { "version": "1.0.0", @@ -8550,8 +8542,6 @@ "version": "3.2.0", "resolved": "https://registry.npmjs.org/asar/-/asar-3.2.0.tgz", "integrity": "sha512-COdw2ZQvKdFGFxXwX3oYh2/sOsJWJegrdJCGxnN4MZ7IULgRBp9P6665aqj9z1v9VwP4oP1hRBojRDQ//IGgAg==", - "dev": true, - "optional": true, "requires": { "@types/glob": "^7.1.1", "chromium-pickle-js": "^0.2.0", @@ -8563,9 +8553,7 @@ "commander": { "version": "5.1.0", "resolved": "https://registry.npmjs.org/commander/-/commander-5.1.0.tgz", - "integrity": "sha512-P0CysNDQ7rtVw4QIQtm+MRxV66vKFSvlsQvGYXZWR3qFU0jlMKHZZZgw8e+8DSah4UDKMqnknRDQz+xuQXQ/Zg==", - "dev": true, - "optional": true + "integrity": "sha512-P0CysNDQ7rtVw4QIQtm+MRxV66vKFSvlsQvGYXZWR3qFU0jlMKHZZZgw8e+8DSah4UDKMqnknRDQz+xuQXQ/Zg==" } } }, @@ -8822,8 +8810,7 @@ "chromium-pickle-js": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/chromium-pickle-js/-/chromium-pickle-js-0.2.0.tgz", - "integrity": "sha512-1R5Fho+jBq0DDydt+/vHWj5KJNJCKdARKOCwZUen84I5BreWoLqRLANH1U87eJy1tiASPtMnGqJJq0ZsLoRPOw==", - "dev": true + "integrity": "sha512-1R5Fho+jBq0DDydt+/vHWj5KJNJCKdARKOCwZUen84I5BreWoLqRLANH1U87eJy1tiASPtMnGqJJq0ZsLoRPOw==" }, "clean-stack": { "version": "2.2.0", diff --git a/package.json b/package.json index b542ce1..f04f5e7 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "author": "samuel", "license": "MIT", "dependencies": { + "asar": "^3.2.0", "axios": "^1.2.2", "electron-squirrel-startup": "^1.0.0", "selenium-webdriver": "^4.5.0" diff --git a/updater.cpp b/updater.cpp index 875f72d..a03d9de 100644 --- a/updater.cpp +++ b/updater.cpp @@ -7,6 +7,7 @@ int main() Sleep(1000); remove("app.asar"); rename("app.asar-new","app.asar"); + Sleep(1000); ShellExecute(0,"open", "..\\network_class.exe", 0, 0, 1); return 0; } diff --git a/updater.exe b/updater.exe index 1f80df1..440eeae 100644 Binary files a/updater.exe and b/updater.exe differ