Last active
January 12, 2023 01:24
-
-
Save ka2n/2fbf99efdad54b9daa73a242a1e8ec19 to your computer and use it in GitHub Desktop.
Find upwards to package root
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
ROOT_DIR=$(git rev-parse --show-toplevel) | |
if [ -z "$ROOT_DIR" ]; then | |
echo "Not a git repository" | |
exit 1 | |
fi | |
if [[ "$PWD" == "$ROOT_DIR" ]]; then | |
exit 0 | |
fi | |
PACKGE_FILES=(package.json go.mod Cargo.toml Gemfile) | |
cd .. | |
prev=. | |
while [[ "$PWD" != "$prev" ]] ; do | |
# If $PWD is the git root directory, exit | |
if [[ "$PWD" == "$ROOT_DIR" ]]; then | |
echo "$PWD" | |
exit 0 | |
fi | |
# Check if any of the package files exist | |
for file in "${PACKGE_FILES[@]}"; do | |
if [ -f "$file" ]; then | |
echo "$PWD" | |
exit 0 | |
fi | |
done | |
prev="$PWD" | |
cd .. | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function upp | |
cd (find-parent-package-dir; or echo ".") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment