-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.sh
212 lines (188 loc) · 6.34 KB
/
script.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# find . -print0 | while IFS= read -r -d '' file
# https://askubuntu.com/questions/266179/how-to-exclude-ignore-hidden-files-and-directories-in-a-wildcard-embedded-find
TEMPLATE=templates/shell.html
INFILE=_index.html
OUTFILE=index.html
HTML_PREAMBLE="<!DOCTYPE html>\n"
# save $1 as $2
delete_file () {
rm $(find $1/* -type f -name "${2}" -print)
}
# inject $1 into $2 to create $3
# TODO put in /bash
inject () {
cat $2 | templated_string_p7Rwrz6=$(cat $1) envsubst > $3
}
#https://stackoverflow.com/questions/40993488/convert-markdown-links-to-html-with-pandoc
md_to_html() {
# cat $TEMPLATE | templated_string_p7Rwrz6=$(cat $1) envsubst | pandoc -f markdown -o $2
# echo "${HTML_PREAMBLE}" $(cat $TEMPLATE | templated_string_p7Rwrz6=$(cat $1) envsubst | pandoc -f markdown ) > $2
# echo "${HTML_PREAMBLE}" $(cat $TEMPLATE | templated_string_p7Rwrz6=$(cat $1) envsubst | pandoc -f markdown -t html5 --lua-filter=links-to-html.lua ) > $2
{ cat templates/start.html; cat $1 | pandoc -f markdown; cat templates/end.html; } > $2
}
html_to_html() {
{ cat templates/start.html; cat $1; cat templates/end.html; } > $2
}
# Transform _index.html into index.html
build_html () {
find $1 -type d \( -path ./node_modules -o -path ./vendor \) -prune -o -not -path '*/\.*' -print0 | while IFS= read -r -d '' FILE
do
if [ ! -d "${FILE}" ] ; then
BASENAME="$(basename ${FILE})";
if [ $BASENAME = $INFILE ]; then
# create file
TARGET="$(dirname "${FILE}")/${OUTFILE}"
cp $FILE $TARGET
fi
fi
done
}
run_popquiz () {
find $1 -type f -name "popquiz.test.mjs" -print0 | while IFS= read -r -d '' FILE
do
if [ ! -d "${FILE}" ]; then
deno run $FILE
fi
done
}
build_indicies() {
local TOP=$1
local DEPTH=$2
local STR=""
if [ $DEPTH -gt 0 ]; then
local HTML=""
for entry in "$TOP"/*
do
if [ -d "${entry}" ]; then
local BASE="$(basename ${entry})"
STR="${STR}\n${BASE}"
HTML="${HTML}<li><a href=\"${BASE}\">${BASE}</a></li>"
build_indicies $entry $((DEPTH-1))
fi
done
VERSION=$(echo $STR | sort -V | tail -1)
(echo "$VERSION" | grep -Eq "(.*)@(\d+\.\d+\.\d+)$") &&\
HTML="<li><a href=\"${VERSION}\">(latest)</a></li>${HTML}"
#write HTML file
HTML="<ul>${HTML}</ul>"
{ cat templates/start.html; echo $HTML; cat templates/end.html; } > "${TOP}/index.html"
fi
}
# given a list of simversioned directoreies, print the highes
latest_version() {
TOP=$1
rm -rf "${TOP}/latest"
STR=""
for entry in "$TOP"/*
do
if [ -d "${entry}" ] ; then
BASE="$(basename ${entry})"
STR="${STR}\n${BASE}"
fi
done
# write HTML file
VERSION=$(echo $STR | sort -V | tail -1)
DIR="${TOP}/${VERSION}"
NAME=$(basename $TOP)
FULL="${NAME}@${VERSION}"
echo "The latest version of ${NAME} is ${VERSION} (${DIR})"
echo "Copying latest versoion of ${NAME}} (${VERSION}) to /latest "
# test for documentation
if [ -f "${DIR}/${INFILE}" ]; then
echo "✅ ${INFILE} found!"
echo "🏭 Building ${OUTFILE} from ${INFILE}"
html_to_html "${DIR}/${INFILE}" "${DIR}/${OUTFILE}"
echo "🏭 ${OUTFILE} built from HTML"
elif [ -f "${DIR}/readme.md" ]; then
echo "🏭 Building ${OUTFILE} from readme.md"
md_to_html "${DIR}/readme.md" "${DIR}/${OUTFILE}"
echo "🏭 ${OUTFILE} built from Markdown"
fi
# test for test compilation
if [ -f "${DIR}/tester.test.mjs" ]; then
echo "✅ tester.test.mjs found!"
echo "🏭 Creating test.html using tester.test.mjs"
echo "<html><head><script type=\"module\" src=\"./tester.test.mjs\"></script></head><body><h1>Test for ${NAME}</h1><h2>Open console for logs.</h2></body></html>" > "${DIR}/test.html"
echo "🏭 tests.html built importing from Javascript"
fi
# test for typescript copilation
if [ -f "${DIR}/index.ts" ]; then
echo "✅ index.ts found!"
echo "🏭 Building ${DIR}/index.mjs from index.ts"
# deno bundle "${DIR}/index.ts" "${DIR}/index.mjs"
# echo "🏭 index.mjs built from Typescript"
fi
if [ -f "${DIR}/index.html" ]; then
echo "✅ index.html found!"
echo "🏭 Adding ${DIR}/index.json from index."
echo "{}" > "${DIR}/index.json";
CONTENT=$(jq ".title = \"${NAME}\"" "${DIR}/index.json")
echo "${CONTENT}" > "${DIR}/index.json"
CONTENT=$(jq ".url = \"${DIR}\"" "${DIR}/index.json")
echo "${CONTENT}" > "${DIR}/index.json"
CONTENT=$(cat ${DIR}/index.html)
# CONTENT=$(echo "${CONTENT}" | sed 's/&/\&/g; s/</\</g; s/>/\>/g; s/"/\"/g; s/'"'"'/\'/g' | tr '\n' ' ')
# CONTENT=$(echo "${CONTENT}" | sed 's/<[^>]*>/\n/g' | tr '\n' ' ')
CONTENT=$(echo $CONTENT | awk '{gsub(/<[^>]*>/,"\n"); print }' | tr '\n' ' ')
CONTENT=$(jq ".content = \"${CONTENT}\"" "${DIR}/index.json" )
echo "${CONTENT}" > "${DIR}/index.json"
fi
# test for publication
if [ -f "${DIR}/readme.md" ]; then
echo "❓ readme found... maybe we can publish? (1 of 5)"
if [ -f "${DIR}/package.json" ]; then
echo "❓ package found (2 of 5)"
PUBLISH=$(cat "${DIR}/package.json" | jq -r '.publish')
if [ "$PUBLISH" == true ]; then
echo "❓ package.publsh=true (3 of 5)"
P_NAME=$(cat "${DIR}/package.json" | jq -r '.name')
if [ "$P_NAME" == "$NAME" ]; then
echo "❓ package.name is \"${NAME}\" (4 of 5)"
P_VERSION=$(cat "${DIR}/package.json" | jq -r '.version')
if [ "$P_VERSION" == "$VERSION" ]; then
echo "❓ package.version is "${VERSION}" (5 of 5)"
echo "✅ we can publish ${FULL}!"
echo "🚀 Publishing ${FULL}"
# npm publish "${DIR}"
# echo "🚀 ${FULL} published!"
fi
fi
fi
fi
fi
}
latest_versions() {
echo $1
for entry in "$1"/*
do
if [ -d "${entry}" ] ; then
echo "---"
latest_version "${entry}"
fi
done
}
build_search_index (){
TEXT=$(jq -s . $(find $1/* -type f -name "index.json" -print))
rm $(find $1/* -type f -name "index.json" -print)
echo $TEXT > index.json
}
DIR="${2:-.}"
DEPTH="${3:-2}"
case "$1" in
"delete_file") delete_file $DIR $3
;;
"build_indicies") build_indicies $DIR $DEPTH
;;
"latest_versions") latest_versions $DIR
;;
"run_popquiz") run_popquiz $DIR
;;
"inject") inject $2 $3 $4
;;
"md_to_html") md_to_html $2 $3
;;
"html_to_html") html_to_html $2 $3
;;
"build_search_index") build_search_index $2
;;
esac