chore(ci): add npm-audit-fix workflow and helper script, expose audit-fix npm script

This commit is contained in:
Nawaz Dhandala
2025-10-29 13:25:31 +00:00
parent 87dc9d88d0
commit 5e7a3795c7
3 changed files with 84 additions and 0 deletions

48
.github/workflows/npm-audit-fix.yml vendored Normal file
View File

@@ -0,0 +1,48 @@
name: NPM Audit Fix
on:
push:
branches:
- master
permissions:
contents: write
jobs:
npm-audit-fix:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20
- name: Run npm audit fix across packages
run: npm run audit-fix
- name: Detect changes
id: changes
run: |
if git status --porcelain | grep .; then
echo "has_changes=true" >> $GITHUB_OUTPUT
else
echo "has_changes=false" >> $GITHUB_OUTPUT
fi
- name: Commit changes
if: steps.changes.outputs.has_changes == 'true'
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add -A
git commit -m "chore: npm audit fix"
- name: Push changes
if: steps.changes.outputs.has_changes == 'true'
run: |
git push origin HEAD

35
npm-audit-fix.sh Normal file
View File

@@ -0,0 +1,35 @@
#!/bin/bash
set -o nounset
set -o pipefail
ROOT_DIR="$(pwd)"
EXIT_CODE=0
while IFS= read -r -d '' package_json; do
PROJECT_DIR="$(dirname "$package_json")"
DISPLAY_DIR="${PROJECT_DIR#./}"
echo "Running npm audit fix in ${DISPLAY_DIR:-.}"
if ! cd "$PROJECT_DIR"; then
echo "Skipping ${DISPLAY_DIR:-.}: cannot change directory" >&2
EXIT_CODE=1
continue
fi
if [ ! -f "package-lock.json" ] && [ ! -f "npm-shrinkwrap.json" ]; then
echo "Skipping ${DISPLAY_DIR:-.}: no package-lock.json or npm-shrinkwrap.json"
cd "$ROOT_DIR"
continue
fi
if ! npm audit fix; then
echo "npm audit fix failed in ${DISPLAY_DIR:-.}" >&2
EXIT_CODE=1
fi
cd "$ROOT_DIR"
done < <(find . -name package.json -not -path '*/node_modules/*' -print0)
exit $EXIT_CODE

View File

@@ -38,6 +38,7 @@
"uninstall": "bash uninstall.sh",
"clean-modules": "bash remove-node-modules.sh",
"install-modules": "bash install-node-modules.sh",
"audit-fix": "bash npm-audit-fix.sh",
"lint": "export NODE_OPTIONS='--max-old-space-size=32768' && npx eslint . --cache",
"fix-lint": "export NODE_OPTIONS='--max-old-space-size=32768' && npx eslint . --fix --cache --debug",
"fix": "npm run fix-lint",