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
|
|
|
|
|
2023-03-13 20:25:42 +03:00
|
|
|
# CLI Dependencies
|
|
|
|
CLI=("git" "npm")
|
|
|
|
|
2020-07-28 21:27:40 +03:00
|
|
|
ACTIONS_WORKFLOW=pages-deploy.yml
|
|
|
|
|
2023-03-13 20:25:42 +03:00
|
|
|
# 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
|
2023-06-28 08:49:13 +03:00
|
|
|
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."
|
|
|
|
}
|
|
|
|
|
2023-03-13 20:25:42 +03:00
|
|
|
# BSD and GNU compatible sed
|
|
|
|
_sedi() {
|
|
|
|
regex=$1
|
|
|
|
file=$2
|
|
|
|
sed -i.$TEMP_SUFFIX "$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
|
2022-12-02 17:50:38 +03:00
|
|
|
echo "Error: Commit unstaged files first, and then run this tool again."
|
2023-02-16 00:02:22 +03:00
|
|
|
exit 1
|
2021-09-28 16:34:07 +03:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-03-13 20:25:42 +03:00
|
|
|
_check_init() {
|
2020-07-28 21:27:40 +03:00
|
|
|
local _has_inited=false
|
|
|
|
|
2022-02-03 18:58:52 +03:00
|
|
|
if [[ ! -d .github ]]; then # using option `--no-gh`
|
|
|
|
_has_inited=true
|
|
|
|
else
|
|
|
|
if [[ -f .github/workflows/$ACTIONS_WORKFLOW ]]; then
|
|
|
|
# on BSD, the `wc` could contains blank
|
2023-02-16 00:02:22 +03:00
|
|
|
local _count
|
|
|
|
_count=$(find .github/workflows/ -type f -name "*.yml" | wc -l)
|
2022-02-03 18:58:52 +03:00
|
|
|
if [[ ${_count//[[:blank:]]/} == 1 ]]; then
|
|
|
|
_has_inited=true
|
2020-09-04 14:49:18 +03:00
|
|
|
fi
|
2020-07-28 21:27:40 +03:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2020-09-01 09:54:29 +03:00
|
|
|
if $_has_inited; then
|
2020-07-28 21:27:40 +03:00
|
|
|
echo "Already initialized."
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-03-13 20:25:42 +03:00
|
|
|
check_env() {
|
|
|
|
_check_cli
|
|
|
|
_check_status
|
|
|
|
_check_init
|
2022-12-27 20:11:27 +03:00
|
|
|
}
|
|
|
|
|
2023-04-16 21:40:25 +03:00
|
|
|
checkout_latest_release() {
|
|
|
|
hash=$(git log --grep="chore(release):" -1 --pretty="%H")
|
|
|
|
git reset --hard "$hash"
|
2023-03-17 19:49:26 +03:00
|
|
|
}
|
|
|
|
|
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
|
2021-09-29 18:14:59 +03:00
|
|
|
## Change the files of `.github`
|
2020-07-28 21:27:40 +03:00
|
|
|
mv .github/workflows/$ACTIONS_WORKFLOW.hook .
|
|
|
|
rm -rf .github
|
|
|
|
mkdir -p .github/workflows
|
|
|
|
mv ./${ACTIONS_WORKFLOW}.hook .github/workflows/${ACTIONS_WORKFLOW}
|
2021-09-28 16:34:07 +03:00
|
|
|
|
2021-09-29 18:14:59 +03:00
|
|
|
## Cleanup image settings in site config
|
2024-04-12 23:34:48 +03:00
|
|
|
_sedi "s/^cdn:.*/cdn:/;s/^avatar:.*/avatar:/" _config.yml
|
2020-07-28 21:27:40 +03:00
|
|
|
fi
|
|
|
|
|
2023-05-03 18:51:34 +03:00
|
|
|
# remove the other files
|
2022-02-03 18:58:52 +03:00
|
|
|
rm -rf _posts/*
|
2020-07-28 21:27:40 +03:00
|
|
|
|
2023-05-03 18:51:34 +03:00
|
|
|
# build assets
|
2023-03-13 20:25:42 +03:00
|
|
|
npm i && npm run build
|
2020-07-28 21:27:40 +03:00
|
|
|
|
2023-03-13 20:25:42 +03:00
|
|
|
# track the js output
|
2024-02-13 18:35:32 +03:00
|
|
|
_sedi "/^assets.*\/dist/d" .gitignore
|
2020-07-28 21:27:40 +03:00
|
|
|
}
|
|
|
|
|
2023-03-13 20:25:42 +03:00
|
|
|
commit() {
|
|
|
|
git add -A
|
|
|
|
git commit -m "chore: initialize the environment" -q
|
|
|
|
echo -e "\n[INFO] Initialization successful!\n"
|
|
|
|
}
|
2020-07-28 21:27:40 +03:00
|
|
|
|
2023-03-13 20:25:42 +03:00
|
|
|
main() {
|
|
|
|
check_env
|
2023-04-16 21:40:25 +03:00
|
|
|
checkout_latest_release
|
2023-03-13 20:25:42 +03:00
|
|
|
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
|
2022-02-03 18:58:52 +03:00
|
|
|
--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
|
|
|
|
|
2023-03-13 20:25:42 +03:00
|
|
|
main
|