这是另一种方法,目前已经在 Mac 和 Windows 上进行了测试。需要“app-root-dir”包,不需要手动添加任何内容到node_modules目录。
把你的文件放在下面资源/$os/, 在哪里$os或者是"mac", "linux", or "win"。构建过程将根据构建目标操作系统从这些目录复制文件。
Put extraFiles
构建配置中的选项如下:
包.json
"build": {
"extraFiles": [
{
"from": "resources/${os}",
"to": "Resources/bin",
"filter": ["**/*"]
}
],
- 使用类似的方法来确定当前平台。
获取平台.js
import { platform } from 'os';
export default () => {
switch (platform()) {
case 'aix':
case 'freebsd':
case 'linux':
case 'openbsd':
case 'android':
return 'linux';
case 'darwin':
case 'sunos':
return 'mac';
case 'win32':
return 'win';
}
};
- 根据环境和操作系统从您的应用程序调用可执行文件。这里我假设构建版本处于生产模式,源版本处于其他模式,但您可以创建自己的调用逻辑。
import { join as joinPath, dirname } from 'path';
import { exec } from 'child_process';
import appRootDir from 'app-root-dir';
import env from './env';
import getPlatform from './get-platform';
const execPath = (env.name === 'production') ?
joinPath(dirname(appRootDir.get()), 'bin'):
joinPath(appRootDir.get(), 'resources', getPlatform());
const cmd = `${joinPath(execPath, 'my-executable')}`;
exec(cmd, (err, stdout, stderr) => {
// do things
});
我想我正在使用电子制造商作为基础,环境文件生成随之而来。基本上它只是一个 JSON 配置文件。