From d89cde61eba8aebf7a47b4e17b8d543c1f940f2a Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Fri, 11 Mar 2022 20:07:12 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E8=B0=83=E6=95=B4=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E5=8C=85,=20=E6=94=B9=E4=B8=BA=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E6=89=93=E5=8C=85=E5=87=BA=E6=9D=A5=E7=9A=84asar=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- desktop/forge.config.js | 3 ++- desktop/package.json | 2 +- desktop/scripts/update.ts | 54 +++++++++++++++++++++++++++++---------- 3 files changed, 44 insertions(+), 15 deletions(-) diff --git a/desktop/forge.config.js b/desktop/forge.config.js index 47cbfa66..df04834a 100644 --- a/desktop/forge.config.js +++ b/desktop/forge.config.js @@ -20,6 +20,7 @@ module.exports = { prod: 'com.tailchat.beta.desktop', }), icon: path.resolve(__dirname, './build/icon'), + asar: true, }, makers: [ { @@ -30,7 +31,7 @@ module.exports = { }, { name: '@electron-forge/maker-zip', - platforms: ['darwin'], + platforms: ['darwin', 'win32'], }, { name: '@electron-forge/maker-deb', diff --git a/desktop/package.json b/desktop/package.json index 7cbd68a4..d35b6046 100644 --- a/desktop/package.json +++ b/desktop/package.json @@ -32,6 +32,7 @@ "@types/gh-pages": "^3.2.1", "adm-zip": "^0.5.9", "electron": "17.0.1", + "gh-pages": "^3.2.3", "ts-node": "^10.7.0", "typescript": "~4.5.4" }, @@ -41,7 +42,6 @@ "electron-unhandled": "^4.0.0", "electron-update-notifier": "^1.5.3", "electron-window-state": "^5.0.3", - "gh-pages": "^3.2.3", "update-electron-app": "^2.0.1" } } diff --git a/desktop/scripts/update.ts b/desktop/scripts/update.ts index c95da070..e8055b82 100644 --- a/desktop/scripts/update.ts +++ b/desktop/scripts/update.ts @@ -4,6 +4,11 @@ import AdmZip from 'adm-zip'; import crypto from 'crypto'; import { version } from '../package.json'; import ghpages from 'gh-pages'; +import os from 'os'; + +/** + * 发布asar + */ const hash = (data: Buffer, type = 'sha256') => { const hmac = crypto.createHmac(type, 'hk4e'); @@ -11,43 +16,64 @@ const hash = (data: Buffer, type = 'sha256') => { return hmac.digest('hex'); }; -function createZip(filePath: string, dest: string) { +function createZip(localFilePath: string, dest: string) { const zip = new AdmZip(); - zip.addLocalFolder(filePath); + zip.addLocalFile(localFilePath); zip.toBuffer(); zip.writeZip(dest); } +const outputDir = path.resolve(__dirname, '../out/'); + +function getAsarFilePath() { + if (os.platform() === 'win32') { + return path.resolve( + outputDir, + './prod/tailchat-desktop-win32-x64/resources/app.asar' + ); + } else if (os.platform() === 'darwin') { + return path.resolve( + outputDir, + './prod/tailchat-desktop.app/Contents/Resources/app.asar' + ); + } + + throw new Error('Not support'); +} async function createUpdateZipAndDeploy() { - const appPath = path.resolve(__dirname, '../dist'); - if (!(await fs.pathExists(appPath))) { - throw new Error('dist 目录不存在'); + const asarPath = getAsarFilePath(); + if (!(await fs.pathExists(asarPath))) { + throw new Error('asar 文件不存在'); } - const outputDir = path.resolve(__dirname, '../out/update/'); - const outputPath = path.resolve(outputDir, 'tmp.zip'); - await fs.ensureDir(outputDir); - await fs.emptyDir(outputDir); + const updateDir = path.resolve(outputDir, './update'); + const outputPath = path.resolve(updateDir, './tmp.zip'); + await fs.ensureDir(updateDir); + await fs.emptyDir(updateDir); - createZip(appPath, outputPath); + console.log('开始创建更新包'); + createZip(asarPath, outputPath); const buffer = await fs.readFile(outputPath); const sha256 = hash(buffer); const hashName = sha256.slice(7, 12); - await fs.move(outputPath, path.resolve(outputDir, `${hashName}.zip`)); + await fs.move(outputPath, path.resolve(updateDir, `${hashName}.zip`)); - await fs.outputJSON(path.resolve(outputDir, 'manifest.json'), { + console.log('正在输出更新文件...'); + await fs.outputJSON(path.resolve(updateDir, 'manifest.json'), { active: true, version, from: '0.0.0', name: `${hashName}.zip`, hash: sha256, date: new Date().valueOf(), + size: buffer.byteLength, }); + console.log('正在部署到远程...'); // 部署到github pages await new Promise((resolve, reject) => { ghpages.publish( - outputDir, + updateDir, { repo: 'git@github.com:msgbyte/tailchat-archive.git', branch: 'gh-pages', @@ -64,6 +90,8 @@ async function createUpdateZipAndDeploy() { } ); }); + + console.log('完成'); } // async function