style: format #45
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: 'Publish npm Packages' | |
| on: | |
| push: | |
| branches: | |
| - main | |
| paths: | |
| - 'packages/**' | |
| - 'pnpm-lock.yaml' | |
| - '.github/workflows/release-npm.yml' | |
| workflow_dispatch: | |
| permissions: | |
| contents: read | |
| id-token: write | |
| concurrency: | |
| group: npm-publish-${{ github.ref }} | |
| cancel-in-progress: false | |
| jobs: | |
| publish: | |
| if: ${{ github.repository_owner == 'MaaXYZ' }} | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 | |
| with: | |
| fetch-depth: 0 | |
| - uses: pnpm/action-setup@f40ffcd9367d9f12939873eb1018b921a783ffaa # v4 | |
| with: | |
| version: 10.34.5 | |
| run_install: false | |
| - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4 | |
| with: | |
| node-version: 24 | |
| cache: 'pnpm' | |
| registry-url: 'https://registry.npmjs.org' | |
| - name: Install dependencies | |
| run: pnpm install --frozen-lockfile | |
| - name: Build packages | |
| run: pnpm build:packages | |
| - name: Preflight package release | |
| env: | |
| NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} | |
| run: | | |
| set -euo pipefail | |
| if [ -z "${NODE_AUTH_TOKEN}" ]; then | |
| echo "NPM_TOKEN is not set." | |
| exit 1 | |
| fi | |
| npm whoami >/dev/null | |
| package_exists() { | |
| local package_ref="$1" | |
| local output | |
| if output=$(npm view "${package_ref}" version 2>&1); then | |
| return 0 | |
| fi | |
| if grep -q 'E404' <<< "${output}"; then | |
| return 1 | |
| fi | |
| echo "${output}" >&2 | |
| exit 1 | |
| } | |
| packages=( | |
| "packages/maa-log-kernel" | |
| "packages/maa-log-parser" | |
| "packages/maa-log-runtime" | |
| "packages/maa-log-adapter" | |
| "packages/maa-log-tools" | |
| ) | |
| pack_root="${RUNNER_TEMP}/mla-npm-packages" | |
| plan_file="${RUNNER_TEMP}/mla-npm-publish-plan.tsv" | |
| mkdir -p "${pack_root}" | |
| : > "${plan_file}" | |
| for index in "${!packages[@]}"; do | |
| pkg_dir="${packages[$index]}" | |
| echo "==== ${pkg_dir} ====" | |
| pkg_name=$(node -p "require('./${pkg_dir}/package.json').name") | |
| pkg_ver=$(node -p "require('./${pkg_dir}/package.json').version") | |
| package_ref="${pkg_name}@${pkg_ver}" | |
| if package_exists "${package_ref}"; then | |
| echo "Skip existing: ${package_ref}" | |
| continue | |
| fi | |
| pack_dir="${pack_root}/${index}" | |
| mkdir -p "${pack_dir}" | |
| pnpm --dir "${pkg_dir}" pack --pack-destination "${pack_dir}" | |
| mapfile -t tarballs < <(find "${pack_dir}" -maxdepth 1 -type f -name '*.tgz') | |
| if [ "${#tarballs[@]}" -ne 1 ]; then | |
| echo "Expected one tarball for ${package_ref}, found ${#tarballs[@]}." >&2 | |
| exit 1 | |
| fi | |
| printf '%s\t%s\n' "${tarballs[0]}" "${package_ref}" >> "${plan_file}" | |
| done | |
| - name: Publish preflighted packages | |
| env: | |
| NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} | |
| run: | | |
| set -euo pipefail | |
| plan_file="${RUNNER_TEMP}/mla-npm-publish-plan.tsv" | |
| if [ ! -s "${plan_file}" ]; then | |
| echo "All package versions already exist." | |
| exit 0 | |
| fi | |
| package_exists() { | |
| local package_ref="$1" | |
| local output | |
| if output=$(npm view "${package_ref}" version 2>&1); then | |
| return 0 | |
| fi | |
| if grep -q 'E404' <<< "${output}"; then | |
| return 1 | |
| fi | |
| echo "${output}" >&2 | |
| exit 1 | |
| } | |
| while IFS=$'\t' read -r tarball package_ref; do | |
| if package_exists "${package_ref}"; then | |
| echo "Skip concurrently published package: ${package_ref}" | |
| continue | |
| fi | |
| echo "Publish ${package_ref}" | |
| npm publish "${tarball}" --access public --provenance | |
| done < "${plan_file}" |