-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscaffold.sh
More file actions
executable file
·148 lines (119 loc) · 2.7 KB
/
scaffold.sh
File metadata and controls
executable file
·148 lines (119 loc) · 2.7 KB
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
#!/usr/bin/env bash
set -e
set -a
for envfile in $(find . -type f \( -name ".env.local" -o -name ".env.development.local" \)); do
while IFS= read -r line; do
if [[ -z "$line" || "$line" == \#* ]]; then
continue
fi
line="${line%%#*}"
key="${line%%=*}"
val="${line#*=}"
val="${val%\"}"
val="${val#\"}"
export "$key=$val"
done < "$envfile"
done
set +a
# Check required env vars
required_vars=(
"CROCT_PROJECT_TEMPLATE"
"CROCT_WORKSPACE"
"CROCT_ORGANIZATION"
"CROCT_DEV_APP"
"CROCT_PROD_APP"
)
for var in "${required_vars[@]}"; do
if [ -z "${!var}" ]; then
echo "Environment variable '$var' is missing."
exit 1
fi
done
is_token_expired() {
local jwt="$1"
local payload_base64="${jwt#*.}"
payload_base64="${payload_base64%%.*}"
local remainder=$(( ${#payload_base64} % 4 ))
if [ $remainder -eq 2 ]; then
payload_base64="${payload_base64}=="
elif [ $remainder -eq 3 ]; then
payload_base64="${payload_base64}="
fi
local payload_json
payload_json="$(echo "$payload_base64" | tr '_-' '/+' | base64 -d 2>/dev/null)"
local exp
exp="$(echo "$payload_json" | sed -n 's/.*"exp":[[:space:]]*\([0-9]*\).*/\1/p')"
[ -z "$exp" ] && return 0
local now
now="$(date +%s)"
if [ "$now" -ge "$exp" ]; then
return 0
else
return 1
fi
}
find_page_folder() {
local path="$1"
local found=""
for dir in "$path"/*/; do
[[ ! -d "$dir" ]] && continue
dir="${dir%/}"
folder="${dir##*/}"
if [[ "$folder" != "api" && -n "$folder" ]]; then
found="$folder"
break
fi
done
if [[ -n "$found" ]]; then
echo "$found"
return 0
else
return 1
fi
}
create_next_config() {
local path="$1"
local folder
local config_name
if folder=$(find_page_folder "$path"); then
if [[ -f next.config.ts ]]; then
config_name="next.config.ts"
else
config_name="next.config.js"
fi
cat > "$config_name" <<EOF
export default {
redirects: () => Promise.resolve([
{
source: '/',
destination: '/$folder',
permanent: true,
},
]),
};
EOF
fi
}
rm -rf build
mkdir build
cd build
COMMAND="npx --yes croct@latest --stateless --no-interaction --dnd use $CROCT_PROJECT_TEMPLATE"
if is_token_expired "${CROCT_CLI_TOKEN:-}"; then
if [ -n "${CROCT_API_KEY:-}" ]; then
env -u CROCT_TOKEN \
CROCT_API_KEY="$CROCT_API_KEY" \
CROCT_SKIP_API_KEY_SETUP=true \
bash -c "$COMMAND"
else
bash -c "npx --yes croct@latest --dnd use $CROCT_PROJECT_TEMPLATE"
fi
else
env -u CROCT_API_KEY \
CROCT_TOKEN="$CROCT_CLI_TOKEN" \
CROCT_SKIP_API_KEY_SETUP=true \
bash -c "$COMMAND"
fi
cd ..
cp -rf build/*/* .
rm -rf build
create_next_config "./app"