singularity-forge/bin/sf-from-source
2026-04-30 12:19:52 +02:00

53 lines
2 KiB
Bash
Executable file

#!/usr/bin/env bash
#
# sf-from-source — run SF directly from this source checkout via node.
#
# Purpose: every local commit in this repo is live immediately without
# rebuilding dist/. Subagents can spawn sf by pointing SF_BIN_PATH at
# this script instead of dist/loader.js.
#
# Why node, not bun:
# - bun doesn't ship node:sqlite (sf-db.ts falls back to filesystem-
# derivation degraded mode under bun).
# - bun's native-addon loader doesn't inherit the system library
# search path under Nix (libz.so.1 not found for forge_engine.node).
# - node 22.5+ has node:sqlite built-in; node 24 supports
# --experimental-strip-types so .ts runs directly.
# - The src/resources/extensions/sf/tests/resolve-ts.mjs loader hook
# already handles .js → .ts import-specifier remapping for runtime
# resolution.
#
# Contract:
# - Executable shim; spawn() / exec() can launch directly.
# - Exports SF_BIN_PATH before handing off to loader.ts so loader.ts's
# `SF_BIN_PATH ||= process.argv[1]` branch preserves the shim path
# instead of clobbering it with the .ts loader path (which is not
# directly executable by child_process.spawn).
#
# Requirements: node >= 22.5 on PATH (24+ recommended for strip-types),
# node_modules populated.
set -euo pipefail
SCRIPT_DIR=$(cd -- "$(dirname -- "$(readlink -f "${BASH_SOURCE[0]}")")" &>/dev/null && pwd)
SF_SOURCE_ROOT=$(cd -- "$SCRIPT_DIR/.." &>/dev/null && pwd)
NODE_BIN=${SF_NODE_BIN:-node}
IS_HEADLESS=0
if [[ "${1:-}" == "headless" ]]; then
IS_HEADLESS=1
echo "[forge] Preparing source runtime for headless command..."
fi
export SF_BIN_PATH="$SCRIPT_DIR/sf-from-source"
export SF_CLI_PATH="${SF_CLI_PATH:-$SCRIPT_DIR/sf-from-source}"
"$NODE_BIN" "$SF_SOURCE_ROOT/scripts/ensure-source-resources.cjs"
if [[ "$IS_HEADLESS" == "1" ]]; then
echo "[forge] Launching source CLI..."
fi
exec "$NODE_BIN" \
--import "$SF_SOURCE_ROOT/src/resources/extensions/sf/tests/resolve-ts.mjs" \
--experimental-strip-types \
--no-warnings \
"$SF_SOURCE_ROOT/src/loader.ts" "$@"