节点查看是否已安装程序

Nic*_*lai 6 ffmpeg node.js

你好即时做,需要一个节点应用:ffmpeg, node-acoutstid with fpcalc and eye3D。现在是我的问题,如何查看客户端计算机上是否安装了这些“程序”。

什么是最好的检查方法?

Coo*_*J86 8

用于本地检测的“shell out”

在 macOS / Linux / bash 中,我们通常使用type -pcommand -v(或which如果你做错了)。

在 Windows 中where,您可以使用 like where node.exe

require('child_process').exec('type -p mything', function (err, stdout) {
  console.log(stdout);
});
Run Code Online (Sandbox Code Playgroud)

如果您不想跨平台兼容并且不必担心清理用户输入,则这种幼稚的方法可以工作。

command-exists

在 npm 上有一个包command-exists。我偷看代码,看起来它可能是最简单的跨平台检测,它以小尺寸覆盖边缘情况:

https://github.com/mathisonian/command-exists/blob/master/lib/command-exists.js

var commandExists = require('command-exists');

// invoked without a callback, it returns a promise
commandExists('ls').then(function (command) {
  // proceed
}).catch(function () {
  // command doesn't exist
});
Run Code Online (Sandbox Code Playgroud)