Skip to content

Commit 2410cd9

Browse files
committed
Create init.sh
1 parent 9593d6b commit 2410cd9

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed

tools/init.sh

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Init the environment for new user.
4+
5+
set -eu
6+
7+
# CLI Dependencies
8+
CLI=("git" "npm")
9+
10+
ACTIONS_WORKFLOW=pages-deploy.yml
11+
12+
RELEASE_HASH=$(git log --grep="chore(release):" -1 --pretty="%H")
13+
14+
# temporary file suffixes that make `sed -i` compatible with BSD and Linux
15+
TEMP_SUFFIX="to-delete"
16+
17+
_no_gh=false
18+
19+
help() {
20+
echo "Usage:"
21+
echo
22+
echo " bash /path/to/init [options]"
23+
echo
24+
echo "Options:"
25+
echo " --no-gh Do not deploy to Github."
26+
echo " -h, --help Print this help information."
27+
}
28+
29+
# BSD and GNU compatible sed
30+
_sedi() {
31+
regex=$1
32+
file=$2
33+
sed -i.$TEMP_SUFFIX -E "$regex" "$file"
34+
rm -f "$file".$TEMP_SUFFIX
35+
}
36+
37+
_check_cli() {
38+
for i in "${!CLI[@]}"; do
39+
cli="${CLI[$i]}"
40+
if ! command -v "$cli" &>/dev/null; then
41+
echo "Command '$cli' not found! Hint: you should install it."
42+
exit 1
43+
fi
44+
done
45+
}
46+
47+
_check_status() {
48+
if [[ -n $(git status . -s) ]]; then
49+
echo "Error: Commit unstaged files first, and then run this tool again."
50+
exit 1
51+
fi
52+
}
53+
54+
_check_init() {
55+
if [[ $(git rev-parse HEAD^1) == "$RELEASE_HASH" ]]; then
56+
echo "Already initialized."
57+
exit 0
58+
fi
59+
}
60+
61+
check_env() {
62+
_check_cli
63+
_check_status
64+
_check_init
65+
}
66+
67+
reset_latest() {
68+
git reset --hard "$RELEASE_HASH"
69+
git clean -fd
70+
git submodule update --init --recursive
71+
}
72+
73+
init_files() {
74+
if $_no_gh; then
75+
rm -rf .github
76+
else
77+
## Change the files of `.github/`
78+
temp="$(mktemp -d)"
79+
find .github/workflows -type f -name "*$ACTIONS_WORKFLOW*" -exec mv {} "$temp/$ACTIONS_WORKFLOW" \;
80+
rm -rf .github && mkdir -p .github/workflows
81+
mv "$temp/$ACTIONS_WORKFLOW" .github/workflows/"$ACTIONS_WORKFLOW"
82+
rm -rf "$temp"
83+
fi
84+
85+
# Cleanup image settings in site config
86+
_sedi "s/(^timezone:).*/\1/;s/(^.*cdn:).*/\1/;s/(^avatar:).*/\1/" _config.yml
87+
88+
# remove the other files
89+
rm -rf tools/init.sh tools/release.sh _posts/*
90+
91+
# build assets
92+
npm i && npm run build
93+
94+
# track the CSS/JS output
95+
_sedi "/^_sass\/vendors/d" .gitignore
96+
_sedi "/^assets\/js\/dist/d" .gitignore
97+
}
98+
99+
commit() {
100+
git add -A
101+
git commit -m "chore: initialize the environment" -q
102+
echo -e "\n> Initialization successful!\n"
103+
}
104+
105+
main() {
106+
check_env
107+
reset_latest
108+
init_files
109+
commit
110+
}
111+
112+
while (($#)); do
113+
opt="$1"
114+
case $opt in
115+
--no-gh)
116+
_no_gh=true
117+
shift
118+
;;
119+
-h | --help)
120+
help
121+
exit 0
122+
;;
123+
*)
124+
# unknown option
125+
help
126+
exit 1
127+
;;
128+
esac
129+
done
130+
131+
main

0 commit comments

Comments
 (0)