Skip to content

Commit f40ca4e

Browse files
authored
feat: add auto-update support to text-diff-view (#32)
* feat: add auto-update support to text-diff-view and browser-space - Add electron-updater to both projects - Initialize autoUpdater in main process (disabled in dev) - Expose installUpdate via preload IPC bridge - Add update notification banner (bottom-right) in renderer - Set publish provider to GitHub Releases in electron-builder.yml - Add latest.yml / blockmap to release workflow artifacts - Create dev-app-update.yml for browser-space * 1.7.0
1 parent c4cf238 commit f40ca4e

8 files changed

Lines changed: 64 additions & 5 deletions

File tree

.github/workflows/electron-build.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ on:
88
jobs:
99
release:
1010
runs-on: ${{ matrix.os }}
11+
permissions:
12+
contents: write
1113

1214
strategy:
1315
matrix:
@@ -57,5 +59,9 @@ jobs:
5759
dist/*.exe
5860
dist/*.snap
5961
dist/*.AppImage
62+
dist/latest.yml
63+
dist/latest-mac.yml
64+
dist/latest-linux.yml
65+
dist/*.blockmap
6066
env:
6167
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

electron-builder.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ appImage:
4343
artifactName: ${name}-${version}.${ext}
4444
npmRebuild: false
4545
publish:
46-
provider: generic
47-
url: https://example.com/auto-updates
46+
provider: github
47+
owner: kaishuu0123
48+
repo: text-diff-view
4849
electronDownload:
4950
mirror: https://npmmirror.com/mirrors/electron/

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "text-diff-view",
3-
"version": "1.6.1",
3+
"version": "1.7.0",
44
"description": "text diff view electron app. Text-only.",
55
"type": "module",
66
"main": "./out/main/index.js",

src/main/index.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import pkg from 'electron-updater'
2+
const { autoUpdater } = pkg
13
import { app, shell, BrowserWindow, ipcMain, Menu } from 'electron'
24
import { join } from 'path'
35
import { electronApp, optimizer, is } from '@electron-toolkit/utils'
@@ -27,12 +29,32 @@ if (process.platform !== 'darwin') {
2729
Menu.setApplicationMenu(null)
2830
}
2931

32+
let mainWindow: BrowserWindow
33+
34+
export function initAutoUpdater(win: BrowserWindow): void {
35+
// 開発環境では動かさない
36+
if (!app.isPackaged) return
37+
38+
autoUpdater.autoDownload = true
39+
autoUpdater.autoInstallOnAppQuit = true
40+
41+
autoUpdater.on('update-downloaded', (info) => {
42+
win.webContents.send('update-downloaded', info)
43+
})
44+
45+
autoUpdater.on('error', (err) => {
46+
console.error('AutoUpdater error:', err)
47+
})
48+
49+
autoUpdater.checkForUpdates()
50+
}
51+
3052
let resizeTimeout: NodeJS.Timeout | null = null
3153
let moveTimeout: NodeJS.Timeout | null = null
3254

3355
function createWindow(): void {
3456
// Create the browser window.
35-
const mainWindow = new BrowserWindow({
57+
mainWindow = new BrowserWindow({
3658
width: store.get('windowWidth') || 800,
3759
height: store.get('windowHeight') || 600,
3860
x: store.get('windowX'),
@@ -115,8 +137,14 @@ app.whenReady().then(() => {
115137
store.set('themeName', data)
116138
})
117139

140+
ipcMain.on('install-update', () => {
141+
autoUpdater.quitAndInstall()
142+
})
143+
118144
createWindow()
119145

146+
initAutoUpdater(mainWindow)
147+
120148
app.on('activate', function () {
121149
// On macOS it's common to re-create a window in the app when the
122150
// dock icon is clicked and there are no other windows open.

src/preload/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ declare global {
66
api: {
77
GetThemeName: () => Promise<string | null>
88
SetThemeName: (themeName: string) => Promise<void>
9+
installUpdate: () => void
910
}
1011
}
1112
}

src/preload/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ const api = {
88
},
99
SetThemeName: (themeName: string): Promise<boolean> => {
1010
return ipcRenderer.invoke('SetThemeName', themeName)
11+
},
12+
installUpdate: (): void => {
13+
ipcRenderer.send('install-update')
1114
}
1215
}
1316

src/renderer/src/App.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ function App(): JSX.Element {
4747
getThemeColorByThemeName('light')
4848
)
4949
const [unifiedDiffDialogOpen, setUnifiedDiffDialogOpen] = useState(false)
50+
const [updateInfo, setUpdateInfo] = useState<{ version: string } | null>(null)
5051

5152
const options: monaco.editor.IDiffEditorOptions = {
5253
fontSize: 14,
@@ -109,6 +110,12 @@ function App(): JSX.Element {
109110
})()
110111
}, [])
111112

113+
useEffect(() => {
114+
window.electron.ipcRenderer.on('update-downloaded', (_event, info) => {
115+
setUpdateInfo(info)
116+
})
117+
}, [])
118+
112119
return (
113120
<div
114121
id="app"
@@ -192,6 +199,18 @@ function App(): JSX.Element {
192199
diffEditor={currentEditor.current}
193200
themeName={selectedTheme.name}
194201
/>
202+
203+
{updateInfo && (
204+
<div className="fixed bottom-4 right-4 z-50 bg-background border border-border text-foreground p-3 rounded-lg shadow-md flex gap-3 items-center">
205+
<span className="text-sm">アップデート v{updateInfo.version} が利用可能です</span>
206+
<Button size="sm" variant="default" onClick={() => window.api.installUpdate()}>
207+
再起動して更新
208+
</Button>
209+
<Button size="sm" variant="ghost" onClick={() => setUpdateInfo(null)}>
210+
後で
211+
</Button>
212+
</div>
213+
)}
195214
</div>
196215
)
197216
}

src/renderer/src/main.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ if (window.api === undefined) {
1212
},
1313
SetThemeName: async (themeName: string): Promise<void> => {
1414
localStorage.setItem('themeName', themeName)
15-
}
15+
},
16+
installUpdate: (): void => {}
1617
}
1718
}
1819

0 commit comments

Comments
 (0)