Last active
July 16, 2024 21:58
-
-
Save iguatemigarin/237973aeacf464c4ba98ed74c5747999 to your computer and use it in GitHub Desktop.
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
#!/bin/bash | |
# Translator CLI Wrapper Script | |
# This script manages the installation, updating, and execution of the @mirohq-internal/translation-cli package. | |
# It provides a wrapper around the translator command, handling automatic updates and configuration management. | |
PACKAGE="{PACKAGE}" | |
COMMAND_NAME="{COMMAND}" | |
CONFIG_DIR="$HOME/.config/$COMMAND_NAME" | |
TIMESTAMP_FILE="$CONFIG_DIR/last_update" | |
UPDATE_INTERVAL=$((24 * 60 * 60)) # 24 hours in seconds | |
GREEN='\033[0;32m' | |
BLUE='\033[0;34m' | |
RED='\033[0;31m' | |
NC='\033[0m' # No Color | |
# Exposed functions | |
--expose-help() { | |
local cmd=$(basename "$0") | |
echo "{PACKAGE} CLI Wrapper" | |
echo "Usage: $cmd [--cli-wrapper <command>] [translator args]" | |
echo | |
echo "Wrapper commands:" | |
echo " help - Display this help message" | |
echo " reset-config - Reset the configuration directory" | |
echo " install - Force installation of the package" | |
echo " update - Check for updates and install if available" | |
echo " uninstall - Uninstall the package and remove configuration" | |
echo " reinstall - Reinstall the package and reset configuration" | |
echo | |
echo "Examples:" | |
echo " $cmd --cli-wrapper help" | |
echo " $cmd --cli-wrapper install" | |
echo " $cmd [translator args]" | |
echo | |
echo "Troubleshooting:" | |
echo " If you encounter issues, try resetting the configuration:" | |
echo " $cmd --cli-wrapper reset-config" | |
echo | |
echo " As a last resort, try reinstalling the package:" | |
echo " $cmd --cli-wrapper reinstall" | |
} | |
--expose-reset-config() { | |
cleanup_config | |
init_config | |
log "Config reset complete" | |
} | |
--expose-install() { | |
log "Installing $PACKAGE..." | |
if npm i -g "$PACKAGE" ; then | |
update_timestamp | |
log "${GREEN}Successfully installed $PACKAGE${NC}" | |
else | |
error "Failed to install $PACKAGE. Exiting." | |
exit 1 | |
fi | |
} | |
--expose-update() { | |
log "Checking for updates..." | |
local latest_version=$(get_latest_version) | |
local current_version=$(get_installed_version) | |
if [ -z "$latest_version" ] || [ -z "$current_version" ]; then | |
error "Failed to retrieve version information. Skipping update." | |
return 1 | |
fi | |
if [ "$latest_version" != "$current_version" ]; then | |
log "New version available. Updating $PACKAGE..." | |
if npm i -g "$PACKAGE" ; then | |
log "${GREEN}Successfully updated $PACKAGE to version $latest_version${NC}" | |
update_timestamp | |
return 0 | |
else | |
error "Failed to update $PACKAGE. Continuing with existing version." | |
return 1 | |
fi | |
else | |
log "${GREEN}$PACKAGE is already up to date (version $current_version)${NC}" | |
update_timestamp | |
return 0 | |
fi | |
} | |
--expose-uninstall() { | |
log "Uninstalling $PACKAGE..." | |
if npm uninstall -g "$PACKAGE"; then | |
log "${GREEN}Successfully uninstalled $PACKAGE${NC}" | |
cleanup_config | |
log "Configuration directory removed" | |
else | |
error "Failed to uninstall $PACKAGE" | |
exit 1 | |
fi | |
} | |
--expose-reinstall() { | |
--expose-uninstall | |
--expose-install | |
} | |
# Internal functions | |
log() { | |
echo -e "${BLUE}[$COMMAND_NAME Wrapper]${NC} $1" | |
} | |
error() { | |
echo -e "${RED}[$COMMAND_NAME Wrapper Error]${NC} $1" >&2 | |
} | |
cleanup_config() { | |
log "Cleaning up config..." | |
rm -rf "$CONFIG_DIR" | |
log "Config cleaned up" | |
} | |
init_config() { | |
mkdir -p "$CONFIG_DIR" | |
} | |
update_timestamp() { | |
echo $(date +%s) > "$TIMESTAMP_FILE" | |
} | |
get_installed_version() { | |
npm list -g "$PACKAGE" --depth=0 2>/dev/null | grep "$PACKAGE@" | cut -d'@' -f3 | |
} | |
get_latest_version() { | |
npm -g view "$PACKAGE" version 2>/dev/null | |
} | |
process_wrapper_commands() { | |
if [ "$1" = "--cli-wrapper" ]; then | |
shift | |
while [ $# -gt 0 ]; do | |
func_name="--expose-$1" | |
if [ "$(type -t -- "$func_name")" = "function" ]; then | |
"$func_name" | |
else | |
error "Unknown wrapper command: $1" | |
--expose-help | |
fi | |
shift | |
done | |
exit 0 | |
fi | |
} | |
# Main script logic | |
main() { | |
# Process wrapper-specific commands | |
process_wrapper_commands "$@" | |
# Normal wrapper functionality | |
init_config | |
if ! command -v $COMMAND_NAME &> /dev/null; then | |
--expose-install | |
elif [ -f "$TIMESTAMP_FILE" ]; then | |
last_update=$(cat "$TIMESTAMP_FILE") | |
current_time=$(date +%s) | |
time_diff=$((current_time - last_update)) | |
if [ $time_diff -ge $UPDATE_INTERVAL ]; then | |
--expose-update | |
fi | |
else | |
--expose-update | |
fi | |
$COMMAND_NAME "$@" | |
} | |
# Execute main function | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment