Skip to content

Commit

Permalink
feat: refine code to better achieve the purpose
Browse files Browse the repository at this point in the history
  • Loading branch information
lishenglian committed Sep 26, 2022
1 parent be2fe97 commit b7beedd
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 59 deletions.
34 changes: 4 additions & 30 deletions docs/api/electron-builder.md
Original file line number Diff line number Diff line change
Expand Up @@ -1428,13 +1428,11 @@ return path.join(target.outDir, <code>__${target.name}-${getArtifactArchName(arc
<li><a href="#module_electron-updater.AppUpdater+setFeedURL"><code>.setFeedURL(options)</code></a></li>
<li><a href="#module_electron-updater.AppUpdater+isUpdaterActive"><code>.isUpdaterActive()</code></a> ⇒ <code>Boolean</code></li>
<li><a href="#module_electron-updater.AppUpdater+quitAndInstall"><code>.quitAndInstall(isSilent, isForceRunAfter)</code></a></li>
<li><a href="#module_electron-updater.AppUpdater+quitAppAndInstall"><code>.quitAppAndInstall(isForceRunAfter)</code></a></li>
</ul>
</li>
<li><a href="#MacUpdater">.MacUpdater</a> ⇐ <code><a href="#AppUpdater">AppUpdater</a></code>
<ul>
<li><a href="#module_electron-updater.MacUpdater+quitAndInstall"><code>.quitAndInstall()</code></a></li>
<li><a href="#module_electron-updater.MacUpdater+quitAppAndInstall"><code>.quitAppAndInstall()</code></a></li>
<li><a href="#module_electron-updater.AppUpdater+addAuthHeader"><code>.addAuthHeader(token)</code></a></li>
<li><a href="#module_electron-updater.AppUpdater+checkForUpdates"><code>.checkForUpdates()</code></a> ⇒ <code>Promise&lt; | <a href="#UpdateCheckResult">UpdateCheckResult</a>&gt;</code></li>
<li><a href="#module_electron-updater.AppUpdater+checkForUpdatesAndNotify"><code>.checkForUpdatesAndNotify(downloadNotification)</code></a> ⇒ <code>Promise&lt; | <a href="#UpdateCheckResult">UpdateCheckResult</a>&gt;</code></li>
Expand Down Expand Up @@ -1587,6 +1585,9 @@ return path.join(target.outDir, <code>__${target.name}-${getArtifactArchName(arc
<p><code id="AppUpdater-autoInstallOnAppQuit">autoInstallOnAppQuit</code> = <code>true</code> Boolean - Whether to automatically install a downloaded update on app quit (if <code>quitAndInstall</code> was not called before).</p>
</li>
<li>
<p><code id="AppUpdater-autoRunAppAfterInstall">autoRunAppAfterInstall</code> = <code>true</code> Boolean - <em>windows-only</em> Whether to run the app after finish install when run the installer <em>NOT in silent mode</em>.</p>
</li>
<li>
<p><code id="AppUpdater-allowPrerelease">allowPrerelease</code> = <code>false</code> Boolean - <em>GitHub provider only.</em> Whether to allow update to pre-release versions. Defaults to <code>true</code> if application version contains prerelease components (e.g. <code>0.12.1-alpha.1</code>, here <code>alpha</code> is a prerelease component), otherwise <code>false</code>.</p>
<p>If <code>true</code>, downgrade will be allowed (<code>allowDowngrade</code> will be set to <code>true</code>).</p>
</li>
Expand Down Expand Up @@ -1644,7 +1645,6 @@ return path.join(target.outDir, <code>__${target.name}-${getArtifactArchName(arc
<li><a href="#module_electron-updater.AppUpdater+setFeedURL"><code>.setFeedURL(options)</code></a></li>
<li><a href="#module_electron-updater.AppUpdater+isUpdaterActive"><code>.isUpdaterActive()</code></a> ⇒ <code>Boolean</code></li>
<li><a href="#module_electron-updater.AppUpdater+quitAndInstall"><code>.quitAndInstall(isSilent, isForceRunAfter)</code></a></li>
<li><a href="#module_electron-updater.AppUpdater+quitAppAndInstall"><code>.quitAppAndInstall(isForceRunAfter)</code></a></li>
</ul>
</li>
</ul>
Expand Down Expand Up @@ -1748,29 +1748,7 @@ This is different from the normal quit event sequence.</p>
<tr>
<td>isForceRunAfter</td>
<td><code>Boolean</code></td>
<td>Run the app after finish even on silent install. Not applicable for macOS. Ignored if <code>isSilent</code> is set to <code>false</code>.</td>
</tr>
</tbody>
</table>
<p><a name="module_electron-updater.AppUpdater+quitAppAndInstall"></a></p>
<h3 id="appupdater.quitAppAndInstall(isforcerunafter)"><code>appUpdater.quitAppAndInstall(isForceRunAfter)</code></h3>
<p>Quit the app and explicit installs the update after it has been downloaded.
It should only be called after <code>update-downloaded</code> has been emitted.</p>
<p><strong>Note:</strong> <code>autoUpdater.quitAppAndInstall()</code> will close all application windows first and only emit <code>before-quit</code> event on <code>app</code> after that.
This is different from the normal quit event sequence.</p>
<table>
<thead>
<tr>
<th>Param</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>isForceRunAfter</td>
<td><code>Boolean</code></td>
<td>Run the app after finish even on silent install. Not applicable for macOS.</td>
<td>Run the app after finish even on silent install. Not applicable for macOS. Ignored if <code>isSilent</code> is set to <code>false</code>(In this case you can still set <code>autoRunAppAfterInstall</code> to <code>false</code> to prevent run the app)</td>
</tr>
</tbody>
</table>
Expand All @@ -1782,7 +1760,6 @@ This is different from the normal quit event sequence.</p>
<li><a href="#MacUpdater">.MacUpdater</a> ⇐ <code><a href="#AppUpdater">AppUpdater</a></code>
<ul>
<li><a href="#module_electron-updater.MacUpdater+quitAndInstall"><code>.quitAndInstall()</code></a></li>
<li><a href="#module_electron-updater.MacUpdater+quitAppAndInstall"><code>.quitAppAndInstall()</code></a></li>
<li><a href="#module_electron-updater.AppUpdater+addAuthHeader"><code>.addAuthHeader(token)</code></a></li>
<li><a href="#module_electron-updater.AppUpdater+checkForUpdates"><code>.checkForUpdates()</code></a> ⇒ <code>Promise&lt; | <a href="#UpdateCheckResult">UpdateCheckResult</a>&gt;</code></li>
<li><a href="#module_electron-updater.AppUpdater+checkForUpdatesAndNotify"><code>.checkForUpdatesAndNotify(downloadNotification)</code></a> ⇒ <code>Promise&lt; | <a href="#UpdateCheckResult">UpdateCheckResult</a>&gt;</code></li>
Expand All @@ -1796,9 +1773,6 @@ This is different from the normal quit event sequence.</p>
<p><a name="module_electron-updater.MacUpdater+quitAndInstall"></a></p>
<h3 id="macupdater.quitandinstall()"><code>macUpdater.quitAndInstall()</code></h3>
<p><strong>Overrides</strong>: <a href="#module_electron-updater.AppUpdater+quitAndInstall"><code>quitAndInstall</code></a><br>
<p><a name="module_electron-updater.MacUpdater+quitAppAndInstall"></a></p>
<h3 id="macupdater.quitandinstall()"><code>macUpdater.quitAppAndInstall()</code></h3>
<p><strong>Overrides</strong>: <a href="#module_electron-updater.AppUpdater+quitAppAndInstall"><code>quitAppAndInstall</code></a><br>
<a name="module_electron-updater.AppUpdater+addAuthHeader"></a></p>
<h3 id="macupdater.addauthheader(token)"><code>macUpdater.addAuthHeader(token)</code></h3>
<p>Shortcut for explicitly adding auth tokens to request headers</p>
Expand Down
20 changes: 8 additions & 12 deletions packages/electron-updater/src/AppUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ export abstract class AppUpdater extends (EventEmitter as new () => TypedEmitter
*/
autoInstallOnAppQuit = true

/**
* *windows-only* Whether to run the app after finish install when run the installer NOT in silent mode.
* @default true
*/
autoRunAppAfterInstall = true

/**
* *GitHub provider only.* Whether to allow update to pre-release versions. Defaults to `true` if application version contains prerelease components (e.g. `0.12.1-alpha.1`, here `alpha` is a prerelease component), otherwise `false`.
*
Expand Down Expand Up @@ -513,21 +519,11 @@ export abstract class AppUpdater extends (EventEmitter as new () => TypedEmitter
* This is different from the normal quit event sequence.
*
* @param isSilent *windows-only* Runs the installer in silent mode. Defaults to `false`.
* @param isForceRunAfter Run the app after finish even on silent install. Not applicable for macOS. Ignored if `isSilent` is set to `false`.
* @param isForceRunAfter Run the app after finish even on silent install. Not applicable for macOS.
* Ignored if `isSilent` is set to `false`(In this case you can still set `autoRunAppAfterInstall` to `false` to prevent run the app after finish).
*/
abstract quitAndInstall(isSilent?: boolean, isForceRunAfter?: boolean): void

/**
* Quit the app and explicit installs the update after it has been downloaded.
* It should only be called after `update-downloaded` has been emitted.
*
* **Note:** `autoUpdater.quitAppAndInstall()` will close all application windows first and only emit `before-quit` event on `app` after that.
* This is different from the normal quit event sequence.
*
* @param isForceRunAfter Run the app after finish. Defaults to `false`. Not applicable for macOS.
*/
abstract quitAppAndInstall(isForceRunAfter?: boolean): void

private async loadUpdateConfig(): Promise<any> {
if (this._appUpdateConfigPath == null) {
this._appUpdateConfigPath = this.app.appUpdateConfigPath
Expand Down
15 changes: 2 additions & 13 deletions packages/electron-updater/src/BaseUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export abstract class BaseUpdater extends AppUpdater {

quitAndInstall(isSilent = false, isForceRunAfter = false): void {
this._logger.info(`Install on explicit quitAndInstall`)
const isInstalled = this.install(isSilent, isSilent ? isForceRunAfter : true)
// If NOT in silent mode use `autoRunAppAfterInstall` to determine whether to force run the app
const isInstalled = this.install(isSilent, isSilent ? isForceRunAfter : this.autoRunAppAfterInstall)
if (isInstalled) {
setImmediate(() => {
// this event is normally emitted when calling quitAndInstall, this emulates that
Expand All @@ -24,18 +25,6 @@ export abstract class BaseUpdater extends AppUpdater {
}
}

quitAppAndInstall(isForceRunAfter = false): void {
const isInstalled = this.install(false, isForceRunAfter);
if (isInstalled) {
setImmediate(() => {
require("electron").autoUpdater.emit("before-quit-for-update")
this.app.quit()
})
} else {
this.quitAndInstallCalled = false
}
}

protected executeDownload(taskOptions: DownloadExecutorTask): Promise<Array<string>> {
return super.executeDownload({
...taskOptions,
Expand Down
4 changes: 0 additions & 4 deletions packages/electron-updater/src/MacUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,4 @@ export class MacUpdater extends AppUpdater {
}
}
}

quitAppAndInstall(): void {
this.quitAndInstall();
}
}

0 comments on commit b7beedd

Please sign in to comment.