-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add lint:changed and lint:changed:fix (#234)
The Git hook that runs on push will only lint files changed within the current branch. However, it won't fix anything; to do that, you'll have to run `yarn lint:fix`. But actually, this runs every file through the linter, so it's fairly inefficient. This commit adds `lint:changed` and `lint:changed:fix` so that you can do the same thing that the Git hook does.
- Loading branch information
Showing
5 changed files
with
78 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/bash | ||
|
||
exec prettier "$@" . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#!/bin/bash | ||
|
||
get-files-to-lint() { | ||
local flag="$1" | ||
local current_branch_name="$(git branch --show-current)" | ||
|
||
if [[ "$current_branch_name" == "main" ]]; then | ||
git diff origin/main...HEAD --name-only --diff-filter=d | ||
else | ||
git diff main...HEAD --name-only --diff-filter=d | ||
fi | ||
|
||
if [[ $flag == "--include-uncommitted" ]]; then | ||
git diff --name-only --diff-filter=d | ||
fi | ||
} | ||
|
||
main() { | ||
local prettier_flag | ||
local include_uncommitted_flag | ||
|
||
while [[ -n "$1" ]]; do | ||
case "$1" in | ||
--check | --write) | ||
prettier_flag="$1" | ||
shift | ||
;; | ||
--include-uncommitted) | ||
include_uncommitted_flag="$1" | ||
shift | ||
;; | ||
*) | ||
echo "ERROR: Unknown option $1." | ||
exit 1 | ||
;; | ||
esac | ||
done | ||
|
||
local files_to_lint="$(get-files-to-lint "$include_uncommitted_flag")" | ||
|
||
if [[ -z "$prettier_flag" ]]; then | ||
echo "ERROR: Missing --check or --write." | ||
exit 1 | ||
fi | ||
|
||
echo "*** Checking for lint violations in changed files ***************" | ||
echo | ||
|
||
if [[ -n "$files_to_lint" ]]; then | ||
echo "Files to check:" | ||
echo "$files_to_lint" | while IFS=$'\n' read -r line; do | ||
echo "- $line" | ||
done | ||
|
||
echo | ||
echo "$files_to_lint" | while IFS=$'\n' read -r line; do | ||
printf '%s\0' "$line" | ||
done | xargs -0 yarn prettier "$prettier_flag" --ignore-unknown | ||
else | ||
echo "No files to lint this time." | ||
fi | ||
} | ||
|
||
main "$@" |