-
-
Notifications
You must be signed in to change notification settings - Fork 119
feat(live-update): add Electron platform support #921
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
robingenz
wants to merge
8
commits into
main
Choose a base branch
from
feat/live-update-electron-platform
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
670bc28
feat(live-update): add Electron platform support
robingenz 18cd192
test(live-update): add Electron example wiring and e2e suite
robingenz 2201d6d
fix(live-update): resolve engine and electron types without sibling c…
robingenz 5cd3054
refactor(live-update): adopt ElectronPlugin base class and derive ver…
robingenz 24d0a30
test(live-update): harden the e2e mock server
robingenz c495d19
docs(live-update): document versioned channels on Electron
robingenz 6708fd7
docs(live-update): use default JSON import in versioned channel example
robingenz c9d6b41
fix(live-update): drop manifest support on Electron
robingenz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@capawesome/capacitor-live-update": minor | ||
| --- | ||
|
|
||
| feat: add support for the Electron platform via [`@capawesome/capacitor-electron`](https://github.com/capawesome-team/capacitor-electron) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| export default { | ||
| input: 'electron/dist/esm/electron/src/index.js', | ||
| output: { | ||
| file: 'electron/dist/plugin.mjs', | ||
| format: 'esm', | ||
| }, | ||
| external: [ | ||
| 'electron', | ||
| '@capacitor/core', | ||
| '@capawesome/capacitor-electron/plugin', | ||
| '@capawesome/electron-live-update/engine', | ||
| /^node:/, | ||
| ], | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,289 @@ | ||
| import type { PluginsConfig } from '@capacitor/cli'; | ||
| import { CapacitorException, ExceptionCode } from '@capacitor/core'; | ||
| import { ElectronPlugin } from '@capawesome/capacitor-electron/plugin'; | ||
| import type { ElectronPluginContext } from '@capawesome/capacitor-electron/plugin'; | ||
| import { LiveUpdateEngine } from '@capawesome/electron-live-update/engine'; | ||
| import { app, powerMonitor } from 'electron'; | ||
| import { createRequire } from 'node:module'; | ||
| import { join } from 'node:path'; | ||
|
|
||
| import type { | ||
| DeleteBundleOptions, | ||
| DownloadBundleOptions, | ||
| FetchChannelsOptions, | ||
| FetchChannelsResult, | ||
| FetchLatestBundleOptions, | ||
| FetchLatestBundleResult, | ||
| GetBlockedBundlesResult, | ||
| GetBundlesResult, | ||
| GetChannelResult, | ||
| GetConfigResult, | ||
| GetCurrentBundleResult, | ||
| GetCustomIdResult, | ||
| GetDeviceIdResult, | ||
| GetDownloadedBundlesResult, | ||
| GetNextBundleResult, | ||
| GetVersionCodeResult, | ||
| GetVersionNameResult, | ||
| IsSyncingResult, | ||
| LiveUpdatePlugin, | ||
| ReadyResult, | ||
| SetChannelOptions, | ||
| SetConfigOptions, | ||
| SetCustomIdOptions, | ||
| SetNextBundleOptions, | ||
| SyncOptions, | ||
| SyncResult, | ||
| } from '../../src/definitions'; | ||
|
|
||
| const AUTO_UPDATE_MIN_INTERVAL = 15 * 60 * 1000; | ||
| const CAPACITOR_RUNTIME = 'capacitor'; | ||
| const ELECTRON_PLATFORM = '2'; | ||
| // Resolve the plugin version from the package's own `package.json`. The built | ||
| // file lives at `electron/dist/plugin.mjs`, so the package root is two levels up. | ||
| const PLUGIN_VERSION: string = createRequire(import.meta.url)( | ||
| '../../package.json', | ||
| ).version; | ||
|
|
||
| type LiveUpdateConfig = NonNullable<PluginsConfig['LiveUpdate']>; | ||
|
|
||
| export class LiveUpdateElectron | ||
| extends ElectronPlugin | ||
| implements Omit<LiveUpdatePlugin, 'addListener' | 'removeAllListeners'> | ||
| { | ||
| public static readonly __capacitorElectronPlugin = { | ||
| name: 'LiveUpdate', | ||
| methods: [ | ||
| 'clearBlockedBundles', | ||
| 'deleteBundle', | ||
| 'downloadBundle', | ||
| 'fetchChannels', | ||
| 'fetchLatestBundle', | ||
| 'getBlockedBundles', | ||
| 'getBundles', | ||
| 'getChannel', | ||
| 'getConfig', | ||
| 'getCurrentBundle', | ||
| 'getCustomId', | ||
| 'getDeviceId', | ||
| 'getDownloadedBundles', | ||
| 'getNextBundle', | ||
| 'getVersionCode', | ||
| 'getVersionName', | ||
| 'isSyncing', | ||
| 'ready', | ||
| 'reload', | ||
| 'reset', | ||
| 'resetConfig', | ||
| 'setChannel', | ||
| 'setConfig', | ||
| 'setCustomId', | ||
| 'setNextBundle', | ||
| 'sync', | ||
| ], | ||
| }; | ||
|
|
||
| private static readonly errorNotImplemented = 'Not implemented on Electron.'; | ||
|
|
||
| private readonly config: LiveUpdateConfig; | ||
| private readonly engine: LiveUpdateEngine; | ||
| private lastAutoUpdateCheck = 0; | ||
|
|
||
| constructor(context: ElectronPluginContext) { | ||
| super(context); | ||
| this.config = | ||
| (context.config.plugins as PluginsConfig | undefined)?.LiveUpdate ?? {}; | ||
| this.engine = new LiveUpdateEngine({ | ||
| appId: this.config.appId, | ||
| autoBlockRolledBackBundles: this.config.autoBlockRolledBackBundles, | ||
| autoDeleteBundles: this.config.autoDeleteBundles, | ||
| dataDirectory: join(app.getPath('userData'), 'capawesome-live-update'), | ||
| defaultChannel: this.config.defaultChannel, | ||
| httpTimeout: this.config.httpTimeout, | ||
| osVersion: process.getSystemVersion(), | ||
| platform: ELECTRON_PLATFORM, | ||
| pluginVersion: PLUGIN_VERSION, | ||
| publicKey: this.config.publicKey, | ||
| readyTimeout: this.config.readyTimeout, | ||
| runtime: CAPACITOR_RUNTIME, | ||
| sdkVersion: PLUGIN_VERSION, | ||
| serverDomain: this.config.serverDomain, | ||
| versionCode: app.getVersion(), | ||
| versionName: app.getVersion(), | ||
| }); | ||
| this.engine.on('downloadBundleProgress', event => | ||
| this.context.notifyListeners('downloadBundleProgress', event), | ||
| ); | ||
| this.engine.on('nextBundleSet', event => | ||
| this.context.notifyListeners('nextBundleSet', event), | ||
| ); | ||
| // The engine owns rollback. When its ready watchdog rolls back, | ||
| // repoint the platform so that all windows reload the target bundle. | ||
| this.engine.on('rolledBack', () => { | ||
| void this.applyCurrentBundle().catch(error => | ||
| console.error(`[LiveUpdate] Failed to apply rollback: ${error}`), | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| public async clearBlockedBundles(): Promise<void> { | ||
| return this.engine.clearBlockedBundles(); | ||
| } | ||
|
|
||
| public async deleteBundle(options: DeleteBundleOptions): Promise<void> { | ||
| return this.engine.deleteBundle(options); | ||
| } | ||
|
|
||
| public async downloadBundle(options: DownloadBundleOptions): Promise<void> { | ||
| return this.engine.downloadBundle(options); | ||
| } | ||
|
|
||
| public async fetchChannels( | ||
| options?: FetchChannelsOptions, | ||
| ): Promise<FetchChannelsResult> { | ||
| return this.engine.fetchChannels(options); | ||
| } | ||
|
|
||
| public async fetchLatestBundle( | ||
| options?: FetchLatestBundleOptions, | ||
| ): Promise<FetchLatestBundleResult> { | ||
| return this.engine.fetchLatestBundle(options); | ||
| } | ||
|
|
||
| public async getBlockedBundles(): Promise<GetBlockedBundlesResult> { | ||
| return this.engine.getBlockedBundles(); | ||
| } | ||
|
|
||
| public async getBundles(): Promise<GetBundlesResult> { | ||
| return this.getDownloadedBundles(); | ||
| } | ||
|
|
||
| public async getChannel(): Promise<GetChannelResult> { | ||
| return this.engine.getChannel(); | ||
| } | ||
|
|
||
| public async getConfig(): Promise<GetConfigResult> { | ||
| return { | ||
| appId: this.config.appId ?? null, | ||
| autoUpdateStrategy: this.config.autoUpdateStrategy ?? 'none', | ||
| }; | ||
| } | ||
|
|
||
| public async getCurrentBundle(): Promise<GetCurrentBundleResult> { | ||
| return this.engine.getCurrentBundle(); | ||
| } | ||
|
|
||
| public async getCustomId(): Promise<GetCustomIdResult> { | ||
| return this.engine.getCustomId(); | ||
| } | ||
|
|
||
| public async getDeviceId(): Promise<GetDeviceIdResult> { | ||
| return this.engine.getDeviceId(); | ||
| } | ||
|
|
||
| public async getDownloadedBundles(): Promise<GetDownloadedBundlesResult> { | ||
| return this.engine.getDownloadedBundles(); | ||
| } | ||
|
|
||
| public async getNextBundle(): Promise<GetNextBundleResult> { | ||
| return this.engine.getNextBundle(); | ||
| } | ||
|
|
||
| public async getVersionCode(): Promise<GetVersionCodeResult> { | ||
| return this.engine.getVersionCode(); | ||
| } | ||
|
|
||
| public async getVersionName(): Promise<GetVersionNameResult> { | ||
| return this.engine.getVersionName(); | ||
| } | ||
|
|
||
| public async load(): Promise<void> { | ||
| await this.engine.initialize(); | ||
| const bundlePath = this.engine.getCurrentBundlePath(); | ||
| if ( | ||
| bundlePath !== null || | ||
| this.context.services.bundles.getActiveBundlePath() !== null | ||
| ) { | ||
| await this.context.services.bundles.setActiveBundle(bundlePath, { | ||
| bootWatchdog: false, | ||
| }); | ||
| } | ||
| if (this.config.autoUpdateStrategy === 'background') { | ||
| this.setUpBackgroundAutoUpdate(); | ||
| } | ||
| } | ||
|
|
||
| public async isSyncing(): Promise<IsSyncingResult> { | ||
| return this.engine.isSyncing(); | ||
| } | ||
|
|
||
| public async ready(): Promise<ReadyResult> { | ||
| return this.engine.ready(); | ||
| } | ||
|
|
||
| public async reload(): Promise<void> { | ||
| await this.engine.applyNextBundle(); | ||
| await this.applyCurrentBundle(); | ||
| this.context.notifyListeners('reloaded'); | ||
| } | ||
|
|
||
| public async reset(): Promise<void> { | ||
| return this.engine.reset(); | ||
| } | ||
|
|
||
| public async resetConfig(): Promise<void> { | ||
| this.throwUnimplementedError(); | ||
| } | ||
|
|
||
| public async setChannel(options: SetChannelOptions): Promise<void> { | ||
| return this.engine.setChannel(options); | ||
| } | ||
|
|
||
| public async setConfig(_options: SetConfigOptions): Promise<void> { | ||
| this.throwUnimplementedError(); | ||
| } | ||
|
|
||
| public async setCustomId(options: SetCustomIdOptions): Promise<void> { | ||
| return this.engine.setCustomId(options); | ||
| } | ||
|
|
||
| public async setNextBundle(options: SetNextBundleOptions): Promise<void> { | ||
| return this.engine.setNextBundle(options); | ||
| } | ||
|
|
||
| public async sync(options?: SyncOptions): Promise<SyncResult> { | ||
| return this.engine.sync(options); | ||
| } | ||
|
|
||
| private async applyCurrentBundle(): Promise<void> { | ||
| await this.context.services.bundles.setActiveBundle( | ||
| this.engine.getCurrentBundlePath(), | ||
| { bootWatchdog: false }, | ||
| ); | ||
| } | ||
|
|
||
| private setUpBackgroundAutoUpdate(): void { | ||
| const check = (): void => { | ||
| const now = Date.now(); | ||
| if (now - this.lastAutoUpdateCheck < AUTO_UPDATE_MIN_INTERVAL) { | ||
| return; | ||
| } | ||
| this.lastAutoUpdateCheck = now; | ||
| void this.sync().catch(error => | ||
| console.warn(`[LiveUpdate] Background sync failed: ${error}`), | ||
| ); | ||
| }; | ||
| void app.whenReady().then(() => { | ||
| check(); | ||
| app.on('browser-window-focus', check); | ||
| powerMonitor.on('resume', check); | ||
| }); | ||
| } | ||
|
|
||
| private throwUnimplementedError(): never { | ||
| throw new CapacitorException( | ||
| LiveUpdateElectron.errorNotImplemented, | ||
| ExceptionCode.Unimplemented, | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "allowUnreachableCode": false, | ||
| "baseUrl": ".", | ||
| "esModuleInterop": true, | ||
| "lib": ["es2022"], | ||
| "module": "esnext", | ||
| "moduleResolution": "bundler", | ||
| "noFallthroughCasesInSwitch": true, | ||
| "noUnusedLocals": true, | ||
| "noUnusedParameters": true, | ||
| "outDir": "dist/esm", | ||
| "paths": { | ||
| // TypeScript tries each candidate in order and uses the first one | ||
| // that exists on disk. Once `@capawesome/electron-live-update` is | ||
| // published, the installed package (package-level or hoisted to the | ||
| // workspace root `node_modules`) is used; the sibling repository | ||
| // checkout is only a fallback for local development before then. | ||
| "@capawesome/electron-live-update/engine": [ | ||
| "../node_modules/@capawesome/electron-live-update/dist/engine/index.d.ts", | ||
| "../../../node_modules/@capawesome/electron-live-update/dist/engine/index.d.ts", | ||
| "../../../../electron-live-update/dist/engine/index.d.ts" | ||
| ], | ||
| "@capawesome/capacitor-electron/plugin": [ | ||
| "../node_modules/@capawesome/capacitor-electron/dist/types/plugin/index.d.ts", | ||
| "../../../node_modules/@capawesome/capacitor-electron/dist/types/plugin/index.d.ts", | ||
| "../../../../capacitor-electron/dist/types/plugin/index.d.ts" | ||
| ] | ||
| }, | ||
| "pretty": true, | ||
| "rootDir": "..", | ||
| "skipLibCheck": true, | ||
| "strict": true, | ||
| "target": "es2022", | ||
| "types": [] | ||
| }, | ||
| "include": ["src/**/*.ts"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| node_modules | ||
| build | ||
| dist | ||
| app | ||
| generated | ||
| vendor |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| # Build resources | ||
|
|
||
| Place your app icons here for packaging with electron-builder: | ||
|
|
||
| - `icon.icns` — macOS (or a 1024x1024 `icon.png`, converted automatically) | ||
| - `icon.ico` — Windows | ||
| - `icon.png` — Linux (512x512 or larger) | ||
|
|
||
| See https://www.electron.build/configuration/icons for details. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.