electron 电子应用程序内部的打包依赖项

iqjalb3h  于 6个月前  发布在  Electron
关注(0)|答案(2)|浏览(103)

我的electron for Mac OSX应用程序有sox依赖项。将其作为electron-package的一部分包含的最佳方法是什么?我希望用户不必单独安装sox(不幸的是,我的大多数用户都不是那么精明)。是否有方法直接包含sox二进制文件或在应用程序之前顺序预安装sox?

332nm8kg

332nm8kg1#

我最终将sox二进制文件包含到包中。我使用node-record-lpcm16包并更新其路径以使用包含的sox二进制文件。这样我就可以将lib路径作为参数传递下去。

lx0bsm1f

lx0bsm1f2#

这是正确的使用二进制是最安全的方式。

如何将二进制文件添加到Electron应用程序中?

它应该以这种方式通过包步骤来完成。

步骤

1.下载适用于任何目标平台的binary version,解压缩它,并将其作为sox目录放在项目根目录中。如果需要,可以创建mac,linux,win32文件夹作为子目录。
1.告诉打包器模块包含sox目录。sox目录将基于目标平台复制到应用程序的Contents/Resources中。

// 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.通过process.resourcesPath找到运行时的sox目录,并根据需要使用它。

// 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.现在,您可以打开一个子进程并直接使用sox,或者使用这个具有recorderPath的派生node-record-lpcm16库来引入sox路径。

// 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(),
});

相关问题