Files
stanp-opencode/install.sh

91 lines
4.4 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
for command in bun curl jq sha256sum tar; do
command -v "$command" >/dev/null || { printf 'Required command not found: %s\n' "$command" >&2; exit 1; }
done
config="${OPENCODE_CONFIG_DIR:-${XDG_CONFIG_HOME:-$HOME/.config}/opencode}"
mkdir -p "$config"
config=$(cd "$config" && pwd -P)
mkdir -p "$config/plugins"
install=$(mktemp -d "$config/install.stanp-opencode.XXXXXXXX")
cleanup() {
if [[ -n "$install" ]]; then rm -rf "$install"; fi
}
trap cleanup EXIT
api='https://git.krtss.de/api/v1/repos/StanPonomarev/stanp-opencode/releases/latest'
curl --fail-with-body --silent --show-error --location --proto '=https' --proto-redir '=https' "$api" -o "$install/release.json"
for asset in stanp-opencode.tgz stanp-opencode.tgz.sha256; do
url=$(jq -er --arg name "$asset" '[.assets[] | select(.name == $name)] | if length == 1 then .[0] | select((.id | type) == "number" and (.browser_download_url | type) == "string" and (.browser_download_url | startswith("https://"))) | "\(.browser_download_url)?asset_id=\(.id)" else error("Missing or duplicate release asset: " + $name) end' "$install/release.json")
curl --fail-with-body --silent --show-error --location --proto '=https' --proto-redir '=https' "$url" -o "$install/$asset"
done
read -r checksum _ < "$install/stanp-opencode.tgz.sha256"
if [[ ! "$checksum" =~ ^[a-fA-F0-9]{64}$ ]]; then
printf 'Invalid release checksum\n' >&2
exit 1
fi
hash_output=$(sha256sum "$install/stanp-opencode.tgz")
read -r actual _ <<< "$hash_output"
if [[ ! "$actual" =~ ^[a-fA-F0-9]{64}$ || "$actual" != "$checksum" ]]; then
printf 'Release checksum mismatch; installation unchanged\n' >&2
exit 1
fi
tar -xzf "$install/stanp-opencode.tgz" -C "$install"
bun -e '
if (!await Bun.file(process.argv[1]).exists()) {
throw new Error("Latest release lacks the OpenCode V2 directory entrypoint. Publish a V2-compatible release first.");
}
const { default: plugin } = await import(process.argv[1]);
if (!plugin || plugin.id !== "stanp-opencode" || typeof plugin.setup !== "function") {
throw new Error("Latest release is not a native OpenCode V2 plugin. Publish a V2-compatible release first.");
}
const tuiFile = Bun.file(process.argv[2]);
const manifest = await Bun.file(process.argv[3]).json();
const exportsTui = manifest?.exports?.["./tui"];
if (!await tuiFile.exists() || !new Bun.Transpiler({ loader: "js" }).scan(await tuiFile.text()).exports.includes("default") || (typeof exportsTui !== "string" && typeof exportsTui?.import !== "string")) {
throw new Error("Latest release lacks the ChatGPT quota TUI plugin.");
}
' "$install/package/dist/index.js" "$install/package/dist/tui.js" "$install/package/package.json"
tag=$(jq -r '.tag_name' "$install/release.json")
config_file="$config/opencode.json"
config_source="$config_file"
if [[ ! -e "$config_source" ]]; then
config_source="$install/empty-config.json"
printf '{}\n' > "$config_source"
fi
jq -e '
if type != "object" then error("opencode.json must contain an object") else . end
| if (.plugins // []) | type != "array" then error("opencode.json plugins must be an array") else . end
| .["$schema"] = (.["$schema"] // "https://opencode.ai/config.json")
| .plugins = (.plugins // [])
| if any(.plugins[]?; .package == "./plugins/stanp-opencode") then . else
.plugins += [{
package: "./plugins/stanp-opencode",
options: { mode: "gpt", provider: "openai", lunaPriority: true }
}]
end
' "$config_source" > "$install/opencode.json"
target="$config/plugins/stanp-opencode"
staged="$config/plugins/.stanp-opencode.new.$$"
backup="$config/plugins/.stanp-opencode.old.$$"
mkdir "$staged"
mv "$install/package/dist/index.js" "$staged/index.js"
mv "$install/package/dist/tui.js" "$staged/tui.js"
cat > "$staged/package.json" <<'EOF'
{"name":"stanp-opencode","type":"module","main":"./index.js","exports":{".":"./index.js","./tui":"./tui.js"}}
EOF
if [[ -e "$target" || -L "$target" ]]; then mv "$target" "$backup"; fi
if ! mv "$staged" "$target"; then
if [[ -e "$backup" || -L "$backup" ]]; then mv "$backup" "$target"; fi
printf 'Could not activate plugin; installation unchanged\n' >&2
exit 1
fi
rm -rf "$backup"
rm -f "$config/plugins/stanp-opencode.js"
mv "$install/opencode.json" "$config_file"
printf 'Installed stanp-opencode %s into %s/plugins/stanp-opencode and updated %s\nRestart opencode2 to load it.\n' "$tag" "$config" "$config_file"