Split formatters, add dry run exit codes

This commit is contained in:
Arnie 2017-12-25 09:32:13 +01:00
parent e664352139
commit 6b856f9731
6 changed files with 117 additions and 36 deletions

View File

@ -46,6 +46,24 @@ main() {
process
return $?
;;
prettier)
__initVariables "$@"
source ${INCLUDES_PATH}/prettier.shinc
__prettier
return $?
;;
csscomb)
__initVariables "$@"
source ${INCLUDES_PATH}/csscomb.shinc
__csscomb
return $?
;;
php-cs-fixer)
__initVariables "$@"
source ${INCLUDES_PATH}/php-cs-fixer.shinc
__phpFixer
return $?
;;
help|--help|-h)
usage
return 0

View File

@ -0,0 +1,14 @@
#!/usr/bin/env bash
__csscomb() {
__msg "Csscomb:"
if [[ ${DRY_RUN} -eq 0 ]]; then
csscomb -v "$@"
else
csscomb --lint -v "$@"
fi
return $?
}

View File

@ -0,0 +1,57 @@
#!/usr/bin/env bash
__phpFixer() {
local failed=0
__msg "PHP-cs-fixer"
# Cannot chain php files without specifying a config CS file :(
if [[ ${DRY_RUN} -eq 0 ]]; then
for file in "${PHP_FILES[@]}"; do
php-cs-fixer fix "${file}"
done
else
local needs_fixing=()
local invalid_syntax=()
for file in "${PHP_FILES[@]}"; do
php-cs-fixer fix --dry-run "${file}"
case $? in
0)
#all good
;;
4)
invalid_syntax+=("${file}")
;;
8)
needs_fixing+=("${file}")
;;
*)
__err "There was an error with php-cs-fixer configuration"
failed=1
;;
esac
done
if [[ ${#needs_fixing[@]} -gt 0 ]]; then
failed=1
__err "Needs fixing:" 1
for file in "${needs_fixing[@]}"; do
__msg "${file}" 2
done
fi
if [[ ${#invalid_syntax[@]} -gt 0 ]]; then
failed=1
__err "Invalid syntax:" 1
for file in "${invalid_syntax[@]}"; do
__msg "${file}" 2
done
fi
fi
return ${failed}
}

View File

@ -0,0 +1,15 @@
#!/usr/bin/env bash
__prettier() {
__msg "Prettier:"
if [[ ${DRY_RUN} -eq 0 ]]; then
prettier --write "$@"
else
__msg "Listing (: unprettiered :) files:" 1
prettier --list-different "$@"
fi
return $?
}

View File

@ -1,54 +1,31 @@
#!/usr/bin/env bash
__csscomb() {
if [[ ${DRY_RUN} -eq 0 ]]; then
csscomb "$@"
else
csscomb --lint "$@"
fi
}
__prettier() {
if [[ ${DRY_RUN} -eq 0 ]]; then
prettier --write "$@"
else
prettier --list-different "$@"
fi
}
__phpFixer() {
# Cannot chain php files without specifying a config CS file :(
if [[ ${DRY_RUN} -eq 0 ]]; then
for file in "${PHP_FILES[@]}"; do
php-cs-fixer fix "${file}"
done
else
for file in "${PHP_FILES[@]}"; do
php-cs-fixer fix --dry-run "${file}"
done
fi
}
source ${INCLUDES_PATH}/formatters/prettier.shinc
source ${INCLUDES_PATH}/formatters/csscomb.shinc
source ${INCLUDES_PATH}/formatters/php-cs-fixer.shinc
process() {
local failed=0
# Run css comb
if [[ ${#CSS_FILES[@]} -ne 0 ]]; then
__csscomb "${CSS_FILES[@]}"
[[ $? -ne 0 ]] && failed=1
fi
# Run JS prettier
if [[ ${#JS_FILES[@]} -ne 0 ]]; then
__prettier --write "${JS_FILES[@]}"
__prettier "${JS_FILES[@]}"
[[ $? -ne 0 ]] && failed=1
fi
# Run PHP cs fixer
if [[ ${#PHP_FILES[@]} -ne 0 ]]; then
__phpFixer ${PHP_FILES[@]}
fi
[[ $? -ne 0 ]] && failed=1
fi
return ${failed}
}

View File

@ -26,7 +26,7 @@ __initVariables() {
if [[ $? -ne 0 ]]; then
__err "Specified path does not exist: ${1}"
else
CSS_FILES+=(${1})
CSS_FILES+=("${1}")
fi
;;
*.js|*.jsx)
@ -34,7 +34,7 @@ __initVariables() {
if [[ $? -ne 0 ]]; then
__err "Specified path does not exist: ${1}"
else
JS_FILES+=(${1})
JS_FILES+=("${1}")
fi
;;
*.php|*.phtml)
@ -42,7 +42,7 @@ __initVariables() {
if [[ $? -ne 0 ]]; then
__err "Specified path does not exist: ${1}"
else
PHP_FILES+=(${1})
PHP_FILES+=("${1}")
fi
;;
esac