Skip to content

Commit

Permalink
Update .husky.
Browse files Browse the repository at this point in the history
• Move `lftest` to `pre-commit`.
• Implement `c_json` and `c_svcom` in `core.sh`.
• Implement a check for possibly breaking deps in `pre-commit`.
  • Loading branch information
SpacingBat3 committed Dec 10, 2022
1 parent 37dcead commit aaf43f4
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 30 deletions.
69 changes: 42 additions & 27 deletions .husky/core.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function npkg() {
local PKG;
local SUPPORT;
# A path to Node.js package manager.
PKG="$(command -v npm || command -v yarn || command -v pnpm || echo "false")";
PKG="$(command -v npm || command -v yarn || command -v pnpm || echo false)";
# A list of supported package manager commands
SUPPORT=(install test run add);
function _install() {
Expand Down Expand Up @@ -71,35 +71,50 @@ function npkg() {
printf '\n%s\n\n' "npkg: Selected package manager: '${PKG##*/}'."
"$PKG" "$("_$1")" "${@:2}"
}
# `lftest` – Tests if lockfile passes tests that allows for it to be commited.

# `c_json` – `JSON.parse()` wrapper for BASH. Part of `core.sh`.
#
# **Usage:**
# ```sh
# lftest # Does not use any arguments.
# c_json .[property] PATH
# ```
function lftest () {
mapfile -t FILES < <(git diff --staged --name-only);
local no_meta no_lock;
no_lock=false;
no_meta=false;
for file in "${FILES[@]}"; do
if [[ "$file" == "package-lock.json" ]]; then
no_lock=true;
elif [[ "$file" == "package.json" ]]; then
no_meta=true;
fi
[[ $no_lock && $no_meta ]] && break;
done;
if [[ "$no_meta" == "true" && "$no_lock" == "false" ]]; then
echo >&2;
echo "locktest: test failed!" >&2;
printf ' %s\n' \
"It seems that you've tried to commit a lock file without any changes"\
"done in 'package-lock.json'! This operation will be blocked as lockfile"\
"should not be bumped unless a development tree has changed in some way"\
"or commit is made that bumps package version and the new tag is going"\
"to be released" >&2
echo >&2;
return 1;
function c_json() {
local query file;
if [[ "$1" == "." ]]; then
query="";
else
query="$1";
fi
file="$(tr -d '\n' <<< "${2//'"'/'\\"'}")";
node -e "console.log(JSON.parse(\"$file\")$query);";
return $?;
}

# `c_svcom` – A SemVer comparassion for BASH.
#
# **Usage:**
# ```
# c_svcom ()
# ```
function c_svcom() {
local sedrule subvr_1 subvr_2 vtype;
case "$1" in
maj{,or}) vtype=1 ;;
min{,ior}) vtype=2 ;;
patch) vtype=3 ;;
*) vtype="$1" ;;
esac
if [[ "$vtype" -lt 1 || "$vtype" -gt 3 ]]; then return 1; fi;
sedrule="s/.*\([\^<]\)\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)/\1 \\$((vtype+1))/";
mapfile -t subvr_1 < <(sed "$sedrule" <<< "$2");
mapfile -t subvr_2 < <(sed "$sedrule" <<< "$3");
if [[ "${subvr_1[0]}" != "${subvr_2[0]}" ]]; then return 2; fi;
if [[ "${subvr_1[1]}" -gt "${subvr_2[1]}" ]]; then
echo 1;
elif [[ "${subvr_1[1]}" -eq "${subvr_2[1]}" ]]; then
echo 0;
elif [[ "${subvr_1[1]}" -lt "${subvr_2[1]}" ]]; then
echo -1;
fi
return 3;
}
55 changes: 52 additions & 3 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,8 +1,57 @@
#!/usr/bin/env -S bash -e
# shellcheck shell=bash

. "$(dirname -- "$0")/_/husky.sh"
. "$(dirname -- "$0")/core.sh"
. "$(dirname -- "$0")/_/husky.sh";
. "$(dirname -- "$0")/core.sh";

PKGROOT="$(dirname -- "$0")/..";

# Check metadata files.

mapfile -t FILES < <(git diff --staged --name-only);
lock=false;
meta=false;
for file in "${FILES[@]}"; do
if [[ "$file" == "package-lock.json" ]]; then
lock=true;
elif [[ "$file" == "package.json" ]]; then
meta=true;
fi
if [[ $lock == "true" && $meta == "true" ]]; then break; fi
done
if [[ "$meta" == "false" && "$lock" == "true" ]]; then
echo >&2;
echo "pre-commit: unnecesary-lockfile" >&2;
printf ' %s\n' \
"It seems that you've tried to commit a lock file without any changes"\
"done in 'package.json'! This operation will be blocked as lockfile"\
"should not be bumped unless a development tree has changed in some way"\
"or commit is made that bumps package version and the new tag is going"\
"to be released" >&2
echo >&2;
exit 1;
elif [[ "$meta" == "true" && "$lock" == "false" ]]; then
old="$(git show HEAD:/package.json)";
new="$(cat "$PKGROOT/package.json")";
versions=(
"$(c_svcomp 1 "$(c_json .version "$new")" "$(c_json .version "$old")")"
"$(c_svcomp 1 "$(c_json .dependencies.electron"$new")" "$(c_json .dependencies.electron "$ld")")"
)
if [[ "${versions[0]}" -eq 0 && "${versions[1]}" -eq 1 ]]; then
echo >&2;
echo "pre-commit: breaking-deps!" >&2;
printf ' %s\n' \
"It seems that you've tried to commit a 'package.json' file in which"\
"there was made a major release bump to Electron. This change is considered"
"as 'breaking' because it can announce new bugs into the application due"\
"to the API changes, Chromium/Node.js bump and potentially untested"\
"features which has been recently announced. For this reason, WebCord"\
"should be bumped to the next major version as well." >&2
echo >&2;
exit 2;
fi
fi

# Run package tests (compiler+linter).

lftest;
npkg test;

0 comments on commit aaf43f4

Please sign in to comment.