diff --git a/.husky/pre-commit b/.husky/pre-commit index 9c3f1a2ac0f..9cf36f8fa8f 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1,11 +1,49 @@ #!/usr/bin/env sh -lint-staged - -types_path="packages/calcite-components/src/components.d.ts" +ensure_types_are_up_to_date() { + types_path="packages/calcite-components/src/components.d.ts" -# make sure the types are always up to date -if [ -n "$(git diff --name-only -- "$types_path")" ]; then + if [ -n "$(git diff --name-only -- "$types_path")" ]; then echo "Automatically staging changes to \"$types_path\"" git add "$types_path" -fi + fi +} + +check_ui_icon_name_consistency() { + svg_icon_dir="packages/calcite-ui-icons/icons" + + # this pattern checks for `-.svg` or `--f.svg` for filled icons + # where `` is kebab-case, `` is 16, 24, or 32 + valid_pattern="^[a-z0-9-]+-(16|24|32)(-f)?\\.svg$" + + # this pattern will check for invalid use of "-f-" anywhere except right before the size + invalid_pattern="-[a-z0-9]+-f-" + + staged_files="$(git diff --cached --name-only --diff-filter=ACM)" + + for file in $staged_files; do + if [[ "$file" == "$svg_icon_dir"* ]]; then + if [[ "$file" == *.svg ]]; then + filename="$(basename "$file")" + + # first, ensure the filename follows the valid pattern + if ! [[ "$filename" =~ $valid_pattern ]]; then + echo "Error: File '$file' does not follow the naming convention (-.svg or --f.svg)." + exit 1 + fi + + # then, ensure there's no invalid use of "-f-" anywhere except right before the size + if [[ "$filename" =~ $invalid_pattern ]]; then + echo "Error: File '$file' has an invalid '-f-' and does not follow the naming convention (-.svg or --f.svg)." + exit 1 + fi + fi + fi + done +} + +lint-staged +ensure_types_are_up_to_date +check_ui_icon_name_consistency + +exit 0