您可以简单地使用节点Child Process核心模块,或者至少使用它来构建您自己的模块。
Child Process
The child_process module provides the ability to spawn child processes in a manner that is similar, but not identical, to popen(3). This capability is primarily provided by the child_process.spawn() function:
特别是exec()
方法。
child_process.exec()
spawns a shell and runs a command within that shell, passing the stdout and stderr to a callback function when complete.
这里是来自文档的示例。
Spawns a shell then executes the command within that shell, buffering any generated output.
const exec = require('child_process').exec;
exec('cat *.js bad_file | wc -l', (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
尝试FS-EXTRA:https://www.npmjs.com/package/fs-extra – datafunk