Skip to content

Commit 6209d4a

Browse files
committed
push web-version script
1 parent c025be4 commit 6209d4a

1 file changed

Lines changed: 117 additions & 0 deletions

File tree

build_and_push.sh

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
DIST_DIR="${DIST_DIR:-dist}"
5+
S3_BUCKET="${S3_BUCKET:-curve-fit.hexq.ru}"
6+
S3_PREFIX="${S3_PREFIX:-}"
7+
S3_PREFIX="${S3_PREFIX#/}"
8+
S3_PREFIX="${S3_PREFIX%/}"
9+
10+
require_command() {
11+
if ! command -v "$1" >/dev/null 2>&1; then
12+
echo "Error: required command '$1' is not available in PATH." >&2
13+
exit 1
14+
fi
15+
}
16+
17+
content_type_for() {
18+
local file_path="$1"
19+
case "$file_path" in
20+
*.html) echo "text/html; charset=utf-8" ;;
21+
*.js) echo "text/javascript; charset=utf-8" ;;
22+
*.css) echo "text/css; charset=utf-8" ;;
23+
*.wasm) echo "application/wasm" ;;
24+
*.svg) echo "image/svg+xml" ;;
25+
*.png) echo "image/png" ;;
26+
*.jpg | *.jpeg) echo "image/jpeg" ;;
27+
*.webp) echo "image/webp" ;;
28+
*.json) echo "application/json" ;;
29+
*.txt) echo "text/plain; charset=utf-8" ;;
30+
*.ico) echo "image/x-icon" ;;
31+
*) echo "application/octet-stream" ;;
32+
esac
33+
}
34+
35+
target_base_uri() {
36+
if [[ -n "$S3_PREFIX" ]]; then
37+
echo "s3://${S3_BUCKET}/${S3_PREFIX}"
38+
else
39+
echo "s3://${S3_BUCKET}"
40+
fi
41+
}
42+
43+
upload_file() {
44+
local src_path="$1"
45+
local rel_path="$2"
46+
local object_key="$rel_path"
47+
local content_type
48+
local target_uri
49+
50+
if [[ -n "$S3_PREFIX" ]]; then
51+
object_key="${S3_PREFIX}/${rel_path}"
52+
fi
53+
54+
content_type="$(content_type_for "$rel_path")"
55+
target_uri="s3://${S3_BUCKET}/${object_key}"
56+
57+
if [[ "$rel_path" == *.wasm ]]; then
58+
local gzip_tmp="${src_path}.gz"
59+
gzip -9 -c "$src_path" >"$gzip_tmp"
60+
yc storage s3 cp "$gzip_tmp" "$target_uri" \
61+
--content-type "$content_type" \
62+
--content-encoding gzip
63+
rm -f "$gzip_tmp"
64+
else
65+
yc storage s3 cp "$src_path" "$target_uri" --content-type "$content_type"
66+
fi
67+
68+
echo "Uploaded: ${rel_path} -> ${target_uri}"
69+
}
70+
71+
require_command trunk
72+
require_command yc
73+
require_command gzip
74+
require_command find
75+
76+
echo "Building web release with trunk..."
77+
trunk build --release
78+
79+
if [[ ! -d "$DIST_DIR" ]]; then
80+
echo "Error: dist directory '$DIST_DIR' does not exist after build." >&2
81+
exit 1
82+
fi
83+
84+
mapfile -d '' dist_files < <(find "$DIST_DIR" -type f -print0 | sort -z)
85+
if [[ "${#dist_files[@]}" -eq 0 ]]; then
86+
echo "Error: no files found in '$DIST_DIR'." >&2
87+
exit 1
88+
fi
89+
90+
regular_files=()
91+
html_files=()
92+
93+
for src_path in "${dist_files[@]}"; do
94+
rel_path="${src_path#"$DIST_DIR"/}"
95+
if [[ "$rel_path" == *.html ]]; then
96+
html_files+=("$src_path")
97+
else
98+
regular_files+=("$src_path")
99+
fi
100+
done
101+
102+
TARGET_BASE_URI="$(target_base_uri)"
103+
echo "Cleaning remote objects at ${TARGET_BASE_URI}..."
104+
yc storage s3 rm "${TARGET_BASE_URI}" --recursive
105+
106+
echo "Uploading static assets to ${TARGET_BASE_URI}..."
107+
for src_path in "${regular_files[@]}"; do
108+
rel_path="${src_path#"$DIST_DIR"/}"
109+
upload_file "$src_path" "$rel_path"
110+
done
111+
112+
for src_path in "${html_files[@]}"; do
113+
rel_path="${src_path#"$DIST_DIR"/}"
114+
upload_file "$src_path" "$rel_path"
115+
done
116+
117+
echo "Publish complete."

0 commit comments

Comments
 (0)