[英]How do I pass the result from a command to another command's option in a single npm run script?
[英]How do i pass result from waited npm script to bash script?
在我的 npm 腳本中,我有以下內容:
#!/usr/bin/env node
import { main } from './main';
import { CONFIG } from '../config';
(async () => {
const res = await main(CONFIG);
process.stdout.write(res.join('\n'));
return res;
})();
現在想根據 bash 腳本中返回的內容做一些事情。 嘗試這樣做將無法正常工作:
npm run update-imports &
PID=$!
UpdateResult=$(wait $PID)
if [ -z "$UpdateResult" ];
then
echo "No imports updated, committing changes"
else
echo "Check the following files:\n ${UpdateResult}"
exit 1
fi
簡而言之 - 如果沒有返回任何內容或返回空字符串 - 繼續執行腳本,否則 - 退出腳本並發出警告。
我如何讓它工作?
bash, wait
返回進程的退出值。 不是您期望的標准 output。 您可以使用process.exit(value)
返回一個值。
如果想抓取並處理node程序的標准output,請看問題的回答: How do I set a variable to the output of a command in Bash?
這應該做的工作:
UpdateResult=$(npm run update-imports)
if [ -z "$UpdateResult" ];
then
echo "No imports updated, committing changes"
else
echo "Check the following files:\n ${UpdateResult}"
exit 1
fi
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.