44
55set -eu
66
7+ # CLI Dependencies
8+ CLI=(" git" " npm" )
9+
710ACTIONS_WORKFLOW=pages-deploy.yml
811
9- TEMP_SUFFIX=" to-delete" # temporary file suffixes that make `sed -i` compatible with BSD and Linux
12+ # temporary file suffixes that make `sed -i` compatible with BSD and Linux
13+ TEMP_SUFFIX=" to-delete"
14+
15+ _no_gh=false
1016
1117help () {
1218 echo " Usage:"
@@ -18,14 +24,32 @@ help() {
1824 echo " -h, --help Print this help information."
1925}
2026
21- check_status () {
27+ # BSD and GNU compatible sed
28+ _sedi () {
29+ regex=$1
30+ file=$2
31+ sed -i.$TEMP_SUFFIX " $regex " " $file "
32+ rm -f " $file " .$TEMP_SUFFIX
33+ }
34+
35+ _check_cli () {
36+ for i in " ${! CLI[@]} " ; do
37+ cli=" ${CLI[$i]} "
38+ if ! command -v " $cli " & > /dev/null; then
39+ echo " Command '$cli ' not found! Hint: you should install it."
40+ exit 1
41+ fi
42+ done
43+ }
44+
45+ _check_status () {
2246 if [[ -n $( git status . -s) ]]; then
2347 echo " Error: Commit unstaged files first, and then run this tool again."
2448 exit 1
2549 fi
2650}
2751
28- check_init () {
52+ _check_init () {
2953 local _has_inited=false
3054
3155 if [[ ! -d .github ]]; then # using option `--no-gh`
@@ -47,9 +71,10 @@ check_init() {
4771 fi
4872}
4973
50- checkout_latest_tag () {
51- tag=$( git describe --tags " $( git rev-list --tags --max-count=1) " )
52- git reset --hard " $tag "
74+ check_env () {
75+ _check_cli
76+ _check_status
77+ _check_init
5378}
5479
5580init_files () {
@@ -63,25 +88,30 @@ init_files() {
6388 mv ./${ACTIONS_WORKFLOW} .hook .github/workflows/${ACTIONS_WORKFLOW}
6489
6590 # # Cleanup image settings in site config
66- sed -i.$TEMP_SUFFIX " s/^img_cdn:.*/img_cdn:/;s/^avatar:.*/avatar:/" _config.yml
67- rm -f _config.yml.$TEMP_SUFFIX
91+ _sedi " s/^img_cdn:.*/img_cdn:/;s/^avatar:.*/avatar:/" _config.yml
6892 fi
6993
7094 # remove the other fies
7195 rm -rf _posts/*
7296
73- # save changes
74- git add -A
75- git commit -m " chore: initialize the environment" -q
97+ # build assest
98+ npm i && npm run build
7699
77- echo " [INFO] Initialization successful!"
100+ # track the js output
101+ _sedi " /^assets.*\/dist/d" .gitignore
78102}
79103
80- check_status
81-
82- check_init
104+ commit () {
105+ git add -A
106+ git commit -m " chore: initialize the environment" -q
107+ echo -e " \n[INFO] Initialization successful!\n"
108+ }
83109
84- _no_gh=false
110+ main () {
111+ check_env
112+ init_files
113+ commit
114+ }
85115
86116while (( $# )) ; do
87117 opt=" $1 "
@@ -102,6 +132,4 @@ while (($#)); do
102132 esac
103133done
104134
105- checkout_latest_tag
106-
107- init_files
135+ main
0 commit comments