1.6.2 • Published 4 days ago

@todesktop/runtime v1.6.2

Weekly downloads
535
License
MIT
Repository
-
Last release
4 days ago

@todesktop/runtime

The ToDesktop runtime package which takes care of auto-updating, crash reporting, and more. For more information, see https://todesktop.com/cli.

Installation

Via npm:

npm install @todesktop/runtime

Or Yarn:

yarn add @todesktop/runtime

Minimal usage

In your main (background process) script, require the package and call the init function. The key is to call it right at the beginning. We'll do the rest.

const { app, BrowserWindow } = require("electron");
const todesktop = require("@todesktop/runtime");

todesktop.init();

function createWindow() {
  // Create the browser window.
  let win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true
    }
  });

  // and load the index.html of the app.
  win.loadFile("index.html");
}

app.whenReady().then(createWindow);

Check out todesktop-quick-start to see a minimal example project.

Also, see the full API below to learn more about the other methods and events.

Default behaviour

By default, we check for updates when the app launches and every 10 minutes thereafter.

When an update is found, we show a notification explaining that an update has been downloaded and once they exit/restart, it'll be installed. Your user doesn't have to accept any prompt or anything like that. Only one notification is shown, max.

This default behaviour can be augmented using the API below, e.g. if you want to offer your users a button to check for updates, a prompt to restart and install the update, etc.

Disabling the default auto-update behaviour

It's also possible to disable some or all of the default behaviour.

What happens when an update is found can be disabled using the update-downloaded event (learn more below).

Everything (the notification, the auto-checks on launch and on interval) can be disabled by passing autoUpdater: false to the .init method (learn more below).

API

.init(options)

This initializes ToDesktop. You must call this at the top of your main script. If the app isn't packaged, this won't do much. .autoUpdater.checkForUpdates will never check or find an update, for example.

Returns nothing.

options (Optional)

Type: object

Default: { autoUpdater: true }

options.autoUpdater (Optional)

Type: boolean

Default: true

If false, this will disable all of the default auto-update behaviour. I.e. it will not auto-check on launch, on interval, or show a notification when an update is found. The events will always be fired.

If you do this, everything is in your hands. You must manually call .autoUpdater.checkForUpdates or else your users will never get an update.

async .autoUpdater.checkForUpdates(options)

This performs a check for updates and downloads it. Only call this after the application is ready.

It returns a Promise which resolves to an object.

If there is no update available, the result will be { updateInfo: null }.

If there is an update available, the result will look like this:

{
  updateInfo: {
    releaseDate: "2011-10-05T14:48:00.000Z",
    version: "2.0.1",
  }
}

This method can reject with an error, e.g. if the network is unstable.

options (Optional)

Type: object

Default: { source: "programmatic-call" }

options.source (Optional)

Type: string

Default: "programmatic-call"

The source/trigger of the update check. The update-downloaded event passes this back to you so you can identify the source(s) of the update check.

Example

try {
  const result = await todesktop.autoUpdater.checkForUpdates();
  if(result.updateInfo) {
    console.log("Update found:", result.updateInfo.version);
    todesktop.autoUpdater.restartAndInstall();
  }
}
catch(e) {
  console.log("Update check failed:", e);
}

.autoUpdater.on(eventName, callback)

This subscribes to an event. The callback will be called when the event occurs.

.autoUpdater is an instance of EventEmitter2 so it supports all of the same methods; e.g. .once, .off, and so on.

eventName

Type: string

callback

Type: Function

.autoUpdater.restartAndInstall()

This restarts the application and installs the new update after one has been found.

Returns nothing.

NOTE: this method 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. However, this is not the case if the method is called from a Squirrel.Windows application.

Events

before-quit-for-update

This is the same as Electron's before-quit-for-update auto-updater event.

update-downloaded

Emitted when there is an update available and downloaded. The update has already been downloaded.

Your callback is called with an object like this:

{
  sources: ["auto-check-on-interval"],
  updateInfo: {
    releaseDate: "2011-10-05T14:48:00.000Z",
    version: "2.1.3"
  }
}

sources (an array of strings) are the sources/triggers of the update check. The default built-in sources are auto-check-on-launch, auto-check-on-interval, and programmatic-call. You can pass a custom source to .autoUpdater.checkForUpdates. NOTE: sources is an array because multiple checks could be triggered around the same time. They do not cancel an existing update check but piggyback onto it instead.

By default, we show a notification explaining that an update has been downloaded and once they exit/restart, it'll be installed. Your user doesn't have to accept any prompt or anything like that. Only one notification is shown, max.

To disable this, you can return false (or a Promise which resolves to promise). If your callback errors or it returns a Promise that doesn't resolve within 500 milliseconds, the default behaviour will proceed.

Example

todesktop.autoUpdater.on("update-downloaded", (event) => {
  console.log("Update found:", event.updateInfo.version);
  if(event.sources.includes("my-custom-source")) {
    return false;
    todesktop.autoUpdater.restartAndInstall();
  }
  // Else: the default behaviour (notification) will happen
});

More documentation

See https://docs.todesktop.com/cli/using-the-todesktop-cli

1.6.3-1

4 days ago

1.6.3-0

12 days ago

1.6.2

12 days ago

1.6.1-2

2 months ago

1.5.7-beta.2

6 months ago

1.6.1-1

6 months ago

1.6.1

6 months ago

1.6.0

6 months ago

1.4.0

8 months ago

1.4.1-0

7 months ago

1.4.1-1

7 months ago

1.5.7

7 months ago

1.5.6

7 months ago

1.6.1-0

6 months ago

1.5.5

7 months ago

1.5.4

7 months ago

1.5.3

7 months ago

1.5.2

7 months ago

1.5.1

8 months ago

1.5.0

8 months ago

1.3.0

9 months ago

1.5.7-beta.1

6 months ago

1.5.7-beta.0

6 months ago

1.2.2-8

1 year ago

1.2.2-1

1 year ago

1.2.2-0

1 year ago

1.2.2-3

1 year ago

1.2.2-2

1 year ago

1.2.1

1 year ago

1.2.2-5

1 year ago

1.2.2-4

1 year ago

1.2.2-7

1 year ago

1.2.2-6

1 year ago

1.2.1-3

1 year ago

1.2.1-2

1 year ago

1.2.1-0

1 year ago

1.2.1-1

1 year ago

1.2.0

1 year ago

1.1.1

2 years ago

1.1.3

1 year ago

1.1.2

1 year ago

1.1.0

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.6.5-0

2 years ago

0.6.5

2 years ago

0.6.4-1

3 years ago

0.6.4-0

3 years ago

0.6.3

3 years ago

0.6.2

3 years ago

0.6.4

3 years ago

0.6.1

3 years ago

0.6.0

3 years ago

0.5.4

3 years ago

0.5.3

3 years ago

0.5.1

3 years ago

0.5.0

3 years ago

0.4.0

4 years ago

0.4.1-0

4 years ago

0.4.0-0

4 years ago

0.3.1

4 years ago

0.3.1-0

4 years ago

0.3.0

4 years ago

0.2.8

4 years ago

0.2.7

4 years ago

0.2.6

4 years ago

0.2.5

4 years ago

0.2.4

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.2.2-0

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.2.0-2

4 years ago

0.2.0-1

4 years ago

0.2.0-0

4 years ago

0.1.2

4 years ago

0.1.2-0

4 years ago

0.1.1

4 years ago

0.1.1-0

4 years ago

0.1.0

4 years ago

0.1.0-rc.7

4 years ago

0.1.0-rc.8

4 years ago

0.1.0-rc.5

4 years ago

0.1.0-rc.6

4 years ago

0.1.0-rc.4

4 years ago

0.1.0-rc.r

4 years ago

0.1.0-rc.3

4 years ago

0.1.0-rc.1

4 years ago

0.1.0-rc.2

4 years ago

0.1.0-rc.0

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago