我制作了一个交互式预推送 git 挂钩,在推送之前允许我在 npm 包中更改版本。
#!/bin/bash
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
echo "Pushing ${current_branch}"
current_version=$(npm run version -s)
echo "Current version: $current_version"
patch_version=$(npx semver $current_version -i patch)
minor_version=$(npx semver $current_version -i minor)
major_version=$(npx semver $current_version -i major)
# Choose the version bump type
PS3="Select the version bump type: "
select bump_type in "patch - Bump into ${patch_version}" "minor - Bump into ${minor_version}" "major - Bump into ${major_version}" "none - Keep Same"; do
case $bump_type in
"patch"*)
# Increment version based on the chosen bump type
new_version="patch"
break
;;
"minor"*)
new_version="minor"
break
;;
"major"*)
new_version="major"
break
;;
"none"*)
echo "No version selected, skipping bump."
exit 0
;;
*)
echo "Invalid selection, please choose a valid option."
;;
esac
done
echo "Bumping version to $new_version"
# Update package.json with the new version
new_version=$(npm version ${new_version} --force --silent)
echo "Version bumped to $new_version"
echo "Keep package-lock.json up to spec"
npm install
git commit -m "AutoBump Version" package.json package-lock.json
但是一旦我进入我的分支:
Pushing dev
Current version: 1.0.3
.husky/pre-push: 17: Syntax error: "do" unexpected
husky - pre-push script failed (code 2)
error: απέτυχε η δημοσίευση κάποιων ref στο 'github.com:ellakcy/fa-checkbox.git'
似乎没有运行,但如果手动运行则可以正常工作:
bash .husky/pre-push
Pushing dev
Current version: 1.0.3
1) patch - Bump into 1.0.4
2) minor - Bump into 1.1.0
3) major - Bump into 2.0.0
4) none - Keep Same
Select the version bump type:
作为解决问题的一种方法,我尝试了这个答案:https://stackoverflow.com/a/22585746/4706711
但似乎没有解决问题。
为了让 husky 用于
bash
运行钩子,请更改.husky/_/h
其中包含:将其更改为:
如果无法进行上述更改,您可以将以下行放在 .husky/pre-push 的开头:
git help hooks
说预推送调用是所以你的
select
语句正在读取来自标准输入的第一个推送的引用。在 的终止符 stmt
<&2
之后添加一个,让它尝试读取 fd 2,我收集的 stderr 仍然是你的终端。select
done