-
Notifications
You must be signed in to change notification settings - Fork 3
/
publish.sh
154 lines (135 loc) · 3.47 KB
/
publish.sh
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
142
143
144
145
146
147
148
149
150
151
152
153
154
#!usr/bin/env
# ------------------ Variables ------------------#
PKGM="pnpm"
GHP_BRANCH="gh-pages"
UPSTREAM_NM="ghupstream"
REPO_URL="https://github.com/Itz-fork/itz-fork.github.io.git"
NO_KEEP_GIT=false
BUILD_START=$(date +%Y-%m-%d-%H-%M-%S)
COMMIT_MSG="build ${BUILD_START}"
FLAGS=""
# Constants, don't change em
CURDIR="./${PWD##*/}/."
TMPDIR="./pages_build/"
# ------------------ Colors codes ------------------#
WHITE="\033[1;37m"
CYAN="\033[1;36m"
YELLOW="\033[1;93m"
RED="\033[1;31m"
RESET="\033[0m"
#----------------- Argument parser -----------------#
while [[ $# -gt 0 ]]; do
case "${1}" in
-f|--flags)
FLAGS="$2"
shift
shift
;;
-pm|--pkgmn)
PKGM="$2"
shift
shift
;;
-m|--msg)
COMMIT_MSG="$2"
shift
shift
;;
-b|--branch)
GHP_BRANCH="$2"
shift
shift
;;
-nk|--no-keep)
NO_KEEP_GIT=true
shift
shift
;;
-h|--help)
echo -e "${YELLOW}
Usage:
bash publish
Arguments:
-f|--flags - Custom flags to pass on to parcel in double quotes. Defaults to \"\"
-pm|--pkgmn - Your package manager. Defaults to 'pnpm'
-m|--msg - Commit messsage. Defaults to 'build <timestamp>'
-b|--branch - Branch name. Defaults to 'gh-pages'
-nk|--no-keep - Resets git history for build branch if passed. Not recommended
-h|--help - Shows this message
${RESET}"
exit 1
esac
done
# ------------------ Output functions ------------------#
say_sh() {
echo -e "${CYAN}$1${RESET}\n"
}
info_sh() {
echo -e " ${WHITE}$1${RESET}\n"
}
warn_sh() {
echo -e "${RED}WARNING !\n $1${RESET}\n"
if [ $1 = -ex ]; then
exit 1
fi
}
# ------------------ Builders ------------------#
setup_env() {
say_sh "> Setting up build environment..."
cd ..
mkdir $TMPDIR
cp -a $CURDIR $TMPDIR
cd $TMPDIR
}
build_site() {
say_sh "> Creating build branch - ${GHP_BRANCH}"
git checkout $GHP_BRANCH &> /dev/null || git checkout -b $GHP_BRANCH
say_sh "> Building site..."
if [ ! -d "node_modules" ]; then
warn_sh "node_modules folder doesn't exist"
info_sh "> Installing dependencies..."
$PKGM install
fi
$PKGM build $FLAGS
}
pre_pub() {
say_sh "> Preparing to publish..."
mkdir nodel
mv dist nodel
if [ $NO_KEEP_GIT = false ]; then
mv .git nodel
fi
find ./ -mindepth 1 ! -regex '^./nodel\(/.*\)?' -delete
mv nodel/dist/* ./ || warn_sh "Unable to find build dir. Aborting..." -ex
mv nodel/.git ./ || warn_sh ".git directory was deleted for this build. Don't use '-nk' to avoid this"
rm -rf nodel
}
gh_publish() {
say_sh "> Publishing your branch to ${REPO_URL}"
if [ ! -d ".git" ]; then
warn_sh "Git folder doesn't exist"
info_sh "> Initializing a new project..."
git init
fi
git add . &> /dev/null
git commit -m "${COMMIT_MSG}" &> /dev/null
git checkout $GHP_BRANCH &> /dev/null || git checkout -b $GHP_BRANCH &> /dev/null
git remote -v
git remote add "${UPSTREAM_NM}" "${REPO_URL}"
git push -u "${UPSTREAM_NM}" "${GHP_BRANCH}" --force
}
cleanup() {
say_sh "> Switching back to working dir"
cd ..
rm -rf $TMPDIR
cd $CURDIR
}
main() {
setup_env
build_site
pre_pub
gh_publish
cleanup
say_sh "> Done"
}
main