#!/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 } # 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 if ! command -v jq &> /dev/null; then echo "❌ jq is required but not installed. Please install jq first." return 1 fi # Get current version using jq local old_version=$(jq -r '.version' "$package_file") # Update version using jq (with proper formatting) local temp_file=$(mktemp) jq --arg version "$version" '.version = $version' "$package_file" > "$temp_file" # Replace original file if jq succeeded if [[ $? -eq 0 ]]; 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" 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 "🔄 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 for package in "${packages[@]}"; do if update_package_version "$package" "$version"; then ((updated_count++)) fi done echo "✅ Successfully synced $updated_count package.json files" } # Run main function main "$@"