From fd26253e7afc7849f5652920a225d7709f3ba113 Mon Sep 17 00:00:00 2001 From: dw-0 Date: Sat, 5 Oct 2024 10:45:39 +0200 Subject: [PATCH] Feature/set up husky and pre-commit hook for linting and format check (#3867) * Set up husky and pre-commit hook for linting and format check * Update changelog --------- Signed-off-by: Dominik Willner --- .husky/pre-commit | 6 ++++++ CHANGELOG.md | 4 ++++ DEVELOPMENT.md | 1 - git-hooks/pre-commit | 26 -------------------------- package-lock.json | 16 ++++++++++++++++ package.json | 4 +++- 6 files changed, 29 insertions(+), 28 deletions(-) create mode 100644 .husky/pre-commit delete mode 100755 git-hooks/pre-commit diff --git a/.husky/pre-commit b/.husky/pre-commit new file mode 100644 index 000000000..4bf77522a --- /dev/null +++ b/.husky/pre-commit @@ -0,0 +1,6 @@ +# Run linting and stop the commit process if any errors are found +# --quiet suppresses warnings (temporary until all warnings are fixed) +npm run lint --quiet || exit 1 + +# Check formatting on modified and uncommitted files, stop the commit if issues are found +npm run format:check --uncommitted || exit 1 diff --git a/CHANGELOG.md b/CHANGELOG.md index 00021fbfc..f2464c350 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +### Added + +- Set up a git-hook via `husky` to lint and format the changes before a commit + ### Fixed - Handled an exception in the historical market data gathering of derived currencies diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md index 0c76a2924..b009679ac 100644 --- a/DEVELOPMENT.md +++ b/DEVELOPMENT.md @@ -14,7 +14,6 @@ 1. Run `npm install` 1. Run `docker compose --env-file ./.env -f docker/docker-compose.dev.yml up -d` to start [PostgreSQL](https://www.postgresql.org) and [Redis](https://redis.io) 1. Run `npm run database:setup` to initialize the database schema -1. Run `git config core.hooksPath ./git-hooks/` to setup git hooks 1. Start the [server](#start-server) and the [client](#start-client) 1. Open https://localhost:4200/en in your browser 1. Create a new user via _Get Started_ (this first user will get the role `ADMIN`) diff --git a/git-hooks/pre-commit b/git-hooks/pre-commit deleted file mode 100755 index b2b86eac1..000000000 --- a/git-hooks/pre-commit +++ /dev/null @@ -1,26 +0,0 @@ -#!/bin/bash - -# Will check if "npm run format" is run before executing. -# Called by "git commit" with no arguments. The hook should -# exit with non-zero status after issuing an appropriate message if -# it wants to stop the commit. - -echo "Running npm run format" - -# Run the command and loop over its output -FILES_TO_STAGE="" -i=0 -while IFS= read -r line; do - # Process each line here - ((i++)) - if [ $i -le 2 ]; then - continue - fi - if [[ $line == Done* ]]; then - break - fi - FILES_TO_STAGE="$FILES_TO_STAGE $line" - -done < <(npm run format) -git add $FILES_TO_STAGE -echo "Files formatted. Committing..." diff --git a/package-lock.json b/package-lock.json index b848e8188..5edf7d510 100644 --- a/package-lock.json +++ b/package-lock.json @@ -143,6 +143,7 @@ "eslint-plugin-cypress": "2.15.1", "eslint-plugin-import": "2.29.1", "eslint-plugin-storybook": "0.6.15", + "husky": "9.1.6", "jest": "29.7.0", "jest-environment-jsdom": "29.7.0", "jest-preset-angular": "14.1.0", @@ -20653,6 +20654,21 @@ "node": ">=8.12.0" } }, + "node_modules/husky": { + "version": "9.1.6", + "resolved": "https://registry.npmjs.org/husky/-/husky-9.1.6.tgz", + "integrity": "sha512-sqbjZKK7kf44hfdE94EoX8MZNk0n7HeW37O4YrVGCF4wzgQjp+akPAkfUK5LZ6KuR/6sqeAVuXHji+RzQgOn5A==", + "dev": true, + "bin": { + "husky": "bin.js" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/typicode" + } + }, "node_modules/hyperdyperid": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/hyperdyperid/-/hyperdyperid-1.2.0.tgz", diff --git a/package.json b/package.json index 584407f1d..06fd92a8c 100644 --- a/package.json +++ b/package.json @@ -33,10 +33,11 @@ "format:check": "nx format:check", "format:write": "nx format:write", "help": "nx help", - "lint": "nx lint", + "lint": "nx run-many --target=lint --all", "ng": "nx", "nx": "nx", "postinstall": "prisma generate", + "prepare": "husky", "prisma": "prisma", "replace-placeholders-in-build": "node ./replace.build.js", "start": "node dist/apps/api/main", @@ -188,6 +189,7 @@ "eslint-plugin-cypress": "2.15.1", "eslint-plugin-import": "2.29.1", "eslint-plugin-storybook": "0.6.15", + "husky": "9.1.6", "jest": "29.7.0", "jest-environment-jsdom": "29.7.0", "jest-preset-angular": "14.1.0",