Files
codium_nostr_extension/install_upgrade.sh
T
2026-07-18 17:49:50 -04:00

112 lines
3.4 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# install_upgrade.sh — build and install/upgrade the nostr-publish extension
# into Codium (or VSCode).
#
# Usage:
# ./install_upgrade.sh # build + install (auto-detect codium/code)
# ./install_upgrade.sh codium # force codium
# ./install_upgrade.sh code # force code (VSCode)
#
# What it does:
# 1. Ensures ../nostr-tools is built (lib/cjs exists).
# 2. npm install (if node_modules is missing).
# 3. npm run compile (esbuild -> dist/extension.js).
# 4. npx vsce package (produces nostr-publish-<version>.vsix).
# 5. Installs the VSIX with --force (upgrades in place).
#
# After running, restart Codium/VSCode to load the new version.
#
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
# --- Detect the editor binary ------------------------------------------------
EDITOR_BIN="${1:-}"
if [[ -z "$EDITOR_BIN" ]]; then
if command -v codium &>/dev/null; then
EDITOR_BIN="codium"
elif command -v code &>/dev/null; then
EDITOR_BIN="code"
else
echo "ERROR: neither 'codium' nor 'code' found in PATH." >&2
echo "Pass the binary name explicitly: ./install_upgrade.sh codium" >&2
exit 1
fi
fi
if ! command -v "$EDITOR_BIN" &>/dev/null; then
echo "ERROR: '$EDITOR_BIN' not found in PATH." >&2
exit 1
fi
echo "=== nostr-publish install/upgrade ==="
echo "Editor: $EDITOR_BIN"
echo "Source: $SCRIPT_DIR"
echo ""
# --- Ensure nostr-tools is built ---------------------------------------------
NOSTR_TOOLS_DIR="$SCRIPT_DIR/../nostr-tools"
if [[ ! -d "$NOSTR_TOOLS_DIR" ]]; then
echo "ERROR: $NOSTR_TOOLS_DIR not found." >&2
echo "Clone the laantungir nostr-tools fork as a sibling directory:" >&2
echo " git clone git@laantungir.net:laantungir/nostr-tools.git ../nostr-tools" >&2
exit 1
fi
if [[ ! -d "$NOSTR_TOOLS_DIR/lib/cjs" ]]; then
echo "Building nostr-tools (lib/cjs missing)..."
pushd "$NOSTR_TOOLS_DIR" >/dev/null
if [[ ! -d node_modules ]]; then
npm install --ignore-scripts
fi
rm -rf lib
if command -v bun &>/dev/null; then
bun run build.js
else
node build.js
fi
npx tsc
popd >/dev/null
echo "nostr-tools built."
echo ""
fi
# --- npm install if needed ---------------------------------------------------
if [[ ! -d node_modules ]]; then
echo "Running npm install..."
npm install
echo ""
fi
# --- Compile -----------------------------------------------------------------
echo "Compiling (esbuild)..."
npm run compile
echo ""
# --- Package -----------------------------------------------------------------
echo "Packaging VSIX..."
VSIX=$(npx vsce package --no-dependencies --baseContentUrl https://laantungir.net/git/laantungir/nostr-publish.git 2>&1 | grep -oP 'Packaged: \K[^ ]+')
if [[ -z "$VSIX" ]]; then
# Fallback: find the vsix in the current directory
VSIX=$(ls -1 nostr-publish-*.vsix 2>/dev/null | head -1)
fi
if [[ -z "$VSIX" || ! -f "$VSIX" ]]; then
echo "ERROR: VSIX packaging failed." >&2
exit 1
fi
echo "VSIX: $VSIX"
echo ""
# --- Install / Upgrade -------------------------------------------------------
echo "Installing into $EDITOR_BIN (--force)..."
"$EDITOR_BIN" --install-extension "$VSIX" --force
echo ""
# --- Done --------------------------------------------------------------------
VERSION=$(node -p "require('./package.json').version")
echo "=== Done ==="
echo "Installed nostr-publish v$VERSION."
echo "Restart $EDITOR_BIN to load the new version."