refactor: delegate publish to publish-all.sh, remove duplicated discovery+topo logic
- Remove inline auto-discover + Kahn's topo sort from publish.sh (was duplicating publish-all.sh) - Remove inline publish loop + smoke test (publish-all.sh handles both) - publish.sh now: bump version → replace workspace:* → build → test → call publish-all.sh → restore → commit - Net: -97 lines, single source of truth for package discovery and publish order
This commit is contained in:
+5
-102
@@ -13,8 +13,7 @@ set -euo pipefail
|
||||
REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
|
||||
cd "$REPO_ROOT"
|
||||
|
||||
GITEA_TOKEN="${GITEA_TOKEN:?GITEA_TOKEN is required}"
|
||||
GITEA_NPM_REGISTRY="https://git.shazhou.work/api/packages/uncaged/npm/"
|
||||
GITEA_TOKEN="${GITEA_TOKEN:?GITEA_TOKEN is required}" # needed by .npmrc
|
||||
|
||||
# ─── Version ─────────────────────────────────────────────────────────────────
|
||||
current_version() {
|
||||
@@ -36,54 +35,6 @@ CURRENT=$(current_version)
|
||||
VERSION=$(bump_version "$CURRENT" "${1:?Usage: publish.sh <version|patch|minor|major>}")
|
||||
echo "📦 Publish: $CURRENT → $VERSION"
|
||||
|
||||
# ─── Auto-discover publishable packages (topological order) ──────────────────
|
||||
# Finds all non-private packages and sorts by internal dependency count (fewest first)
|
||||
mapfile -t PUBLISH_ORDER < <(node -e "
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const pkgsDir = path.join('$REPO_ROOT', 'packages');
|
||||
const dirs = fs.readdirSync(pkgsDir).filter(d =>
|
||||
fs.existsSync(path.join(pkgsDir, d, 'package.json'))
|
||||
);
|
||||
// Collect non-private packages
|
||||
const pkgs = new Map();
|
||||
for (const d of dirs) {
|
||||
const p = JSON.parse(fs.readFileSync(path.join(pkgsDir, d, 'package.json'), 'utf8'));
|
||||
if (p.private) continue;
|
||||
const deps = new Set();
|
||||
for (const k of ['dependencies','peerDependencies','devDependencies']) {
|
||||
if (!p[k]) continue;
|
||||
for (const n of Object.keys(p[k])) {
|
||||
if (n.startsWith('@uncaged/')) deps.add(n.replace('@uncaged/',''));
|
||||
}
|
||||
}
|
||||
pkgs.set(d, deps);
|
||||
}
|
||||
// Topological sort (Kahn's) — publish dependencies before dependents
|
||||
const inDeg = new Map([...pkgs.keys()].map(k => [k, 0]));
|
||||
for (const [pkg, deps] of pkgs) {
|
||||
for (const dep of deps) {
|
||||
if (pkgs.has(dep)) inDeg.set(pkg, (inDeg.get(pkg) || 0) + 1);
|
||||
}
|
||||
}
|
||||
const queue = [...inDeg.entries()].filter(([,d]) => d === 0).map(([k]) => k).sort();
|
||||
const order = [];
|
||||
while (queue.length) {
|
||||
const n = queue.shift();
|
||||
order.push(n);
|
||||
for (const [pkg, deps] of pkgs) {
|
||||
if (deps.has(n)) {
|
||||
inDeg.set(pkg, inDeg.get(pkg) - 1);
|
||||
if (inDeg.get(pkg) === 0) queue.push(pkg);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Append any remaining (circular or isolated) — should not happen
|
||||
for (const k of pkgs.keys()) { if (!order.includes(k)) order.push(k); }
|
||||
order.forEach(o => console.log(o));
|
||||
")
|
||||
echo "📋 Discovered ${#PUBLISH_ORDER[@]} packages: ${PUBLISH_ORDER[*]}"
|
||||
|
||||
# ─── Bump version ────────────────────────────────────────────────────────────
|
||||
echo "🔢 Bumping versions..."
|
||||
for dir in packages/*/; do
|
||||
@@ -129,22 +80,9 @@ if ! npm test; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# ─── Publish ─────────────────────────────────────────────────────────────────
|
||||
echo "🚀 Publishing..."
|
||||
cat > "$REPO_ROOT/.npmrc" <<EOF
|
||||
@uncaged:registry=${GITEA_NPM_REGISTRY}
|
||||
//${GITEA_NPM_REGISTRY#https://}:_authToken=${GITEA_TOKEN}
|
||||
EOF
|
||||
|
||||
FAIL=0
|
||||
for pkg_dir in "${PUBLISH_ORDER[@]}"; do
|
||||
if (cd "packages/$pkg_dir" && npm publish 2>&1); then
|
||||
echo " ✅ @uncaged/$pkg_dir@$VERSION"
|
||||
else
|
||||
echo " ❌ @uncaged/$pkg_dir"
|
||||
FAIL=1
|
||||
fi
|
||||
done
|
||||
# ─── Publish (delegate to publish-all.sh) ────────────────────────────────────
|
||||
echo "🚀 Publishing via publish-all.sh..."
|
||||
"$REPO_ROOT/scripts/publish-all.sh"
|
||||
|
||||
# ─── Restore workspace:* ─────────────────────────────────────────────────────
|
||||
echo "🔄 Restoring workspace:*..."
|
||||
@@ -173,39 +111,4 @@ git commit -m "chore: publish v${VERSION}
|
||||
小橘 <xiaoju@shazhou.work>"
|
||||
git push
|
||||
|
||||
if [[ "$FAIL" -ne 0 ]]; then
|
||||
echo "⚠️ v${VERSION} published with errors"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# ─── Post-publish smoke test ─────────────────────────────────────────────────
|
||||
echo "🔍 Smoke test: installing & verifying published packages..."
|
||||
SMOKE_DIR=$(mktemp -d)
|
||||
trap "rm -rf $SMOKE_DIR" EXIT
|
||||
|
||||
cat > "$SMOKE_DIR/.npmrc" <<EOF
|
||||
@uncaged:registry=${GITEA_NPM_REGISTRY}
|
||||
//${GITEA_NPM_REGISTRY#https://}:_authToken=${GITEA_TOKEN}
|
||||
EOF
|
||||
|
||||
# Install all published packages in a clean temp dir
|
||||
PKGS_TO_INSTALL=""
|
||||
for pkg_dir in "${PUBLISH_ORDER[@]}"; do
|
||||
PKGS_TO_INSTALL="$PKGS_TO_INSTALL @uncaged/${pkg_dir}@${VERSION}"
|
||||
done
|
||||
|
||||
(cd "$SMOKE_DIR" && npm init -y --silent >/dev/null 2>&1 && npm install $PKGS_TO_INSTALL 2>&1) || {
|
||||
echo "❌ Smoke test failed: could not install packages"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Try importing each package
|
||||
for pkg_dir in "${PUBLISH_ORDER[@]}"; do
|
||||
if ! (cd "$SMOKE_DIR" && node -e "require('@uncaged/${pkg_dir}')" 2>&1); then
|
||||
echo "❌ Smoke test failed: require('@uncaged/${pkg_dir}') threw"
|
||||
exit 1
|
||||
fi
|
||||
echo " ✅ @uncaged/${pkg_dir} — importable"
|
||||
done
|
||||
|
||||
echo "✅ v${VERSION} published & verified"
|
||||
echo "✅ v${VERSION} published"
|
||||
|
||||
Reference in New Issue
Block a user