Skip to content

Instantly share code, notes, and snippets.

@alvaropaco
Last active April 26, 2024 18:02
Show Gist options
  • Save alvaropaco/6d678518a929690b8ac54e78be124eb4 to your computer and use it in GitHub Desktop.
Save alvaropaco/6d678518a929690b8ac54e78be124eb4 to your computer and use it in GitHub Desktop.
#!/bin/bash
# First of all, install the jq lib
# Path to the user's .zshrc file
ZSHRC="$HOME/.zshrc"
# Check if jq is installed, install if it's not
if ! command -v jq &> /dev/null; then
echo "jq is not installed. Installing jq..."
# Install jq based on the OS
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
sudo apt-get install -y jq
elif [[ "$OSTYPE" == "darwin"* ]]; then
brew install jq
else
echo "Unsupported OS. Please install jq manually."
exit 1
fi
fi
# Append the function to .zshrc if it doesn't already exist
if ! grep -q "parse_package_json()" "$ZSHRC"; then
echo "Adding npm script autocompletion to .zshrc..."
cat <<EOF >> "$ZSHRC"
# Function to extract script names from package.json for autocompletion
parse_package_json() {
local scripts=\$(jq -r '.scripts | keys | .[]' package.json)
reply=(\${(ps:\n:)scripts})
}
# Attach autocomplete function to npm command
compctl -K parse_package_json npm
EOF
else
echo "Autocompletion setup already exists in .zshrc"
fi
echo "Setup complete. Please restart your terminal or run 'source ~/.zshrc'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment