#!/bin/bash # Version synchronization script for ailog # Syncs version from Cargo.toml to all package.json files set -e # Get the directory of this script SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" # Function to extract version from Cargo.toml get_cargo_version() { grep '^version = ' "$PROJECT_ROOT/Cargo.toml" | cut -d'"' -f2 } # Fallback function for when jq is not available update_package_version_fallback() { local package_file="$1" local version="$2" # Get current version using grep/sed local old_version=$(grep '"version"' "$package_file" | sed 's/.*"version":\s*"\([^"]*\)".*/\1/') # Skip update if version is already correct if [[ "$old_version" == "$version" ]]; then echo "✅ $(basename "$package_file"): Already up to date ($version)" return 0 fi # Update version using sed if sed -i.bak "s/\"version\":\s*\"[^\"]*\"/\"version\": \"$version\"/" "$package_file"; then rm -f "$package_file.bak" echo "✅ Updated $(basename "$package_file"): $old_version → $version (sed)" return 0 else rm -f "$package_file.bak" echo "❌ Failed to update $package_file with sed" return 1 fi } # Function to update package.json version using jq update_package_version() { local package_file="$1" local version="$2" if [[ ! -f "$package_file" ]]; then echo "⚠️ Package.json not found: $package_file" return 1 fi # Check if jq is available, fallback to sed if not if ! command -v jq &> /dev/null; then echo "⚠️ jq not found, using sed fallback for $package_file" return update_package_version_fallback "$package_file" "$version" fi # Get current version using jq local old_version=$(jq -r '.version' "$package_file") # Skip update if version is already correct if [[ "$old_version" == "$version" ]]; then echo "✅ $(basename "$package_file"): Already up to date ($version)" return 0 fi # Update version using jq (with proper formatting) local temp_file=$(mktemp) if jq --arg version "$version" '.version = $version' "$package_file" > "$temp_file" 2>/dev/null; then mv "$temp_file" "$package_file" echo "✅ Updated $(basename "$package_file"): $old_version → $version" return 0 else rm -f "$temp_file" echo "❌ Failed to update $package_file with jq" return 1 fi } # Main execution main() { local version version=$(get_cargo_version) if [[ -z "$version" ]]; then echo "❌ Could not find version in Cargo.toml" exit 1 fi echo "📦 Cargo.toml version: $version" echo "🔍 Project root: $PROJECT_ROOT" echo "🔄 Syncing versions..." # List of package.json files to update local packages=( "$PROJECT_ROOT/oauth/package.json" "$PROJECT_ROOT/pds/package.json" ) local updated_count=0 local failed_count=0 for package in "${packages[@]}"; do if update_package_version "$package" "$version"; then ((updated_count++)) else ((failed_count++)) echo "⚠️ Failed to update: $package" fi done echo "📊 Summary: $updated_count updated, $failed_count failed" if [[ $failed_count -gt 0 ]]; then echo "❌ Some files failed to update" exit 1 else echo "✅ Successfully synced all package.json files" fi } # Run main function main "$@"