clang-format-js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/bin/bash
  2. # Copyright 2016 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. if [[ -z "${@}" ]]; then
  6. echo >&2 "Usage: `basename $0` <paths_to_clang_format...>"
  7. exit 1
  8. fi
  9. which clang-format >/dev/null 2>&1
  10. if [[ $? -ne 0 ]]; then
  11. echo >&2 "Sorry, but you need \`clang-format\` on your \$PATH to run this script"
  12. exit 1
  13. fi
  14. for arg in ${@}; do
  15. echo "Processing ${arg}"
  16. dir=`readlink -f "${arg}"`
  17. if [[ -d "${dir}" ]]; then
  18. dir="${dir}/stripped-by-dirname-on-next-line"
  19. fi
  20. while dir=`dirname ${dir}`; do
  21. if [[ -f "${dir}/.clang-format" ]]; then
  22. echo "Using style from: ${dir}/.clang-format"
  23. break
  24. elif [[ "${dir}" == "/" ]]; then
  25. echo >&2 "No .clang-format file found. Make one at or above ${arg}"
  26. exit 1
  27. fi
  28. done
  29. js_files=$(git ls-tree -r --name-only HEAD -- "${arg}" | grep '\.js$')
  30. for js_file in ${js_files}; do
  31. echo "Formatting ${js_file}"
  32. clang-format -i -style=file "$js_file"
  33. done
  34. done