1.2.0 • Published 1 year ago

graceful-updater v1.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

graceful-updater

NPM version CI node version npm download

Software updates solution for Electron applications, It is convenient to complete full software update and dynamic update.

English | 简体中文

Installment

$ npm i graceful-updater --save

Sample

please visit: https://github.com/electron-modules/electron-modules-sample

// 1. options
const options = {
  url: getFeedUrl(),
  logger: console, // logger
  productName: 'demo',
  updateInfoFormatter: (res) => {
    return res;
  },
  ifNeedUpdate: (res) => {
    console.log('local version', currentVersion);
    console.log('local project version', currentBuildNumber);
    console.log('remote version', res.version);
    console.log('remote project version', res.project_version);
    return semver.gt(res.version, currentVersion) ||
      res.project_version > currentBuildNumber;
  },
};
// 2. initialization
const electronUpdator = new MacUpdator(options);

// 3. Bind events
electronUpdator.on(EventType.UPDATE_DOWNLOADED, (...args) => {
  console.log('updator >> %s, args: %j', EventType.UPDATE_DOWNLOADED, args);
});
electronUpdator.on(EventType.CHECKING_FOR_UPDATE, (...args) => {
  console.log('updator >> %s, args: %j', EventType.CHECKING_FOR_UPDATE, args);
});
electronUpdator.on(EventType.UPDATE_AVAILABLE, (data) => {
  const { version, project_version } = data?.updateInfo || {};
  const message = [
    'available',
    `local version: ${currentVersion}`,
    `local project version: ${currentBuildNumber}`,
    `remote version: ${version}`,
    `remote project version: ${project_version}`,
  ].join('\n');
  dialog.showMessageBoxSync({
    message,
  });
});
electronUpdator.on(EventType.UPDATE_NOT_AVAILABLE, (data) => {
  const { version, project_version } = data?.updateInfo || {};
  const message = [
    'not available',
    `local version: ${currentVersion}`,
    `local project version: ${currentBuildNumber}`,
    `remote version: ${version}`,
    `remote project version: ${project_version}`,
  ].join('\n');
  dialog.showMessageBoxSync({
    message,
  });
});
electronUpdator.on(EventType.ERROR, (...args) => {
  console.log('updator >> %s, args: %j', EventType.ERROR, args);
});
electronUpdator.on(EventType.UPDATE_DOWNLOAD_PROGRESS, (data) => {
  const { status, progress } = data;
  console.log('updator >> %s, status: %s, progress: %d', EventType.UPDATE_DOWNLOAD_PROGRESS, status, progress);
  app.windowManager.get('updator').webContents.send('updator:updateDownloadProgress', { status, progress });
});

Documents

Options

ParamTypeRequiredDescriptionDefault value
urlStringYesCheck for update remote address, and the returned data follows the UpdateInfo object
ifNeedUpdateFunctionYesCheck if update is required
updateInfoFormatterFunctionNoThe server returns data format adaptation. If the returned format cannot match the UpdateInfo, this method can be used to format
loggerObjectNoLog methodconsole
productNameStringYesApplication Name
autoDownloadStringNoWhether to download automaticallyfalse
getWindowsHelperExeDirFunctionNoWindows helper directoryfalse

UpdateInfo

ParamTypeRequiredDescriptionDefault value
versionStringYesversion
projectVersionNumberNoproject version
filesArray\<Object>YesThe list of files to be downloaded. The returned data follows the File object
updateTypeEnum\<String>YesUpdate type, full update or dynamic update.Package is full update,Asar is dynamic update
releaseNotesArray\<String>YesThe release notes.

File

ParamTypeRequiredDescriptionDefault value
urlStringNodownload address
signatureStringNodownload address signature
updateTypeEnum\<String>YesUpdate type, full update or dynamic update.Package is full update,Asar is dynamic update

Methods

  1. checkForUpdates(ExecuteType)
  • ExecuteType ExecuteType(User or Auto)

Check whether there is content to be updated. If the ExecuteType is User, the update-available event will be triggered directly after the update is detected. Otherwise, the update-available event will be triggered after the package is automatically downloaded

  1. setFeedUrl(url)
  • url: New update URL According to the needs of different scenarios, dynamically set the URL for checking updates
  1. downloadUpdate(ExecuteType)
  • ExecuteType ExecuteType(User or Auto)

Start downloading the installation package. If the ExecuteType is User, no pre-check will be performed. After the download is completed, the update-downloaded event will be triggered directly. Otherwise, the update-downloaded event will be triggered after the internal pre-check is completed

  1. quitAndInstall() Exit the app and start the installation. If the installation package has been downloaded, the application will be restarted directly and the new version will be installed. Otherwise, enter the download process

Events

  1. checking-for-update

Triggered when checking for updates

  1. update-available
  • params: update info
  • params.updateInfo: UpdateInfo

Triggered when an available update is checked

  1. update-not-available
  • params: update info
  • params.updateInfo: UpdateInfo

Triggered when no updates are checked

  1. update-download-progress
  • params: status and file info the download process.
  • params.status: download status begin, downloading, end
  • params.progress: Current download progress percentage. 0 ~ 100
  • params.data: The file stream of downloaded content can be used for signature verification

Triggering during download

  1. update-downloaded

Triggered when the download is complete

  1. error
  • params: Error

Triggered when an error occurs inside the updater

Contributors

zlyixudafengsnapre

This project follows the git-contributor spec, auto updated at Tue Jan 31 2023 14:31:40 GMT+0800.

License

The MIT License (MIT)

1.2.0

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.0.9

1 year ago

1.1.2

1 year ago

1.1.2-beta.1

1 year ago

1.1.1-beta.0

1 year ago

1.1.1-beta.1

1 year ago

1.1.2-beta.0

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago