From 1c17c94289369da6601245d521ba6a2a9611590b Mon Sep 17 00:00:00 2001 From: vpolets Date: Fri, 8 Oct 2021 13:44:26 +0300 Subject: [PATCH 1/2] add pre-commit hook to react and reactTypescript --- .../configs/git-hooks/react/pre-commit | 42 +++++++++++++++++++ .../git-hooks/reactTypescript/pre-commit | 42 +++++++++++++++++++ mate-scripts/src/commands/Init.command.ts | 10 +++++ 3 files changed, 94 insertions(+) create mode 100644 mate-scripts/configs/git-hooks/react/pre-commit create mode 100644 mate-scripts/configs/git-hooks/reactTypescript/pre-commit diff --git a/mate-scripts/configs/git-hooks/react/pre-commit b/mate-scripts/configs/git-hooks/react/pre-commit new file mode 100644 index 00000000..53db396c --- /dev/null +++ b/mate-scripts/configs/git-hooks/react/pre-commit @@ -0,0 +1,42 @@ +#!/bin/bash + +function join_by { local IFS="$1"; shift; echo "$*"; } + +PASS=true + +# ESLint +STAGED_JS_FILES=$(git diff --cached --name-only --diff-filter=ACM ./src | grep ".\(j\|t\)sx\{0,1\}$") + +if [[ "$STAGED_JS_FILES" != "" ]]; then + echo "Linting javascript files" + + npx mate-scripts lint --javascript $(join_by " " ${STAGED_JS_FILES}) + + if [[ "$?" == 0 ]]; then + printf "\033[32mESLint Passed\033[0m\n\n" + else + printf "\033[031mESLint Failed\033[0m\n\n" + PASS=false + fi +fi + +# StyleLint +STAGED_CSS_FILES=$(git diff --cached --name-only --diff-filter=ACM ./src | grep ".s\?css$") + +if [[ "$STAGED_CSS_FILES" != "" ]]; then + echo "Linting CSS files" + + npx mate-scripts lint --styles $(join_by " " ${STAGED_CSS_FILES}) + + if [[ "$?" == 0 ]]; then + printf "\033[32mStyleLint Passed\033[0m\n\n" + else + printf "\033[031mStyleLint Failed\033[0m\n\n" + PASS=false + fi +fi + +if ! ${PASS}; then + printf "Pre-commits check failed. Fix problems above before commit.\n\n" + exit 1 +fi diff --git a/mate-scripts/configs/git-hooks/reactTypescript/pre-commit b/mate-scripts/configs/git-hooks/reactTypescript/pre-commit new file mode 100644 index 00000000..53db396c --- /dev/null +++ b/mate-scripts/configs/git-hooks/reactTypescript/pre-commit @@ -0,0 +1,42 @@ +#!/bin/bash + +function join_by { local IFS="$1"; shift; echo "$*"; } + +PASS=true + +# ESLint +STAGED_JS_FILES=$(git diff --cached --name-only --diff-filter=ACM ./src | grep ".\(j\|t\)sx\{0,1\}$") + +if [[ "$STAGED_JS_FILES" != "" ]]; then + echo "Linting javascript files" + + npx mate-scripts lint --javascript $(join_by " " ${STAGED_JS_FILES}) + + if [[ "$?" == 0 ]]; then + printf "\033[32mESLint Passed\033[0m\n\n" + else + printf "\033[031mESLint Failed\033[0m\n\n" + PASS=false + fi +fi + +# StyleLint +STAGED_CSS_FILES=$(git diff --cached --name-only --diff-filter=ACM ./src | grep ".s\?css$") + +if [[ "$STAGED_CSS_FILES" != "" ]]; then + echo "Linting CSS files" + + npx mate-scripts lint --styles $(join_by " " ${STAGED_CSS_FILES}) + + if [[ "$?" == 0 ]]; then + printf "\033[32mStyleLint Passed\033[0m\n\n" + else + printf "\033[031mStyleLint Failed\033[0m\n\n" + PASS=false + fi +fi + +if ! ${PASS}; then + printf "Pre-commits check failed. Fix problems above before commit.\n\n" + exit 1 +fi diff --git a/mate-scripts/src/commands/Init.command.ts b/mate-scripts/src/commands/Init.command.ts index 764f1339..6510ca55 100644 --- a/mate-scripts/src/commands/Init.command.ts +++ b/mate-scripts/src/commands/Init.command.ts @@ -45,6 +45,16 @@ export class InitCommand extends Command { this.initGitHooks(ProjectTypes.Javascript); }; + protected react = () => { + this.copyProjectTypeSpecificConfigs(ProjectTypes.React); + this.initGitHooks(ProjectTypes.React); + }; + + protected reactTypescript = () => { + this.copyProjectTypeSpecificConfigs(ProjectTypes.ReactTypescript); + this.initGitHooks(ProjectTypes.ReactTypescript); + }; + private copyCommonConfigs() { const commonConfigsDir = path.join(InitCommand.configsDir, 'common'); From a8af9bd3ac9b5d28ed945688298cca9551b3d223 Mon Sep 17 00:00:00 2001 From: vpolets Date: Fri, 8 Oct 2021 13:46:41 +0300 Subject: [PATCH 2/2] update package json version --- mate-scripts/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mate-scripts/package.json b/mate-scripts/package.json index f3200bb5..33b7790c 100644 --- a/mate-scripts/package.json +++ b/mate-scripts/package.json @@ -1,6 +1,6 @@ { "name": "@mate-academy/scripts", - "version": "0.9.6", + "version": "0.9.7", "description": "Scripts to init, run, test, deploy Mate academy homework projects", "main": "bin/mateScripts.js", "scripts": {