Skip to content

Instantly share code, notes, and snippets.

@NeoHBz
Created July 29, 2024 10:21
Show Gist options
  • Save NeoHBz/f20d267de2ea8f8794f2029fbc826935 to your computer and use it in GitHub Desktop.
Save NeoHBz/f20d267de2ea8f8794f2029fbc826935 to your computer and use it in GitHub Desktop.
Run: a zshrc function
run() {
local package_manager="bun"
local script_name="start"
local choice
if [ -f "ecosystem.config.js" ]; then
if [ -f "package.json" ]; then
echo "Both ecosystem.config.js and package.json are present. Which one do you want to run? (e/p, default: p): "
read choice
choice=${choice:-p}
else
echo "ecosystem.config.js is present. Do you want to run it? (y/n, default: y): "
read choice
choice=${choice:-y}
if [ "$choice" = "y" ]; then
choice="e"
elif [ "$choice" = "n" ]; then
echo "Skipping running ecosystem.config.js"
return 0
else
echo "Invalid choice. Please enter 'y' for yes or 'n' for no."
return 1
fi
fi
elif [ -f "package.json" ]; then
choice="p"
else
echo "Neither package.json nor ecosystem.config.js found in the current directory."
return 1
fi
if [ "$choice" = "p" ]; then
if [ -f "package.json" ]; then
local dev_script_exists=$(jq -r '.scripts.dev' package.json)
local start_script_exists=$(jq -r '.scripts.start' package.json)
if [ "$dev_script_exists" != "null" ]; then
script_name="dev"
fi
local command="$package_manager $script_name"
eval "$command"
else
echo "package.json not found in the current directory."
return 1
fi
elif [ "$choice" = "e" ]; then
eval "pm2 start ecosystem.config.js"
else
echo "Invalid choice. Please enter 'e' for ecosystem.config.js or 'p' for package.json."
return 1
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment