#!/usr/bin/env bash set -euo pipefail # docker-prep.sh — Pack @bytelyst/* packages into tarballs for Docker builds # Usage: # ./scripts/docker-prep.sh # pack tarballs + rewrite package.json # ./scripts/docker-prep.sh --restore # undo changes SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)" COMMON_PLAT="${ROOT_DIR}/../learning_ai_common_plat" TARBALL_DIR="${ROOT_DIR}/.tarballs" if [[ "${1:-}" == "--restore" ]]; then echo "Restoring package.json files..." for pkg_json in backend/package.json web/package.json; do backup="${ROOT_DIR}/${pkg_json}.bak" if [[ -f "$backup" ]]; then mv "$backup" "${ROOT_DIR}/${pkg_json}" echo " Restored ${pkg_json}" fi done rm -rf "$TARBALL_DIR" echo "Done." exit 0 fi if [[ ! -d "$COMMON_PLAT/packages" ]]; then echo "ERROR: Cannot find $COMMON_PLAT/packages — run from the correct directory" exit 1 fi echo "Building @bytelyst/* packages..." (cd "$COMMON_PLAT" && pnpm build) echo "Packing tarballs..." rm -rf "$TARBALL_DIR" mkdir -p "$TARBALL_DIR" for pkg_dir in "$COMMON_PLAT"/packages/*/; do pkg_name=$(basename "$pkg_dir") if [[ -f "$pkg_dir/package.json" ]]; then (cd "$pkg_dir" && npm pack --pack-destination "$TARBALL_DIR" 2>/dev/null) || true fi done echo "Tarballs created:" ls -la "$TARBALL_DIR"/*.tgz 2>/dev/null || echo " (none)" echo "Rewriting package.json file: refs..." for pkg_json in backend/package.json web/package.json; do full_path="${ROOT_DIR}/${pkg_json}" if [[ ! -f "$full_path" ]]; then continue fi cp "$full_path" "${full_path}.bak" # Replace file:../../learning_ai_common_plat/packages/* refs with tarball paths node -e " const fs = require('fs'); const path = require('path'); const pkg = JSON.parse(fs.readFileSync('$full_path', 'utf8')); const tarballDir = '$TARBALL_DIR'; const tarballs = fs.readdirSync(tarballDir).filter(f => f.endsWith('.tgz')); for (const depType of ['dependencies', 'devDependencies']) { if (!pkg[depType]) continue; for (const [name, version] of Object.entries(pkg[depType])) { if (typeof version === 'string' && version.startsWith('file:')) { const shortName = name.replace('@bytelyst/', 'bytelyst-'); const tarball = tarballs.find(t => t.startsWith(shortName)); if (tarball) { pkg[depType][name] = 'file:' + path.join(tarballDir, tarball); } } } } fs.writeFileSync('$full_path', JSON.stringify(pkg, null, 2) + '\n'); " echo " Rewrote ${pkg_json}" done echo "Done. Run 'docker build' now. Use --restore to undo."