name: 'Setup Node.js and Dependencies' description: '安装 Node.js 和项目依赖,支持缓存优化' inputs: node-version: description: 'Node.js 版本' required: false default: '20' registry-url: description: 'NPM registry URL' required: false default: 'https://registry.npmjs.org/' install-global: description: '要安装的全局依赖包 (如: "cross-env zx" 或 "cross-env zx tsx")' required: false default: 'cross-env zx tsx' install-python-setuptools: description: '是否在 macOS 上安装 python-setuptools' required: false default: 'false' install-scripts: description: '是否安装 scripts 目录下的依赖' required: false default: 'false' runs: using: 'composite' steps: - name: Install Node.js uses: actions/setup-node@v4.0.0 with: node-version: ${{ inputs.node-version }} registry-url: ${{ inputs.registry-url }} - name: Install global dependencies if: inputs.install-global != '' shell: bash run: npm install -g ${{ inputs.install-global }} # Windows 特定设置:避免长路径问题 - name: Configure Windows npm settings if: runner.os == 'Windows' shell: bash run: | npm config set fund false npm config set audit false - name: Install python-setuptools (macOS) if: inputs.install-python-setuptools == 'true' && runner.os == 'macOS' shell: bash run: brew install python-setuptools # 缓存所有依赖(统一策略) - name: Cache workspace dependencies uses: actions/cache@v4 id: cache-workspace-deps with: path: | node_modules scripts/node_modules packages/shared/node_modules packages/core/node_modules packages/web/node_modules packages/electron/node_modules ~/.cache/electron ~/.cache/electron-builder ~/Library/Caches/electron ~/Library/Caches/electron-builder ~/AppData/Local/electron/Cache ~/AppData/Local/electron-builder/Cache key: ${{ runner.os }}-node-workspace-${{ hashFiles('package-lock.json', 'scripts/package-lock.json', 'packages/*/package-lock.json') }} restore-keys: | ${{ runner.os }}-node-workspace- # 安装所有依赖 - name: Install workspace dependencies if: steps.cache-workspace-deps.outputs.cache-hit != 'true' shell: bash run: | # 使用统一的安装参数 INSTALL_FLAGS="--no-audit --no-fund" # Windows特殊处理:使用legacy-peer-deps避免依赖冲突 if [ "${{ runner.os }}" = "Windows" ]; then INSTALL_FLAGS="$INSTALL_FLAGS --legacy-peer-deps" fi # 根目录安装时排除problematic native packages if [ "${{ runner.os }}" = "Windows" ]; then # Windows上先安装基础依赖,排除node-pty等原生包 npm install $INSTALL_FLAGS --ignore-scripts else npm install $INSTALL_FLAGS fi if [ "${{ inputs.install-scripts }}" = "true" ]; then (cd scripts && npm install $INSTALL_FLAGS) fi (cd packages/shared && npm install --include=optional $INSTALL_FLAGS) (cd packages/web && npm install --include=optional $INSTALL_FLAGS && npm install favicons $INSTALL_FLAGS) # 根据平台处理包含原生依赖的包 if [ "${{ runner.os }}" = "Windows" ]; then # Windows上单独安装原生依赖包,避免工作区冲突 (cd packages/core && npm install --include=optional $INSTALL_FLAGS --no-workspaces) (cd packages/electron && npm install --include=optional $INSTALL_FLAGS --no-workspaces) else # 其他平台正常安装 (cd packages/core && npm install --include=optional $INSTALL_FLAGS) (cd packages/electron && npm install --include=optional $INSTALL_FLAGS) fi