🔨 misc(deps): add script to upgrade mermaid
parent
405f7643e9
commit
d73c4bde5a
@ -0,0 +1,165 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
set -eu pipefail
|
||||||
|
|
||||||
|
MERMAID_DIR="static/js"
|
||||||
|
MERMAID_FILE="mermaid.min.js"
|
||||||
|
MERMAID_PATH="${MERMAID_DIR}/${MERMAID_FILE}"
|
||||||
|
UGLIFY_ITERATIONS=5
|
||||||
|
TEMP_DIR=$(mktemp -d)
|
||||||
|
|
||||||
|
cleanup() {
|
||||||
|
rm -rf "$TEMP_DIR"
|
||||||
|
}
|
||||||
|
|
||||||
|
trap cleanup EXIT
|
||||||
|
|
||||||
|
exit_with_message() {
|
||||||
|
echo "$1" >&2
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
check_dependency() {
|
||||||
|
if ! command -v "$1" &> /dev/null; then
|
||||||
|
exit_with_message "$1 is required but not installed."
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
get_latest_version_jsdelivr() {
|
||||||
|
curl -s https://data.jsdelivr.com/v1/package/npm/mermaid | jq -r '.tags.latest'
|
||||||
|
}
|
||||||
|
|
||||||
|
get_latest_version_github() {
|
||||||
|
curl -s https://api.github.com/repos/mermaid-js/mermaid/releases/latest |
|
||||||
|
jq -r '.tag_name' |
|
||||||
|
sed -E 's/^v?mermaid@?//' # Remove 'v', 'mermaid@', or both if present.
|
||||||
|
}
|
||||||
|
|
||||||
|
compare_md5() {
|
||||||
|
local new_file="$1"
|
||||||
|
local current_file="$2"
|
||||||
|
local new_md5=$(md5sum "$new_file" | awk '{ print $1 }')
|
||||||
|
|
||||||
|
if [ -f "$current_file" ]; then
|
||||||
|
local current_md5=$(md5sum "$current_file" | awk '{ print $1 }')
|
||||||
|
if [ "$new_md5" = "$current_md5" ]; then
|
||||||
|
echo "same"
|
||||||
|
else
|
||||||
|
echo "different"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "new"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
uglify_file() {
|
||||||
|
local output_file="$1"
|
||||||
|
local iterations="$2"
|
||||||
|
|
||||||
|
for i in $(seq 1 "$iterations"); do
|
||||||
|
echo "Running UglifyJS iteration $i"
|
||||||
|
uglifyjs --compress --mangle -- "$output_file" > "${output_file}.tmp"
|
||||||
|
mv "${output_file}.tmp" "$output_file"
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
generate_commit_message() {
|
||||||
|
local template="$1"
|
||||||
|
local version="$2"
|
||||||
|
echo "${template//\{VERSION\}/${version}}"
|
||||||
|
}
|
||||||
|
|
||||||
|
upgrade_mermaid() {
|
||||||
|
if [ ! -d "$MERMAID_DIR" ]; then
|
||||||
|
exit_with_message "Directory ${MERMAID_DIR} does not exist. Are you running this script from the root of the repository?"
|
||||||
|
fi
|
||||||
|
|
||||||
|
local commit_msg_template=$(cat << EOM
|
||||||
|
⬆️ chore(deps): upgrade mermaid to v{VERSION}
|
||||||
|
|
||||||
|
Changelog: https://github.com/mermaid-js/mermaid/releases/tag/mermaid%40{VERSION}
|
||||||
|
|
||||||
|
Source: https://cdn.jsdelivr.net/npm/mermaid@{VERSION}/dist/mermaid.min.js
|
||||||
|
EOM
|
||||||
|
)
|
||||||
|
|
||||||
|
local latest_version=$(get_latest_version_jsdelivr || get_latest_version_github)
|
||||||
|
|
||||||
|
if [ -z "$latest_version" ]; then
|
||||||
|
exit_with_message "Unable to determine the latest Mermaid.js version."
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Latest Mermaid.js version: ${latest_version}"
|
||||||
|
|
||||||
|
local download_url="https://cdn.jsdelivr.net/npm/mermaid@${latest_version}/dist/mermaid.min.js"
|
||||||
|
curl -L "${download_url}" -o "${TEMP_DIR}/${MERMAID_FILE}"
|
||||||
|
|
||||||
|
uglify_file "${TEMP_DIR}/${MERMAID_FILE}" "$UGLIFY_ITERATIONS"
|
||||||
|
|
||||||
|
local comparison_result=$(compare_md5 "${TEMP_DIR}/${MERMAID_FILE}" "${MERMAID_PATH}")
|
||||||
|
|
||||||
|
case "$comparison_result" in
|
||||||
|
"same")
|
||||||
|
echo "New version is the same as current version. No update needed."
|
||||||
|
return 0
|
||||||
|
;;
|
||||||
|
"different")
|
||||||
|
echo "New version differs from current version. Proceeding with update."
|
||||||
|
mv "${TEMP_DIR}/${MERMAID_FILE}" "${MERMAID_PATH}"
|
||||||
|
;;
|
||||||
|
"new")
|
||||||
|
echo "Creating new file: ${MERMAID_PATH}"
|
||||||
|
mv "${TEMP_DIR}/${MERMAID_FILE}" "${MERMAID_PATH}"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
echo "Mermaid.js updated and minified successfully!"
|
||||||
|
echo "Preparing to commit changes…"
|
||||||
|
git add "${MERMAID_PATH}"
|
||||||
|
local commit_msg=$(generate_commit_message "$commit_msg_template" "$latest_version")
|
||||||
|
git commit -m "${commit_msg}"
|
||||||
|
|
||||||
|
echo "Most recent commit:"
|
||||||
|
git log -1
|
||||||
|
|
||||||
|
echo "To push the changes:"
|
||||||
|
echo "git push"
|
||||||
|
}
|
||||||
|
|
||||||
|
main() {
|
||||||
|
check_dependency "jq"
|
||||||
|
check_dependency "uglifyjs"
|
||||||
|
check_dependency "curl"
|
||||||
|
check_dependency "git"
|
||||||
|
check_dependency "sed"
|
||||||
|
check_dependency "awk"
|
||||||
|
check_dependency "md5sum"
|
||||||
|
|
||||||
|
local default_branch=$(git remote show origin | sed -n '/HEAD branch/s/.*: //p')
|
||||||
|
local current_branch=$(git rev-parse --abbrev-ref HEAD)
|
||||||
|
if [ "$current_branch" != "$default_branch" ]; then
|
||||||
|
exit_with_message "Not on $default_branch branch. Switch to $default_branch before running this script."
|
||||||
|
fi
|
||||||
|
|
||||||
|
if ! git diff --cached --quiet; then
|
||||||
|
exit_with_message "There are staged changes. Unstage them before running this script."
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Updating local repository…"
|
||||||
|
git fetch origin
|
||||||
|
local git_status=$(git status -uno)
|
||||||
|
if echo "$git_status" | grep -q "Your branch is behind"; then
|
||||||
|
exit_with_message "Your local branch is behind the remote. Pull the latest changes before running this script."
|
||||||
|
elif echo "$git_status" | grep -q "Your branch is ahead"; then
|
||||||
|
echo "Your local branch is ahead of the remote. Checking if local changes can be pushed…"
|
||||||
|
if ! git push --dry-run &> /dev/null; then
|
||||||
|
exit_with_message "Unable to push local changes. Resolve any conflicts before running this script."
|
||||||
|
fi
|
||||||
|
elif ! echo "$git_status" | grep -q "Your branch is up to date"; then
|
||||||
|
exit_with_message "Unable to determine if branch is up to date. Check your git status manually."
|
||||||
|
fi
|
||||||
|
echo "Local repository is ready."
|
||||||
|
|
||||||
|
upgrade_mermaid
|
||||||
|
}
|
||||||
|
|
||||||
|
main "$@"
|
Loading…
Reference in New Issue