node js node-exec-promise.exec 命令通过命令行调用 retire.js 有效,但以错误结



尝试运行下面部分中提到的命令,它旨在在某些javascript文件/库上运行off.js并将输出通过管道传输到json文件中。该命令工作并在 json 文件中生成输出,但以错误结束。

当我通过在命令行中复制粘贴命令直接尝试相同的命令时,它可以正常工作。

我也尝试了exexSync版本和shelljs,尽管在Shelljs中也出现了错误。但是,我无法理解导致问题的原因。

使用 node-exec-promise.exec :

exec('node C:/Users/walee/AppData/Roaming/npm/node_modules/retire/bin/retire --outputformat json --outputpath D:/Internship/local/testing/1.json').
then(function(retire_out){
console.log('Retire Command Success ');
console.log(' Retire_out Result  ',retire_out);
return retire_out;
}, function(err){
console.error(err)
}

使用 Shelljs:

if (shell.exec('node 
C:/Users/walee/AppData/Roaming/npm/node_modules/retire/bin/retire --outputformat json --outputpath D:/Internship/local/testing/1.json').code !== 0) {
shell.echo('Error: Git commit failed');
shell.exit(1);
}

预期结果是一个 Json 文件,其中包含 retire.js 发现的已知漏洞,该文件将填充并具有有效的 json。

但是,我在命令行中收到以下错误:

{ Error: Command failed: node C:/Users/walee/AppData/Roaming/npm/node_modules/retire/bin/retire --outputformat json --outputpath D:/Internship/local/testing/1.json
at ChildProcess.exithandler (child_process.js:294:12)
at ChildProcess.emit (events.js:189:13)
at maybeClose (internal/child_process.js:970:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:259:5)
killed: false,
code: 13,
signal: null,
cmd:
'node C:/Users/walee/AppData/Roaming/npm/node_modules/retire/bin/retire --outputformat json --outputpath D:/Internship/local/testing/1.json' }

因为如果发现漏洞retire退出代码为 13,所以您需要在任何.then之前.catch"允许"退出代码 13。

另外,由于您已经为retire指定了--outputpath,输出不会转到 stdout,因此您需要读取.then代码中的输出文件 - 此外,由于退休会返回"错误"(13) - node-exec-promise 甚至不允许您在传递给.catch代码的err中访问 stdout/stderr!

以下是伪代码,因为我并不完全熟悉node-exec-promise:

exec('node C:/Users/walee/AppData/Roaming/npm/node_modules/retire/bin/retire --outputformat json --outputpath D:/Internship/local/testing/1.json')
.catch(err => {
if (err.code !== 13) {
throw err;
}
})
.then(() => {
// read the output file if you want to display it here 
})
.catch(err => { 
//handle other errors here
});

最新更新