Migrations
Integrate ToDesktop into an electron project
This is a guide for those looking to integrate ToDesktop CLI into an existing Electron project. If you don't have an Electron app, check out todesktop-quick-start for a minimal template project to get you going.
This guide will cover the minimum code changes needed and some noteworthy configuration options. It won't cover all configuration options or features. It also assumes you've already signed up and have created an app in the ToDesktop web app.
Drop your old setup
I want to set clear expectations before we start. Once you've migrated and released your app on ToDesktop, you won't need:
- Multiple machines / environments to manually compile binaries.
- To host auto-updates and downloads.
- And so on.
You will just need to call our CLI from one machine and the rest is taken care of.
Install ToDesktop dependencies
If you're using todesktop-quick-start, some of these steps have already been completed for you. However, it's still beneficial to familiarize yourself with what's required.
- Run
npm install -g @todesktop/cli
. - Run
npm install --save @todesktop/runtime
at the root of your app directory. For now, we'll assume this the same location as your project root. Later, we'll cover projects that have their Electron app in a sub-directory. - (Optional) You may also install
@todesktop/cli
in your project directory (rather than just globally on your machine) for version consistency and CI/CD compatibility. If needed, run:npm install --save-dev @todesktop/cli
at the root of your project.
Feel free to use yarn
instead.
todesktop.json
Add Create a todesktop.json
file at the root of your project. Here's a minimal example:
id
must contain your application's ToDesktop ID. To find this:- Go to app.todesktop.com
- Select your app from the dashboard
- Click "Settings" in the nav bar
- Your ToDesktop ID will be displayed at the top of the settings page
icon
must point to a PNG or ICNS. This will be used as your app icon.schemaVersion
is thetodesktop.json
schema version. This must be1
.
Many other options are supported in todesktop.json
. For example, you can have a different icon per OS. Check out the documentation for the full list of options.
package.json
requirements
App Your app's package.json
must have the following:
- Electron must be in your
devDependencies
and it must be a fixed version. I.e. it doesn't start with^
or~
. - You must set the author property, e.g.
"author" : "Barney Rubble <[email protected]>"
.
You should also set the productName
property. Otherwise, your app name will default to the value of the name
property.
@todesktop/runtime
in your app
Use At the top of your app's main script (i.e. in the main process), import @todesktop/runtime
and use it;
This handles auto-updates and more for you. The behaviour can be customized. Check out the @todesktop/runtime
documentation for the full list of options and methods.
Also, you need to remove any usage of Electron's built-in autoUpdater
API, electron-updater, or similar.
Optional: add npm scripts for releasing your app
It's probably best to add these to add npm scripts to your project's package.json
for running ToDesktop builds and releases;
For a step-by-step guide on building your app, head here.
Common questions
How are dependencies and native modules handled?
The local node_modules
you have installed on your machine are not uploaded. Your dependencies are installed on our servers instead. Three times to be exact; once on Linux, Mac, and Windows. It's done this way because your dependencies could have platform-specific postinstall
steps, e.g. native modules. It's also faster.
We rebuild your native dependencies against the version of Node.js that your Electron app is using, you don't need any extra config or anything like that.
Also, your devDependencies
are excluded from your final app binaries to keep the file size down.
Optional: for local development, using electron-rebuild (in a postinstall
script in your app's package.json
) will ensure your dependencies are built against the version of Node.js that your Electron project is using.
What if I need to compile my app code?
If you have a compilation step (e.g. Babel, TypeScript, etc.), that's fine. In general, if you can run your app with the Electron CLI (i.e. electron path/to/app
), it will work with ToDesktop.
First, add npm scripts for releasing your app ( as we just covered above). Assuming you already have script for compiling your app like the compile
script below, call it in your build
and release
scripts.
What if my app is in a sub-directory?
If your Electron app is in a sub-directory in your project, you likely have a structure like this:
Here's what you need to know:
@todesktop/runtime
needs to be installed in theapp
directory, not at the root.- When adding npm scripts for releasing your app, you need to install
@todesktop/cli
at the root of your project add the scripts to the rootpackage.json
(not in theapp
directory). todesktop.json
stays at the root of your project, where you'll be calling the ToDesktop CLI from, but you should set theappPath
option to./app
. This is the path to your Electron application directory.
📓 Side note: if your package.json
contains a postinstall
script which references other files, these must be accessible within the appPath
directory as only the appPath
is uploaded to our servers.
How can I exclude unnecessary files?
If you have files in your app directory which don't need to be uploaded to our servers or included in your app, you can use the appFiles
option in your todesktop.json
. This option allows you to decide which files get packed into your app (or not).
For example, use "appFiles": [ "dist/**"]
if you want to only include the files in the dist
directory within your app directory (i.e. your appPath
).
By default, all files in your app directory are included in your upload to your server, except for node_modules
and .git
. Dependencies are installed on our build servers (see How are dependencies and native modules handled? above). Also, your devDependencies
are excluded from your final app binaries to keep the file size down.
You can also explicitly exclude some files; for example: "appFiles": ["**/*", "!static/**"]
.
The following are always included if they exist:
/package.json
/package-lock.json
/yarn.lock
Check out the documentation to learn more about appFiles
.
What if I want to trigger a release using a CI platform or a non-interactive script?
You may want to automate builds with your Continuous Integration (CI) provider. To achieve this, simply set the TODESKTOP_ACCESS_TOKEN
and TODESKTOP_EMAIL
environment variables.
If you want to build-and-release in one step, you can run:
Can I use my own custom electron builds?
Yes, check out the electronMirror config option.
Can I configure my own certificates?
Yes, this can be done via the web UI ( app.todesktop.com).
Can I set environment variables for my builds?
Yes, this can be done in the following steps via the web UI ( app.todesktop.com):
- Click in to your desired application from the applications overview.
- Navigate to the Settings menu.
- Navigate to the Build and Deploy submenu.
- Scroll down and add your environment variable KEY and VALUE.
Can I auto-update existing users to a ToDesktop release?
Yes, that's no problem. Assuming you have set everything up, this is how it goes:
Make a release via ToDesktop that you're happy with.
- Both the legacy app and the ToDesktop app need to be signed with a code signing certificate that belongs to the same organisation or individual.
- Make sure that this release has the same
appId
as your legacy app. You can set theappId
property intodesktop.json
. You can find out the app ID (or bundle identifier) of your legacy app by running the following command in terminal.
Make a release via your legacy infrastructure, which uses
@todesktop/runtime
and containstodesktop-runtime-config.json
andapp-update.yml
in the resources directory. The app version must be less than that of step #1.- The content of the
todesktop-runtime-config.json
file should be:
- The content of the
b. You should copy the app-update.yml
file from the resources directory in your new ToDesktop release. It will look something like this:
That's it.
Contact us if you're doing this. We'll help you through the process and double check each step.
But what about X framework or tool?
Here are our other guides:
- How to migrate from electron-builder to ToDesktop.
- How to integrate ToDesktop into an electron-webpack project.
- How to migrate from electron-forge to ToDesktop.
- How to migrate from electron-packager to ToDesktop.
If you need any help, don't hesitate to contact us.
More features
This is just the tip of the iceberg as far as features go. Check out the @todesktop/cli documentation, the @todesktop/runtime documentation, and app.todesktop.com for more.