[英]Exit status from shell scripts in npm run scripts on windows
我有一個非常簡單的 shell 腳本test.sh
#!/usr/bin/env bash
exit 1
我從package.json
test
運行腳本中調用它
"scripts": {
"test": "test.sh && echo \"unexpected output\""
},
運行npm test
我得到這個:
$ npm test
> testtest@1.0.0 test C:\Users\[path]\testtest
> test.sh && echo "unexpected output"
"unexpected output"
似乎 npm 並不關心 test.sh 的非零退出代碼。 我沒想到會看到“意外的輸出”。
當"test"
命令執行的步驟之一(在本例中為test.sh
)因錯誤退出時,如何停止執行"test"
命令?
在我的 package.json: "test": "test.sh && echo $?",
這是輸出:
$ npm test
> testtest@1.0.0 test C:\Users\[path]\testtest
> test.sh && echo $?
$?
有了這個: "test": "test.sh && echo \\"$?\\"",
我明白了:
$ npm test
> testtest@1.0.0 test C:\Users\[path]\testtest
> test.sh && echo "$?"
"$?"
作為完整性檢查,我在test.sh
退出后添加了一個 echo。 謝天謝地它不打印:)
#!/usr/bin/env bash
exit 1
echo "This should not print"
“這不應該打印”文本永遠不會出現在我的控制台中。
實際上,在exit 1
之前添加echo
也不會向我的 GitBash 控制台打印任何內容。 相反,它會打印到 npm 啟動的臨時 cmd 窗口。 所以我猜退出狀態在那個 cmd 會話中丟失了。
這對我有用:
"scripts": {
"test": "bash test.sh && echo \"unexpected output\""
},
使用額外的“bash”,腳本在當前的 Git Bash 中運行。 不打印“意外輸出”。 如果我省略“bash”,則會打開一個新的 Bash 窗口,並且我也會得到“意外輸出”。
$ npm test
> npm2@1.0.0 test D:\Martin\dev\npm-test\npm2
> bash test.sh && echo "unexpected output"
Hi from script! Next: exit 1
npm ERR! Test failed. See above for more details.
附帶說明一下,如果您正在開發跨平台的 Node.js 項目,最好避免使用 Bash/Cmd/Powershell 腳本。 只需使用 Node.js 腳本,而無需安裝其他工具。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.