Replace prettier with tslint where possible

This commit is contained in:
glacambre 2019-04-06 22:50:01 +02:00
parent a2cf6671f2
commit 93a66ec96b
No known key found for this signature in database
GPG key ID: B9625DB1767553AC
3 changed files with 39 additions and 10 deletions

View file

@ -2,13 +2,15 @@
source ./scripts/common source ./scripts/common
jsfiles=$(cachedFiles) jsfiles=$(cachedTSLintFiles)
otherfiles=$(cachedPrettierFiles)
[ -z "$jsfiles" ] && exit 0
# Check if any of the files are ugly or contain a console.log call # Check if any of the files are ugly or contain a console.log call
consoleFiles=$(noisy $jsfiles) consoleFiles=$(noisy $jsfiles)
uglyFiles=$(ugly $jsfiles) uglyFiles="$(tslintUgly $jsfiles)"
if [ ! -n "$uglyFiles" ]; then
uglyFiles="$(prettierUgly $otherfiles)"
fi
if [ -n "$consoleFiles" ]; then if [ -n "$consoleFiles" ]; then
echo "Warning: adding console.log calls in ${consoleFiles[@]}" echo "Warning: adding console.log calls in ${consoleFiles[@]}"

View file

@ -2,8 +2,14 @@
# Accepts no arguments # Accepts no arguments
# Returns git-add'ed files as a list of filenames separated by a newline character # Returns git-add'ed files as a list of filenames separated by a newline character
cachedFiles() { cachedTSLintFiles() {
git diff --cached --name-only --diff-filter=ACM "*.js" "*.jsx" "*.ts" "*.tsx" "*.md" "*.css" git diff --cached --name-only --diff-filter=ACM "*.js" "*.jsx" "*.ts" "*.tsx"
}
# Accepts no arguments
# Returns git-add'ed files as a list of filenames separated by a newline character
cachedPrettierFiles() {
git diff --cached --name-only --diff-filter=ACM "*.md" "*.css"
} }
# Accepts a single argument which is the name of a file tracked by git # Accepts a single argument which is the name of a file tracked by git
@ -14,7 +20,7 @@ staged() {
# Accepts a single string argument made of multiple file names separated by a newline # Accepts a single string argument made of multiple file names separated by a newline
# Returns an array of files that prettier wants to lint # Returns an array of files that prettier wants to lint
ugly() { prettierUgly() {
local acc="" local acc=""
local IFS=$'\n' local IFS=$'\n'
for jsfile in $1; do for jsfile in $1; do
@ -23,6 +29,20 @@ ugly() {
echo "$acc" echo "$acc"
} }
tslintUgly() {
local acc=""
local IFS=$'\n'
local tmpdir=$(mktemp -d "tslint.XXXXXXXXX")
for jsfile in $1; do
tmpfile="$tmpdir/$jsfile"
mkdir -p "$(dirname "$tmpfile")"
staged "$jsfile" > "$tmpfile"
tslint -q "$tmpfile" 2>/dev/null || acc="$jsfile"$'\n'"$acc"
done
rm -rf "$tmpdir"
echo "$acc"
}
noisy() { noisy() {
local acc=() local acc=()
for jsfile in "$@"; do for jsfile in "$@"; do

View file

@ -27,7 +27,7 @@ unlock() {
trap "unlock $(git rev-parse --show-toplevel)/.git/index.lock || true" ERR trap "unlock $(git rev-parse --show-toplevel)/.git/index.lock || true" ERR
main() { main() {
local stagedFiles=$(cachedFiles) local stagedFiles="$(cachedTSLintFiles)"$'\n'"$(cachedPrettierFiles)"
if [ -n "$stagedFiles" ]; then if [ -n "$stagedFiles" ]; then
# Could use git-update-index --cacheinfo to add a file without creating directories and stuff. # Could use git-update-index --cacheinfo to add a file without creating directories and stuff.
@ -37,7 +37,10 @@ main() {
if cmp -s <(staged "$file") "$file"; then if cmp -s <(staged "$file") "$file"; then
echo "WARN: Staged copy of '$file' matches working copy. Modifying both" echo "WARN: Staged copy of '$file' matches working copy. Modifying both"
lock .git/index.lock lock .git/index.lock
prettier --write "$file" case "$file" in
*.md | *.css) prettier --write "$file";;
*) tslint --project . --fix "$file";;
esac
unlock .git/index.lock unlock .git/index.lock
git add "$file" git add "$file"
else else
@ -46,7 +49,11 @@ main() {
cd "$tmpdir" cd "$tmpdir"
mkdir -p $(dirname $file) mkdir -p $(dirname $file)
lock ../.git/index.lock lock ../.git/index.lock
staged "$file" | prettier --stdin-filepath "$file" > "$file" case "$file" in
*.md | *.css) staged "$file" | prettier --stdin-filepath "$file" > "$file";;
*) staged "$file" > "$file"
tslint -c ../tslint.json --fix "$file" 2>/dev/null;;
esac
chmod --reference="../$file" "$file" # match permissions chmod --reference="../$file" "$file" # match permissions
# Can't hold lock while git add occurs. Hopefully release and reacquire happen fast enough to prevent race. # Can't hold lock while git add occurs. Hopefully release and reacquire happen fast enough to prevent race.
unlock ../.git/index.lock unlock ../.git/index.lock