xoail
xoail

Reputation: 3064

Packaging dependencies inside electron app

My electron app for Mac OSX has sox dependency. What's the best way to include it as part of electron-package? I'd prefer the user not having to install sox separately (unfortunately most of my users are not that savy). Is there a way to include sox binaries directly or sequence pre-install of sox before my app?

Upvotes: 1

Views: 608

Answers (2)

Navid Shad
Navid Shad

Reputation: 1016

That's right using binary is the safest way.

How to add binary files into an Electron app?

It should be done by the package step in this way.

Steps:

  1. Download the binary version for any target platform, unzip it, and put it in your project root as sox directory. create mac,linux,win32 folders if you want as subdirectories.

  2. Tell the packager module to include the sox directory. the sox dir will copied into Contents/Resources of the app based on the target platform.

// Assuming you use electron forge, 
// and this is the config file
const config: ForgeConfig = {
  packagerConfig: {
    extraResource: [
      // Sox binaries for recording audio
      join(__dirname, "sox"),
    ],
  },
}
  1. Find the sox directory on runtime by process.resourcesPath and use it as you want.
// Inside the main trade
import { tmpdir, platform } from "os";

function getSoxPath() {

  switch (platform()) {
    case "darwin":
      return join(process.resourcesPath, "sox", "mac");
    case "win32":
      return join(process.resourcesPath, "sox", "win32");
    case "linux":
      return join(process.resourcesPath, "sox", "linux");
    default:
      throw new Error("Unsupported platform");
  }

}

  1. Now, you can open a child process and use sox directly, or use this forked node-record-lpcm16 library that has recorderPath for introducing sox path.
// To Install 
// yarn add https://github.com/navidshad/node-record-lpcm16

// To use
import recorder from "node-record-lpcm16";

recorder.record({
  recorder: "sox",
  recorderPath: getSoxPath(),
});

Upvotes: 0

xoail
xoail

Reputation: 3064

I ended up including the sox binary into the package. I used node-record-lpcm16 package and updated its path to use included sox binary. This way I can pass down the the lib path as a parameter.

Upvotes: 4

Related Questions