Skip to content
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

feat: add custom dialog option #126

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,29 @@ export interface IStaticUpdateSource {

export type IUpdateSource = IElectronUpdateServiceSource | IStaticUpdateSource;

export interface IDialogMessages {
/**
* @param {String} title The title of the dialog box.
* Defaults to `Application Update`
*/
title: string;
/**
* @param {String} detail The text of the dialog box.
* Defaults to `A new version has been downloaded. Restart the application to apply the updates.`
*/
detail: string;
/**
* @param {String} restartButtonText The text of the restart button.
* Defaults to `Restart`
*/
restartButtonText: string;
/**
* @param {String} laterButtonText The text of the later button.
* Defaults to `Later`
*/
laterButtonText: string;
}

export interface IUpdateElectronAppOptions<L = ILogger> {
/**
* @param {String} repo A GitHub repository in the format `owner/repo`.
Expand Down Expand Up @@ -74,6 +97,7 @@ export interface IUpdateElectronAppOptions<L = ILogger> {
* prompted to apply the update immediately after download.
*/
readonly notifyUser?: boolean;
readonly dialog?: IDialogMessages;
}

const pkg = require('../package.json');
Expand Down Expand Up @@ -162,13 +186,14 @@ function initUpdater(opts: ReturnType<typeof validateInput>) {
(event, releaseNotes, releaseName, releaseDate, updateURL) => {
log('update-downloaded', [event, releaseNotes, releaseName, releaseDate, updateURL]);

const { title, restartButtonText, laterButtonText, detail } = opts.dialog;

const dialogOpts = {
type: 'info',
buttons: ['Restart', 'Later'],
title: 'Application Update',
buttons: [restartButtonText, laterButtonText],
title,
message: process.platform === 'win32' ? releaseNotes : releaseName,
detail:
'A new version has been downloaded. Restart the application to apply the updates.',
detail,
};

dialog.showMessageBox(dialogOpts).then(({ response }) => {
Expand Down Expand Up @@ -200,8 +225,15 @@ function validateInput(opts: IUpdateElectronAppOptions) {
updateInterval: '10 minutes',
logger: console,
notifyUser: true,
dialog: {
title: 'Application Update',
detail: 'A new version has been downloaded. Restart the application to apply the updates.',
restartButtonText: 'Restart',
laterButtonText: 'Later'
}
};
const { host, updateInterval, logger, notifyUser } = Object.assign({}, defaults, opts);
const assignedDialog = Object.assign({}, defaults.dialog, opts.dialog);

// allows electron to be mocked in tests
const electron: typeof Electron.Main = (opts as any).electron || require('electron');
Expand Down Expand Up @@ -249,5 +281,5 @@ function validateInput(opts: IUpdateElectronAppOptions) {

assert(logger && typeof logger.log, 'function');

return { updateSource, updateInterval, logger, electron, notifyUser };
return { updateSource, updateInterval, logger, electron, notifyUser, dialog: assignedDialog };
}