Skip to content

Instantly share code, notes, and snippets.

@shikachii
Created November 30, 2023 14:04
Show Gist options
  • Save shikachii/5353deca7930fb78b878654898e3598c to your computer and use it in GitHub Desktop.
Save shikachii/5353deca7930fb78b878654898e3598c to your computer and use it in GitHub Desktop.
manage your local development node server
#!/bin/zsh
DEV_COMMAND="[YOUR DEV COMMAND, PLEASE REPLACE]"
COMMAND_PROCESS=$(pgrep -f $DEV_COMMAND)
if [ $? -ne 0 ]; then
echo "The command '$DEV_COMMAND' is not currently running."
exit 1
fi
set -e
# get running node server process
PROCESS_DETAIL=$(pgrep -lf $DEV_COMMAND)
# get working dir
WORKDIR=$(echo $PROCESS_DETAIL | awk '{print $3}' | awk -F/node_modules '{print $1}')
# get branch name
BRANCH=$(git -C $WORKDIR branch --contains | cut -d ' ' -f 2)
# if exist kill option
while getopts "hk" opt; do
case $opt in
h|\?)
echo "Usage: ./node-server-manager.sh [OPTION]..."
echo "This command is manage your development node server."
echo ""
echo "Options:"
echo " -h display this help and exit"
echo " -k kill node server and exit"
exit 0
;;
k)
SERVER_PROCESS=$(pgrep -P $COMMAND_PROCESS)
kill -9 $COMMAND_PROCESS $SERVER_PROCESS
exit 0
;;
esac
done
# echo "You are currently develeping in $WORKDIR, and you are on the $BRANCH"
echo $WORKDIR:$BRANCH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment