funkyFlaskTest/reset.sh

93 lines
2.4 KiB
Bash
Executable File

#!/bin/bash
set -euo pipefail
TEMPLATE_REMOTE="git@git.chipperfluff.at:projects/funkyFlaskTest.git"
CONFIRM_MODULE=""
SELF_PATH="$0"
# === Dependency check ===
require() {
if ! command -v "$1" &>/dev/null; then
echo "❗ Missing dependency: $1"
read -rp "👉 Install $1 now? (y/n): " confirm
if [[ "$confirm" =~ ^[Yy]$ ]]; then
if [[ -f /etc/debian_version ]]; then
sudo apt-get update && sudo apt-get install -y "$1" || {
echo "💥 Failed to install $1 via apt-get"
exit 1
}
elif [[ "$(uname)" == "Darwin" ]]; then
brew install "$1" || {
echo "💥 Failed to install $1 via brew"
exit 1
}
else
echo "🤷 I don't know how to install $1 on this system."
exit 1
fi
else
echo "🚫 Cannot continue without $1"
exit 1
fi
fi
}
require git
require jq
# === Parse CLI args ===
while [[ $# -gt 0 ]]; do
case "$1" in
--confirm) CONFIRM_MODULE="$2"; shift ;;
--help)
echo "Usage: ./reset.sh [--confirm your-current-module-name]"
echo "Wipes current repo and resets to template:"
echo " $TEMPLATE_REMOTE"
exit 0 ;;
*)
echo "❌ Unknown option: $1"
exit 1 ;;
esac
shift
done
# === Detect current module name from package.json ===
if [[ ! -f source/package.json ]]; then
echo "❌ Can't find source/package.json. Not a proper squirrel module?"
exit 1
fi
CURRENT_NAME=$(jq -r '.name' source/package.json)
# === Confirm the name matches or prompt ===
if [[ -z "$CONFIRM_MODULE" ]]; then
read -rp "⚠️ Type the current module name to confirm reset ($CURRENT_NAME): " CONFIRM_MODULE
fi
if [[ "$CONFIRM_MODULE" != "$CURRENT_NAME" ]]; then
echo "🛑 Confirmation failed. You typed '$CONFIRM_MODULE', but module is '$CURRENT_NAME'."
exit 1
fi
echo "🧨 Confirmed. Resetting module '$CURRENT_NAME' to template."
# === Ensure git repo exists ===
if [[ ! -d .git ]]; then
echo "📁 No git repo found. Initializing..."
git init
git remote add origin "$TEMPLATE_REMOTE"
fi
# === Ensure correct remote is set ===
if ! git remote get-url origin | grep -q "$TEMPLATE_REMOTE"; then
echo "🔗 Setting template remote to $TEMPLATE_REMOTE"
git remote set-url origin "$TEMPLATE_REMOTE"
fi
# === Reset hard to remote ===
echo "🔥 Fetching template and resetting..."
git fetch origin
git reset --hard origin/HEAD
echo "✅ Reset complete. Your sins have been erased."