forked from cotes2020/jekyll-theme-chirpy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit
More file actions
executable file
·141 lines (118 loc) · 2.53 KB
/
Copy pathinit
File metadata and controls
executable file
·141 lines (118 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env bash
#
# Init the environment for new user.
set -eu
# CLI Dependencies
CLI=("git" "npm")
ACTIONS_WORKFLOW=pages-deploy.yml
# temporary file suffixes that make `sed -i` compatible with BSD and Linux
TEMP_SUFFIX="to-delete"
_no_gh=false
help() {
echo "Usage:"
echo
echo " bash /path/to/init.sh [options]"
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
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() {
if [[ -n $(git status . -s) ]]; then
echo "Error: Commit unstaged files first, and then run this tool again."
exit 1
fi
}
_check_init() {
local _has_inited=false
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
local _count
_count=$(find .github/workflows/ -type f -name "*.yml" | wc -l)
if [[ ${_count//[[:blank:]]/} == 1 ]]; then
_has_inited=true
fi
fi
fi
if $_has_inited; then
echo "Already initialized."
exit 0
fi
}
check_env() {
_check_cli
_check_status
_check_init
}
checkout_latest_release() {
hash=$(git log --grep="chore(release):" -1 --pretty="%H")
git reset --hard "$hash"
}
init_files() {
if $_no_gh; then
rm -rf .github
else
## Change the files of `.github`
mv .github/workflows/$ACTIONS_WORKFLOW.hook .
rm -rf .github
mkdir -p .github/workflows
mv ./${ACTIONS_WORKFLOW}.hook .github/workflows/${ACTIONS_WORKFLOW}
## Cleanup image settings in site config
_sedi "s/^img_cdn:.*/img_cdn:/;s/^avatar:.*/avatar:/" _config.yml
fi
# remove the other files
rm -rf _posts/*
# build assets
npm i && npm run build
# track the js output
_sedi "/^assets.*\/dist/d" .gitignore
}
commit() {
git add -A
git commit -m "chore: initialize the environment" -q
echo -e "\n[INFO] Initialization successful!\n"
}
main() {
check_env
checkout_latest_release
init_files
commit
}
while (($#)); do
opt="$1"
case $opt in
--no-gh)
_no_gh=true
shift
;;
-h | --help)
help
exit 0
;;
*)
# unknown option
help
exit 1
;;
esac
done
main