Skip to content

Commit

Permalink
add lint precommit hook (#127)
Browse files Browse the repository at this point in the history
* add lint precommit hook

* update comments
  • Loading branch information
nhanphan authored May 21, 2024
1 parent 38c7e3c commit 6098413
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 2 deletions.
24 changes: 24 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/sh

set +e

# Stash un-staged changes
git stash -q --keep-index

# Run pnpm commands
echo "Linting..."
pnpm lint
LINT_EXIT_CODE=$?

# Restore un-staged changes
git stash pop -q

# Check if tests or lint failed
if [ $LINT_EXIT_CODE -ne 0 ]; then
echo "Linting failed run 'pnpm lint:fix' to fix linting issues"
exit 1
fi

set -e

exit 0
14 changes: 14 additions & 0 deletions configs/scripts/client/format-js.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
# go to parent folder
cd $(dirname $(dirname $(dirname $SCRIPT_DIR)))
WORKING_DIR=$(pwd)

# command-line input
ARGS=$*

# js client tests folder
cd ${WORKING_DIR}/clients/js

pnpm install && pnpm format:fix
14 changes: 14 additions & 0 deletions configs/scripts/client/lint-js.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
# go to parent folder
cd $(dirname $(dirname $(dirname $SCRIPT_DIR)))
WORKING_DIR=$(pwd)

# command-line input
ARGS=$*

# js client tests folder
cd ${WORKING_DIR}/clients/js

pnpm install && pnpm format
19 changes: 19 additions & 0 deletions configs/scripts/program/format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
OUTPUT="./programs/.bin"
# go to parent folder
cd $(dirname $(dirname $(dirname ${SCRIPT_DIR})))

rm -rf $OUTPUT

if [ -z ${PROGRAMS+x} ]; then
PROGRAMS="$(cat .github/.env | grep "PROGRAMS" | cut -d '=' -f 2)"
fi

PROGRAMS=$(echo ${PROGRAMS} | jq -c '.[]' | sed 's/"//g')
WORKING_DIR=$(pwd)

for p in ${PROGRAMS[@]}; do
cargo fmt --all --manifest-path ./programs/${p}/Cargo.toml
done
19 changes: 19 additions & 0 deletions configs/scripts/program/lint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
OUTPUT="./programs/.bin"
# go to parent folder
cd $(dirname $(dirname $(dirname ${SCRIPT_DIR})))

rm -rf $OUTPUT

if [ -z ${PROGRAMS+x} ]; then
PROGRAMS="$(cat .github/.env | grep "PROGRAMS" | cut -d '=' -f 2)"
fi

PROGRAMS=$(echo ${PROGRAMS} | jq -c '.[]' | sed 's/"//g')
WORKING_DIR=$(pwd)

for p in ${PROGRAMS[@]}; do
cargo fmt --all --manifest-path ./programs/${p}/Cargo.toml -- --check
done
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@
"validator": "CI=1 amman start --config ./configs/validator.cjs",
"validator:debug": "amman start --config ./configs/validator.cjs",
"validator:logs": "CI=1 amman logs",
"validator:stop": "amman stop"
"validator:stop": "amman stop",
"lint:fix": "./configs/scripts/client/format-js.sh && ./configs/scripts/program/format.sh",
"format:fix": "pnpm lint:fix",
"lint": "./configs/scripts/client/lint-js.sh && ./configs/scripts/program/lint.sh",
"prepare": "husky"
},
"devDependencies": {
"@metaplex-foundation/amman": "^0.12.1",
"@metaplex-foundation/kinobi": "0.18.8-alpha.0",
"@metaplex-foundation/shank-js": "^0.1.7",
"husky": "^9.0.11",
"typescript": "^4.9.4"
},
"packageManager": "[email protected]"
}
}
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6098413

Please sign in to comment.