Fix Gist setup and first-run bootstrap

This commit is contained in:
2026-07-29 17:43:26 +08:00
parent 13cf327f48
commit f19466a5df
9 changed files with 189 additions and 65 deletions
+51 -19
View File
@@ -5,24 +5,63 @@ command -v node >/dev/null 2>&1 || {
printf '%s\n' 'Node.js is required. Install it with your system package manager.' >&2
exit 1
}
command -v gh >/dev/null 2>&1 || {
printf '%s\n' 'GitHub CLI (gh) is required: https://cli.github.com/' >&2
exit 1
}
if ! gh auth status >/dev/null 2>&1; then
printf '%s\n' 'GitHub authentication is required.'
gh auth login </dev/tty >/dev/tty
if [ -z "${ZED_SETTINGS_BUNDLE_PATH:-}" ]; then
command -v gh >/dev/null 2>&1 || {
printf '%s\n' 'GitHub CLI (gh) is required: https://cli.github.com/' >&2
exit 1
}
unset GH_TOKEN GITHUB_TOKEN
if ! gh auth status >/dev/null 2>&1; then
printf '%s\n' 'GitHub authentication is required.'
gh auth login </dev/tty >/dev/tty
fi
fi
if [ -n "${ZED_SETTINGS_GIST_ID:-}" ]; then
entered=$ZED_SETTINGS_GIST_ID
else
printf 'GitHub Gist ID or full URL: ' >/dev/tty
printf 'GitHub Gist ID or URL (leave blank to create a new Secret Gist): ' >/dev/tty
IFS= read -r entered </dev/tty
fi
gist_id=$(printf '%s' "$entered" | sed -nE 's#.*[^a-fA-F0-9]([a-fA-F0-9]{8,})/?$#\1#p')
if [ -z "$gist_id" ]; then
temporary_dir=$(mktemp -d)
trap 'rm -rf "$temporary_dir"' EXIT INT TERM
response="$temporary_dir/gist.json"
bundle_path=${ZED_SETTINGS_BUNDLE_PATH:-}
if [ -n "$bundle_path" ]; then
gist_id=$(printf '%s' "$entered" | sed -nE 's#.*[^a-fA-F0-9]([a-fA-F0-9]{8,})/?$#\1#p')
elif [ -z "$entered" ]; then
bundle_path="$temporary_dir/zed-settings-sync.json"
payload="$temporary_dir/create-gist.json"
curl -fsSL 'https://git.okk.cool/purp1e/zed-sync/raw/branch/master/zed-settings-sync.json' -o "$bundle_path"
node - "$bundle_path" "$payload" <<'NODE_CREATE'
const fs = require("node:fs");
const [bundlePath, payloadPath] = process.argv.slice(2);
const content = fs.readFileSync(bundlePath, "utf8");
fs.writeFileSync(payloadPath, JSON.stringify({
description: "Zed settings sync",
public: false,
files: { "zed-settings-sync.json": { content } },
}));
NODE_CREATE
gh api --method POST gists --input "$payload" > "$response"
gist_id=$(node -e 'const g=require(process.argv[1]); process.stdout.write(g.id)' "$response")
gist_url=$(node -e 'const g=require(process.argv[1]); process.stdout.write(g.html_url)' "$response")
printf 'Created Secret Gist: %s\n' "$gist_url"
else
gist_id=$(printf '%s' "$entered" | sed -nE 's#.*[^a-fA-F0-9]([a-fA-F0-9]{8,})/?$#\1#p')
if [ -z "$gist_id" ]; then
gist_id=$(printf '%s' "$entered" | sed -nE 's#^([a-fA-F0-9]{8,})$#\1#p')
fi
if [ -z "$gist_id" ]; then
printf '%s\n' 'Enter a valid GitHub Gist ID or URL.' >&2
exit 1
fi
gh api "gists/$gist_id" > "$response"
fi
if [ -z "${gist_id:-}" ]; then
gist_id=$(printf '%s' "$entered" | sed -nE 's#^([a-fA-F0-9]{8,})$#\1#p')
fi
if [ -z "$gist_id" ]; then
@@ -30,14 +69,7 @@ if [ -z "$gist_id" ]; then
exit 1
fi
temporary_dir=$(mktemp -d)
trap 'rm -rf "$temporary_dir"' EXIT INT TERM
response="$temporary_dir/gist.json"
if [ -z "${ZED_SETTINGS_BUNDLE_PATH:-}" ]; then
gh api "gists/$gist_id" > "$response"
fi
node - "$response" "$gist_id" "${ZED_SETTINGS_BUNDLE_PATH:-}" <<'NODE'
node - "$response" "$gist_id" "$bundle_path" <<'NODE'
const { createHash } = require("node:crypto");
const fs = require("node:fs");
const os = require("node:os");