Skip to content

Instantly share code, notes, and snippets.

@Darkflib
Created September 18, 2024 19:43
Show Gist options
  • Save Darkflib/5f89341c21ca53dd0492c17fbf708bd8 to your computer and use it in GitHub Desktop.
Save Darkflib/5f89341c21ca53dd0492c17fbf708bd8 to your computer and use it in GitHub Desktop.
vscode ext manager
#!/bin/bash
# MIT License - [email protected]
vsc-ext-man() {
VSCODE_BINARY=${VSCODE_BINARY:-code}
COMMAND=$1
FILE=$2 # FILE is set if provided
if ! command -v "$VSCODE_BINARY" &> /dev/null; then
echo "Error: $VSCODE_BINARY not found."
return 1
fi
case $COMMAND in
backup)
FILE=${FILE:-extensions.txt}
# List extensions with versions and save in format extensionName@version
"$VSCODE_BINARY" --list-extensions --show-versions > "$FILE"
echo "Extensions backed up to $FILE"
;;
list)
"$VSCODE_BINARY" --list-extensions
;;
restore)
FILE=${FILE:-extensions.txt}
if [ -f "$FILE" ]; then
# Install extensions with specific versions
grep -v '^#' "$FILE" | xargs -n1 "$VSCODE_BINARY" --install-extension
echo "Extensions restored from $FILE"
else
echo "Error: Backup file $FILE does not exist."
return 1
fi
;;
uninstall)
if [ -z "$FILE" ]; then
# No file specified, uninstall all extensions
echo "This will uninstall all installed extensions."
read -p "Are you sure? [y/N]: " confirm
if [[ "$confirm" =~ ^[Yy]$ ]]; then
# Get list of installed extensions and uninstall them
"$VSCODE_BINARY" --list-extensions | xargs -n1 "$VSCODE_BINARY" --uninstall-extension
echo "All extensions have been uninstalled."
else
echo "Operation cancelled."
fi
else
if [ -f "$FILE" ]; then
# Uninstall extensions listed in the file, ignoring comments
grep -v '^#' "$FILE" | xargs -n1 "$VSCODE_BINARY" --uninstall-extension
echo "Extensions uninstalled from $FILE"
else
echo "Error: Backup file $FILE does not exist."
return 1
fi
fi
;;
*)
echo "Usage: vsc-ext-man {backup|list|restore|uninstall} [file]"
;;
esac
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment