#!/usr/bin/env bash # Enclawed CrowdCode™ installer for Linux, macOS, WSL, Android (Termux) and iOS/iPadOS (iSH). # # bash scripts/install.sh install (from this checkout, or from npm) # bash scripts/install.sh --from FILE install from an npm pack tarball or a checkout # bash scripts/install.sh uninstall remove CrowdCode™ (your settings and history stay) # bash scripts/install.sh --check say what would happen, change nothing # # Options: --yes (no questions), --no-path (leave the shell's PATH alone), # --no-setup (don't offer `ccode setup`), --force (replace a `ccode` # that isn't CrowdCode™'s). # # What it does, without sudo: # - checks Node.js 22 or newer and npm (and says how to get them); # - installs into ~/.local/share/crowdcode/versions/ (npm, with # install scripts off), then points ~/.local/share/crowdcode/current at it; # - creates the commands `ccode` and `crowdcode` in ~/.local/bin (never # overwriting a command that isn't CrowdCode™'s); # - offers to add ~/.local/bin to PATH in your shell's start file; # - offers to run `ccode setup` (where the models run). set -euo pipefail PREFIX="${CROWDCODE_PREFIX:-$HOME/.local/share/crowdcode}" BIN_DIR="${CROWDCODE_BIN_DIR:-$HOME/.local/bin}" NPM_PACKAGE="enclawed-crowdcode" MARK="# enclawed-crowdcode shim" PATH_MARK="# added by the Enclawed CrowdCode™ installer" ACTION=install FROM="" YES=0 NO_PATH=0 NO_SETUP=0 FORCE=0 CHECK=0 while [ $# -gt 0 ]; do case "$1" in install|uninstall) ACTION=$1 ;; --from) FROM="${2:?--from needs a file or folder}"; shift ;; --yes|-y) YES=1 ;; --no-path) NO_PATH=1 ;; --no-setup) NO_SETUP=1 ;; --force) FORCE=1 ;; --check) CHECK=1 ;; -h|--help) sed -n '2,24p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;; *) echo "install.sh: unknown option $1 (see --help)" >&2; exit 2 ;; esac shift done # ---- looks ---- if [ -t 1 ] && [ -z "${NO_COLOR:-}" ]; then B=$'\e[38;2;74;107;150m' C=$'\e[38;2;143;179;224m' W=$'\e[1m' D=$'\e[2m' G=$'\e[32m' R=$'\e[31m' Z=$'\e[0m' else B="" C="" W="" D="" G="" R="" Z="" fi banner() { cat <${Z}${B} ██ █▀${Z} ${D}every tool call decided by Enclawed${Z} ${B} ▀█▄ ▀█▄▄▄▄▄▄▄▄▄█▀ ▄█▀${Z} ${B} ▀█▄▄▀███████▀▄▄█▀${Z} ${B} ▀▀█▄▄▄▄▄█▀▀${Z} ${B} ▀▀▀${Z} EOF } step() { printf '%s\n' "${C}●${Z} $*"; } ok() { printf '%s\n' " ${G}✓${Z} $*"; } fail() { printf '%s\n' " ${R}✗${Z} $*" >&2; exit 1; } ask() { # ask "question" default(y|n) local q=$1 d=$2 a if [ $YES -eq 1 ]; then [ "$d" = y ]; return; fi if [ ! -t 0 ]; then [ "$d" = y ]; return; fi read -r -p " $q [$([ "$d" = y ] && echo Y/n || echo y/N)] " a || a="" a=${a:-$d} [[ $a =~ ^[Yy] ]] } run() { if [ $CHECK -eq 1 ]; then printf '%s\n' " ${D}would run: $*${Z}"; else "$@"; fi; } # ---- shell start file ---- rc_file() { case "$(basename "${SHELL:-bash}")" in zsh) echo "$HOME/.zshrc" ;; fish) echo "$HOME/.config/fish/config.fish" ;; bash) if [ "$(uname -s)" = Darwin ]; then echo "$HOME/.bash_profile"; else echo "$HOME/.bashrc"; fi ;; *) echo "$HOME/.profile" ;; esac } uninstall() { banner step "Removing Enclawed CrowdCode™" for c in ccode crowdcode; do if [ -f "$BIN_DIR/$c" ] && grep -q "$MARK" "$BIN_DIR/$c"; then run rm -f "$BIN_DIR/$c"; ok "removed $BIN_DIR/$c"; fi done if [ -d "$PREFIX" ]; then run rm -rf "$PREFIX"; ok "removed $PREFIX"; fi local rc; rc=$(rc_file) if [ -f "$rc" ] && grep -q "$PATH_MARK" "$rc"; then if [ $CHECK -eq 0 ]; then grep -v "$PATH_MARK" "$rc" > "$rc.crowdcode-tmp" && cat "$rc.crowdcode-tmp" > "$rc" && rm -f "$rc.crowdcode-tmp" fi ok "removed the PATH line from $rc" fi printf '%s\n' " ${D}Your settings (~/.config/crowdcode) and history (~/.local/state/crowdcode) are kept; delete them yourself if you want them gone.${Z}" } if [ "$ACTION" = uninstall ]; then uninstall; exit 0; fi banner [ $CHECK -eq 1 ] && printf '%s\n' " ${D}(check only: nothing will be changed)${Z}" # ---- 1. system ---- step "Checking this computer" OS=$(uname -s) NODE_HOW="from https://nodejs.org (or your package manager)" MOBILE=0 case "$OS" in Linux) if [ -n "${TERMUX_VERSION:-}" ] || [[ "${PREFIX:-}" == */com.termux/* ]]; then ok "Android (Termux; Enclawed asks about or refuses the commands it would run contained elsewhere)"; NODE_HOW="with: pkg install nodejs-lts"; MOBILE=1 elif uname -r | grep -qi -- '-ish$' || [ -e /proc/ish ]; then ok "iOS/iPadOS (iSH; Enclawed asks about or refuses the commands it would run contained elsewhere)"; NODE_HOW="with: apk add nodejs npm (Alpine 3.21 or newer)"; MOBILE=1 elif grep -qi microsoft /proc/version 2>/dev/null; then ok "Windows Subsystem for Linux"; else ok "Linux"; fi ;; Darwin) ok "macOS (tool commands that must run contained are refused there for now)" ;; *) fail "this installer is for Linux, macOS, WSL, Android (Termux) and iOS/iPadOS (iSH); on Windows use scripts/install.ps1" ;; esac if ! command -v node >/dev/null 2>&1; then fail "Node.js is not installed. Get Node.js 22 or newer $NODE_HOW, then run this again." fi NODE_MAJOR=$(node -p 'process.versions.node.split(".")[0]') [ "$NODE_MAJOR" -ge 22 ] || fail "Node.js $(node -v) is too old; CrowdCode™ needs 22 or newer ($NODE_HOW)." ok "Node.js $(node -v)" command -v npm >/dev/null 2>&1 || fail "npm is missing (it comes with Node.js; get it $NODE_HOW)." ok "npm $(npm -v)" # ---- 2. what to install ---- step "Getting CrowdCode™" HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" SRC="" if [ -n "$FROM" ]; then SRC="$FROM" elif [ -f "$HERE/../package.json" ] && grep -q "\"name\": \"$NPM_PACKAGE\"" "$HERE/../package.json"; then SRC="$(cd "$HERE/.." && pwd)" fi WORK=$(mktemp -d "${TMPDIR:-/tmp}/crowdcode-install.XXXXXX") trap 'rm -rf "$WORK"' EXIT if [ -n "$SRC" ] && [ -d "$SRC" ]; then VERSION=$(node -p "require('$SRC/package.json').version") ok "from the folder $SRC (version $VERSION)" if [ $CHECK -eq 0 ]; then (cd "$SRC" && npm pack --silent --pack-destination "$WORK" >/dev/null) fi SPEC="$WORK/$NPM_PACKAGE-$VERSION.tgz" elif [ -n "$SRC" ]; then [ -f "$SRC" ] || fail "$SRC does not exist" SPEC="$(cd "$(dirname "$SRC")" && pwd)/$(basename "$SRC")" VERSION=$(tar -xOzf "$SPEC" package/package.json | node -p 'JSON.parse(require("fs").readFileSync(0,"utf8")).version') ok "from the package file $SPEC (version $VERSION)" else VERSION=$(npm view "$NPM_PACKAGE" version 2>/dev/null) || fail "$NPM_PACKAGE is not on npm yet; run this script from a CrowdCode™ folder, or pass --from FILE" SPEC="$NPM_PACKAGE@$VERSION" ok "from npm: $SPEC" fi # ---- 3. install ---- step "Installing into $PREFIX" DEST="$PREFIX/versions/$VERSION" run mkdir -p "$PREFIX/versions" if [ $CHECK -eq 0 ]; then rm -rf "$DEST.partial" mkdir -p "$DEST.partial" # Install scripts stay off: CrowdCode™ and Enclawed need none to run. # Android and iSH: one of Enclawed's dependencies (@lancedb/lancedb, a native # library its framework never loads) lists only computer platforms, which npm # refuses there; --force skips that platform check (install scripts stay off). npm install --prefix "$DEST.partial" --omit=dev --ignore-scripts --no-audit --no-fund --loglevel=error $([ "$MOBILE" = 1 ] && echo --force) "$SPEC" >/dev/null rm -rf "$DEST" mv "$DEST.partial" "$DEST" ln -sfn "$DEST" "$PREFIX/current.new" && mv -T "$PREFIX/current.new" "$PREFIX/current" 2>/dev/null || { rm -f "$PREFIX/current"; ln -s "$DEST" "$PREFIX/current"; rm -f "$PREFIX/current.new"; } fi ok "version $VERSION" ENTRY="$PREFIX/current/node_modules/$NPM_PACKAGE/bin/crowdcode.mjs" # ---- 4. commands ---- step "Creating the commands ccode and crowdcode in $BIN_DIR" run mkdir -p "$BIN_DIR" for c in ccode crowdcode; do target="$BIN_DIR/$c" if [ -e "$target" ] && ! grep -q "$MARK" "$target" 2>/dev/null && [ $FORCE -eq 0 ]; then fail "$target exists and is not CrowdCode™'s; move it away or run with --force" fi other=$(command -v "$c" 2>/dev/null || true) if [ -n "$other" ] && [ "$other" != "$target" ] && ! grep -q "$MARK" "$other" 2>/dev/null; then printf '%s\n' " ${D}note: another $c is at $other; whichever comes first on PATH wins${Z}" fi if [ $CHECK -eq 0 ]; then cat > "$target" <> "$RC" else printf '%s\n' "export PATH=\"$BIN_DIR:\$PATH\" $PATH_MARK" >> "$RC" fi fi ok "added; open a new terminal (or run: export PATH=\"$BIN_DIR:\$PATH\")" fi fi ;; esac # ---- 6. check ---- if [ $CHECK -eq 0 ]; then "$BIN_DIR/ccode" --version >/dev/null || fail "ccode does not start; see the messages above" ok "$("$BIN_DIR/ccode" --version) works" fi printf '\n%s\n' "${W}Done.${Z} Start it in any project folder with: ${W}ccode${Z}" # ---- 7. models ---- if [ $NO_SETUP -eq 0 ] && [ $CHECK -eq 0 ] && [ -t 0 ] && [ $YES -eq 0 ]; then if ask "Set up where the models run now (ccode setup)?" y; then "$BIN_DIR/ccode" setup else printf '%s\n' " ${D}Later: ccode setup${Z}" fi fi