Last active
September 5, 2018 21:48
-
-
Save smellymonk/cc2974185ecbe64fb4f340cd7fc89026 to your computer and use it in GitHub Desktop.
Bash utilities for working with NodeJS projects
This file contains hidden or 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
# Some useful random Bash utils, mostly for working with NodeJS projects. I use `dep` and `findjs` the most. :) | |
# You can just stuff these in your .bashrc or .zshrc | |
alias bork='rm -rf ./node_modules && yarn install' | |
# an actual bork: | |
# alias bork='find ./node_modules -type d -depth 1 | sort -R | sed 1q | rm -rf' | |
# simple libre: | |
# alias libre='find . -type d -name "node_modules" -prune -o -name "package-lock.json" -exec rm {} \;' | |
# finds files, ignores several types of directories (you might need to modify that): | |
# e.g. `libre yarn.lock -print` or `libre -d yarn.lock` | |
libre() { | |
local ignores=(node_modules dist es lib server) | |
local FIND=(.) | |
for i in "${ignores[@]}"; do | |
FIND+=(-type d -name "$i" -prune -o) | |
done | |
FIND+=(-name) | |
if [ "$1" = "-d" ]; then | |
shift | |
FIND+=($1 -exec rm {} \;) | |
shift | |
fi | |
find "${FIND[@]}" "$@" | |
} | |
# possibly the most useful utility (beware it ignores the same directories in `libre` function): | |
# e.g. `findit '*.js' someFunctionName` | |
findit() { | |
libre "$1" -print0 | xargs -0 grep -n "$2" | |
} | |
alias findjs="findit '*.js'" | |
# requires jq tool, `brew install jq` | |
# regex search of dependencies. super useful in a project directory or a monorepo. | |
# e.g. `dep react` or `dep ^react$` | |
dep() { | |
libre package.json -exec sh -c "\ | |
jq -r '.dependencies | keys[]' {} | \ | |
grep -q \"$1\" && \ | |
jq -r '.name' {} && \ | |
jq -r '.dependencies | to_entries | map(select(.key | match(\"$1\"))) | map(\"\t\"+.key+\"@\"+.value) | .[]' {} \ | |
" \; | |
} | |
# the following are useful for pseudo monorepos, where every git repo is in packages/ dir | |
update-repos() { | |
for repo in packages/*/; do | |
pushd "$repo" >/dev/null | |
if [[ -z "$1" ]]; then | |
git pull | |
else | |
git checkout "$1" && git pull | |
fi | |
popd >/dev/null | |
done | |
} | |
get-branches() { | |
local branch | |
local name | |
for repo in packages/*/; do | |
pushd "$repo" >/dev/null | |
branch=$(git symbolic-ref --short HEAD) | |
name=$(cat package.json | jq -r '.name') | |
printf "${name} ${branch}\n" | |
popd >/dev/null | |
done | |
} | |
branches() { | |
get-branches | column -t | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment