每日一练【NodeJs包装第三方静态文件】

背景:

有些时候我们需要调用第三方可执行文件来完成某项任务。

本地编译或者安装这些第三方可执行文件费时费力。

于是就有了把这些第三方可执行文件打包好的静态文件做成库的操作。

这样只要安装一个依赖就可以调用这些第三方可执行文件。

//
// With credits to https://github.com/eugeneware/ffmpeg-static
//
var os = require('os');
var path = require('path');

var platform = os.platform();
if (platform !== 'darwin' && platform !=='linux' && platform !== 'win32') {
  console.error('Unsupported platform.');
  process.exit(1);
}

var arch = os.arch();
if (platform === 'darwin' && arch !== 'x64' && arch !== 'arm64') {
  console.error('Unsupported architecture.');
  process.exit(1);
}

var ffprobePath = path.join(
  __dirname,
  'bin',
  platform,
  arch,
  platform === 'win32' ? 'ffprobe.exe' : 'ffprobe'
);

exports.path = ffprobePath;

要点:

先判断系统,再判断平台。

容错处理。

猜你喜欢

转载自blog.csdn.net/u012787757/article/details/130380797