diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f2de62..c794b24 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,11 +5,30 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html) (as of version 0.1.0). +## [0.4.0] `2026-05-11` + +### Added +- Add new scripts (checks) for the following: eslint, prettier, tsc and vite. +- Add new lib functions add_about_info, split_about_info. +- Add a template script file (including comments) which can be used to create new scripts. + +### Changed +- Improve existing scripts (checks). +- Make more dynamic the addition/removal of checks. +- Make dynamic the `about` section as well, by moving the logic from config.sh and gitcc to each one of the scripts. +- Rename `scripts/examples` directory to `scripts/prepared` +- Improve the extract_version lib function. +- Update the README.md file. + +### Fixed +- Several bug fixes. + + ## [0.3.0] `2026-05-08` ### Changed -- Adjusted the gitcc command to run properly when the git commit is run from the host machine (no container found). -- Moved the example scripts into the scripts/examples directory. +- Adjust the gitcc command to run properly when the git commit is run from the host machine (no container found). +- Move the example scripts into the scripts/examples directory. ## [0.2.0] `2026-05-07` @@ -26,6 +45,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Rename main script to gitcc. - Optimize for performance. + ## [0.1.0] `2025-04-20` ### Added diff --git a/README.md b/README.md index 1ca9aad..bd1bc06 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ --- -- Download the git-commit-check directory and place it within your repo (e.g. `path/to/your/repo/bin/git-commit-check`) +- Download the git-commit-check directory and place it within your repo. - Run the following commands to copy the pre-commit hook script example and the config example files and edit their values according to your project. @@ -15,8 +15,21 @@ cd path/to/your/repo cp bin/git-commit-check/hooks/pre-commit.example bin/git-commit-check/hooks/pre-commit cp bin/git-commit-check/config.sh.example bin/git-commit-check/config.sh ``` -- Run the following command to instruct git to call `gitcc` before allowing the developer to create a new commit. +- Edit the config.sh file to set the values for the checks you want to run. + - Set the ENABLED variable with the ',' separated script IDs that you want to enable. +- Create your own new script and add it to the `scripts` directory. Script naming conventions: + 1. Prepend a two-digit script ID to the script name. + 2. Append the .sh extension to the script name. + 3. Copy the contents of the `scripts/template` script into the new script file. + 4. Adjust the values in the script file to suit your needs, as described in the comments of the template script. + 5. Try to keep your scripts POSIX-compliant so that they will work in all (most) SHELL. Check online here: https://www.shellcheck.net +- Alternatively, you may copy one of the existing scripts under the `scripts/examples` directory into the `scripts` directory. +- Make the pre-commit hook executable. Run the following command to instruct git to call `gitcc` before allowing the developer to create a new commit. ``` git config core.hooksPath bin/git-commit-check/hooks` ``` + +- Run `git commit` to test the pre-commit hook. + +- Done! diff --git a/config.sh.example b/config.sh.example index 4391390..293d1a7 100644 --- a/config.sh.example +++ b/config.sh.example @@ -7,35 +7,21 @@ GITCC_SCRIPTS_PATH="$GITCC_PATH/scripts" cd "$PROJECT_PATH" || exit -# [script IDs] - Include the ids of those scripts that should be executed. -ENABLED="01,02,03,04,05" - -# [about] -BACKEND_SYNTAX_VERSION=$(php -r 'echo phpversion();') -BACKEND_SYNTAX_TOOL="PHP" -FRAMEWORK_VERSION=$(php artisan --version) -FRAMEWORK_TOOL="Laravel" -BACKEND_STANDARDS_VERSION=$(./vendor/bin/phpcs '--version') -BACKEND_STANDARDS_TOOL="CodeSniffer" -BACKEND_TESTS_VERSION=$(./vendor/bin/phpunit '--version') -BACKEND_TESTS_TOOL="PHPUnit" -BACKEND_DEPENDENCIES_VERSION=$(composer '--version' 2>&1 | head -n 1) -BACKEND_DEPENDENCIES_TOOL="Composer" -FRONTEND_DEPENDENCIES_VERSION=$(node --version) -FRONTEND_DEPENDENCIES_TOOL="Node" -FRONTEND_DEPENDENCIES_VERSION_2=$(npm --version) -FRONTEND_DEPENDENCIES_TOOL_2="NPM" +# [script IDs] - Set the ids of those scripts that should be executed. To disable all scripts, set an empty string. +ENABLED="01,02,03,04,05,06,07,08,09" # [interface] -COMPACT=0 +SHOW_OUTPUT=1 +SHOW_ABOUT=1 +SHOW_LOG_URL=1 WIDTH=93 RED="\e[0;31m" -BRED="\e[1;31m" #Bold red +BRED="\e[1;31m" # Bold red. GREEN="\e[0;32m" YELLOW="\e[0;33m" CYAN="\e[0;36m" BOLD="\e[1m" -NC="\e[0m" #No color/modifier +NC="\e[0m" # No color/modifier. SECTION_SYMBOL="" HEADER_SYMBOL=➡️ MESSAGE_SYMBOL="" @@ -43,7 +29,9 @@ LOG_SYMBOL=📋 TIME_SYMBOL=🕒 LOAD_SYMBOL=⌛ PASS_SYMBOL=🟢 +#PASS_SYMBOL=🔵 WARN_SYMBOL=🟡 +#WARN_SYMBOL=🟠 FAIL_SYMBOL=🔴 # [log] @@ -51,6 +39,5 @@ LOG=1 LOG_FILE="./path/to/log-file" LOG_FORMAT="[%s] local.%s: %s\nMessage: %s" # datetime, log level, header, message -# [log-viewer] -LOG_VIEWER=1 -LOG_VIEWER_URL="https://www.example.com" +# [log link] +LOG_URL="https://www.example.com/logs" diff --git a/gitcc b/gitcc index 8abce61..f0395c5 100755 --- a/gitcc +++ b/gitcc @@ -1,6 +1,6 @@ #!/bin/sh # Git Commit Check (gitcc) -# Version: 0.3.0 +# Version: 0.4.0 # Author: Angelos Theodorakopoulos # Change to the directory of the script and source the required scripts. @@ -9,27 +9,40 @@ cd "$(dirname "$0")" || exit 1 cd "$(dirname "$0")" || exit 1 . ./lib.sh +init() { + START=$(date +%s) + HAS_ERRORS=0 + HAS_SYNTAX_ERRORS=0 +} + header() { line3 "╔" "╗" $((WIDTH+8)) "═" + printf " %s${CYAN}Load\n" "$SECTION_SYMBOL" + line "Configuration" "${1:-./config.sh}" +} - if [ "$COMPACT" = 0 ]; then - printf " %s${CYAN}Load\n" "$SECTION_SYMBOL" - line "Configuration" "${1:-./config.sh}" - fi +check() { + [ -n "$ENABLED" ] && printf "\n %s${CYAN}Check${NC}\n" "$SECTION_SYMBOL" + + for script in $(find "$GITCC_SCRIPTS_PATH" -type f -name '*.sh' | sort); do + _name=$(basename "$script") + _id="${_name%%_*}" + # Check if the script is enabled in the configuration and then source the script + # so that it runs in the same shell process and the variables are accessible within it. + if contains_string "$_id" "$ENABLED"; then + . "$script" + fi + done } about() { - [ "$COMPACT" = 1 ] && return + [ "$SHOW_ABOUT" = 0 ] && return printf "\n %s${CYAN}About\n" "$SECTION_SYMBOL" - line "$BACKEND_SYNTAX_TOOL" "$BACKEND_SYNTAX_VERSION" - line "$FRAMEWORK_TOOL" "$(extract_version "$FRAMEWORK_VERSION")" - line "$BACKEND_STANDARDS_TOOL" "$(extract_version "$BACKEND_STANDARDS_VERSION")" - line "$BACKEND_TESTS_TOOL" "$(extract_version "$BACKEND_TESTS_VERSION")" - line "$BACKEND_DEPENDENCIES_TOOL" "$(extract_version "$BACKEND_DEPENDENCIES_VERSION")" - line "$FRONTEND_DEPENDENCIES_TOOL" "$(extract_version "$FRONTEND_DEPENDENCIES_VERSION")" - line "$FRONTEND_DEPENDENCIES_TOOL_2" "$(extract_version "$FRONTEND_DEPENDENCIES_VERSION_2")" + split_about_info | while IFS='|' read -r left right; do + line "$left" "$(extract_version "$right")" + done } footer() { @@ -37,39 +50,19 @@ footer() { END=$(date +%s) time=$((END-START)) - [ $LOG_VIEWER = 1 ] && line3 "$LOG_SYMBOL $LOG_VIEWER_URL" "$TIME_SYMBOL ${time}sec" "$((WIDTH+7))" " " -} -check() { - [ "$COMPACT" = 0 ] && [ "$ENABLED" != "" ] && printf "\n %s${CYAN}Check${NC}\n" "$SECTION_SYMBOL" - [ "$LOG" = 1 ] && log info "══════════════" "" - - # Directly run all scripts (without any extra logic such as contains_string). - #[ "$ENABLED" != "" ] && find "$GITCC_PATH/scripts" -type f -name '*.sh' | sort -h | xargs -L 1 -d '\n' sh; - - if [ "$ENABLED" != "" ]; then - for script in $(find "$GITCC_SCRIPTS_PATH" -type f -name '*.sh' | sort); do - _name=$(basename "$script") - _id="${_name%%_*}" - # Check if the script is enabled in the configuration and then - # source the script so that it runs in the same shell process and the variables are accessible within it. - contains_string "$_id" "$ENABLED" && . "$script" - done + if [ "$SHOW_LOG_URL" -eq 1 ]; then + line3 "$LOG_SYMBOL $LOG_URL" "$TIME_SYMBOL ${time}sec" "$((WIDTH+7))" " " + else + line3 "" "$TIME_SYMBOL ${time}sec" "$((WIDTH+7))" " " fi } -init() { - START=$(date +%s) - HAS_ERRORS=0 - STAGED_PHP_FILES=$(git diff --cached --name-only --diff-filter=ACMR HEAD | grep .php) - HAS_SYNTAX_ERRORS=0 -} - main() { init header - about check + about footer if [ "$HAS_ERRORS" -ne 0 ]; then diff --git a/hooks/pre-commit.example b/hooks/pre-commit.example index 0c740e4..9c23bfa 100644 --- a/hooks/pre-commit.example +++ b/hooks/pre-commit.example @@ -3,15 +3,15 @@ # To configure git to run this script before each commit, run `git config core.hooksPath hooks` # [pre-commit] -PROJECT_PATH="/path/to/project" # Absolute path to the project, e.g. /var/www/my-project -GITCC_PATH="path/to/gitcc" # Relative path to the project, e.g. "bin/git-commit-check" +PROJECT_PATH="/path/to/project" # Absolute path to the project, e.g. /var/www/my-project +GITCC_PATH="path/to/gitcc" # Relative path to the project, e.g. "bin/git-commit-check" CONTAINER_ENGINE=$(which docker 2>/dev/null || which podman 2>/dev/null) CONTAINER_NAME="container_name" SHELL="/bin/sh" -COMMAND="$PROJECT_PATH/$GITCC_PATH/gitcc" +COMMAND="$GITCC_PATH/gitcc" if [ -z "$CONTAINER_ENGINE" ]; then - eval "$COMMAND" + $COMMAND else $CONTAINER_ENGINE exec -tw "$PROJECT_PATH" "$CONTAINER_NAME" "$SHELL" -c "$COMMAND" fi diff --git a/lib.sh b/lib.sh index 0e1bc69..090e55d 100755 --- a/lib.sh +++ b/lib.sh @@ -1,6 +1,9 @@ #!/bin/sh cd "$PROJECT_PATH" || exit +ABOUT="" +DELIMITER="<>" + line() { left="$1" right="$2" @@ -30,6 +33,24 @@ line3() { printf "%s\n" "$right" } +# Concatenate the Left/Right strings to the about section data. +add_about_info() { + ABOUT_LEFT="$1" + ABOUT_RIGHT="$2" + + if [ -z "$ABOUT" ]; then + ABOUT="$ABOUT_LEFT|$ABOUT_RIGHT" + else + ABOUT="$ABOUT$DELIMITER$ABOUT_LEFT|$ABOUT_RIGHT" + fi +} + +# Split the about section data into strings that can be used to generate a new line in the about section. +split_about_info() { + printf '%s\n' "$ABOUT" | tr -s "$DELIMITER" '\n' +} + +# Return 1 if the 2nd argument contains the 1st argument. Return 0 otherwise. contains_string() { _string="$2" _substring="$1" @@ -44,6 +65,7 @@ contains_string() { esac } +# Extract the version of an application from a command output. extract_version() { if [ $# -eq 0 ]; then printf "(Unknown)\n" @@ -51,16 +73,19 @@ extract_version() { fi version=$(printf "%s" "$1" | sed -n ' - # Match either: - # 1. A "v" followed by version numbers after non-digit - # 2. Version numbers preceded by non-digit - # 3. Version numbers at string start - s/^.*[^0-9]v\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\).*$/\1/p - t - s/^.*[^0-9]\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\).*$/\1/p - t - s/^v\{0,1\}\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\).*$/\1/p - ') + # Match either: + # 1. Version numbers preceded by / + # 2. A "v" followed by version numbers after non-digit + # 3. Version numbers preceded by non-digit + # 4. Version numbers at string start + s/^.*\/\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\).*$/\1/p + t + s/^.*[^0-9]v\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\).*$/\1/p + t + s/^.*[^0-9]\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\).*$/\1/p + t + s/^v\{0,1\}\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\).*$/\1/p + ') if [ -z "$version" ]; then printf "(Unknown)\n" @@ -70,8 +95,7 @@ extract_version() { printf "%s" "$version" } -# Fills a line in the terminal with $4 chars until it reaches $WIDTH in length -# (including the left and right side lengths). +# Fill a line in the terminal with $4 chars until it reaches $WIDTH in length (including the left and right side). fill() { left="$1" right="$2" @@ -88,6 +112,7 @@ fill() { done } +# Parse the output of npm audit and provide concise information. parse_npm_audit() { if [ $# -eq 0 ]; then printf "Error: No input file specified" @@ -104,7 +129,7 @@ parse_npm_audit() { total=$(printf '%s' "$auditFile" | jq -r .metadata.vulnerabilities.total) } -# Creates a Log entry according to config value LOG_FORMAT and redirects to config value LOG_FILE +# Create a Log file entry according to config value LOG_FORMAT and redirects to config value LOG_FILE log() { level="$1" header="$2" @@ -123,8 +148,6 @@ log() { dateTime=$(date "+%Y-%m-%d %H:%M:%S") logEntry=$(printf "$LOG_FORMAT" "$dateTime" "${level}" "${header}" "$message") - # logEntry="[${dateTime}] local.${level}: ${header} - # Message: ${message}" # Pass log output from sed to remove control chars and color codes printf "%s\n" "$(printf "%s" "$logEntry" | sed -e "s/\x1b\[.\{1,5\}m//g")" >> "$logFile" @@ -135,11 +158,12 @@ message() { titleOrText="$2" text="$3" - if [ "$#" -eq 2 ] && [ "$COMPACT" = 0 ]; then + if [ "$#" -eq 2 ] && [ "$SHOW_OUTPUT" = 1 ]; then printf " %s\n" "$titleOrText" fi + if [ "$#" -eq 3 ]; then - [ "$COMPACT" = 0 ] && printf " $text\n" + [ "$SHOW_OUTPUT" = 1 ] && printf " $text\n" [ "$LOG" = 1 ] && log "$level" "$titleOrText" "$text" fi } @@ -147,6 +171,7 @@ message() { pass() { sek=$((sek-1)) printf "\b%s\n" "$PASS_SYMBOL" + [ -n "$1" ] && message info "${1}" "${2}" } warn() { diff --git a/scripts/examples/01_backend_syntax.sh b/scripts/examples/01_backend_syntax.sh deleted file mode 100755 index 127b89f..0000000 --- a/scripts/examples/01_backend_syntax.sh +++ /dev/null @@ -1,36 +0,0 @@ -#!/bin/sh -# [Init] -HEADER="BACKEND SYNTAX" -COMMAND="php -l -d display_errors=0" - -# [Run] -line2 "$HEADER" "$LOAD_SYMBOL" -output="" - -if [ "$STAGED_PHP_FILES" != "" ]; then - shouldFail=0 - tempFile=$(mktemp) - - for PHP_FILE in $STAGED_PHP_FILES; do - # Using a piped command (but we can't preserve exit status in a POSIX-compliant way) - # output="$output\n"$(${COMMAND} ./$PHP_FILE 2>&1 | head -n 2) # Alternative piped command: sed -n '1p;2p' - ${COMMAND} ./"$PHP_FILE" > "$tempFile" 2>&1 - exitStatus=$? - output="$output\n"$(head -n 2 "$tempFile") - - if [ $exitStatus -ne 0 ]; then - shouldFail=1 - fi - FILES="$FILES ./$PHP_FILE" - done - - rm "$tempFile" - - if [ "$shouldFail" -ne 0 ]; then - HAS_SYNTAX_ERRORS=1 - fail "$HEADER" "$output\n" - return - fi -fi - -pass diff --git a/scripts/examples/02_backend_standards.sh b/scripts/examples/02_backend_standards.sh deleted file mode 100755 index 0a43940..0000000 --- a/scripts/examples/02_backend_standards.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/bin/sh -# [Init] -HEADER="BACKEND STANDARDS" -COMMAND="./vendor/bin/phpcs --colors" - -# [Run] -line2 "$HEADER" "$LOAD_SYMBOL" - -if [ "$STAGED_PHP_FILES" != "" ]; then - for PHP_FILE in $STAGED_PHP_FILES; do - FILES="$FILES ./$PHP_FILE" - done - - output=$(${COMMAND} $FILES) # Don't use double quotes for FILES - if [ $? -ne 0 ]; then - fail "$HEADER" "$output" - return - fi -fi - -pass diff --git a/scripts/prepared/01_php_lint.sh b/scripts/prepared/01_php_lint.sh new file mode 100755 index 0000000..109401a --- /dev/null +++ b/scripts/prepared/01_php_lint.sh @@ -0,0 +1,42 @@ +#!/bin/sh +# [Init] +HEADER="BACKEND SYNTAX" +COMMAND="php -l -d display_errors=0" +line2 "$HEADER" "$LOAD_SYMBOL" +add_about_info "PHP" "$(php -r 'echo phpversion();')" + +# [Run] +STAGED_PHP_FILES=$(git diff --cached --name-only --diff-filter=ACMR HEAD | grep .php) +output="" + +if [ -z "$STAGED_PHP_FILES" ]; then + warn "$HEADER" "No staged PHP files." + return +fi + +shouldFail=0 +tempFile=$(mktemp) + +for PHP_FILE in $STAGED_PHP_FILES; do + ${COMMAND} ./"$PHP_FILE" > "$tempFile" 2>&1 + exitStatus=$? + output="$output\n"$(head -n 2 "$tempFile") + + if [ $exitStatus -ne 0 ]; then + shouldFail=1 + fi +done + +rm "$tempFile" + +if [ "$shouldFail" -ne 0 ]; then + HAS_SYNTAX_ERRORS=1 + fail "$HEADER" "$output\n" + return +else + add_about_info "Laravel" "$(php artisan --version)" +fi + +log info "$HEADER" "Pass." + +pass diff --git a/scripts/prepared/02_phpcs.sh b/scripts/prepared/02_phpcs.sh new file mode 100755 index 0000000..e61bc4c --- /dev/null +++ b/scripts/prepared/02_phpcs.sh @@ -0,0 +1,36 @@ +#!/bin/sh +# [Init] +HEADER="BACKEND STANDARDS" +COMMAND="./vendor/bin/phpcs --colors" +line2 "$HEADER" "$LOAD_SYMBOL" + +# [Run] +if [ "$HAS_SYNTAX_ERRORS" -ne 0 ]; then + fail "$HEADER" " ${MESSAGE_SYMBOL}Found syntax errors that block this check." + return +else + add_about_info "CodeSniffer" "$(./vendor/bin/phpcs '--version')" +fi + +STAGED_PHP_FILES=$(git diff --cached --name-only --diff-filter=ACMR HEAD | grep .php) + +if [ -z "$STAGED_PHP_FILES" ]; then + warn "$HEADER" "No staged PHP files." + return +fi + +for PHP_FILE in $STAGED_PHP_FILES; do + FILES="$FILES ./$PHP_FILE" +done + +output=$(${COMMAND} $FILES) # Don't use double quotes for FILES + +# Don't use double quotes for FILES +if [ $? -ne 0 ]; then + fail "$HEADER" "$output" + return +fi + +log info "$HEADER" "Pass." + +pass diff --git a/scripts/examples/03_backend_tests.sh b/scripts/prepared/03_php_artisan_test.sh similarity index 67% rename from scripts/examples/03_backend_tests.sh rename to scripts/prepared/03_php_artisan_test.sh index 3fd50a0..37b545a 100755 --- a/scripts/examples/03_backend_tests.sh +++ b/scripts/prepared/03_php_artisan_test.sh @@ -2,25 +2,27 @@ # [Init] HEADER="BACKEND TESTS" COMMAND="php artisan test --compact --color=always" -CONFIG="phpunit.xml" -ENV=".env.automated-tests" +line2 "$HEADER" "$LOAD_SYMBOL" # [Run] -line2 "$HEADER" "$LOAD_SYMBOL" +CONFIG="phpunit.xml" +ENV=".env.automated-tests" if [ ! -f "${PWD}/${CONFIG}" ]; then - fail "$HEADER" " ${MESSAGE_SYMBOL}The configuration file ${BOLD}${CONFIG}${NC} for tests can not be found" + fail "$HEADER" " ${MESSAGE_SYMBOL}The configuration file ${BOLD}${CONFIG}${NC} for tests can not be found." return fi if [ ! -f "${PWD}/${ENV}" ]; then - fail "$HEADER" " ${MESSAGE_SYMBOL}The env file ${BOLD}${ENV}${NC} for tests can not be found" + fail "$HEADER" " ${MESSAGE_SYMBOL}The env file ${BOLD}${ENV}${NC} for tests can not be found." return fi if [ "$HAS_SYNTAX_ERRORS" -ne 0 ]; then - warn "$HEADER" " ${MESSAGE_SYMBOL}Found syntax errors that prevent running tests" + fail "$HEADER" " ${MESSAGE_SYMBOL}Found syntax errors that block this check." return +else + add_about_info "PHPUnit" "$(./vendor/bin/phpunit '--version')" fi output=$(${COMMAND}) @@ -30,4 +32,6 @@ if [ $? -ne 0 ]; then return fi +log info "$HEADER" "Pass." + pass diff --git a/scripts/examples/04_backend_deps.sh b/scripts/prepared/04_composer_audit.sh similarity index 92% rename from scripts/examples/04_backend_deps.sh rename to scripts/prepared/04_composer_audit.sh index 417951d..6ddf302 100755 --- a/scripts/examples/04_backend_deps.sh +++ b/scripts/prepared/04_composer_audit.sh @@ -2,16 +2,17 @@ # [Init] HEADER="BACKEND DEPENDENCIES" COMMAND="composer audit --no-cache --format=json" -REPORT="$GITCC_REPORTS_PATH/composer-audit.json" - -# [Run] line2 "$HEADER" "$LOAD_SYMBOL" +add_about_info "Composer" "$(composer '--version' 2>&1 | head -n 1)" +# [Run] if (! composer --version | grep -q "2\.[4-9]") > /dev/null 2>&1; then fail "$HEADER" " ${MESSAGE_SYMBOL}Composer audit requires version 2.4 or later" return fi +REPORT="$GITCC_REPORTS_PATH/composer-audit.json" + ${COMMAND} > $REPORT 2>&1 # Catch a possible network error @@ -37,4 +38,6 @@ if [ "$advisories" -ne 0 ]; then return fi +log info "$HEADER" "Pass." + pass diff --git a/scripts/prepared/05_eslint.sh b/scripts/prepared/05_eslint.sh new file mode 100755 index 0000000..94b01cd --- /dev/null +++ b/scripts/prepared/05_eslint.sh @@ -0,0 +1,25 @@ +#!/bin/sh +# [Init] +HEADER="FRONTEND SYNTAX" +STAGED_TS_FILES=$(git diff --cached --name-only --diff-filter=ACMR HEAD | grep -E '\.(ts|tsx|css)$') +COMMAND="eslint $STAGED_TS_FILES --fix" +line2 "$HEADER" "$LOAD_SYMBOL" +add_about_info "ESLint" "$(eslint --version)" + +# [Run] +if [ -z "$STAGED_TS_FILES" ]; then + warn "$HEADER" " No staged TS/TSX files." + return +fi + +output=$($COMMAND 2>&1) +exit=$? + +if [ $exit -ne 0 ]; then + fail "$HEADER" "$output" + return +fi + +log info "$HEADER" "Pass." + +pass diff --git a/scripts/prepared/06_prettier.sh b/scripts/prepared/06_prettier.sh new file mode 100755 index 0000000..28ccc14 --- /dev/null +++ b/scripts/prepared/06_prettier.sh @@ -0,0 +1,35 @@ +#!/bin/sh +# [Init] +HEADER="FRONTEND STANDARDS" +COMMAND="prettier --check resources/" +line2 "$HEADER" "$LOAD_SYMBOL" +add_about_info "Prettier" "$(prettier --version)" + +# [Run] +output=$($COMMAND 2>&1) +exit=$? + +if [ $exit -ne 0 ]; then + fail "$HEADER" "$output" + + output2=$(prettier --write resources/ --log-level silent) + exit2=$? + if [ $exit2 -ne 0 ]; then + fail "$HEADER" "$output2" + return + fi + + STAGED_TS_FILES=$(git diff --cached --name-only --diff-filter=ACMR HEAD | grep -E '\.(ts|tsx|css)$') + + git add $STAGED_TS_FILES + + MESSAGE="Formatting fixed and changes staged. Please try 'git commit' again!" + printf " %s\n" "$MESSAGE" + log info "$MESSAGE" "" + + return +fi + +log info "$HEADER" "Pass." + +pass diff --git a/scripts/prepared/07_tsc.sh b/scripts/prepared/07_tsc.sh new file mode 100755 index 0000000..7174c9a --- /dev/null +++ b/scripts/prepared/07_tsc.sh @@ -0,0 +1,18 @@ +#!/bin/sh +# [Init] +HEADER="FRONTEND TYPES" +COMMAND="tsc --noEmit" +line2 "$HEADER" "$LOAD_SYMBOL" +add_about_info "TypeScript" "$(tsc --version)" + +# [Run] +output=$(${COMMAND} 2>&1) + +if [ $? -ne 0 ]; then + fail "$HEADER" "$output" + return +fi + +log info "$HEADER" "Pass." + +pass diff --git a/scripts/examples/05_frontend_deps.sh b/scripts/prepared/08_npm_audit.sh similarity index 89% rename from scripts/examples/05_frontend_deps.sh rename to scripts/prepared/08_npm_audit.sh index ef6462f..241ebbe 100755 --- a/scripts/examples/05_frontend_deps.sh +++ b/scripts/prepared/08_npm_audit.sh @@ -2,10 +2,12 @@ # [Init] HEADER="FRONTEND DEPENDENCIES" COMMAND="npm audit --json --audit-level=low" -REPORT="$GITCC_REPORTS_PATH/package-audit.json" +line2 "$HEADER" "$LOAD_SYMBOL" +add_about_info "NPM" "$(npm --version)" +add_about_info "Node" "$(node --version)" # [Run] -line2 "$HEADER" "$LOAD_SYMBOL" +REPORT="$GITCC_REPORTS_PATH/package-audit.json" ${COMMAND} > $REPORT 2>&1 @@ -28,4 +30,6 @@ if [ "$total" -ne 0 ]; then return fi +log info "$HEADER" "Pass." + pass diff --git a/scripts/prepared/09_vite_build.sh b/scripts/prepared/09_vite_build.sh new file mode 100755 index 0000000..db7f4c8 --- /dev/null +++ b/scripts/prepared/09_vite_build.sh @@ -0,0 +1,18 @@ +#!/bin/sh +# [Init] +HEADER="FRONTEND BUILD" +COMMAND="vite build" +line2 "$HEADER" "$LOAD_SYMBOL" +add_about_info "Vite" "$(vite --version)" + +# [Run] +output=$(${COMMAND} 2>&1) + +if [ $? -ne 0 ]; then + fail "$HEADER" "$output" + return +fi + +log info "$HEADER" "$output" + +pass diff --git a/scripts/template b/scripts/template new file mode 100755 index 0000000..1f8ce6e --- /dev/null +++ b/scripts/template @@ -0,0 +1,17 @@ +#!/bin/sh +# [Init] TODO - 1. Configure the values of the following variables/commands according to your needs. +HEADER="MY HEADER" +COMMAND="my_command" +line2 "$HEADER" "$LOAD_SYMBOL" +# Here you can configure gitcc to print a relevant line in the about section (typically with command name and version). +add_about_info "command name" "$(command --version)" +# If your script performs more than 1 check, then you can call the add_about_info function multiple times. +add_about_info "command2 name" "$(command2 --version)" + +# [Run] TODO - 2. Add script logic here. + +# ... + +pass # TODO - 3. Call the 'pass' function at the end of the script, after your script has passed all checks successfully. + +# pass "$HEADER" "Pass." # Optionally, you may pass 2 arguments to the 'pass' function to create a log file entry.