diff --git a/.git-hooks/README.md b/.git-hooks/README.md new file mode 100644 index 0000000..948b79e --- /dev/null +++ b/.git-hooks/README.md @@ -0,0 +1,38 @@ +# Git Hooks + +This directory contains Git hooks for the project. These hooks help maintain code quality and ensure that only properly tested code gets committed and pushed to the repository. + +## Available Hooks + +### Pre-commit Hook +This hook runs automatically before each commit and: +- Lints JavaScript files with ESLint to catch code quality issues +- Formats code with Prettier to ensure consistent style +- Runs tests related to the changed files + +### Pre-push Hook +This hook runs automatically before each push and: +- Runs all tests with coverage +- Ensures code coverage meets minimum thresholds (80%) +- Checks for skipped tests (tests marked with .skip or .only) + +## How to Enable These Hooks + +To use these hooks, you need to configure Git to look for hooks in this directory instead of the default `.git/hooks` directory. Run the following command from the root of the repository: + +```bash +git config core.hooksPath .git-hooks +``` + +This is a one-time setup per clone of the repository. + +## Bypassing Hooks + +In emergency situations, you can bypass the hooks using the `--no-verify` flag: + +```bash +git commit --no-verify +git push --no-verify +``` + +However, this is not recommended as it bypasses the quality checks. diff --git a/.git-hooks/pre-commit b/.git-hooks/pre-commit new file mode 100755 index 0000000..bdfe13e --- /dev/null +++ b/.git-hooks/pre-commit @@ -0,0 +1,50 @@ +#!/bin/sh + +# This is the pre-commit hook that runs before each commit +# It ensures that code quality standards are met before allowing the commit to proceed + +# Exit immediately if a command exits with a non-zero status +set -e + +# Get the root directory of the git repository +ROOT_DIR="$(git rev-parse --show-toplevel)" + +# Check if the staged files include JavaScript files in the javascriptapp directory +STAGED_JS_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E '^javascriptapp/.*\.js$' || true) + +# If no JavaScript files in the javascriptapp directory are staged, exit early +if [ -z "$STAGED_JS_FILES" ]; then + echo "No JavaScript files in javascriptapp directory were staged. Skipping pre-commit hooks." + exit 0 +fi + +# Change directory to the javascriptapp directory +cd "$ROOT_DIR/javascriptapp" + +# Extract just the filenames without the javascriptapp/ prefix +JS_FILES_WITHOUT_PREFIX=$(echo "$STAGED_JS_FILES" | sed 's|^javascriptapp/||') + +echo "Running pre-commit hooks for JavaScript files..." + + # Run ESLint on staged JavaScript files + echo "Running ESLint..." + npx eslint --fix $JS_FILES_WITHOUT_PREFIX + + # Run Prettier on staged JavaScript files + echo "Running Prettier..." + npx prettier --write $JS_FILES_WITHOUT_PREFIX + + # Run Jest tests related to the changed files + echo "Running Jest tests..." + npx jest --findRelatedTests --passWithNoTests $JS_FILES_WITHOUT_PREFIX + + # Add the fixed files back to the staging area + cd "$ROOT_DIR" + for file in $STAGED_JS_FILES; do + git add "$file" + done + + echo "Pre-commit hooks completed successfully!" + +# If all checks pass, the commit will proceed +# If any check fails, the commit will be aborted diff --git a/.git-hooks/pre-push b/.git-hooks/pre-push new file mode 100755 index 0000000..7d1fc50 --- /dev/null +++ b/.git-hooks/pre-push @@ -0,0 +1,40 @@ +#!/bin/sh + +# This is the pre-push hook that runs before each push to the remote repository +# It ensures that all tests pass and meet coverage thresholds before allowing the push to proceed + +# Exit immediately if a command exits with a non-zero status +set -e + +# Get the root directory of the git repository +ROOT_DIR="$(git rev-parse --show-toplevel)" + +# Check if we're pushing changes to the javascriptapp directory +CHANGED_FILES=$(git diff --name-only HEAD@{1} HEAD 2>/dev/null | grep -E '^javascriptapp/' || true) + +# If no files in the javascriptapp directory are changed, exit early +if [ -z "$CHANGED_FILES" ]; then + echo "No changes to javascriptapp directory. Skipping pre-push hooks." + exit 0 +fi + +# Change directory to the javascriptapp directory +cd "$ROOT_DIR/javascriptapp" + +echo "Running pre-push hooks for javascriptapp..." + +# Run Jest tests with coverage +echo "Running Jest tests with coverage..." +npx jest --coverage --coverageThreshold='{"global":{"statements":80,"branches":80,"functions":80,"lines":80}}' + +# Check for skipped tests (tests marked with .skip or .only) +echo "Checking for skipped or focused tests..." +if grep -r --include="*.test.js" --include="*.spec.js" -E "(describe|it|test)\.only|\.skip" tests/; then + echo "Error: Found skipped or focused tests. Please remove .skip or .only from your tests." + exit 1 +fi + +echo "Pre-push hooks completed successfully!" + +# If all checks pass, the push will proceed +# If any check fails, the push will be aborted diff --git a/README.md b/README.md index 8e9ad27..d5ae4c8 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,19 @@ This repository contains various projects and examples used for AI experimentati - **javascriptapp/**: A simple JavaScript application with tests and coverage reports. - **mermaid_diagrams/**: Examples of using Mermaid to create diagrams for various architectures and workflows. +- **.git-hooks/**: Git hooks for code quality and testing. See [Git Hooks](#git-hooks) section below. + +## Git Hooks + +This repository includes Git hooks to ensure code quality and proper testing. The hooks are located in the `.git-hooks` directory. + +To enable these hooks, run the following command from the root of the repository: + +```bash +git config core.hooksPath .git-hooks +``` + +For more information about the available hooks and how to use them, see the [.git-hooks/README.md](.git-hooks/README.md) file. ## Note diff --git a/javascriptapp/README.md b/javascriptapp/README.md index c6ea3a0..6854c5f 100644 --- a/javascriptapp/README.md +++ b/javascriptapp/README.md @@ -77,11 +77,16 @@ git push --no-verify ``` ### How the Hooks Work -The hooks are implemented as shell scripts in the `.git/hooks` directory: +The hooks are implemented as shell scripts in the `.git-hooks` directory at the root of the repository: - They only run on JavaScript files in the javascriptapp directory - The pre-commit hook runs ESLint, Prettier, and related tests - The pre-push hook runs all tests with coverage checks +To enable these hooks, run the following command from the root of the repository: +``` +git config core.hooksPath .git-hooks +``` + ## Technologies Used Express.js - Backend server diff --git a/javascriptapp/server.js b/javascriptapp/server.js index 94cdfb6..0556717 100644 --- a/javascriptapp/server.js +++ b/javascriptapp/server.js @@ -10,7 +10,7 @@ const API_KEY = process.env.OPENWEATHER_API_KEY; // Serve static files from the "public" folder app.use(express.static(path.join(__dirname, 'public'))); -// GET /api/weather?city=London - Endpoint to fetch weather data +// GET /api/weather?city=London - Endpoint to fetch weather data from OpenWeather API app.get('/api/weather', async (req, res) => { try { const city = req.query.city;