web/tools/init.sh

131 lines
2.4 KiB
Bash
Raw Normal View History

2021-01-25 01:20:51 +03:00
#!/usr/bin/env bash
2020-07-28 21:27:40 +03:00
#
2023-02-28 19:48:25 +03:00
# Init the environment for new user.
2020-07-28 21:27:40 +03:00
set -eu
# CLI Dependencies
CLI=("git" "npm")
2020-07-28 21:27:40 +03:00
ACTIONS_WORKFLOW=pages-deploy.yml
2024-04-27 21:51:16 +03:00
RELEASE_HASH=$(git log --grep="chore(release):" -1 --pretty="%H")
# temporary file suffixes that make `sed -i` compatible with BSD and Linux
TEMP_SUFFIX="to-delete"
_no_gh=false
2021-09-29 20:57:28 +03:00
2020-07-28 21:27:40 +03:00
help() {
echo "Usage:"
echo
echo " bash /path/to/init [options]"
2020-07-28 21:27:40 +03:00
echo
echo "Options:"
echo " --no-gh Do not deploy to Github."
echo " -h, --help Print this help information."
}
# BSD and GNU compatible sed
_sedi() {
regex=$1
file=$2
2024-04-27 21:51:16 +03:00
sed -i.$TEMP_SUFFIX -E "$regex" "$file"
rm -f "$file".$TEMP_SUFFIX
}
_check_cli() {
for i in "${!CLI[@]}"; do
cli="${CLI[$i]}"
if ! command -v "$cli" &>/dev/null; then
echo "Command '$cli' not found! Hint: you should install it."
exit 1
fi
done
}
_check_status() {
2021-09-28 16:34:07 +03:00
if [[ -n $(git status . -s) ]]; then
echo "Error: Commit unstaged files first, and then run this tool again."
exit 1
2021-09-28 16:34:07 +03:00
fi
}
_check_init() {
2024-04-27 21:51:16 +03:00
if [[ $(git rev-parse HEAD^1) == "$RELEASE_HASH" ]]; then
2020-07-28 21:27:40 +03:00
echo "Already initialized."
exit 0
fi
}
check_env() {
_check_cli
_check_status
_check_init
}
2024-04-27 21:51:16 +03:00
reset_latest() {
git reset --hard "$RELEASE_HASH"
git clean -fd
git submodule update --init --recursive
}
2020-07-28 21:27:40 +03:00
init_files() {
2020-09-01 09:54:29 +03:00
if $_no_gh; then
2020-07-28 21:27:40 +03:00
rm -rf .github
else
2024-04-27 21:51:16 +03:00
## Change the files of `.github/`
temp="$(mktemp -d)"
find .github/workflows -type f -name "*$ACTIONS_WORKFLOW*" -exec mv {} "$temp/$ACTIONS_WORKFLOW" \;
rm -rf .github && mkdir -p .github/workflows
2024-04-27 21:51:16 +03:00
mv "$temp/$ACTIONS_WORKFLOW" .github/workflows/"$ACTIONS_WORKFLOW"
rm -rf "$temp"
2020-07-28 21:27:40 +03:00
fi
2024-08-13 08:50:40 +03:00
# Cleanup image settings in site config
_sedi "s/(^timezone:).*/\1/;s/(^.*cdn:).*/\1/;s/(^avatar:).*/\1/" _config.yml
2023-05-03 18:51:34 +03:00
# remove the other files
2024-08-13 08:50:40 +03:00
rm -rf tools/init.sh tools/release.sh _posts/*
2020-07-28 21:27:40 +03:00
2023-05-03 18:51:34 +03:00
# build assets
npm i && npm run build
2020-07-28 21:27:40 +03:00
# track the CSS/JS output
_sedi "/.*\/dist$/d;/^_app$/d" .gitignore
2020-07-28 21:27:40 +03:00
}
commit() {
git add -A
git commit -m "chore: initialize the environment" -q
2024-04-27 21:51:16 +03:00
echo -e "\n> Initialization successful!\n"
}
2020-07-28 21:27:40 +03:00
main() {
check_env
2024-04-27 21:51:16 +03:00
reset_latest
init_files
commit
}
2020-07-28 21:27:40 +03:00
2020-09-01 09:54:29 +03:00
while (($#)); do
2020-07-28 21:27:40 +03:00
opt="$1"
case $opt in
--no-gh)
_no_gh=true
shift
;;
-h | --help)
help
exit 0
;;
*)
# unknown option
help
exit 1
;;
2020-07-28 21:27:40 +03:00
esac
done
main