From db505bd3f291ff6c26738f4cd22bf8678d911e07 Mon Sep 17 00:00:00 2001 From: codejedi365 Date: Tue, 5 Oct 2021 23:14:30 -0400 Subject: [PATCH 1/5] Build(deps-dev): Add husky & is-ci for githooks \### Rationale Githooks help fellow contributors keep to standards before it is too far down the pipeline for them to easily correct. Makes everyone's lives easier --- package-lock.json | 23 +++++++++++++++++++++++ package.json | 5 ++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index 065a6a9..2a25331 100644 --- a/package-lock.json +++ b/package-lock.json @@ -25,6 +25,8 @@ "eslint-plugin-jest": "^24.5.0", "eslint-plugin-mdx": "^1.15.1", "eslint-plugin-prettier": "^4.0.0", + "husky": "^7.0.2", + "is-ci": "^3.0.0", "jest": "^27.2.4", "jest-extended": "^0.11.5", "prettier": "^2.4.1", @@ -5106,6 +5108,21 @@ "node": ">=8.12.0" } }, + "node_modules/husky": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/husky/-/husky-7.0.2.tgz", + "integrity": "sha512-8yKEWNX4z2YsofXAMT7KvA1g8p+GxtB1ffV8XtpAEGuXNAbCV5wdNKH+qTpw8SM9fh4aMPDR+yQuKfgnreyZlg==", + "dev": true, + "bin": { + "husky": "lib/bin.js" + }, + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/typicode" + } + }, "node_modules/iconv-lite": { "version": "0.4.24", "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", @@ -18940,6 +18957,12 @@ "integrity": "sha512-SEQu7vl8KjNL2eoGBLF3+wAjpsNfA9XMlXAYj/3EdaNfAlxKthD1xjEQfGOUhllCGGJVNY34bRr6lPINhNjyZw==", "dev": true }, + "husky": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/husky/-/husky-7.0.2.tgz", + "integrity": "sha512-8yKEWNX4z2YsofXAMT7KvA1g8p+GxtB1ffV8XtpAEGuXNAbCV5wdNKH+qTpw8SM9fh4aMPDR+yQuKfgnreyZlg==", + "dev": true + }, "iconv-lite": { "version": "0.4.24", "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", diff --git a/package.json b/package.json index a5cb268..c4c6b1e 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,8 @@ "format": "npm run lint -- --fix", "lint": "eslint . --ext ts,js,md", "test": "jest", - "generate-readme-table": "ts-node build/generate-readme-table.ts" + "generate-readme-table": "ts-node build/generate-readme-table.ts", + "prepare": "is-ci || husky install" }, "devDependencies": { "@types/eslint": "^7.28.0", @@ -56,6 +57,8 @@ "eslint-plugin-jest": "^24.5.0", "eslint-plugin-mdx": "^1.15.1", "eslint-plugin-prettier": "^4.0.0", + "husky": "^7.0.2", + "is-ci": "^3.0.0", "jest": "^27.2.4", "jest-extended": "^0.11.5", "prettier": "^2.4.1", From cb3127553b7aef2dfb3ea38aae0838d1cd6647ff Mon Sep 17 00:00:00 2001 From: codejedi365 Date: Tue, 5 Oct 2021 23:16:58 -0400 Subject: [PATCH 2/5] feat-dev: add git pre-commit lint action --- .husky/pre-commit | 4 ++++ 1 file changed, 4 insertions(+) create mode 100755 .husky/pre-commit diff --git a/.husky/pre-commit b/.husky/pre-commit new file mode 100755 index 0000000..20d0d06 --- /dev/null +++ b/.husky/pre-commit @@ -0,0 +1,4 @@ +#!/bin/sh +. "$(dirname "$0")/_/husky.sh" + +npm run lint From b550ccc65ced6d02d0d6161cd2caa24dcea369cd Mon Sep 17 00:00:00 2001 From: codejedi365 Date: Tue, 5 Oct 2021 23:20:03 -0400 Subject: [PATCH 3/5] feat-dev: Improv lint precommit msg & flow --- .husky/hook-utils.sh | 10 ++++++++++ .husky/pre-commit | 13 ++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 .husky/hook-utils.sh diff --git a/.husky/hook-utils.sh b/.husky/hook-utils.sh new file mode 100644 index 0000000..abb2236 --- /dev/null +++ b/.husky/hook-utils.sh @@ -0,0 +1,10 @@ +#!/bin/sh +[ -z "$LOG_PREFIX" ] && LOG_PREFIX="[.husky/???]" + +log() { + echo "${LOG_PREFIX} $1" +} + +error() { + echo >&2 "${LOG_PREFIX} $1" +} diff --git a/.husky/pre-commit b/.husky/pre-commit index 20d0d06..689d179 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1,4 +1,15 @@ #!/bin/sh . "$(dirname "$0")/_/husky.sh" -npm run lint +export NODE_ENV=production + +LOG_PREFIX="[.husky/pre-commit]" + +. "$(dirname "$0")/hook-utils.sh" + +log "$> npm run lint" +if ! npm run lint; then + error "ERROR: Dirty code detected!" + error "Fix the lint errors before committing to the repository." + exit -1 +fi From c03e4295858acd791573dce916946e693c846e88 Mon Sep 17 00:00:00 2001 From: codejedi365 Date: Tue, 5 Oct 2021 23:21:56 -0400 Subject: [PATCH 4/5] feat-dev: auto-`npm i` with branches \### Rationale Lower change for flaws and weird errors with old dependencies or missing dependencies after a change of branch. --- .husky/post-checkout | 33 ++++++++++++++++++++++++++ .husky/post-merge | 56 ++++++++++++++++++++++++++++++++++++++++++++ .husky/post-rewrite | 13 ++++++++++ 3 files changed, 102 insertions(+) create mode 100755 .husky/post-checkout create mode 100755 .husky/post-merge create mode 100755 .husky/post-rewrite diff --git a/.husky/post-checkout b/.husky/post-checkout new file mode 100755 index 0000000..9319c38 --- /dev/null +++ b/.husky/post-checkout @@ -0,0 +1,33 @@ +#!/bin/sh +. "$(dirname "$0")/_/husky.sh" + +LOG_PREFIX="[.husky/post-checkout]" + +. "$(dirname "$0")/hook-utils.sh" + +HEAD_PREV="$1" +HEAD_NEW="$2" +CKOUT_TYPE_FLAG="$3" # 0 = retrieve file from index, 1 = changing branches + +# check if this is a post-checkout after a `git clone` +IS_CLONING=$( [ -z "$HEAD_PREV" ] && echo true || echo false ) + +update_npm_dependencies() { + # Only run if chaning branches and not performing the initial `git clone` + if [ $CKOUT_TYPE_FLAG == 1 ] && [ $IS_CLONING == false ]; then + local changed_files="" + # derived from https://gist.github.com/taurus227/28960de89e6c43bb3d492125368f1224 + changed_files="$(git diff-tree -r --name-only --no-commit-id $HEAD_PREV $HEAD_NEW)" + + if echo "$changed_files" | grep --quiet "package-lock.json"; then + log "CHANGE DETECTED: 'package-lock.json'" + log "Dependency requirements changed! This will take a few seconds..." + + local cmd="npm install --prefer-offline" + log "$> $cmd" + eval "$cmd" + fi + fi +} + +update_npm_dependencies diff --git a/.husky/post-merge b/.husky/post-merge new file mode 100755 index 0000000..e40976e --- /dev/null +++ b/.husky/post-merge @@ -0,0 +1,56 @@ +#!/bin/sh +. "$(dirname "$0")/_/husky.sh" + +[ -z "$LOG_PREFIX" ] && LOG_PREFIX="[.husky/post-merge]" + +. "$(dirname "$0")/hook-utils.sh" + +update_npm_dependencies() { + # derived from https://gist.github.com/taurus227/28960de89e6c43bb3d492125368f1224 + local changed_files="" + changed_files="$(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD)" + + local NEEDS_INSTALL=true + + if echo -e "$changed_files" | grep --quiet "package-lock.json"; then + log "CHANGE DETECTED: 'package-lock.json'" + log "Lock files rarely merge properly. Deleting and re-generating it..." + + local output="$changed_files" + output="$(echo -e "$output" | grep "package-lock.json")" + local lockfiles=("$output") # make array of filepaths + rm ${lockfiles[@]} + + elif echo -e "$changed_files" | grep --quiet "package.json"; then + log "CHANGE DETECTED: 'package.json'" + log "Dependencies might of changed, lets make sure we are g2g!" + else + NEEDS_INSTALL=false + fi + + if [ "$NEEDS_INSTALL" == true ]; then + cmd="npm install" + log "$> $cmd" + eval "$cmd" + + changed_files="$(git diff --name-only --diff-filter=d)" + if echo -e "$changed_files" | grep --quiet "package-lock.json"; then + log "ALL FIXED! package-lock.json needed a refresh." + + local lockfiles=($(echo -e "$changed_files" | grep "package-lock.json")) + git add -- "${lockfiles[@]}" + + for lockfile in "${lockfiles[@]}"; do + log "STAGED: $lockfile" + done + + local filenoun="file" + if [ "${#lockfiles[@]}" -gt 1 ]; then + filenoun="${filenoun}s" + fi + log "Please commit the newly generated $filenoun for the team!" + fi + fi +} + +update_npm_dependencies diff --git a/.husky/post-rewrite b/.husky/post-rewrite new file mode 100755 index 0000000..a1036b7 --- /dev/null +++ b/.husky/post-rewrite @@ -0,0 +1,13 @@ +#!/bin/sh +. "$(dirname "$0")/_/husky.sh" + +CALLER_CMD="$1" # rebase || amend +export LOG_PREFIX="[.husky/post-rewrite]" + +. "$(dirname "$0")/hook-utils.sh" + +# derived from https://gist.github.com/taurus227/28960de89e6c43bb3d492125368f1224 +if [ "$CALLER_CMD" == "rebase" ]; then + log "DETECTED: git-rebase."; + $(dirname "$0")/post-merge +fi From 043ad03be31c104a29ce2e1f0fa7368b97bedbb4 Mon Sep 17 00:00:00 2001 From: codejedi365 Date: Tue, 5 Oct 2021 23:47:25 -0400 Subject: [PATCH 5/5] fix-dev: handle npm unavailable circumstance \### Rationale For vscode switching of branches where it doesn't always have a login shell that commands are being run in --- .husky/post-checkout | 4 ++++ .husky/post-merge | 4 ++++ .husky/pre-commit | 13 +++++++++---- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/.husky/post-checkout b/.husky/post-checkout index 9319c38..07330df 100755 --- a/.husky/post-checkout +++ b/.husky/post-checkout @@ -24,6 +24,10 @@ update_npm_dependencies() { log "Dependency requirements changed! This will take a few seconds..." local cmd="npm install --prefer-offline" + if ! command -v npm &>/dev/null; then + log "NPM not found on \$PATH, however '$cmd' is desired. Please accomplish manually." + return + fi log "$> $cmd" eval "$cmd" fi diff --git a/.husky/post-merge b/.husky/post-merge index e40976e..492426e 100755 --- a/.husky/post-merge +++ b/.husky/post-merge @@ -30,6 +30,10 @@ update_npm_dependencies() { if [ "$NEEDS_INSTALL" == true ]; then cmd="npm install" + if ! command -v npm &>/dev/null; then + log "NPM not found on \$PATH, however '$cmd' is desired. Please accomplish manually." + return + fi log "$> $cmd" eval "$cmd" diff --git a/.husky/pre-commit b/.husky/pre-commit index 689d179..1f3b10a 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -7,9 +7,14 @@ LOG_PREFIX="[.husky/pre-commit]" . "$(dirname "$0")/hook-utils.sh" -log "$> npm run lint" -if ! npm run lint; then - error "ERROR: Dirty code detected!" - error "Fix the lint errors before committing to the repository." +if command -v npm &>/dev/null; then + log "$> npm run lint" + if ! npm run lint; then + error "ERROR: Dirty code detected!" + error "Fix the lint errors before committing to the repository." + exit -1 + fi +else + error "NPM not found on \$PATH, unable to run pre-commit lint hook." exit -1 fi