From 04ba84faefce71faa885d5c25d537d3a03741eff Mon Sep 17 00:00:00 2001 From: Kari Lintulaakso Date: Thu, 20 Nov 2025 15:36:46 +0200 Subject: [PATCH] Add pre-commit checks and cache lint workflow --- .github/workflows/lint.yaml | 24 ++++++++---------------- .husky/README.md | 9 +++++++++ .husky/pre-commit | 13 +++++++++++++ package-lock.json | 17 +++++++++++++++++ package.json | 4 +++- 5 files changed, 50 insertions(+), 17 deletions(-) create mode 100644 .husky/README.md create mode 100755 .husky/pre-commit diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml index cb2d8a4b..28cca471 100644 --- a/.github/workflows/lint.yaml +++ b/.github/workflows/lint.yaml @@ -15,22 +15,14 @@ jobs: - uses: actions/setup-node@v4 with: node-version: 20 + cache: 'npm' + cache-dependency-path: | + package-lock.json + backend/package-lock.json + frontend/package-lock.json - name: 'Install root dependencies' run: npm run setup - - name: 'Run cypress linter' - run: npm run lint:cypress - - name: 'Run backend linter' - working-directory: 'backend' + - name: 'Run lint (root, backend, frontend, cypress)' run: npm run lint - - name: 'Run backend tsc' - working-directory: 'backend' - run: npx tsc --noEmit - - name: 'Install frontend dependencies' - working-directory: 'frontend' - run: npm ci - - name: 'Run frontend linter' - working-directory: 'frontend' - run: npm run lint - - name: 'Run frontend tsc' - working-directory: 'frontend' - run: npx tsc --noEmit + - name: 'Run tsc (backend & frontend)' + run: npm run tsc diff --git a/.husky/README.md b/.husky/README.md new file mode 100644 index 00000000..d90112b9 --- /dev/null +++ b/.husky/README.md @@ -0,0 +1,9 @@ +# Husky hooks + +## Pre-commit +- Runs `npm run lint` and `npm run tsc` to align with CI. +- Requires dependencies installed via `npm run setup`. + +## Skipping in emergencies +- Temporarily bypass by prefixing commands with `HUSKY_SKIP_CHECKS=1` or `HUSKY=0`, e.g. `HUSKY=0 git commit -m "message"`. +- Re-enable hooks after resolving issues. diff --git a/.husky/pre-commit b/.husky/pre-commit new file mode 100755 index 00000000..00a0eb21 --- /dev/null +++ b/.husky/pre-commit @@ -0,0 +1,13 @@ +#!/usr/bin/env sh + +if [ "$HUSKY_SKIP_CHECKS" = "1" ]; then + echo "Husky pre-commit checks skipped via HUSKY_SKIP_CHECKS." + exit 0 +fi + +command -v npm >/dev/null 2>&1 || { + echo "npm not found. Install dependencies before committing." >&2 + exit 1 +} + +npm run lint && npm run tsc diff --git a/package-lock.json b/package-lock.json index 65419937..a638cb8a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,6 +18,7 @@ "eslint-plugin-mocha": "^10.5.0", "eslint-plugin-prettier": "^5.2.1", "globals": "^15.12.0", + "husky": "^9.1.7", "prettier": "^3.3.3", "supertest": "^7.0.0", "typescript": "^5.9.3" @@ -5139,6 +5140,22 @@ "node": ">=8.12.0" } }, + "node_modules/husky": { + "version": "9.1.7", + "resolved": "https://registry.npmjs.org/husky/-/husky-9.1.7.tgz", + "integrity": "sha512-5gs5ytaNjBrh5Ow3zrvdUUY+0VxIuWVL4i9irt6friV+BqdCfmV11CQTWMiBYWHbXhco+J1kHfTOUkePhCDvMA==", + "dev": true, + "license": "MIT", + "bin": { + "husky": "bin.js" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/typicode" + } + }, "node_modules/ieee754": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz", diff --git a/package.json b/package.json index 166c1b8d..0229b4d1 100644 --- a/package.json +++ b/package.json @@ -60,7 +60,8 @@ "config:anon:host": "sed -i -e \"s|nowdb-db-dev|nowdb-db-anon|g\" .anon.env", "config:anon:coordinator": "sed -i -e \"s|IZ|TEST-SU|g\" .anon.env", "config:dev": "cp -n .template.env .dev.env && npm run config:geonames -- .dev.env", - "config:geonames": "sed -i -e \"s|GEONAMES_USERNAME: \\\"\\\"|GEONAMES_USERNAME: $GEONAMES_USERNAME|g\"" + "config:geonames": "sed -i -e \"s|GEONAMES_USERNAME: \\\"\\\"|GEONAMES_USERNAME: $GEONAMES_USERNAME|g\"", + "prepare": "husky" }, "author": "", "devDependencies": { @@ -74,6 +75,7 @@ "eslint-plugin-mocha": "^10.5.0", "eslint-plugin-prettier": "^5.2.1", "globals": "^15.12.0", + "husky": "^9.1.7", "prettier": "^3.3.3", "supertest": "^7.0.0", "typescript": "^5.9.3"