feat: add init and reset scripts for module management

This commit is contained in:
Dominik Krenn 2025-07-19 11:28:26 +02:00
parent 6672ce76b0
commit eadb4c0cb5
2 changed files with 126 additions and 0 deletions

64
init.sh Normal file
View File

@ -0,0 +1,64 @@
#!/bin/bash
set -euo pipefail
MODULE_PATH="source/package.json"
NEW_NAME=""
NEW_VERSION="0.0.1"
SELF_DELETE=false
function help() {
echo "Usage: ./init.sh --name newname [--version x.y.z] [--once]"
echo
echo "If any args are missing, you'll be prompted like a good squirrel."
exit 0
}
# === CLI Args ===
while [[ $# -gt 0 ]]; do
case "$1" in
--name) NEW_NAME="$2"; shift ;;
--version) NEW_VERSION="$2"; shift ;;
--once) SELF_DELETE=true ;;
--help) help ;;
*) echo "❌ Unknown option: $1"; help ;;
esac
shift
done
# === Prompt if lazy ===
[[ -z "$NEW_NAME" ]] && {
read -rp "📦 Enter new module name (e.g. @fluff/my-module): " NEW_NAME
}
[[ -z "$NEW_VERSION" ]] && {
read -rp "🕒 Enter version [default: 0.0.1]: " user_version
NEW_VERSION="${user_version:-$NEW_VERSION}"
}
echo "✨ Renaming module in '$MODULE_PATH'"
echo " → name: $NEW_NAME"
echo " → version: $NEW_VERSION"
# === Safety check ===
if [[ ! -f "$MODULE_PATH" ]]; then
echo "💥 package.json not found at $MODULE_PATH"
exit 1
fi
# === Requires jq ===
if ! command -v jq &>/dev/null; then
echo "🧨 'jq' not installed. Please install it first."
exit 1
fi
# === Apply changes ===
tmpfile=$(mktemp)
jq ".name = \"$NEW_NAME\" | .version = \"$NEW_VERSION\"" "$MODULE_PATH" > "$tmpfile"
mv "$tmpfile" "$MODULE_PATH"
echo "✅ Success! Module is now '$NEW_NAME@$NEW_VERSION'"
# === Self delete ===
if [[ "$SELF_DELETE" = true ]]; then
echo "🗑️ Self-destructing..."
rm -- "$0"
fi

62
reset.sh Normal file
View File

@ -0,0 +1,62 @@
#!/bin/bash
set -euo pipefail
TEMPLATE_REMOTE="git@git.chipperfluff.at:projects/funkyFlaskTest.git"
CONFIRM_MODULE=""
SELF_PATH="$0"
# === 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."