Skip to content

Commit 72d93b1

Browse files
committed
build: improve init tool
1 parent e09831b commit 72d93b1

File tree

1 file changed

+16
-26
lines changed

1 file changed

+16
-26
lines changed

tools/init

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ CLI=("git" "npm")
99

1010
ACTIONS_WORKFLOW=pages-deploy.yml
1111

12+
RELEASE_HASH=$(git log --grep="chore(release):" -1 --pretty="%H")
13+
1214
# temporary file suffixes that make `sed -i` compatible with BSD and Linux
1315
TEMP_SUFFIX="to-delete"
1416

@@ -28,7 +30,7 @@ help() {
2830
_sedi() {
2931
regex=$1
3032
file=$2
31-
sed -i.$TEMP_SUFFIX "$regex" "$file"
33+
sed -i.$TEMP_SUFFIX -E "$regex" "$file"
3234
rm -f "$file".$TEMP_SUFFIX
3335
}
3436

@@ -50,22 +52,7 @@ _check_status() {
5052
}
5153

5254
_check_init() {
53-
local _has_inited=false
54-
55-
if [[ ! -d .github ]]; then # using option `--no-gh`
56-
_has_inited=true
57-
else
58-
if [[ -f .github/workflows/$ACTIONS_WORKFLOW ]]; then
59-
# on BSD, the `wc` could contains blank
60-
local _count
61-
_count=$(find .github/workflows/ -type f -name "*.yml" | wc -l)
62-
if [[ ${_count//[[:blank:]]/} == 1 ]]; then
63-
_has_inited=true
64-
fi
65-
fi
66-
fi
67-
68-
if $_has_inited; then
55+
if [[ $(git rev-parse HEAD^1) == "$RELEASE_HASH" ]]; then
6956
echo "Already initialized."
7057
exit 0
7158
fi
@@ -77,22 +64,25 @@ check_env() {
7764
_check_init
7865
}
7966

80-
checkout_latest_release() {
81-
hash=$(git log --grep="chore(release):" -1 --pretty="%H")
82-
git reset --hard "$hash"
67+
reset_latest() {
68+
git reset --hard "$RELEASE_HASH"
69+
git clean -fd
70+
git submodule update --init --recursive
8371
}
8472

8573
init_files() {
8674
if $_no_gh; then
8775
rm -rf .github
8876
else
89-
## Change the files of `.github`
90-
mv .github/workflows/starter/$ACTIONS_WORKFLOW .
77+
## Change the files of `.github/`
78+
temp="$(mktemp -d)"
79+
find .github/workflows -type f -name "*$ACTIONS_WORKFLOW*" -exec mv {} "$temp/$ACTIONS_WORKFLOW" \;
9180
rm -rf .github && mkdir -p .github/workflows
92-
mv ./$ACTIONS_WORKFLOW .github/workflows/${ACTIONS_WORKFLOW}
81+
mv "$temp/$ACTIONS_WORKFLOW" .github/workflows/"$ACTIONS_WORKFLOW"
82+
rm -rf "$temp"
9383

9484
## Cleanup image settings in site config
95-
_sedi "s/^cdn:.*/cdn:/;s/^avatar:.*/avatar:/" _config.yml
85+
_sedi "s/(^.*cdn:).*/\1/;s/(^avatar:).*/\1/" _config.yml
9686
fi
9787

9888
# remove the other files
@@ -108,12 +98,12 @@ init_files() {
10898
commit() {
10999
git add -A
110100
git commit -m "chore: initialize the environment" -q
111-
echo -e "\n[INFO] Initialization successful!\n"
101+
echo -e "\n> Initialization successful!\n"
112102
}
113103

114104
main() {
115105
check_env
116-
checkout_latest_release
106+
reset_latest
117107
init_files
118108
commit
119109
}

0 commit comments

Comments
 (0)