← Files Flows by plugin.spaceARCHIVED FILE
scripts/run.sh
6.79 KB · Nov 30, 2026 · 00:17 UTC
#!/bin/sh
# Bootstrap without Node, Python, npm, sudo or a global installation.
set -eu
flow_scripts=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd -P)
flow_command=${1:---help}
case "$flow_command" in
--help|-h|help)
printf '%s\n' 'Flows: sh <PLUGIN_ROOT>/scripts/run.sh <command> [arguments]' \
'Commands: mcp, serve, library, validate, test-scenarios, setup-capture, capture, capture-status, attach-screen, runtime' \
'Uses Node.js 22.13+ or downloads a checksum-verified private runtime from nodejs.org.' \
'FLOWS_OFFLINE=1 disables downloads; FLOWS_USE_MANAGED_NODE=1 selects the private runtime.'
exit 0 ;;
mcp|serve|library|validate|test-scenarios|setup-capture|capture|capture-status|attach-screen) flow_entry="$flow_scripts/$flow_command.mjs" ;;
runtime) flow_entry="$flow_scripts/check-runtime.mjs" ;;
*) printf 'Flows: unknown command: %s. Use --help.\n' "$flow_command" >&2; exit 2 ;;
esac
shift
flow_need_npm=0
if [ "$flow_command" = setup-capture ]; then
for flow_arg in "$@"; do [ "$flow_arg" != --install ] || flow_need_npm=1; done
fi
flow_usable() {
if [ "$flow_need_npm" = 1 ]; then
"$1" "$flow_scripts/check-runtime.mjs" --check --npm >/dev/null 2>&1
else
"$1" "$flow_scripts/check-runtime.mjs" --check >/dev/null 2>&1
fi
}
flow_fail() { printf 'Flows: %s\n' "$*" >&2; exit 1; }
flow_node=
if [ -n "${FLOWS_NODE:-}" ]; then
flow_usable "$FLOWS_NODE" || flow_fail 'FLOWS_NODE must point to Node.js 22.13+ (with npm for capture setup). Unset it to use automatic setup.'
flow_node=$FLOWS_NODE
elif [ "${FLOWS_USE_MANAGED_NODE:-0}" != 1 ]; then
flow_candidate=$(command -v node || true)
if [ -n "$flow_candidate" ] && flow_usable "$flow_candidate"; then flow_node=$flow_candidate; fi
fi
if [ -z "$flow_node" ]; then
flow_version=$(cat "$flow_scripts/../runtime/node-version.txt")
case "$flow_version" in ''|*[!0-9.]*) flow_fail 'Invalid bundled Node.js version.' ;; esac
case "$(uname -s)" in
Darwin) flow_os=darwin ;;
Linux)
[ ! -f /etc/alpine-release ] || flow_fail 'Automatic setup requires glibc Linux. On Alpine, provide compatible Node.js 22.13+ using FLOWS_NODE.'
flow_os=linux ;;
*) flow_fail 'Automatic setup supports macOS and glibc Linux. On Windows use scripts/run.ps1, or provide Node.js 22.13+.' ;;
esac
case "$(uname -m)" in
arm64|aarch64) flow_arch=arm64 ;;
x86_64|amd64) flow_arch=x64 ;;
*) flow_fail 'Automatic setup supports x64 and ARM64. Provide Node.js 22.13+ on this architecture.' ;;
esac
flow_dist="node-v$flow_version-$flow_os-$flow_arch"
flow_archive="$flow_dist.tar.gz"
flow_sha=$(awk -v file="$flow_archive" '$2 == file { print $1 }' "$flow_scripts/../runtime/SHASUMS256.txt")
[ "${#flow_sha}" = 64 ] || flow_fail 'Missing bundled checksum for this platform.'
flow_cache=${FLOWS_RUNTIME_DIR:-${XDG_CACHE_HOME:-$HOME/.cache}/flows/node}
case "$flow_cache" in /*) ;; *) flow_fail 'FLOWS_RUNTIME_DIR / XDG_CACHE_HOME must be an absolute path.' ;; esac
flow_target="$flow_cache/$flow_dist"
flow_node="$flow_target/bin/node"
flow_cached() {
[ -f "$flow_target/.flows-runtime-ready" ] &&
"$flow_node" "$flow_scripts/check-runtime.mjs" --check --npm --expected "$flow_version" >/dev/null 2>&1
}
if ! flow_cached; then
[ "${FLOWS_OFFLINE:-0}" != 1 ] || flow_fail 'No compatible Node.js or cached runtime. Connect to the internet and rerun without FLOWS_OFFLINE, or set FLOWS_NODE to Node.js 22.13+.'
command -v tar >/dev/null 2>&1 || flow_fail 'Automatic setup needs tar.'
if command -v sha256sum >/dev/null 2>&1; then flow_hasher=sha256sum
elif command -v shasum >/dev/null 2>&1; then flow_hasher=shasum
else flow_fail 'Automatic setup needs sha256sum or shasum to verify the download.'; fi
if command -v curl >/dev/null 2>&1; then flow_downloader=curl
elif command -v wget >/dev/null 2>&1; then flow_downloader=wget
else flow_fail 'Automatic setup needs curl or wget. Install one or provide Node.js 22.13+.'; fi
umask 077
mkdir -p "$flow_cache"
flow_lock="$flow_cache/.$flow_dist.lock"
flow_stage=
flow_locked=0
flow_cleanup() {
[ -z "$flow_stage" ] || rm -rf -- "$flow_stage"
[ "$flow_locked" = 0 ] || rmdir "$flow_lock"
return 0
}
trap flow_cleanup EXIT
trap 'exit 130' INT
trap 'exit 143' TERM
flow_wait=0
while :; do
if mkdir "$flow_lock" 2>/dev/null; then flow_locked=1; break; fi
if flow_cached; then break; fi
flow_wait=$((flow_wait + 1))
[ "$flow_wait" -le 60 ] || flow_fail "Runtime setup is locked at $flow_lock. Wait for the other setup; if it was interrupted, remove that empty lock directory and retry."
[ "$flow_wait" != 1 ] || printf '%s\n' 'Flows: waiting for another runtime setup...' >&2
sleep 1
done
# A completed concurrent setup can be reused without owning its lock.
if ! flow_cached; then
flow_stage=$(mktemp -d "$flow_cache/.download-XXXXXX")
printf 'Flows: preparing private Node.js %s for %s-%s from nodejs.org...\n' "$flow_version" "$flow_os" "$flow_arch" >&2
flow_url="https://nodejs.org/dist/v$flow_version/$flow_archive"
if [ "$flow_downloader" = curl ]; then
curl --fail --location --silent --show-error --proto '=https' --proto-redir '=https' --tlsv1.2 --connect-timeout 15 --max-time 300 --retry 2 --output "$flow_stage/$flow_archive" "$flow_url" || flow_fail 'Could not download Node.js. Check your connection/proxy and retry; no global installation was changed.'
else
wget --https-only --timeout=30 --tries=2 -q -O "$flow_stage/$flow_archive" "$flow_url" || flow_fail 'Could not download Node.js. Check your connection/proxy and retry; no global installation was changed.'
fi
if [ "$flow_hasher" = sha256sum ]; then flow_actual=$(sha256sum "$flow_stage/$flow_archive")
else flow_actual=$(shasum -a 256 "$flow_stage/$flow_archive"); fi
flow_actual=${flow_actual%% *}
[ "$flow_actual" = "$flow_sha" ] || flow_fail 'Node.js checksum mismatch. Download discarded; retry or update the Flows package.'
tar -xzf "$flow_stage/$flow_archive" -C "$flow_stage"
"$flow_stage/$flow_dist/bin/node" "$flow_scripts/check-runtime.mjs" --check --npm --expected "$flow_version" || flow_fail 'Downloaded Node.js cannot run on this OS. Provide compatible Node.js 22.13+ using FLOWS_NODE.'
printf '%s\n' "$flow_sha" > "$flow_stage/$flow_dist/.flows-runtime-ready"
[ ! -L "$flow_target" ] || flow_fail 'Runtime cache target is a symlink. Choose a dedicated FLOWS_RUNTIME_DIR.'
if [ -e "$flow_target" ]; then mv "$flow_target" "$flow_stage/previous-runtime"; fi
mv "$flow_stage/$flow_dist" "$flow_target"
printf 'Flows: runtime ready in %s\n' "$flow_target" >&2
fi
flow_cleanup
trap - EXIT INT TERM
fi
fi
PATH="$(dirname "$flow_node"):$PATH"
export PATH
exec "$flow_node" "$flow_entry" "$@"
SHA-256: f9ffd9c6c2e3d137654d6c05b4a2c22b3dc43743d4dc71e7c514ab114c529930