子进程使用
#
1、cmd#
方法cmd(file, arg, options)
#
使用异步调用
const res = cmd('lisa', ['-v'])res.stdout.pipe(process.stdout)await res
#
options可查阅api文档查看。下面列举几个常用的参数
类型 | 描述 | |
---|---|---|
cwd | string | 执行的路径 |
env | object | 环境变量 |
encoding | string / null | 默认为utf8,若为null,则stdout为Buffer |
timeout | number | 超时时间 |
#
2、exec#
方法exec(file, arg, options, onProcess)
#
使用同步调用
const code = await exec('lisa', ['-v'], {}, (line) => { console.log(line)})
#
返回返回该进程的exitCode
#
options与cmd的options一致