2

I'm building an Electron application and I've created a small daemon binary written in Go that is to be packaged along with the application as an extraResource. However, there are different versions of this binary that need to be packaged depending on the platform, specifically, agent.exe for Windows and agent without the extension for MacOS, and for Linux, the same filename as Mac.

I have these binaries in the bin directory in my project root:

├── bin/
│   ├── windows/
│   │   └── agent.exe
│   ├── mac/
│   │   └── agent
│   ├── linux/
│   │   └── agent

Testing for Windows, I included the bin/windows directory, and it works fine (packages that exe in the resources directory as expected)

Here is the relevant snipped of my package.json

"build": {
  "win": {
    "icon": "build/app.ico",
    "target": [
      {
        "target": "nsis",
        "arch": [
          "x64"
        ]
      }
    ]
  },
  "nsis": {
    "include": "build/installer.nsh",
    "oneClick": false,
    "allowToChangeInstallationDirectory": true,
    "license": "build/eula.txt",
    "installerIcon": "build/app.ico",
    "artifactName": "...",
    "shortcutName": "..."
  },
  "extraResources": [
    "bin/windows/agent.exe"
  ]
}

Is it possible to do this? As it is now, I would have to edit the package.json file by changing the extraResources value in between different platform builds, which for obvious reasons is not ideal (as I could forget and wind up including the wrong binary by forgetting to change it back).

Note that I am not supporting 32-bit Windows, so all Windows builds would include the same (64-bit) binary.

I am using electron-builder 19.50.0

halfer
  • 19,824
  • 17
  • 99
  • 186
mwieczorek
  • 2,107
  • 6
  • 31
  • 37

1 Answers1

3

Add three new directories to your build directory: win, mac and linux, as is my sample folder structure above (changing windows to win), and add all the platform-specific binaries (and anything else that needs to be packaged with the distribution) into their respective directories.

Then add the following to your build section of package.json:

"extraResources": [
  {
    "from": "bin/${os}",
    "to": "bin",
    "filter": [
      "**/*"
    ]
  }
]

Once packaged, those files will be added to the bin directory of your resources root, so the following files and directories appear in your resources directory. In my case, for the windows package that is built in dist/win-unpacked/resources (relative to project root)

app.asar.unpacked/
bin/
app.asar
electron.asar
elevate.exe

This way, once your app is installed on the target system, it can be accessed at runtime using process.resourcesPath:

const binaryFileName = process.platform == 'win32' ? 'binfile.exe' : 'binfile';
const binaryFile = path.join(process.resourcesPath, `bin/${binaryFileName}`);
...
spawn(binaryFile, [...args]);

Credit to this post here on Stack Overflow.

halfer
  • 19,824
  • 17
  • 99
  • 186
mwieczorek
  • 2,107
  • 6
  • 31
  • 37