scripts: link-all.sh support register/consume/unlink modes

This commit is contained in:
2026-05-12 10:59:12 +08:00
parent 5f2458238f
commit 2af39463de
+33 -20
View File
@@ -1,35 +1,48 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Link all @uncaged/* packages from the workflow monorepo for local development. # Link / unlink all @uncaged/* packages from the workflow monorepo.
# #
# Usage: # Usage:
# ./scripts/link-all.sh # Register all packages (run from monorepo root) # ./scripts/link-all.sh # Register all packages (run from monorepo root)
# ./scripts/link-all.sh --consume # Link all packages into CWD's project # ./scripts/link-all.sh --consume # Link all packages into CWD's project
# ./scripts/link-all.sh --unlink # Unregister all packages and restore CWD's deps
set -euo pipefail set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
MONOREPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" MONOREPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
if [[ "${1:-}" == "--consume" ]]; then # Iterate package dirs, calling callback(dir, name) for each
# Consumer mode: bun link each @uncaged/* package into the current project each_pkg() {
local cb="$1"
for dir in "$MONOREPO_ROOT"/packages/*/; do for dir in "$MONOREPO_ROOT"/packages/*/; do
[[ -f "$dir/package.json" ]] || continue [[ -f "$dir/package.json" ]] || continue
local name
name=$(grep -m1 '"name"' "$dir/package.json" | sed 's/.*: *"\(.*\)".*/\1/') name=$(grep -m1 '"name"' "$dir/package.json" | sed 's/.*: *"\(.*\)".*/\1/')
echo "linking $name" "$cb" "$dir" "$name"
bun link "$name" 2>&1 | grep -v "^$"
done done
echo "✅ All @uncaged/* packages linked into $(pwd)" }
else
# Register mode: register all packages from monorepo do_register() { printf " register %s\n" "$2"; (cd "$1" && bun link 2>&1) > /dev/null; }
for dir in "$MONOREPO_ROOT"/packages/*/; do do_consume() { printf " link %s\n" "$2"; (bun link "$2" 2>&1) > /dev/null; }
[[ -f "$dir/package.json" ]] || continue do_unlink() { printf " unlink %s\n" "$2"; (cd "$1" && bun unlink 2>&1) > /dev/null || true; }
name=$(grep -m1 '"name"' "$dir/package.json" | sed 's/.*: *"\(.*\)".*/\1/')
echo "registering $name" case "${1:-}" in
(cd "$dir" && bun link 2>&1 | grep -v "^$") --consume)
done each_pkg do_consume
echo "✅ All @uncaged/* packages registered" echo "✅ All @uncaged/* packages linked into $(pwd)"
echo "" echo " To restore: $0 --unlink"
echo "To consume in another project, run:" ;;
echo " $0 --consume" --unlink)
echo " (from the target project directory)" each_pkg do_unlink
fi if [[ -f "package.json" ]]; then
echo " reinstalling deps..."
bun install 2>&1 > /dev/null || true
fi
echo "✅ All @uncaged/* packages unlinked, deps restored"
;;
*)
each_pkg do_register
echo "✅ All @uncaged/* packages registered"
echo " cd <project> && $0 --consume"
;;
esac