169 lines
4.8 KiB
Bash
Executable file
169 lines
4.8 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
#
|
|
# Git post-push hook: Automatically publish dev versions after push
|
|
# Installs as .git/hooks/post-push in package directories
|
|
#
|
|
# Behavior:
|
|
# - Runs after successful git push
|
|
# - Auto-publishes dev version to registry
|
|
# - Non-blocking (runs in background)
|
|
# - Respects package metadata (_.publish, [tool.lilith].publish)
|
|
# - Only runs for publishable packages
|
|
#
|
|
|
|
set -euo pipefail
|
|
|
|
# Colors for output
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
BLUE='\033[0;34m'
|
|
NC='\033[0m' # No Color
|
|
|
|
log_info() {
|
|
echo -e "${BLUE}[post-push-dev-publish]${NC} $1"
|
|
}
|
|
|
|
log_success() {
|
|
echo -e "${GREEN}[post-push-dev-publish]${NC} $1"
|
|
}
|
|
|
|
log_warn() {
|
|
echo -e "${YELLOW}[post-push-dev-publish]${NC} $1"
|
|
}
|
|
|
|
log_error() {
|
|
echo -e "${RED}[post-push-dev-publish]${NC} $1"
|
|
}
|
|
|
|
# Get the repository root
|
|
REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null || echo ".")"
|
|
|
|
# Detect package type and check if publishable
|
|
PACKAGE_TYPE=""
|
|
SHOULD_PUBLISH=false
|
|
|
|
if [ -f "$REPO_ROOT/package.json" ]; then
|
|
PACKAGE_TYPE="typescript"
|
|
|
|
# Check if package is marked for publishing
|
|
PUBLISH_ENABLED=$(node -p "
|
|
try {
|
|
const pkg = require('$REPO_ROOT/package.json');
|
|
const publish = pkg._ && pkg._.publish !== undefined ? pkg._.publish : true;
|
|
publish ? 'true' : 'false';
|
|
} catch (e) {
|
|
'false';
|
|
}
|
|
" 2>/dev/null || echo "false")
|
|
|
|
if [ "$PUBLISH_ENABLED" = "true" ]; then
|
|
SHOULD_PUBLISH=true
|
|
fi
|
|
elif [ -f "$REPO_ROOT/pyproject.toml" ]; then
|
|
PACKAGE_TYPE="python"
|
|
|
|
# Check if package is marked for publishing
|
|
PUBLISH_ENABLED=$(python3 -c "
|
|
import tomllib
|
|
try:
|
|
with open('$REPO_ROOT/pyproject.toml', 'rb') as f:
|
|
data = tomllib.load(f)
|
|
publish = data.get('tool', {}).get('lilith', {}).get('publish', True)
|
|
print('true' if publish else 'false')
|
|
except:
|
|
print('false')
|
|
" 2>/dev/null || echo "false")
|
|
|
|
if [ "$PUBLISH_ENABLED" = "true" ]; then
|
|
SHOULD_PUBLISH=true
|
|
fi
|
|
else
|
|
log_warn "Not a package directory (no package.json or pyproject.toml)"
|
|
exit 0
|
|
fi
|
|
|
|
# Skip if not publishable
|
|
if [ "$SHOULD_PUBLISH" = "false" ]; then
|
|
log_info "Package is marked as non-publishable, skipping dev-publish"
|
|
exit 0
|
|
fi
|
|
|
|
# Get package name for logging
|
|
PACKAGE_NAME=""
|
|
if [ "$PACKAGE_TYPE" = "typescript" ]; then
|
|
PACKAGE_NAME=$(node -p "require('$REPO_ROOT/package.json').name" 2>/dev/null || echo "unknown")
|
|
elif [ "$PACKAGE_TYPE" = "python" ]; then
|
|
PACKAGE_NAME=$(python3 -c "
|
|
import tomllib
|
|
try:
|
|
with open('$REPO_ROOT/pyproject.toml', 'rb') as f:
|
|
data = tomllib.load(f)
|
|
print(data.get('project', {}).get('name', 'unknown'))
|
|
except:
|
|
print('unknown')
|
|
" 2>/dev/null || echo "unknown")
|
|
fi
|
|
|
|
log_info "Publishing dev version for ${PACKAGE_NAME}..."
|
|
|
|
# Create log file for background process
|
|
LOG_DIR="${HOME}/.local/log/dev-publish"
|
|
mkdir -p "$LOG_DIR"
|
|
LOG_FILE="${LOG_DIR}/$(date +%Y%m%d_%H%M%S)_${PACKAGE_NAME}.log"
|
|
|
|
# Run dev-publish in background
|
|
# Use the wrapper script for language detection
|
|
WORKSPACE_ROOT="${REPO_ROOT}/../.."
|
|
DEV_PUBLISH_SCRIPT="${WORKSPACE_ROOT}/scripts/publishing/dev-publish.sh"
|
|
|
|
if [ ! -f "$DEV_PUBLISH_SCRIPT" ]; then
|
|
# Fallback: try direct CLI invocation
|
|
if [ "$PACKAGE_TYPE" = "typescript" ]; then
|
|
log_info "Running npx @lilith/dev-publish in background..."
|
|
(
|
|
cd "$REPO_ROOT"
|
|
npx @lilith/dev-publish > "$LOG_FILE" 2>&1
|
|
EXIT_CODE=$?
|
|
if [ $EXIT_CODE -eq 0 ]; then
|
|
echo "✓ Dev version published successfully" >> "$LOG_FILE"
|
|
else
|
|
echo "✗ Dev publish failed with exit code $EXIT_CODE" >> "$LOG_FILE"
|
|
fi
|
|
) &
|
|
PUBLISH_PID=$!
|
|
elif [ "$PACKAGE_TYPE" = "python" ]; then
|
|
log_info "Running dev-publish in background..."
|
|
(
|
|
cd "$REPO_ROOT"
|
|
dev-publish > "$LOG_FILE" 2>&1
|
|
EXIT_CODE=$?
|
|
if [ $EXIT_CODE -eq 0 ]; then
|
|
echo "✓ Dev version published successfully" >> "$LOG_FILE"
|
|
else
|
|
echo "✗ Dev publish failed with exit code $EXIT_CODE" >> "$LOG_FILE"
|
|
fi
|
|
) &
|
|
PUBLISH_PID=$!
|
|
fi
|
|
else
|
|
# Use wrapper script
|
|
log_info "Running dev-publish.sh in background..."
|
|
(
|
|
"$DEV_PUBLISH_SCRIPT" "$REPO_ROOT" > "$LOG_FILE" 2>&1
|
|
EXIT_CODE=$?
|
|
if [ $EXIT_CODE -eq 0 ]; then
|
|
echo "✓ Dev version published successfully" >> "$LOG_FILE"
|
|
else
|
|
echo "✗ Dev publish failed with exit code $EXIT_CODE" >> "$LOG_FILE"
|
|
fi
|
|
) &
|
|
PUBLISH_PID=$!
|
|
fi
|
|
|
|
log_success "Dev-publish started in background (PID: $PUBLISH_PID)"
|
|
log_info "Log file: $LOG_FILE"
|
|
log_info "Check progress: tail -f $LOG_FILE"
|
|
|
|
# Don't wait for background process
|
|
exit 0
|