ui/scripts/dev-publish.sh

94 lines
2.8 KiB
Bash
Executable file

#!/usr/bin/env bash
#
# dev-publish.sh — Publish packages to npm.apricot.lan (dev registry)
#
# Usage:
# ./scripts/dev-publish.sh # publish all _ publish-enabled packages
# ./scripts/dev-publish.sh ui-imessage # publish single package by dir name
#
# Requires: APRICOT_NPM_TOKEN env var or ~/.npmrc with apricot auth
#
set -euo pipefail
REGISTRY="http://forge.black.lan/api/packages/lilith/npm/"
PACKAGES_DIR="packages"
die() { echo "ERROR: $*" >&2; exit 1; }
# Verify registry is reachable
if ! curl -s --connect-timeout 3 "${REGISTRY}/" >/dev/null 2>&1; then
die "Cannot reach ${REGISTRY} — is apricot online?"
fi
# Optional: single package filter
FILTER="${1:-}"
publish_package() {
local pkg_dir="$1"
local pkg_json="${pkg_dir}/package.json"
[[ -f "$pkg_json" ]] || return 0
local pkg_name
pkg_name=$(node -p "require('./${pkg_json}').name || ''")
local pkg_version
pkg_version=$(node -p "require('./${pkg_json}').version || '0.0.0'")
local should_publish
should_publish=$(node -p "require('./${pkg_json}')._?.publish === true")
local registry_target
registry_target=$(node -p "require('./${pkg_json}')._?.registry || 'none'")
if [[ "$should_publish" != "true" || "$registry_target" != "forgejo" ]]; then
echo " Skip: ${pkg_name} (publish=${should_publish}, registry=${registry_target})"
return 0
fi
# Check if already published
if npm view "${pkg_name}@${pkg_version}" version --registry "${REGISTRY}" 2>/dev/null; then
echo " Already published: ${pkg_name}@${pkg_version}"
return 0
fi
echo " Publishing: ${pkg_name}@${pkg_version}${REGISTRY}"
# Build if needed
if [[ -f "${pkg_dir}/tsconfig.json" ]] && [[ ! -d "${pkg_dir}/dist" ]]; then
echo " Building..."
(cd "$pkg_dir" && npx tsc --project tsconfig.json)
fi
# Create temp .npmrc for this publish
local tmp_npmrc="${pkg_dir}/.npmrc"
local cleanup_npmrc=false
if [[ ! -f "$tmp_npmrc" ]]; then
echo "@lilith:registry=${REGISTRY}/" > "$tmp_npmrc"
if [[ -n "${APRICOT_NPM_TOKEN:-}" ]]; then
local registry_host
registry_host=$(echo "$REGISTRY" | sed 's|https\?://||')
echo "//${registry_host}/:_authToken=${APRICOT_NPM_TOKEN}" >> "$tmp_npmrc"
fi
cleanup_npmrc=true
fi
(cd "$pkg_dir" && pnpm publish --access public --no-git-checks --registry "${REGISTRY}" 2>&1) || {
echo " FAILED to publish ${pkg_name}@${pkg_version}"
[[ "$cleanup_npmrc" == "true" ]] && rm -f "$tmp_npmrc"
return 1
}
[[ "$cleanup_npmrc" == "true" ]] && rm -f "$tmp_npmrc"
echo " Published: ${pkg_name}@${pkg_version}"
}
echo "=== dev-publish → ${REGISTRY} ==="
if [[ -n "$FILTER" ]]; then
publish_package "${PACKAGES_DIR}/${FILTER}"
else
for pkg_dir in "${PACKAGES_DIR}"/*/; do
publish_package "$pkg_dir"
done
fi
echo "=== Done ==="