|
| 1 | +import { autoUpdater } from 'electron-updater' |
| 2 | +import { ProgressInfo } from 'electron-builder-http/out/ProgressCallbackTransform' |
| 3 | +import * as log from 'electron-log' |
| 4 | +import * as isDev from 'electron-is-dev' |
| 5 | + |
| 6 | +/** |
| 7 | + * Should we peform the auto-update check? |
| 8 | + */ |
| 9 | +const shouldCheck = !isDev |
| 10 | + |
| 11 | +/* |
| 12 | +# Testing In Development |
| 13 | +
|
| 14 | +You may want to enable this in dev-mode the ensure everything |
| 15 | +is working properly. To do this, you'll need to have a config |
| 16 | +file called "dev-app-update.yml" in the project's root. |
| 17 | +
|
| 18 | +If you're shipping via S3 or another provider, please the |
| 19 | +[electron-builder docs](https://github.com/electron-userland/electron-builder/wiki/Publishing-Artifacts). |
| 20 | +
|
| 21 | +# Example File |
| 22 | +
|
| 23 | +```yml |
| 24 | +provider: 'github' |
| 25 | +repo: 'electron-starter' |
| 26 | +owner: 'skellock' |
| 27 | +``` |
| 28 | +
|
| 29 | +Heads up: dev-app-update.yml is in the .gitignore file. |
| 30 | +*/ |
| 31 | + |
| 32 | +/** |
| 33 | + * Setup the auto-update capabilities. |
| 34 | + * |
| 35 | + * @param app The electron app. |
| 36 | + */ |
| 37 | +export function createUpdater(app: Electron.App): void { |
| 38 | + // jet if we shoulldn't be here |
| 39 | + if (!shouldCheck) return |
| 40 | + |
| 41 | + // configure the autoUpdater's logger |
| 42 | + autoUpdater.logger = log |
| 43 | + |
| 44 | + // fires when the app is ready |
| 45 | + app.on('ready', () => { |
| 46 | + autoUpdater.checkForUpdates() |
| 47 | + }) |
| 48 | + |
| 49 | + autoUpdater.on('checking-for-update', () => { |
| 50 | + log.info('checking for update') |
| 51 | + }) |
| 52 | + |
| 53 | + autoUpdater.on('update-available', info => { |
| 54 | + log.info('update available') |
| 55 | + }) |
| 56 | + |
| 57 | + autoUpdater.on('update-not-available', info => { |
| 58 | + log.info('update not available') |
| 59 | + }) |
| 60 | + |
| 61 | + autoUpdater.on('error', err => { |
| 62 | + log.error('error updating', err.message) |
| 63 | + }) |
| 64 | + |
| 65 | + autoUpdater.on('download-progress', (progressObj: ProgressInfo) => { |
| 66 | + log.info(`${progressObj.percent}%`) |
| 67 | + }) |
| 68 | + |
| 69 | + // fires when an update has been downloaded |
| 70 | + autoUpdater.on('update-downloaded', info => { |
| 71 | + log.info('update downloaded') |
| 72 | + autoUpdater.quitAndInstall() |
| 73 | + }) |
| 74 | +} |
0 commit comments