Files
syops/.githooks/_common.sh
T

70 lines
1.6 KiB
Bash

# shellcheck shell=bash
# 供 pre-commit / pre-push 引用;勿直接执行。
hook_should_skip() {
[[ "${SKIP_GIT_HOOKS:-}" == "1" ]] || [[ "${SKIP_FRONTEND_HOOKS:-}" == "1" ]]
}
frontend_paths_in() {
local file_list="${1:-}"
echo "${file_list}" | grep -qE '^frontend/'
}
ensure_frontend_deps() {
local frontend_dir="${1}/frontend"
if [[ ! -d "${frontend_dir}/node_modules" ]]; then
echo "[git-hook] frontend/node_modules 不存在,请先执行:"
echo " cd frontend && npm install"
return 1
fi
return 0
}
run_frontend_typecheck() {
local root_dir="${1}"
echo "[pre-commit] 检测到 frontend/ 变更,运行 vue-tsc 类型检查..."
if ! ensure_frontend_deps "${root_dir}"; then
return 1
fi
(
cd "${root_dir}/frontend"
npx vue-tsc -b --pretty false
)
}
run_frontend_build() {
local root_dir="${1}"
echo "[pre-push] 检测到将要推送的提交包含 frontend/ 变更,运行 npm run build..."
if ! ensure_frontend_deps "${root_dir}"; then
return 1
fi
(
cd "${root_dir}/frontend"
npm run build
)
}
ensure_commitlint_deps() {
local root_dir="${1}"
if [[ ! -d "${root_dir}/node_modules/@commitlint/cli" ]]; then
echo "[commit-msg] 根目录 node_modules 不存在,请先执行:"
echo " cd ${root_dir}"
echo " npm install"
echo " 或重新运行: ./scripts/install_git_hooks.sh"
return 1
fi
return 0
}
run_commitlint() {
local root_dir="${1}"
local msg_file="${2}"
if ! ensure_commitlint_deps "${root_dir}"; then
return 1
fi
(
cd "${root_dir}"
npx --no-install commitlint --edit "${msg_file}"
)
}