#!/bin/sh # PersistMemory CLI installer. # # curl -fsSL https://persistmemory.com/install.sh | sh # # Read it first if you would rather: # curl -fsSL https://persistmemory.com/install.sh | less # # Environment: # PM_VERSION install a specific npm version (default: latest) set -eu # Everything is inside this function, invoked on the very last line. A # truncated download cannot execute half an install: it fails to parse. main() { RED=''; BOLD=''; DIM=''; OFF='' if [ -t 1 ]; then RED=$(printf '\033[31m'); BOLD=$(printf '\033[1m') DIM=$(printf '\033[2m'); OFF=$(printf '\033[0m') fi step() { printf '%s==>%s %s\n' "$BOLD" "$OFF" "$1"; } note() { printf '%s%s%s\n' "$DIM" "$1" "$OFF"; } die() { printf '%s error:%s %s\n' "$RED" "$OFF" "$1" >&2; exit 1; } step "Installing the PersistMemory CLI" # A supported platform, named rather than assumed. os=$(uname -s 2>/dev/null || echo unknown) case "$os" in Linux|Darwin) ;; MINGW*|MSYS*|CYGWIN*) die "on Windows, install with: npm install -g @persistmemory/cli" ;; *) die "unsupported platform '$os'. Install with: npm install -g @persistmemory/cli" ;; esac # Node, because the CLI is a Node program. Checked BEFORE anything is # downloaded, so the failure arrives before the work rather than after it. if ! command -v node >/dev/null 2>&1; then die "Node.js 20 or newer is required, and 'node' was not found on PATH. Install it from https://nodejs.org and run this again." fi node_major=$(node -p 'process.versions.node.split(".")[0]' 2>/dev/null || echo 0) if [ "$node_major" -lt 20 ] 2>/dev/null; then die "Node.js 20 or newer is required; found $(node -v)." fi if ! command -v npm >/dev/null 2>&1; then die "'npm' was not found on PATH. It ships with Node.js." fi # Read through printenv so an unset variable is not an error under 'set -u' # and no brace expansion is needed. See the note at the top of this file. wanted=$(printenv PM_VERSION 2>/dev/null || true) package="@persistmemory/cli" if [ -n "$wanted" ]; then package="@persistmemory/cli@$wanted" fi step "Fetching $package" # No sudo, deliberately. If the global prefix is not writable, npm says so # and the two safe ways forward are printed below. if ! npm install -g "$package" >/dev/null 2>&1; then printf '\n' note "npm could not install globally. That is usually a permissions" note "problem with npm's own prefix rather than anything to do with" note "this package. Two ways forward, neither needing sudo:" printf '\n' printf ' 1. Point npm somewhere you own:\n' printf ' npm config set prefix ~/.local\n' printf ' export PATH="$HOME/.local/bin:$PATH"\n' printf ' curl -fsSL https://persistmemory.com/install.sh | sh\n' printf '\n' printf ' 2. Skip installing and run it on demand:\n' printf ' npx @persistmemory/cli auth login\n' printf '\n' die "install failed" fi if ! command -v pm >/dev/null 2>&1; then printf '\n' note "Installed, but 'pm' is not on your PATH. Add npm's global bin" note "directory to it:" printf '\n' printf ' export PATH="$(npm prefix -g)/bin:$PATH"\n' printf '\n' fi step "Installed $(pm --version 2>/dev/null || echo "$package")" printf '\n' printf 'Next:\n' printf ' %spm auth login%s sign in with your browser\n' "$BOLD" "$OFF" printf ' %spm remember "we chose Postgres"%s capture something\n' "$BOLD" "$OFF" printf ' %spm search "what did we choose"%s ask for it back\n' "$BOLD" "$OFF" printf '\n' printf 'Docs: https://persistmemory.com/docs/cli\n' } main "$@"