-
Notifications
You must be signed in to change notification settings - Fork 81
/
apheleia-from-project-root
executable file
·40 lines (34 loc) · 1.21 KB
/
apheleia-from-project-root
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
#!/usr/bin/env bash
# USAGE: apheleia-from-project-root ROOT-FNAME CMD...
#
# If there is a file called ROOT-FNAME in the current directory or any
# parent directory up to the root of the filesystem, first change to
# that directory. If not, no worries, proceed without error. Then
# execute CMD with provided args.
#
# This can be used to make sure that a formatter is executed in the
# root directory of a project, if the file is within a project. For
# example, ROOT-FNAME could be ".git" or ".formatter.exs" or
# "package.json" or "pyproject.toml".
if (( "$#" <= 1 )); then
echo >&2 "usage: apheleia-from-project-root ROOT-FNAME CMD..."
exit 1
fi
# This function prints the name of the current directory if it
# contains a file or directory named after the first argument, or the
# parent directory if it contains such a file, or the parent's parent,
# and so on. If no such file is found it returns nonzero.
# https://unix.stackexchange.com/a/22215
find_upwards() {
fname="$1"
path="${PWD}"
while [[ -n "${path}" && ! -e "${path}/${fname}" ]]; do
path="${path%/*}"
done
[[ -n "${path}" ]] && echo "${path}"
}
if dir="$(find_upwards "$1")"; then
cd -- "${dir}" || exit
fi
shift
exec "$@"