-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.sh
More file actions
executable file
·264 lines (222 loc) · 7.1 KB
/
dev.sh
File metadata and controls
executable file
·264 lines (222 loc) · 7.1 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/bin/bash
# colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # no color
# get root directory
ROOT_DIR=$(pwd)
# function to log with colors
log() {
echo -e "${BLUE}[INFO]${NC} $1"
}
error() {
echo -e "${RED}[ERROR]${NC} $1"
exit 1
}
warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
# function to check environment files
check_env_files() {
log "Checking environment files..."
# function to check env files against their examples
check_single_env() {
local env_file="$1"
local example_file="$2"
local folder_name="$3"
# check if .env exists
if [ ! -f "$env_file" ]; then
error "$folder_name/.env file is missing. Please create one from $folder_name/.env.example"
fi
# get required keys from .env.example (lines starting with letters, excluding comments and empty lines)
local required_keys=$(grep -E '^[A-Za-z]' "$example_file" | cut -d'=' -f1)
# check each required key
local missing_keys=()
for key in $required_keys; do
if ! grep -q "^${key}=" "$env_file"; then
missing_keys+=("$key")
fi
done
# if there are missing keys, show error
if [ ${#missing_keys[@]} -ne 0 ]; then
error "$folder_name/.env is missing required variables: ${missing_keys[*]}"
fi
}
# check web environment
check_single_env \
"$ROOT_DIR/final_demo/web/.env" \
"$ROOT_DIR/final_demo/web/.env.example" \
"web"
# check internal-dashboard environment
check_single_env \
"$ROOT_DIR/internal-dashboard/.env" \
"$ROOT_DIR/internal-dashboard/.env.example" \
"internal-dashboard"
# check pwa environment
check_single_env \
"$ROOT_DIR/final_demo/pwa/.env" \
"$ROOT_DIR/final_demo/pwa/.env.example" \
"pwa"
log "Environment files check passed"
}
# function to check if required tools are installed
check_requirements() {
log "Checking requirements..."
command -v dfx >/dev/null 2>&1 || error "dfx is required but not installed"
command -v bun >/dev/null 2>&1 || error "bun is required but not installed"
command -v rustup >/dev/null 2>&1 || error "rustup is required but not installed"
}
# function to start the ic replica and deploy canisters
deploy_local() {
log "Deploying local environment..."
cd "$ROOT_DIR"
# generate declarations first
bash utils.sh --generate-declarations --all || error "Failed to generate declarations"
cd "$ROOT_DIR/canister/scripts/deployments"
bash local.sh --background || error "Failed to deploy local environment"
cd "$ROOT_DIR"
}
# function to select package manager
select_package_manager() {
printf "${BLUE}[INFO]${NC} Select your preferred package manager:\n"
select pm in "bun" "npm" "yarn" "pnpm"; do
if [ "$pm" = "bun" ] || [ "$pm" = "npm" ] || [ "$pm" = "yarn" ] || [ "$pm" = "pnpm" ]; then
echo "$pm"
return
else
warning "Invalid selection. Please choose a number from 1-4."
fi
done
}
# function to install dependencies with selected package manager
install_deps() {
local pm=$1
if [ "$pm" = "bun" ]; then
bun install
elif [ "$pm" = "npm" ]; then
npm install
elif [ "$pm" = "yarn" ]; then
yarn install
elif [ "$pm" = "pnpm" ]; then
pnpm install
fi
}
# function to start dev server with selected package manager
start_dev() {
local pm=$1
local port=$2
local prefix=$3
if [ "$pm" = "bun" ]; then
PORT=$port bun dev 2>&1 | sed "s/^/[$prefix] /" &
elif [ "$pm" = "npm" ]; then
PORT=$port npm run dev 2>&1 | sed "s/^/[$prefix] /" &
elif [ "$pm" = "yarn" ]; then
PORT=$port yarn dev 2>&1 | sed "s/^/[$prefix] /" &
elif [ "$pm" = "pnpm" ]; then
PORT=$port pnpm dev 2>&1 | sed "s/^/[$prefix] /" &
fi
}
# function to start the web app
start_webapp() {
log "Starting web application..."
cd "$ROOT_DIR/final_demo/web"
install_deps $PACKAGE_MANAGER
start_dev $PACKAGE_MANAGER 3012 "WEBAPP"
cd "$ROOT_DIR"
}
# function to start the internal dashboard
start_dashboard() {
log "Starting internal dashboard..."
cd "$ROOT_DIR/internal-dashboard"
install_deps $PACKAGE_MANAGER
start_dev $PACKAGE_MANAGER 3011 "WEBADMIN"
cd "$ROOT_DIR"
}
# function to start the pwa
start_pwa() {
log "Starting PWA application..."
cd "$ROOT_DIR/final_demo/pwa"
install_deps $PACKAGE_MANAGER
start_dev $PACKAGE_MANAGER 3010 "PWA"
cd "$ROOT_DIR"
}
# function to cleanup processes
cleanup() {
log "Cleaning up processes..."
# kill all processes running on our ports
for port in 3010 3011 3012; do
lsof -ti:$port | xargs kill -9 2>/dev/null || true
done
if [ -f /tmp/dfx.pid ]; then
kill $(cat /tmp/dfx.pid) 2>/dev/null || true
rm /tmp/dfx.pid
else
dfx stop || true
fi
}
# trap ctrl-c and call cleanup
trap cleanup INT TERM
# main execution
main() {
if [ "$1" = "--canister" ]; then
# check requirements
check_requirements
# start services
deploy_local
# keep running until ctrl+c
log "Canister is running. Press Ctrl+C to stop."
wait
elif [ "$1" = "--front" ]; then
# check environment files first
check_env_files
# select package manager for frontend projects
export PACKAGE_MANAGER=$(select_package_manager)
log "Using ${PACKAGE_MANAGER} as package manager"
# start frontends
start_webapp
start_dashboard
start_pwa
# show urls
log "Frontend applications are ready!"
log "PWA running at: http://localhost:3010"
log "Internal dashboard running at: http://localhost:3011"
log "Web app running at: http://localhost:3012"
# keep running until ctrl-c
log "Press Ctrl+C to stop all frontends."
# wait for all background jobs
while true; do
sleep 1
if ! jobs %% >/dev/null 2>&1; then
break
fi
done
elif [ -z "$1" ]; then
# original behavior - run everything
check_env_files
check_requirements
# select package manager for frontend projects
export PACKAGE_MANAGER=$(select_package_manager)
log "Using ${PACKAGE_MANAGER} as package manager"
# start services
deploy_local
start_webapp
start_dashboard
start_pwa
# keep script running and show urls
log "Development environment is ready!"
log "PWA running at: http://localhost:3010"
log "Internal dashboard running at: http://localhost:3011"
log "Web app running at: http://localhost:3012"
log "IC replica running at: http://localhost:4943"
log "Candid UI and other endpoints are listed above ⬆️"
# wait for user input to stop
read -p "Press any key to stop all services..."
else
print_usage
exit 1
fi
}
# run main function
main "$@"