#!/bin/bash # # Common Initialization for All Scripts # Sets up error handling, paths, and loads core libraries # # Usage: source scripts/lib/core/init.sh # # This script: # - Enables strict error handling (set -euo pipefail) # - Determines SCRIPTS_ROOT (the scripts/ directory) # - Loads colors.sh and logger.sh # - Exports paths for use by other scripts # set -euo pipefail # Determine scripts root directory # Works whether sourced from scripts/lib/core/ or from anywhere else if [[ -n "${SCRIPTS_ROOT:-}" ]]; then # Already set, use it : elif [[ -f "${BASH_SOURCE[0]}" ]]; then # Determine from this file's location (scripts/lib/core/init.sh) SCRIPTS_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" else # Fallback: assume we're in the project root SCRIPTS_ROOT="$(pwd)/scripts" fi export SCRIPTS_ROOT export SCRIPTS_LIB_DIR="${SCRIPTS_ROOT}/lib" export SCRIPTS_COMMANDS_DIR="${SCRIPTS_ROOT}/commands" export SCRIPTS_INTERNAL_DIR="${SCRIPTS_ROOT}/internal" # Determine project root (one level up from scripts/) export PROJECT_ROOT="${SCRIPTS_ROOT%/scripts}" # Load core libraries source "${SCRIPTS_LIB_DIR}/core/colors.sh" source "${SCRIPTS_LIB_DIR}/core/logger.sh" # Helper: resolve path relative to project root resolve_project_path() { local path="$1" if [[ "$path" = /* ]]; then echo "$path" else echo "${PROJECT_ROOT}/${path}" fi } # Helper: check if a command exists command_exists() { command -v "$1" &>/dev/null } # Helper: require a command or exit with error require_command() { local cmd="$1" local install_hint="${2:-}" if ! command_exists "$cmd"; then log_error "Required command not found: $cmd" if [[ -n "$install_hint" ]]; then log_info "Install with: $install_hint" fi exit 1 fi }