Skip to content

Commit 303f5cd

Browse files
committed
docs(guide/recipes): add auto update guide
1 parent ef03755 commit 303f5cd

File tree

1 file changed

+105
-15
lines changed

1 file changed

+105
-15
lines changed

docs/guide/recipes.md

Lines changed: 105 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,103 @@
44

55
[[toc]]
66

7+
## Auto Update
8+
9+
> Configure app updates with [electron-builder auto update](https://www.electron.build/auto-update)
10+
11+
[Example Repo](https://github.com/nklayman/electron-auto-update-example)
12+
13+
### Install Required Deps
14+
15+
First, install [electron-updater](https://www.npmjs.com/package/electron-updater):
16+
17+
With Yarn:
18+
19+
`yarn add electron-updater`
20+
21+
or with NPM:
22+
23+
`npm install electron-updater`
24+
25+
### Enable Publishing to GitHub
26+
27+
Add `publish: ['github']` to Electron Builder's config in your `vue.config.js`:
28+
29+
```js
30+
module.exports = {
31+
pluginOptions: {
32+
electronBuilder: {
33+
builderOptions: {
34+
publish: ['github']
35+
}
36+
}
37+
}
38+
}
39+
```
40+
41+
### Check for Updates in `background.(js|ts)`
42+
43+
Add the following to your main process file (`background.(js|ts)` by default):
44+
45+
```diff
46+
...
47+
+ import { autoUpdater } from "electron-updater"
48+
...
49+
50+
if (process.env.WEBPACK_DEV_SERVER_URL) {
51+
// Load the url of the dev server if in development mode
52+
win.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
53+
if (!process.env.IS_TEST) win.webContents.openDevTools()
54+
} else {
55+
createProtocol('app')
56+
// Load the index.html when not in development
57+
win.loadURL('app://./index.html')
58+
+ autoUpdater.checkForUpdatesAndNotify()
59+
}
60+
...
61+
```
62+
63+
### GitHub Personal Access Token
64+
65+
**Note:** You will need a GitHub personal access token for this step. To get one, go to [https://github.com/settings/tokens](https://github.com/settings/tokens) and click `Generate new token`.
66+
67+
In order for Electron Builder to upload a release to GitHub, you will need to make your token available by setting the `GH_TOKEN` env variable to your token:
68+
69+
On Linux/MacOS:
70+
71+
`export GH_TOKEN=TOKEN-GOES-HERE`
72+
73+
On Windows:
74+
75+
`set GH_TOKEN=TOKEN-GOES-HERE`
76+
77+
### Upload Release to GitHub
78+
79+
Now that you have configured everything, tell electron-builder to upload your app to GitHub by running `electron:build` with the `-p always` argument:
80+
81+
With Yarn:
82+
83+
`yarn electron:build -p always`
84+
85+
or with NPM:
86+
87+
`npm run electron:build -- -p always`
88+
89+
### Publish Release
90+
91+
Open your repo in GitHub, and click on the releases tab. You should see a draft of your new version with all the binaries included. Publish this release so users can update to it.
92+
93+
### Check for Updates
94+
95+
Install your app, then run it. You won't get an update notification yet, because this is the latest version. You will have to publish a new version by increasing the `version` field in your `package.json`, then repeating the 3 previous steps. Now, your old app should give you an update notification.
96+
797
## Icons
898

999
> Customize your app's launcher and tray icon
10100
11101
[Example Repo](https://github.com/nklayman/electron-icon-example)
12102

13-
#### Install Required Deps
103+
### Install Required Deps
14104

15105
First, add [electron-icon-builder](https://www.npmjs.com/package/electron-icon-builder) as a `devDependency`:
16106

@@ -22,19 +112,19 @@ or with NPM:
22112

23113
`npm install --save-dev electron-icon-builder`
24114

25-
#### Add Icon to App
115+
### Add Icon to App
26116

27117
Place your square icon in `public/icon.png`.
28118

29-
#### Add Generation Script
119+
### Add Generation Script
30120

31121
Add the following script to your `package.json`:
32122

33123
```json
34124
"electron:generate-icons": "electron-icon-builder --input=./public/icon.png --output=build --flatten"
35125
```
36126

37-
#### Generate Icons
127+
### Generate Icons
38128

39129
Run the new script:
40130

@@ -46,7 +136,7 @@ or with NPM:
46136

47137
`npm run electron:generate-icons`
48138

49-
#### Set Tray Icon
139+
### Set Tray Icon
50140

51141
Edit your background file (`src/background.(js|ts)` by default):
52142

@@ -74,11 +164,11 @@ If you get the linting error `'__static' is not defined`, add `/* global __stati
74164
75165
[Example Repo](https://github.com/nklayman/electron-multipage-example)
76166

77-
#### Add Your Pages
167+
### Add Your Pages
78168

79169
Follow [Vue CLI's instructions](https://cli.vuejs.org/config/#pages) for adding pages to your app.
80170

81-
#### Create Variable for Second Page
171+
### Create Variable for Second Page
82172

83173
Add the `secondWin` and `createdAppProtocol` variables to your background file (`src/background.(js|ts)` by default):
84174

@@ -90,7 +180,7 @@ let secondWin
90180
let createdAppProtocol = false
91181
```
92182

93-
#### Accept Page Arguments for `createWindow` Function
183+
### Accept Page Arguments for `createWindow` Function
94184

95185
In your background file, update the `createWindow` function to take arguments about the page:
96186

@@ -143,7 +233,7 @@ function createWindow(winVar, devPath, prodPath) {
143233
}
144234
```
145235

146-
#### Create Both Windows on App Launch
236+
### Create Both Windows on App Launch
147237

148238
Create both windows inside the `app.on('ready')` callback in your background file:
149239

@@ -165,7 +255,7 @@ app.on('ready', async () => {
165255
})
166256
```
167257

168-
#### Recreate Both Windows When Dock Icon is Clicked
258+
### Recreate Both Windows When Dock Icon is Clicked
169259

170260
Create both windows inside the `app.on('activate')` callback in your background file:
171261

@@ -195,7 +285,7 @@ if (secondWin === null) {
195285
196286
Read [Visual Studio Code's docs on debugging](https://code.visualstudio.com/docs/editor/debugging) before following this guide.
197287
198-
#### Enable Sourcemaps
288+
### Enable Sourcemaps
199289
200290
Enable sourcemaps in your `vue.config.js`:
201291
@@ -207,7 +297,7 @@ module.exports = {
207297
}
208298
```
209299
210-
#### Add Debug Task
300+
### Add Debug Task
211301
212302
Add the `electron-debug` task to `.vscode/tasks.json`, which will start the Electron dev server in debug mode:
213303
@@ -241,7 +331,7 @@ Add the `electron-debug` task to `.vscode/tasks.json`, which will start the Elec
241331
}
242332
```
243333
244-
#### Add Debugging Configurations
334+
### Add Debugging Configurations
245335
246336
Add `Electron: Main`, `Electron: Renderer`, and `Electron: All` debug configurations to `.vscode/launch.json`:
247337
@@ -284,11 +374,11 @@ Add `Electron: Main`, `Electron: Renderer`, and `Electron: All` debug configurat
284374
}
285375
```
286376
287-
#### Add Some Breakpoints
377+
### Add Some Breakpoints
288378
289379
Add "red dot" [breakpoints](https://code.visualstudio.com/docs/editor/debugging#_breakpoints) by clicking VSCode's gutter in your Vue app or background file.
290380
291-
#### Launch Debug Mode
381+
### Launch Debug Mode
292382
293383
Run the `Electron: All` launch configuration. Execution should stop upon reaching one of your breakpoints, and VSCode will allow you to debug your code.
294384

0 commit comments

Comments
 (0)