Files
HabitTrove/Budfile
2025-01-02 18:26:52 -05:00

104 lines
2.9 KiB
Bash
Executable File

#!/bin/bash
_warn() {
echo -e -n "\033[1;33mWarning:\033[0m "
echo "${1}"
shift
for arg in "$@"; do
echo " ${arg}"
done
}
bump_version() {
echo "Which version part would you like to bump? ([M]ajor/[m]inor/[p]atch)"
read -r version_part
# Get current version
current_version=$(node -p "require('./package.json').version")
IFS='.' read -r major minor patch <<<"$current_version"
# Calculate new version
if [[ "$version_part" =~ ^M$ ]]; then
new_version="$((major + 1)).0.0"
elif [[ "$version_part" =~ ^m$ ]]; then
new_version="$major.$((minor + 1)).0"
elif [[ "$version_part" =~ ^p$ ]]; then
new_version="$major.$minor.$((patch + 1))"
else
echo "Invalid version part. Please use M, m, or p"
return 1
fi
# Update package.json with new version
sed -i "s/\"version\": \"$current_version\"/\"version\": \"$new_version\"/" package.json
echo "Version bumped from $current_version to $new_version"
}
commit() {
# First check if versions match between package.json and CHANGELOG.md
if ! check_versions; then
_warn "Version mismatch between package.json and CHANGELOG.md" "Please update the changelog or package.json before committing"
return 1
fi
# Check if package.json version has changed in staged changes
if git diff --cached package.json | grep -q '"version":'; then
# Get the new version from package.json
new_version=$(node -p "require('./package.json').version")
echo "Version has been changed. Would you like to tag this release as v$new_version? (y/n)"
read -r response
if [[ "$response" =~ ^[Yy]$ ]]; then
git commit
git tag -a "v$new_version" -m "Release version $new_version"
echo "Created tag v$new_version"
elif [[ "$response" =~ ^[Nn]$ ]]; then
git commit
else
_warn "Unrecognized reply: $response"
return 1
fi
else
git commit
fi
}
check_versions() {
# Get version from package.json
pkg_version=$(node -p "require('./package.json').version")
# Get latest version from CHANGELOG.md (first version entry)
changelog_version=$(grep -m 1 "^## Version" CHANGELOG.md | sed 's/^## Version //')
# Compare versions
if [ "$pkg_version" = "$changelog_version" ]; then
return 0
else
return 1
fi
}
docker_push() {
local version=$(node -p "require('./package.json').version")
# check if version already exist on dockerhub, if so, don't tag and push versioned image
if docker pull "dohsimpson/habittrove:v$version" &>/dev/null; then
echo "Docker image with tag v$version already exists on DockerHub. Skipping versioned image push"
docker tag habittrove dohsimpson/habittrove:latest
docker push dohsimpson/habittrove:latest
echo "Pushed Docker images with tags: latest"
else
docker tag habittrove "dohsimpson/habittrove:v$version"
docker push "dohsimpson/habittrove:v$version"
echo "Pushed Docker images with tags: v$version"
fi
}
run() {
npm run dev
}
build() {
npm run build
}