Skip to content

Commit

Permalink
Eget node prosjekt for å kjøre ktlint med husky
Browse files Browse the repository at this point in the history
  • Loading branch information
Bjorn Golberg committed Oct 9, 2024
1 parent d1c0435 commit 3c0157d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

changedKotlinFiles=$(git diff --name-only --cached | grep '\.kt[s"]\?$' || true)

if [ -n "${changedKotlinFiles-unset}" ]; then
kotlinversion=$(ktlint -v)
expectedVersion="ktlint version 1.3.1"
if [ "$kotlinversion" != "$expectedVersion" ]; then
echo "Running version $kotlinversion needs to be $expectedVersion"
exit 1
fi

echo "🛠 Running ktlint"

#store the result of the commit in a variable and split into array items with newline
committedFiles=$(git diff --name-only --cached --diff-filter=d)
files=$(echo $committedFiles | tr ";" "\\n")

# https://github.com/pinterest/ktlint pre-commit hook
git diff --name-only --cached | grep '\.kt[s"]\?$' | xargs ktlint -F
echo "✍️ Formatted kotlin files"

#replay items in the commits array and add only those files
for file in $files
do
git add $file
done
fi

echo "🏆 Files staged"

exit 0
12 changes: 12 additions & 0 deletions ktlint/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "ktlint",
"version": "1.0.0",
"description": "",
"scripts": {
"prepare": "cd ../ && husky .husky"
},
"devDependencies": {
"husky": "~9.0.11"
},
"packageManager": "[email protected]+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
}
8 changes: 8 additions & 0 deletions ktlint/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


husky@~9.0.11:
version "9.0.11"
resolved "https://registry.yarnpkg.com/husky/-/husky-9.0.11.tgz#fc91df4c756050de41b3e478b2158b87c1e79af9"
integrity sha512-AB6lFlbwwyIqMdHYhwPe+kjOC3Oc5P3nThEoW/AaO2BX3vJDjWPFxYLxokUZOo6RNX20He3AaT8sESs9NJcmEw==

0 comments on commit 3c0157d

Please sign in to comment.