summaryrefslogtreecommitdiff
path: root/scripts/frontend/prettier.sh
blob: 463145591d3cc27a241b7b30aa85a2dff14215c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env bash
set -o noglob

: ${1?"Usage: $0 <check|save|check-all|save-all>"}

PRETTIER_PATH=$(git rev-parse --show-toplevel)/node_modules/.bin/prettier

if [ ! -f $PRETTIER_PATH ]; then
    echo "ERROR: Prettier not found!"
    exit 1
fi

case $1 in
check)
  action=check
  prettier_args=--list-different
  files=staged
  ;;
save)
  action=save
  prettier_args=--write
  files=staged
  ;;
check-all)
  action=check
  prettier_args=--list-different
  files=all
  ;;
save-all)
  action=save
  prettier_args=--write
  files=all
  ;;
*)
  echo "$1 is not a valid argument, please use check, save, check-all or save-all"
  exit 1
  ;;
esac

echo -e "Loading $files files ...\n"

if [ "$files" == "staged" ]; then
    file_list=$(git diff --name-only --cached --diff-filter=ACMRTUB '*.vue' '*.js' '*.scss'  | tr '\r\n' ' ')
else
    file_list="**/*.vue **/*.js **/*.scss"
fi

if [ "$action" == "check" ]; then
    if $PRETTIER_PATH $prettier_args $file_list; then
        echo -e "\nFormat of $files files is correct."
        exit 0
    else
        echo -e "\n===============================\nGitLab uses Prettier to format all JavaScript code.\nPlease format each file listed above or run 'yarn prettier-$files-save'\n===============================\n"
        exit 1
    fi
fi

if [ "$action" == "save" ]; then
    if $PRETTIER_PATH $prettier_args $file_list; then
        echo -e "\nFormatted $files files successfully with prettier."
        exit 0
    else
        echo -e "\nSomething went wrong while formatting with prettier."
        exit 1
    fi
fi

echo "ERROR: Something went wrong"

exit 1