#!/bin/sh # Cyclotron for macOS and Linux: https://cyclotron.videogame.ai # # curl -fsSL https://cyclotron.videogame.ai/install.sh | sh # # Opens Cyclotron with a starter racing game in ~/Cyclotron/my-race. Run it again to reopen that game. # It uses the Node.js on PATH when that is version 24 or later for this machine; otherwise it downloads # Node.js 24 from nodejs.org into ~/.volter/node, checks it against nodejs.org's published SHA-256, and # changes nothing outside your home folder. set -eu fail() { printf 'Cyclotron: %s\n' "$1" >&2 exit 1 } # " -" for a Node.js, or "0" when it doesn't run. node_kind() { kind=$("$1" -p 'process.versions.node.split(".")[0] + " " + process.platform + "-" + process.arch' 2>/dev/null) || kind=0 printf '%s\n' "$kind" } # Cyclotron's editor is built for these three (the Windows one is install.ps1). node_fits() { kind=$(node_kind "$1") major=${kind%% *} case "$major" in '' | *[!0-9]*) return 1 ;; esac [ "$major" -ge 24 ] || return 1 case "${kind#* }" in darwin-arm64 | linux-x64) return 0 ;; *) return 1 ;; esac } # Runs Cyclotron. Under `curl | sh` stdin is the used-up pipe, so Cyclotron gets the terminal when there is one. run_attached() { if (: /dev/null; then "$@" /dev/null) || return 1 [ -n "$cache" ] && [ -d "$cache/_npx" ] || return 1 repaired=1 for entry in "$cache"/_npx/*/; do [ -d "$entry" ] || continue if [ ! -f "${entry}package.json" ] && [ -d "${entry}node_modules/@volter/cyclotron" ]; then rm -rf "${entry%/}" && repaired=0 fi done return "$repaired" } # A GAME KEEPS THE CYCLOTRON IT WAS MADE WITH, so running this again opens that one, fixes or not. When npm # has a newer Cyclotron, say so and how to move the game to it; nothing here changes the game. $1 is the PATH # line the advice needs when this script put its own Node.js on PATH (that change ends with this script). show_newer_release() { # npm view's --fetch-timeout starts only once a connection is made, so a network that drops packets held the # reopen for the system's connect timeout. Node asks the registry itself, cut off at 5 s however far it got. out=$(node -e 'const a = require(`./node_modules/@volter/cyclotron/package.json`).version, n = v => /^\d+\.\d+\.\d+$/.test(v) && v.split(`.`).map(Number); fetch((process.env.npm_config_registry || `https://registry.npmjs.org/`).replace(/\/*$/, `/@volter%2fcyclotron`), { headers: { accept: `application/vnd.npm.install-v1+json` }, signal: AbortSignal.timeout(5000) }).then(r => r.json()).then(p => { const b = p[`dist-tags`].latest, x = n(a), y = n(b); if (x && y && (y[0] - x[0] || y[1] - x[1] || y[2] - x[2]) > 0) console.log(b + ` ` + a) }, () => {}).finally(() => setTimeout(() => process.exit(), 100).unref())' 2>/dev/null /dev/null || echo 0)" = 1 ]; then arch=arm64; fi case "$os-$arch" in darwin-arm64 | linux-x64) ;; darwin-*) fail "Cyclotron on macOS needs Apple Silicon (an M-series Mac)." ;; *) fail "Cyclotron on Linux needs an x64 processor." ;; esac command -v curl >/dev/null 2>&1 || fail "this needs curl." command -v tar >/dev/null 2>&1 || fail "this needs tar." base=https://nodejs.org/dist/latest-v24.x work=$(mktemp -d) trap 'rm -rf "$work"' EXIT curl -fsSL "$base/SHASUMS256.txt" -o "$work/SHASUMS256.txt" || fail "couldn't reach nodejs.org." line=$(grep -E "^[0-9a-f]{64} node-v24\.[0-9]+\.[0-9]+-$os-$arch\.tar\.gz\$" "$work/SHASUMS256.txt" | head -n 1) || true [ -n "$line" ] || fail "nodejs.org lists no Node.js 24 for $os-$arch." sum=${line%% *} file=${line##* } printf 'Downloading Node.js for Cyclotron (%s) from nodejs.org...\n' "$file" curl -fL --progress-bar "$base/$file" -o "$work/$file" || fail "the Node.js download failed." if command -v sha256sum >/dev/null 2>&1; then got=$(sha256sum "$work/$file"); else got=$(shasum -a 256 "$work/$file"); fi [ "${got%% *}" = "$sum" ] || fail "the Node.js download doesn't match nodejs.org's checksum." rm -rf "$target.partial" mkdir -p "$target.partial" tar -xzf "$work/$file" -C "$target.partial" --strip-components 1 rm -rf "$target" mv "$target.partial" "$target" # Done with the download folder; Cyclotron itself can run for a while. rm -rf "$work" trap - EXIT } main() { # npm's own notices (a newer npm, funding, audit) aren't for someone opening Cyclotron. npm_config_update_notifier=false npm_config_fund=false npm_config_audit=false export npm_config_update_notifier npm_config_fund npm_config_audit [ -n "${HOME:-}" ] || fail "HOME isn't set." node_home="$HOME/.volter/node" games="$HOME/Cyclotron" game="$games/my-race" path_prefix= node=$(command -v node 2>/dev/null) || node= if [ -z "$node" ] || ! node_fits "$node"; then if [ ! -x "$node_home/bin/node" ] || ! node_fits "$node_home/bin/node"; then install_node "$node_home"; fi PATH="$node_home/bin:$PATH" export PATH path_prefix="export PATH=\"\$HOME/.volter/node/bin:\$PATH\" && " fi if [ -f "$game/volter.project.json" ]; then cd "$game" # A first run that stopped during its install left the game without its packages. if [ ! -d node_modules/@volter/cyclotron ]; then printf "Installing your game's packages...\n" npm install || fail "npm couldn't install your game's packages." fi show_newer_release "$path_prefix" printf 'Opening your game at %s...\n' "$game" attached npx --no-install cyclotron edit . return fi [ ! -e "$game" ] || fail "$game is already there and isn't a Cyclotron game. Move it, then run this again." mkdir -p "$games" cd "$games" # npx says nothing while it downloads Cyclotron, about half a minute on a first run. printf 'Downloading Cyclotron, then making a starter racing game at %s...\n' "$game" if ! run_attached npx --yes @volter/cyclotron@latest create my-race --template playable; then if [ ! -e "$game" ] && repair_npx_cache; then printf 'An earlier download of Cyclotron was cut short; downloading it again...\n' attached npx --yes @volter/cyclotron@latest create my-race --template playable else fail "Cyclotron didn't open; the lines above say why." fi fi } # The whole script has arrived before anything runs, even when it is piped from curl. main "$@"