feat: 增加清理项目根 .pnpm-store 残留的选项并优化 store 目录配置

This commit is contained in:
Lyda
2025-09-18 18:22:54 +08:00
parent af648e65f2
commit d35b42c064

View File

@@ -54,6 +54,11 @@ inputs:
description: '附加到默认安装命令的参数(当未提供 install-command 时生效)' description: '附加到默认安装命令的参数(当未提供 install-command 时生效)'
required: false required: false
default: '' default: ''
clean-project-store:
description: '在安装前清理项目根的 .pnpm-store 残留true/false'
required: false
default: 'false'
cache-hash: cache-hash:
description: '缓存hash值推荐使用hashFiles计算' description: '缓存hash值推荐使用hashFiles计算'
@@ -140,6 +145,11 @@ runs:
if [[ "$MODE" == "node_modules" ]]; then if [[ "$MODE" == "node_modules" ]]; then
CACHE_PATH="${{ inputs.node-modules-path }}" CACHE_PATH="${{ inputs.node-modules-path }}"
# 即使只缓存 node_modulespnpm 也会使用 store。为避免在项目根生成 .pnpm-store这里同样固定 PNPM_STORE_DIR
if [[ "$MANAGER" == "pnpm" ]]; then
DEFAULT_PNPM_STORE="${RUNNER_TEMP:-$HOME}/.pnpm-store"
echo "PNPM_STORE_DIR=${DEFAULT_PNPM_STORE}" >> "$GITHUB_ENV"
fi
else else
case "$MANAGER" in case "$MANAGER" in
"npm") "npm")
@@ -198,6 +208,25 @@ runs:
if: (inputs.cache-mode == 'node_modules' && steps.cache.outputs.cache-hit != 'true') || (inputs.cache-mode == 'store') if: (inputs.cache-mode == 'node_modules' && steps.cache.outputs.cache-hit != 'true') || (inputs.cache-mode == 'store')
shell: bash shell: bash
run: | run: |
# 若使用 pnpm在本步骤内始终显式设置 PNPM_STORE_DIR覆盖可能存在的相对配置
if [[ "${{ inputs.package-manager }}" == "pnpm" ]]; then
if [[ "${{ inputs.cache-mode }}" == "store" ]]; then
# store 模式cache-path 的 path 即为期望的 store 目录
export PNPM_STORE_DIR="${{ steps.cache-path.outputs.path }}"
else
# node_modules 模式:不要回退到 cache-path那是 node_modules 目录),而是使用 RUNNER_TEMP/HOME
export PNPM_STORE_DIR="${PNPM_STORE_DIR:-${RUNNER_TEMP:-$HOME}/.pnpm-store}"
fi
echo "🧩 已设置 PNPM_STORE_DIR=${PNPM_STORE_DIR}"
fi
# 可选清理:如启用并发现项目根存在残留的 .pnpm-store且与目标目录不同则清理
if [[ "${{ inputs.package-manager }}" == "pnpm" && "${{ inputs.clean-project-store }}" == "true" ]]; then
if [[ -d ".pnpm-store" && "${PNPM_STORE_DIR}" != "$PWD/.pnpm-store" ]]; then
echo "🧹 清理项目根的残留 .pnpm-store目标store为 ${PNPM_STORE_DIR}"
rm -rf .pnpm-store || true
fi
fi
# 如果提供了自定义安装命令,使用自定义命令 # 如果提供了自定义安装命令,使用自定义命令
if [[ -n "${{ inputs.install-command }}" ]]; then if [[ -n "${{ inputs.install-command }}" ]]; then
echo "🔧 使用自定义安装命令: ${{ inputs.install-command }}" echo "🔧 使用自定义安装命令: ${{ inputs.install-command }}"