我有一个shell脚本,从基本目录递归搜索package.json并像这样运行npm test.
find "$parent_dir" -name package.json -maxdepth 2 -execdir npm test \;
Run Code Online (Sandbox Code Playgroud)
我想以下列形式拦截测试失败:
npm ERR! Test failed. See above for more details.
npm ERR! not ok code 0
Run Code Online (Sandbox Code Playgroud)
所以我可以让shell脚本退出并显示错误状态.
任何帮助表示赞赏.
又晚了三年,也在寻找解决方案。
终于找到了,如果这些将来对其他人有帮助。我使用了 shell 脚本 $? 命令,返回最后执行的命令的退出状态。即,如果 shell 脚本失败,则返回 1;如果成功,则返回 0。NPM 因此实现了这个返回。
npm run test
if [ $? -eq 0 ]
then
echo "SUCCESS"
else
echo "FAIL"
fi
Run Code Online (Sandbox Code Playgroud)
小智 1
如果我理解正确的话,如果 npm ERR! 你想要执行某些操作或停止执行脚本!已被抛出?这可能有帮助:
if ./somecommand | grep -q 'npm ERR!'; then
#what you want to do
# exit executing the script
return
fi
Run Code Online (Sandbox Code Playgroud)
抱歉,我迟到了两年,刚刚加入 SO 社区。