您可以簡單地使用節點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