Skip to content

Instantly share code, notes, and snippets.

@ka2n
Last active January 12, 2023 01:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ka2n/2fbf99efdad54b9daa73a242a1e8ec19 to your computer and use it in GitHub Desktop.
Save ka2n/2fbf99efdad54b9daa73a242a1e8ec19 to your computer and use it in GitHub Desktop.
Find upwards to package root
#!/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
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