mirror of
https://github.com/vale981/tridactyl
synced 2025-03-05 09:31:41 -05:00
Replace prettier with tslint where possible
This commit is contained in:
parent
a2cf6671f2
commit
93a66ec96b
3 changed files with 39 additions and 10 deletions
|
@ -2,13 +2,15 @@
|
|||
|
||||
source ./scripts/common
|
||||
|
||||
jsfiles=$(cachedFiles)
|
||||
|
||||
[ -z "$jsfiles" ] && exit 0
|
||||
jsfiles=$(cachedTSLintFiles)
|
||||
otherfiles=$(cachedPrettierFiles)
|
||||
|
||||
# Check if any of the files are ugly or contain a console.log call
|
||||
consoleFiles=$(noisy $jsfiles)
|
||||
uglyFiles=$(ugly $jsfiles)
|
||||
uglyFiles="$(tslintUgly $jsfiles)"
|
||||
if [ ! -n "$uglyFiles" ]; then
|
||||
uglyFiles="$(prettierUgly $otherfiles)"
|
||||
fi
|
||||
|
||||
if [ -n "$consoleFiles" ]; then
|
||||
echo "Warning: adding console.log calls in ${consoleFiles[@]}"
|
||||
|
|
|
@ -2,8 +2,14 @@
|
|||
|
||||
# Accepts no arguments
|
||||
# Returns git-add'ed files as a list of filenames separated by a newline character
|
||||
cachedFiles() {
|
||||
git diff --cached --name-only --diff-filter=ACM "*.js" "*.jsx" "*.ts" "*.tsx" "*.md" "*.css"
|
||||
cachedTSLintFiles() {
|
||||
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
|
||||
|
@ -14,7 +20,7 @@ staged() {
|
|||
|
||||
# Accepts a single string argument made of multiple file names separated by a newline
|
||||
# Returns an array of files that prettier wants to lint
|
||||
ugly() {
|
||||
prettierUgly() {
|
||||
local acc=""
|
||||
local IFS=$'\n'
|
||||
for jsfile in $1; do
|
||||
|
@ -23,6 +29,20 @@ ugly() {
|
|||
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() {
|
||||
local acc=()
|
||||
for jsfile in "$@"; do
|
||||
|
|
|
@ -27,7 +27,7 @@ unlock() {
|
|||
trap "unlock $(git rev-parse --show-toplevel)/.git/index.lock || true" ERR
|
||||
|
||||
main() {
|
||||
local stagedFiles=$(cachedFiles)
|
||||
local stagedFiles="$(cachedTSLintFiles)"$'\n'"$(cachedPrettierFiles)"
|
||||
|
||||
if [ -n "$stagedFiles" ]; then
|
||||
# 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
|
||||
echo "WARN: Staged copy of '$file' matches working copy. Modifying both"
|
||||
lock .git/index.lock
|
||||
prettier --write "$file"
|
||||
case "$file" in
|
||||
*.md | *.css) prettier --write "$file";;
|
||||
*) tslint --project . --fix "$file";;
|
||||
esac
|
||||
unlock .git/index.lock
|
||||
git add "$file"
|
||||
else
|
||||
|
@ -46,7 +49,11 @@ main() {
|
|||
cd "$tmpdir"
|
||||
mkdir -p $(dirname $file)
|
||||
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
|
||||
# Can't hold lock while git add occurs. Hopefully release and reacquire happen fast enough to prevent race.
|
||||
unlock ../.git/index.lock
|
||||
|
|
Loading…
Add table
Reference in a new issue