@pmate/cli 0.42.6 → 0.43.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -0
- package/dist/index.d.ts +9 -9
- package/dist/index.js +309 -393
- package/dist/templates/electron/automatic-updates.cjs +83 -0
- package/dist/templates/electron/main.cjs +3 -2
- package/dist/templates/electron/preload.cjs +6 -1
- package/dist/templates/electron/updates.cjs +31 -3
- package/package.json +2 -1
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
const { readFileSync, writeFileSync, renameSync, mkdirSync } = require('node:fs')
|
|
2
|
+
const path = require('node:path')
|
|
3
|
+
/** Owns the updater in the main process. Renderer commands cannot supply URLs,
|
|
4
|
+
* install paths or versions; every download starts from the verified registry. */
|
|
5
|
+
function createAutomaticUpdates({ app, updater, findUpdate, appId, platform = process.platform, arch = process.arch, notify = () => {} }) {
|
|
6
|
+
updater.autoDownload = false
|
|
7
|
+
updater.autoInstallOnAppQuit = false
|
|
8
|
+
updater.allowDowngrade = false
|
|
9
|
+
updater.allowPrerelease = false
|
|
10
|
+
let state = { phase: 'idle', currentVersion: app.getVersion(), autoDownload: false }
|
|
11
|
+
let release, checking, downloading, cancellationToken, cancelRequested = false
|
|
12
|
+
const preferences = path.join(app.getPath('userData'), 'update-preferences.json')
|
|
13
|
+
try { state.autoDownload = JSON.parse(readFileSync(preferences, 'utf8')).autoDownload === true } catch {}
|
|
14
|
+
const publish = patch => { state = { ...state, ...patch }; notify({ ...state }); return { ...state } }
|
|
15
|
+
updater.on('download-progress', p => {
|
|
16
|
+
if (state.phase === 'downloading') publish({ transferred: p.transferred, total: p.total, percent: Math.max(0, Math.min(100, p.percent)) })
|
|
17
|
+
})
|
|
18
|
+
updater.on('error', () => {
|
|
19
|
+
if (['downloading', 'installing'].includes(state.phase)) publish({ phase: 'available', error: 'Update failed. Please try again.' })
|
|
20
|
+
})
|
|
21
|
+
async function check() {
|
|
22
|
+
if (['downloading', 'ready', 'installing'].includes(state.phase)) return state.version ? { version: state.version } : null
|
|
23
|
+
if (checking) return checking
|
|
24
|
+
checking = (async () => {
|
|
25
|
+
publish({ phase: 'checking', error: undefined })
|
|
26
|
+
try {
|
|
27
|
+
release = await findUpdate({ appId, currentVersion: app.getVersion(), platform, arch })
|
|
28
|
+
publish({ phase: release ? 'available' : 'current', version: release?.version, total: release?.size, percent: undefined, transferred: undefined })
|
|
29
|
+
if (release && state.autoDownload) void download().catch(() => {})
|
|
30
|
+
return release ? { version: release.version } : null
|
|
31
|
+
} catch (error) { publish({ phase: 'error', error: 'Could not check for updates. Please try again.' }); throw error }
|
|
32
|
+
finally { checking = undefined }
|
|
33
|
+
})()
|
|
34
|
+
return checking
|
|
35
|
+
}
|
|
36
|
+
async function download() {
|
|
37
|
+
if (downloading) return downloading
|
|
38
|
+
if (state.phase === 'ready') return true
|
|
39
|
+
if (!release || state.phase !== 'available') throw Error('Check for updates first')
|
|
40
|
+
cancelRequested = false
|
|
41
|
+
const selected = release
|
|
42
|
+
downloading = (async () => {
|
|
43
|
+
publish({ phase: 'downloading', percent: 0, transferred: 0, error: undefined })
|
|
44
|
+
try {
|
|
45
|
+
if (!selected.updaterFeed) throw Error('This release does not support automatic installation')
|
|
46
|
+
updater.setFeedURL({ provider: 'generic', url: selected.updaterFeed })
|
|
47
|
+
const result = await updater.checkForUpdates()
|
|
48
|
+
if (!result?.updateInfo || result.updateInfo.version !== selected.version) throw Error('Update version does not match registry')
|
|
49
|
+
cancellationToken = result.cancellationToken
|
|
50
|
+
if (cancelRequested) { cancellationToken?.cancel(); return false }
|
|
51
|
+
await updater.downloadUpdate(cancellationToken)
|
|
52
|
+
if (cancelRequested) return false
|
|
53
|
+
publish({ phase: 'ready', percent: 100, error: undefined })
|
|
54
|
+
return true
|
|
55
|
+
} catch (error) { publish({ phase: 'available', error: cancelRequested ? undefined : 'Could not download the update. Please try again.' }); if (cancelRequested) return false; throw error }
|
|
56
|
+
finally { downloading = undefined; cancellationToken = undefined }
|
|
57
|
+
})()
|
|
58
|
+
return downloading
|
|
59
|
+
}
|
|
60
|
+
function cancel() {
|
|
61
|
+
if (state.phase !== 'downloading') return false
|
|
62
|
+
cancelRequested = true; cancellationToken?.cancel()
|
|
63
|
+
publish({ phase: 'available', error: undefined, percent: undefined, transferred: undefined })
|
|
64
|
+
return true
|
|
65
|
+
}
|
|
66
|
+
function install() {
|
|
67
|
+
if (state.phase !== 'ready') throw Error('Update is not ready to install')
|
|
68
|
+
publish({ phase: 'installing', error: undefined })
|
|
69
|
+
try { updater.quitAndInstall(false, true); return true }
|
|
70
|
+
catch (error) { publish({ phase: 'ready', error: 'Could not restart to install. Please try again.' }); throw error }
|
|
71
|
+
}
|
|
72
|
+
function setAutoDownload(value) {
|
|
73
|
+
if (typeof value !== 'boolean') throw Error('Expected a boolean preference')
|
|
74
|
+
mkdirSync(path.dirname(preferences), { recursive: true })
|
|
75
|
+
const temporary = preferences + '.tmp'
|
|
76
|
+
writeFileSync(temporary, JSON.stringify({ autoDownload: value }), { mode: 0o600 }); renameSync(temporary, preferences)
|
|
77
|
+
publish({ autoDownload: value })
|
|
78
|
+
if (value && state.phase === 'available') void download().catch(() => {})
|
|
79
|
+
return { ...state }
|
|
80
|
+
}
|
|
81
|
+
return { check, download, cancel, install, getState: () => ({ ...state }), setAutoDownload }
|
|
82
|
+
}
|
|
83
|
+
module.exports = { createAutomaticUpdates }
|
|
@@ -45,7 +45,8 @@ else {
|
|
|
45
45
|
return false
|
|
46
46
|
})
|
|
47
47
|
installNotificationSettings({ ipcMain, shell, origin: config.origin, getWindow: () => mainWindow })
|
|
48
|
-
|
|
48
|
+
let updateController
|
|
49
|
+
const checkForUpdates = createUpdateChecker({ app, dialog, shell, appId: config.app, getController: () => updateController })
|
|
49
50
|
Menu.setApplicationMenu(Menu.buildFromTemplate([
|
|
50
51
|
{ role: 'appMenu' }, { role: 'editMenu' }, { role: 'viewMenu' }, { role: 'windowMenu' },
|
|
51
52
|
{ label: config.productName, submenu: [{ label: '检查更新…', click: checkForUpdates }, { type: 'separator' }, { label: 'Back to app', click: () => { if (!mainWindow) createWindow(); else void mainWindow.loadURL(config.origin).catch(error => console.error("Page load failed:", error.message)) } }] },
|
|
@@ -58,7 +59,7 @@ else {
|
|
|
58
59
|
} })
|
|
59
60
|
desktopApps = installDesktopApps({ ipcMain, BrowserWindow, session: desktopSession, config, installNavigationRestrictions,
|
|
60
61
|
openExternal: url => shell.openExternal(url), trusted: event => desktopNotifications.trusted(event) })
|
|
61
|
-
installUpdateHandlers({ ipcMain, app, shell, appId: config.app, trusted: event => desktopNotifications.trusted(event) })
|
|
62
|
+
updateController = installUpdateHandlers({ ipcMain, app, shell, appId: config.app, trusted: event => desktopNotifications.trusted(event), updater: require('electron-updater').autoUpdater, getWindow: () => mainWindow })
|
|
62
63
|
const instanceId = desktopInstanceId(app.getPath('userData'))
|
|
63
64
|
const rendererReach = createRendererReach({ ipcMain, getWindow: () => mainWindow, trusted: event => desktopNotifications.trusted(event) })
|
|
64
65
|
if (supportsDesktopCalls(config.app)) desktopCalls = createDesktopCalls({ appId: config.app, instanceId, origin: config.origin, BrowserWindow, ipcMain, dialog })
|
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
const { contextBridge, ipcRenderer } = require('electron')
|
|
2
2
|
const origin = process.argv.find(value => value.startsWith('--pmate-origin='))?.slice('--pmate-origin='.length)
|
|
3
3
|
if (process.isMainFrame && location.origin === origin) contextBridge.exposeInMainWorld('pmateDesktop', Object.freeze({
|
|
4
|
-
runtime: Object.freeze({ platform: process.platform, appVersion: process.argv.find(value => value.startsWith('--pmate-app-version='))?.slice('--pmate-app-version='.length), electronVersion: process.versions.electron, bridgeVersion: '1.
|
|
4
|
+
runtime: Object.freeze({ platform: process.platform, appVersion: process.argv.find(value => value.startsWith('--pmate-app-version='))?.slice('--pmate-app-version='.length), electronVersion: process.versions.electron, bridgeVersion: '1.2.0', contentSource: 'remote' }),
|
|
5
5
|
updates: Object.freeze({
|
|
6
6
|
check: () => ipcRenderer.invoke('pmate:update:check'),
|
|
7
7
|
download: () => ipcRenderer.invoke('pmate:update:download'),
|
|
8
|
+
install: () => ipcRenderer.invoke('pmate:update:install'),
|
|
9
|
+
cancel: () => ipcRenderer.invoke('pmate:update:cancel'),
|
|
10
|
+
getState: () => ipcRenderer.invoke('pmate:update:state'),
|
|
11
|
+
setAutoDownload: value => ipcRenderer.invoke('pmate:update:preference', value),
|
|
12
|
+
onState: handler => { const listener = (_event, state) => handler(state); ipcRenderer.on('pmate:update:state', listener); return () => ipcRenderer.removeListener('pmate:update:state', listener) },
|
|
8
13
|
}),
|
|
9
14
|
openScreenSettings: () => ipcRenderer.invoke('pmate:screen-settings'),
|
|
10
15
|
apps: Object.freeze({
|
|
@@ -26,14 +26,30 @@ async function findUpdate({ appId, currentVersion, platform, arch, fetchImpl = f
|
|
|
26
26
|
const extension = target === 'mac' ? '.dmg' : '.exe'
|
|
27
27
|
const installer = manifest.artifacts.find(item => typeof item.name === 'string' && item.name.toLowerCase().endsWith(extension))
|
|
28
28
|
if (!installer || /[/\\]/.test(installer.name) || installer.downloadUrl !== prefix + encodeURIComponent(installer.name) || !/^[a-f0-9]{64}$/.test(installer.sha256) || !Number.isSafeInteger(installer.size) || installer.size <= 0) throw new Error('Invalid update installer')
|
|
29
|
-
|
|
29
|
+
const metadataName = target === 'mac' ? 'latest-mac.yml' : 'latest.yml'
|
|
30
|
+
const metadata = manifest.artifacts.find(item => item.name === metadataName)
|
|
31
|
+
if (metadata && (metadata.downloadUrl !== prefix + metadataName || !/^[a-f0-9]{64}$/.test(metadata.sha256))) throw new Error('Invalid update metadata')
|
|
32
|
+
return { version: release.version, ...installer, ...(metadata ? { updaterFeed: prefix } : {}) }
|
|
30
33
|
}
|
|
31
|
-
function createUpdateChecker({ app, dialog, shell, appId, platform = process.platform, arch = process.arch, fetchImpl = fetch }) {
|
|
34
|
+
function createUpdateChecker({ app, dialog, shell, appId, platform = process.platform, arch = process.arch, fetchImpl = fetch, getController }) {
|
|
32
35
|
let checking = false
|
|
33
36
|
return async function checkForUpdates() {
|
|
34
37
|
if (checking) return
|
|
35
38
|
checking = true
|
|
36
39
|
try {
|
|
40
|
+
const controller = getController?.()
|
|
41
|
+
if (controller) {
|
|
42
|
+
await controller.check()
|
|
43
|
+
const state = controller.getState()
|
|
44
|
+
if (state.phase === 'ready') {
|
|
45
|
+
const choice = await dialog.showMessageBox({ type: 'info', title: '更新已准备好', message: `${app.getName()} ${state.version}`, detail: '重启后自动安装并重新打开应用。', buttons: ['重启并安装', '稍后'], defaultId: 0, cancelId: 1 })
|
|
46
|
+
if (choice.response === 0) controller.install()
|
|
47
|
+
} else if (state.phase === 'available') {
|
|
48
|
+
const choice = await dialog.showMessageBox({ type: 'info', title: '发现新版本', message: `${app.getName()} ${state.version}`, detail: '在应用内下载,准备好后可重启安装。', buttons: ['立即更新', '稍后'], defaultId: 0, cancelId: 1 })
|
|
49
|
+
if (choice.response === 0) await controller.download()
|
|
50
|
+
} else if (state.phase === 'current') await dialog.showMessageBox({ type: 'info', title: '检查更新', message: '已是最新版本', buttons: ['确定'] })
|
|
51
|
+
return
|
|
52
|
+
}
|
|
37
53
|
const currentVersion = app.getVersion()
|
|
38
54
|
const update = await findUpdate({ appId, currentVersion, platform, arch, fetchImpl })
|
|
39
55
|
if (!update) {
|
|
@@ -48,7 +64,19 @@ function createUpdateChecker({ app, dialog, shell, appId, platform = process.pla
|
|
|
48
64
|
} finally { checking = false }
|
|
49
65
|
}
|
|
50
66
|
}
|
|
51
|
-
function installUpdateHandlers({ ipcMain, app, shell, appId, trusted, platform = process.platform, arch = process.arch, fetchImpl = fetch }) {
|
|
67
|
+
function installUpdateHandlers({ ipcMain, app, shell, appId, trusted, platform = process.platform, arch = process.arch, fetchImpl = fetch, updater, getWindow }) {
|
|
68
|
+
if (updater) {
|
|
69
|
+
const { createAutomaticUpdates } = require('./automatic-updates.cjs')
|
|
70
|
+
const controller = createAutomaticUpdates({ app, updater, appId, platform, arch,
|
|
71
|
+
findUpdate: input => findUpdate({ ...input, fetchImpl }),
|
|
72
|
+
notify: state => { const w = getWindow?.(); if (w && !w.isDestroyed() && trusted({ sender: w.webContents, senderFrame: w.webContents.mainFrame })) w.webContents.send('pmate:update:state', state) },
|
|
73
|
+
})
|
|
74
|
+
for (const [name, method] of Object.entries({ check: 'check', download: 'download', cancel: 'cancel', install: 'install', state: 'getState', preference: 'setAutoDownload' })) {
|
|
75
|
+
ipcMain.handle('pmate:update:' + name, (event, value) => { if (!trusted(event)) throw Error('Untrusted update sender'); return controller[method](value) })
|
|
76
|
+
}
|
|
77
|
+
if (app.isPackaged) { const timer = setTimeout(() => { void controller.check().catch(() => {}) }, 15000); timer.unref() }
|
|
78
|
+
return controller
|
|
79
|
+
}
|
|
52
80
|
let checking
|
|
53
81
|
let available
|
|
54
82
|
ipcMain.handle('pmate:update:check', async event => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pmate/cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.43.0",
|
|
4
4
|
"description": "CLI utilities for the pMate project",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"bin": {
|
|
@@ -52,6 +52,7 @@
|
|
|
52
52
|
"@pmate/utils": "^1.1.4",
|
|
53
53
|
"@types/node": "^25.0.3",
|
|
54
54
|
"@types/pngjs": "^6.0.5",
|
|
55
|
+
"electron-updater": "6.8.3",
|
|
55
56
|
"rimraf": "^6.1.2",
|
|
56
57
|
"ts-node": "^10.9.2",
|
|
57
58
|
"tsup": "^8.5.0",
|