#!/bin/sh
# Open Sourcebook: the one-line install for Mac and Linux.
#
# curl -fsSL https://opensourcebook.app/install | sh
#
# It finds Node 24 or puts a private copy in the Open Sourcebook folder, runs
# the opensourcebook package's own install through that Node, and makes
# "opensourcebook" a command. Everything lives in one folder, ~/.opensourcebook.
# It never asks for a password and never uses sudo.
#
# Settings, all optional:
# OPENSOURCEBOOK_HOME the folder to install into (default ~/.opensourcebook)
# OPENSOURCEBOOK_PACKAGE what npx runs (default opensourcebook@latest)
# OPENSOURCEBOOK_NODE_DIST where Node is downloaded from
# OPENSOURCEBOOK_NO_PATH=1 do not make "opensourcebook" a command
# Words after "sh -s --" are handed to "opensourcebook install".
#
# The whole script is one function called on the last line, so a download that
# was cut off half way runs nothing.
set -eu
main() {
step='getting ready'
said=''
work=''
bold=''
red=''
dim=''
plain=''
if [ -t 1 ] && [ -z "${NO_COLOR:-}" ] && [ "${TERM:-dumb}" != dumb ]; then
bold=$(printf '\033[1m')
red=$(printf '\033[31m')
dim=$(printf '\033[2m')
plain=$(printf '\033[0m')
fi
trap 'finish $?' EXIT
trap 'exit 130' INT
trap 'exit 143' TERM
if [ -z "${HOME:-}" ] || [ ! -d "$HOME" ]; then
fail 'This computer did not say where your home folder is.' \
'Open a new terminal window and paste the line again.'
fi
home=${OPENSOURCEBOOK_HOME:-$HOME/.opensourcebook}
case $home in
/*) ;;
*) home=$(pwd)/$home ;;
esac
package=${OPENSOURCEBOOK_PACKAGE:-opensourcebook@latest}
node_dist=${OPENSOURCEBOOK_NODE_DIST:-https://nodejs.org/dist/latest-v24.x}
node_dist=${node_dist%/}
starts=yes
opens=yes
for word in "$@"; do
case $word in
--no-start) starts=no; opens=no ;;
--no-open) opens=no ;;
esac
done
say "${bold}Open Sourcebook${plain}"
say ''
say 'This is what will happen:'
say ' 1. Open Sourcebook runs on a free program called Node, version 24 or newer.'
say ' If this computer already has it, that copy is used. If not, a private'
say " copy is put in the folder below. A Node you already have is never replaced."
say " 2. Open Sourcebook is downloaded into $home"
say ' 3. The word "opensourcebook" becomes a command you can type in a terminal.'
if [ "$opens" = yes ]; then
say ' 4. Open Sourcebook starts and opens in your browser.'
elif [ "$starts" = yes ]; then
say ' 4. Open Sourcebook starts.'
fi
say 'Nothing else on this computer is changed, and you are not asked for a password.'
say ''
step='making the Open Sourcebook folder'
mkdir -p "$home" 2>/dev/null || fail "The folder $home could not be made." \
'Check that you are allowed to write in your home folder, then paste the line again.'
step='looking for Node'
find_node
step='downloading and setting up Open Sourcebook'
run_package "$@"
step='making opensourcebook a command'
make_command
step='finishing'
say ''
if [ "$starts" = yes ]; then
say "${bold}Open Sourcebook is running.${plain}"
else
say "${bold}Open Sourcebook is installed.${plain}"
fi
if [ "$(uname -s 2>/dev/null)" = Darwin ]; then
say 'To start it again, type opensourcebook in a terminal, or open "Open Sourcebook" from Applications.'
else
say 'To start it again, type opensourcebook in a terminal.'
fi
say "To remove it, delete the folder $home"
}
say() {
printf '%s\n' "$1"
}
note() {
printf '%s\n' "${dim}$1${plain}"
}
# One sentence on what went wrong, one next step, and a non-zero exit.
fail() {
said=yes
printf '\n%s\n%s\n' "${red}$1${plain}" "Next step: $2" >&2
exit 1
}
# Runs whenever the script ends. Clears the download folder, and if the script
# stopped without having explained itself, says where it stopped.
finish() {
code=$1
trap - EXIT
if [ -n "$work" ] && [ -d "$work" ]; then
rm -rf "$work"
fi
if [ "$code" -ne 0 ] && [ -z "$said" ]; then
printf '\n%s\n%s\n' \
"${red}The install stopped while $step.${plain}" \
'Next step: Paste the same line again; it is safe to run twice.' >&2
fi
exit "$code"
}
have() {
command -v "$1" >/dev/null 2>&1
}
# True when the Node at $1 runs and is version 24 or newer.
node_is_new_enough() {
found=$("$1" -v 2>/dev/null) || return 1
found=${found#v}
found=${found%%.*}
case $found in
'' | *[!0-9]*) return 1 ;;
esac
[ "$found" -ge 24 ]
}
# Sets node_dir to the folder holding a Node 24 and its npx.
find_node() {
node_dir=''
on_path=$(command -v node 2>/dev/null || true)
case $on_path in
/*)
if node_is_new_enough "$on_path" && [ -x "$(dirname "$on_path")/npx" ]; then
node_dir=$(dirname "$on_path")
say "Using the Node already on this computer ($("$on_path" -v))."
return 0
fi
;;
esac
if node_is_new_enough "$home/node/bin/node" && [ -x "$home/node/bin/npx" ]; then
node_dir=$home/node/bin
say "Using the private copy of Node in $home/node ($("$node_dir/node" -v))."
return 0
fi
download_node
node_dir=$home/node/bin
}
# Prints the name Node gives builds for this computer, or stops with a sentence.
node_build() {
system=$(uname -s 2>/dev/null || echo unknown)
chip=$(uname -m 2>/dev/null || echo unknown)
case $system in
Darwin)
case $chip in
arm64 | aarch64) echo darwin-arm64 ;;
x86_64)
# A terminal running in Intel mode on an Apple chip still wants the Apple build.
if [ "$(sysctl -n sysctl.proc_translated 2>/dev/null || echo 0)" = 1 ]; then
echo darwin-arm64
else
echo darwin-x64
fi
;;
*) unsupported "this kind of Mac ($chip)" ;;
esac
;;
Linux)
if (ldd --version 2>&1 || true) | grep -qi musl || ls /lib/ld-musl-* >/dev/null 2>&1; then
unsupported 'this kind of Linux (Alpine and others built on musl)'
fi
case $chip in
x86_64 | amd64) echo linux-x64 ;;
aarch64 | arm64) echo linux-arm64 ;;
*) unsupported "this kind of computer ($chip)" ;;
esac
;;
*) unsupported "this system ($system)" ;;
esac
}
unsupported() {
fail "Node cannot be set up automatically on $1." \
'Install Node 24 from https://nodejs.org, then paste the line again.'
}
# fetch
fetch() {
if have curl; then
curl -fsSL --retry 2 -o "$2" "$1"
else
wget -q -O "$2" "$1"
fi
}
# Prints the sha256 of a file.
fingerprint() {
if have sha256sum; then
sha256sum "$1" | awk '{print $1}'
elif have shasum; then
shasum -a 256 "$1" | awk '{print $1}'
else
openssl dgst -sha256 "$1" | awk '{print $NF}'
fi
}
# Puts a private Node 24 in /node. Only that folder is replaced.
download_node() {
step='checking what this computer is'
# node_build has already said what is wrong when it does not give a name.
build=$(node_build) || {
said=yes
exit 1
}
if ! have curl && ! have wget; then
fail 'This computer has neither curl nor wget, so nothing can be downloaded.' \
'Install Node 24 from https://nodejs.org, then paste the line again.'
fi
if ! have tar; then
fail 'This computer has no tar program, so Node cannot be unpacked.' \
'Install Node 24 from https://nodejs.org, then paste the line again.'
fi
if ! have sha256sum && ! have shasum && ! have openssl; then
fail 'This computer has no program to check that a download arrived whole.' \
'Install Node 24 from https://nodejs.org, then paste the line again.'
fi
work=$home/.install-download.$$
rm -rf "$work"
mkdir -p "$work"
step='finding the newest Node 24'
say 'No Node 24 was found, so a private copy is being downloaded (about 50 MB, a minute or so).'
if ! fetch "$node_dist/SHASUMS256.txt" "$work/SHASUMS256.txt"; then
fail "The list of Node downloads could not be read from $node_dist" \
'Check your internet connection, then paste the line again.'
fi
wanted=$(awk -v tail="-$build.tar.gz" '
{
name = $2
sub(/^\*/, "", name)
if (name ~ /^node-v[0-9][0-9.]*-/ && length(name) > length(tail) &&
substr(name, length(name) - length(tail) + 1) == tail) {
print $1 " " name
exit
}
}' "$work/SHASUMS256.txt")
sum=${wanted%% *}
file=${wanted#* }
case $file in
'' | */* | *[!A-Za-z0-9._-]*) file='' ;;
esac
case $sum in
*[!0-9a-fA-F]*) sum='' ;;
esac
if [ -z "$file" ] || [ "${#sum}" -ne 64 ]; then
fail "No Node 24 for this computer ($build) is listed at $node_dist" \
'Install Node 24 from https://nodejs.org, then paste the line again.'
fi
step="downloading $file"
note "Downloading $file"
if ! fetch "$node_dist/$file" "$work/$file"; then
fail "The download of $file did not finish." \
'Check your internet connection, then paste the line again.'
fi
step='checking the download'
got=$(fingerprint "$work/$file" | tr 'A-F' 'a-f')
if [ "$got" != "$(printf '%s' "$sum" | tr 'A-F' 'a-f')" ]; then
fail "The download of $file did not arrive whole (its checksum does not match), so it was thrown away." \
'Paste the line again to download it once more.'
fi
note 'The download arrived whole.'
step='unpacking Node'
mkdir "$work/unpacked"
if ! tar -xzf "$work/$file" -C "$work/unpacked"; then
fail "$file could not be unpacked." \
"Check that there is free space on this computer, then paste the line again."
fi
inner=$work/unpacked/${file%.tar.gz}
if ! node_is_new_enough "$inner/bin/node"; then
fail 'The Node that was downloaded does not run on this computer.' \
'Install Node 24 from https://nodejs.org, then paste the line again.'
fi
rm -rf "$home/node"
mv "$inner" "$home/node"
rm -rf "$work"
work=''
say "A private copy of Node ($("$home/node/bin/node" -v)) is now in $home/node"
}
# Runs "opensourcebook install" from the package, with the chosen Node first on PATH.
run_package() {
# A local file given as a relative path still has to be found from the home folder.
# It is also marked as a file, or npx would try to run it as a program.
if [ -f "$package" ]; then
case $package in
/*) ;;
*) package=$(cd "$(dirname "$package")" && pwd)/$(basename "$package") ;;
esac
package=file:$package
fi
say ''
status=0
(
cd "$home"
PATH=$node_dir:$PATH
OPENSOURCEBOOK_HOME=$home
npm_config_update_notifier=false
npm_config_fund=false
export PATH OPENSOURCEBOOK_HOME npm_config_update_notifier npm_config_fund
# The script itself may be arriving on standard input, so the package gets none of it.
"$node_dir/npx" --yes "$package" install "$@" &2
exit 1
fi
case ":$PATH:" in
*":$home/bin:"*)
say 'The word opensourcebook already works in a terminal.'
return 0
;;
esac
# A folder for personal commands that is already known: one link is enough.
link=$HOME/.local/bin/opensourcebook
case ":$PATH:" in
*":$HOME/.local/bin:"*)
if [ -L "$link" ] || [ ! -e "$link" ]; then
if mkdir -p "$HOME/.local/bin" 2>/dev/null && rm -f "$link" 2>/dev/null &&
ln -s "$home/bin/opensourcebook" "$link" 2>/dev/null; then
say "The word opensourcebook now works in a terminal (a link was put in $HOME/.local/bin)."
return 0
fi
fi
;;
esac
case $home in
*'"'* | *'$'* | *'`'* | *'\'* | *"'"*)
say "To use the word opensourcebook in a terminal, add $home/bin to your PATH yourself; its name has signs this installer does not write for you."
return 0
;;
esac
if [ "$home" = "$HOME/.opensourcebook" ]; then
folder='$HOME/.opensourcebook/bin'
else
folder=$home/bin
fi
marker='# Open Sourcebook command'
line="export PATH=\"$folder:\$PATH\" $marker"
changed=''
case $(basename "${SHELL:-sh}") in
zsh)
add_line "${ZDOTDIR:-$HOME}/.zprofile"
# Outside the Mac, a terminal window reads .zshrc and not .zprofile.
if [ "$(uname -s 2>/dev/null)" != Darwin ]; then
add_line "${ZDOTDIR:-$HOME}/.zshrc"
fi
;;
bash)
add_line "$HOME/.bashrc"
if [ -f "$HOME/.bash_profile" ]; then
add_line "$HOME/.bash_profile"
elif [ -f "$HOME/.bash_login" ]; then
add_line "$HOME/.bash_login"
else
add_line "$HOME/.profile"
fi
;;
fish)
line="set -gx PATH \"$folder\" \$PATH $marker"
mkdir -p "${XDG_CONFIG_HOME:-$HOME/.config}/fish/conf.d" 2>/dev/null || true
add_line "${XDG_CONFIG_HOME:-$HOME/.config}/fish/conf.d/opensourcebook.fish"
;;
*)
add_line "$HOME/.profile"
;;
esac
if [ -n "$changed" ]; then
say "One line was added to$changed so that the word opensourcebook works."
say 'Open a new terminal window before you type it; this window does not know it yet.'
else
say 'The word opensourcebook is already set up; it works in a new terminal window.'
fi
}
# Adds $line to the file $1 unless the marker is already in it.
add_line() {
if [ -f "$1" ] && grep -qF "$marker" "$1" 2>/dev/null; then
return 0
fi
if [ -s "$1" ] && [ -n "$(tail -c 1 "$1")" ]; then
printf '\n' >>"$1" 2>/dev/null || true
fi
if printf '%s\n' "$line" >>"$1" 2>/dev/null; then
changed="$changed $1"
else
say "The file $1 could not be changed, so add this line to it yourself: $line"
fi
}
main "$@"