Skip to content

Commit f4acea2

Browse files
✨ add electron-updater
1 parent b5b562d commit f4acea2

File tree

6 files changed

+122
-19
lines changed

6 files changed

+122
-19
lines changed

.github/workflows/node.js.yml

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,7 @@ jobs:
3131
- name: Create package-lock.json
3232
shell: sh
3333
run: |
34-
cat << EOF > package-lock.json
35-
{
36-
"name": "electron-vvt",
37-
"version": "0.0.1",
38-
"lockfileVersion": 3,
39-
"requires": true,
40-
"packages": {
41-
"": {
42-
"name": "electron-vvt",
43-
"version": "0.0.1",
44-
"license": "Apache-2.0",
45-
"engines": {
46-
"node": ">=20"
47-
}
48-
}
49-
}
50-
}
51-
EOF
34+
cp package.json package-lock.json
5235
5336
- name: Use Node.js ${{ matrix.node-version }}
5437
uses: actions/setup-node@v4

.github/workflows/npm-publish.yml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
2+
# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages
3+
4+
name: Node.js Publish
5+
6+
on:
7+
push:
8+
# This will trigger on any tag push
9+
tags:
10+
- '*'
11+
12+
env:
13+
GH_TOKEN: ${{ secrets.GH_TOKEN }}
14+
15+
jobs:
16+
build:
17+
18+
runs-on: ${{ matrix.os }}
19+
20+
strategy:
21+
matrix:
22+
node-version: [ 22.x ]
23+
os: [ ubuntu-latest, macos-latest, windows-latest ]
24+
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
25+
26+
steps:
27+
28+
- name: Checkout repository
29+
uses: actions/checkout@v4
30+
31+
- name: Create package-lock.json
32+
shell: sh
33+
run: |
34+
cp package.json package-lock.json
35+
36+
- name: Use Node.js ${{ matrix.node-version }}
37+
uses: actions/setup-node@v4
38+
with:
39+
node-version: ${{ matrix.node-version }}
40+
cache: 'npm'
41+
42+
- name: Delete .npmrc
43+
shell: sh
44+
run: rm -rf .npmrc
45+
46+
- name: Install dependencies
47+
run: npm install
48+
49+
- name: Mac Publish with npm
50+
if: ${{ matrix.os == 'macos-latest' }}
51+
env:
52+
CSC_LINK: ${{ secrets.MAC_CERT_BASE64 }}
53+
CSC_KEY_PASSWORD: ${{ secrets.MAC_CERT_PASSWORD }}
54+
run: npm run build:publish
55+
56+
- name: Windows Publish with npm
57+
if: ${{ matrix.os == 'windows-latest' }}
58+
env:
59+
CSC_LINK: ${{ secrets.WINDOWS_CERT_BASE64 }}
60+
CSC_KEY_PASSWORD: ${{ secrets.WINDOWS_CERT_PASSWORD }}
61+
run: npm run build:publish
62+
63+
- name: Linux Publish with npm
64+
if: ${{ matrix.os == 'ubuntu-latest' }}
65+
env:
66+
CSC_LINK: ${{ secrets.LINUX_CERT_BASE64 }}
67+
CSC_KEY_PASSWORD: ${{ secrets.LINUX_CERT_PASSWORD }}
68+
run: npm run build:publish

electron-builder.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,18 @@ export default <Configuration>{
2828
'main.js',
2929
// 预加载文件
3030
'preload.js',
31+
// 更新配置文件
32+
'updater.js'
3133
],
3234
// 生成资源的目录
3335
directories: {
3436
output: 'release/${version}',
3537
},
38+
linux: {
39+
target: [
40+
{
41+
target: 'AppImage',
42+
}
43+
]
44+
}
3645
}

main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import log from 'electron-log'
77
import Store from 'electron-store'
88
import fs from 'fs/promises'
99

10+
import './updater.js'
11+
1012
const store = new Store()
1113

1214
const __dirname = fileURLToPath(new URL('.', import.meta.url))

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"dev": "vite",
1818
"build-only": "vite build",
1919
"build": "run-p type-check \"build-only {@}\" -- && electron-builder",
20+
"build:publish": "npm run build -- --publish always",
2021
"preview": "vite preview",
2122
"test:unit": "vitest",
2223
"test:e2e": "playwright test",
@@ -35,6 +36,7 @@
3536
"electron-builder",
3637
"electron-log",
3738
"electron-store",
39+
"electron-updater",
3840
"pinia",
3941
"playwright",
4042
"typescript",
@@ -45,7 +47,8 @@
4547
],
4648
"dependencies": {
4749
"electron-log": "5.3.4",
48-
"electron-store": "10.0.1"
50+
"electron-store": "10.0.1",
51+
"electron-updater": "6.6.2"
4952
},
5053
"devDependencies": {
5154
"@playwright/test": "1.52.0",

updater.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { app } from 'electron'
2+
import log from 'electron-log'
3+
import electronUpdater from 'electron-updater'
4+
5+
const logUpdater = log.create({ logId: 'updater' })
6+
logUpdater.transports.file.fileName = 'updater.log'
7+
logUpdater.scope.defaultLabel = 'updater'
8+
logUpdater.scope.labelPadding = 8
9+
10+
const { autoUpdater } = electronUpdater
11+
12+
app.on('ready', function () {
13+
logUpdater.info('App is ready')
14+
15+
// 使用 electron-log 的日志接管 autoUpdater 的日志
16+
autoUpdater.logger = logUpdater
17+
18+
// 强制开发中检查更新(需要在项目根目录中添加 dev-app-update.yml 文件,可参考打包安装后安装目录下的 app-update.yml 并做相应的修改,用于指定开发中检查更新的配置)
19+
// autoUpdater.forceDevUpdateConfig = true
20+
21+
if (autoUpdater.forceDevUpdateConfig !== true) {
22+
// 未强制开启检查更新
23+
24+
if (process.env.NODE_ENV === 'development') {
25+
// 开发环境、未强制开启检查更新,则不执行以下代码,直接结束
26+
return
27+
}
28+
if (process.env.NODE_ENV === 'preview') {
29+
// 预览环境、未强制开启检查更新,则不执行以下代码,直接结束
30+
return
31+
}
32+
}
33+
34+
// 检查更新:启动程序后立即执行
35+
autoUpdater.checkForUpdates().then((updateCheckResult) => {
36+
logUpdater.info('UpdateCheckResult.updateInfo:\n', updateCheckResult.updateInfo)
37+
})
38+
})

0 commit comments

Comments
 (0)