ray-template/scripts/sync-frontend.sh
XiaoDaiGua-Ray 59c1090ea4
feat(fullstack): add optional backend and source distributions
Add the NestJS backend with PostgreSQL and Redis, local setup, and
independent frontend and fullstack source archives.

Enforce permissions across redirects, let users dismiss the 403 page,
and restore missing translation warnings in development.
2026-11-06 19:53:49 +08:00

53 lines
2.1 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
directory=$(cd "${1:?Provide the extracted frontend distribution}" && pwd -P)
source=$(git rev-parse HEAD)
git_dir=$(git rev-parse --absolute-git-dir)
ref=refs/heads/frontend
node -e '
const { readFileSync } = require("node:fs")
const manifest = JSON.parse(readFileSync(process.argv[1], "utf8"))
if (manifest.generatedBy !== "ray-template/distributions" ||
manifest.edition !== "frontend" ||
manifest.source?.dirty !== false ||
manifest.source.commit !== process.argv[2])
throw new Error("Use a clean frontend distribution generated from this checkout.")
' "$directory/distribution.json" "$source"
scratch=$(mktemp -d)
trap 'rm -rf "$scratch"' EXIT
export GIT_INDEX_FILE="$scratch/index"
tree=$(
cd "$directory"
git --git-dir="$git_dir" --work-tree="$directory" add --force --all -- .
git --git-dir="$git_dir" write-tree
)
commit_args=("$tree" -m "chore(distributions): generate frontend from $source")
remote_tip=$(git ls-remote --heads origin "$ref")
if [[ -n "$remote_tip" ]]; then
git fetch --no-tags origin "$ref"
tip=$(git rev-parse FETCH_HEAD)
previous=$(git show "$tip:distribution.json" | node -e '
const manifest = JSON.parse(require("node:fs").readFileSync(0, "utf8"))
if (manifest.generatedBy !== "ray-template/distributions" ||
manifest.edition !== "frontend" ||
manifest.source?.dirty !== false ||
!/^[0-9a-f]{40}$/.test(manifest.source.commit))
throw new Error("The existing frontend branch is not a generated distribution.")
process.stdout.write(manifest.source.commit)
')
[[ "$(git show -s --format=%s "$tip")" == "chore(distributions): generate frontend from $previous" ]] ||
{ echo "The existing frontend tip was not generated by this script." >&2; exit 1; }
git merge-base --is-ancestor "$previous" "$source"
if [[ "$(git rev-parse "$tip^{tree}")" == "$tree" ]]; then
echo "Frontend branch already matches the generated source."
exit 0
fi
commit_args+=(-p "$tip")
fi
commit=$(git commit-tree "${commit_args[@]}")
git push origin "$commit:$ref"