-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
360 lines (301 loc) · 9.48 KB
/
Copy pathinstall.sh
File metadata and controls
360 lines (301 loc) · 9.48 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/bin/bash
set -e
PHASET_CLI_VERSION="1"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Installation directory
BIN_DIR="$HOME/.local/bin"
print_error() {
echo -e "${RED}Error: $1${NC}" >&2
}
print_success() {
echo -e "${GREEN}$1${NC}"
}
print_info() {
echo -e "${YELLOW}$1${NC}"
}
# Create the phaset CLI wrapper
create_cli() {
mkdir -p "$BIN_DIR"
cat > "$BIN_DIR/phaset" << 'EOF'
#!/bin/bash
INSTALL_DIR="$HOME/.phaset"
VERSION_FILE="$INSTALL_DIR/VERSION"
API_FILE="$INSTALL_DIR/phaset_api.mjs"
print_error() {
echo -e "\033[0;31mError: $1\033[0m" >&2
}
print_success() {
echo -e "\033[0;32m$1\033[0m"
}
print_info() {
echo -e "\033[1;33m$1\033[0m"
}
check_node() {
if ! command -v node &> /dev/null; then
print_error "Node.js is not installed"
echo "Please install Node.js 24 or later from https://nodejs.org/"
exit 1
fi
NODE_VERSION=$(node -v | cut -d 'v' -f 2 | cut -d '.' -f 1)
if [ "$NODE_VERSION" -lt 24 ]; then
print_error "Node.js version 24 or later is required (found v$NODE_VERSION)"
echo "Please upgrade Node.js from https://nodejs.org/"
exit 1
fi
print_success "Node.js v$(node -v | cut -d 'v' -f 2) detected"
}
phaset_install() {
print_info "Installing Phaset..."
check_node
# Create installation directory
mkdir -p "$INSTALL_DIR"
# Download latest version
print_info "Downloading latest version from releases.phaset.dev..."
TEMP_FILE="/tmp/phaset_api_latest.zip"
if command -v curl &> /dev/null; then
curl -sSL -o "$TEMP_FILE" "https://releases.phaset.dev/phaset_api_latest.zip"
elif command -v wget &> /dev/null; then
wget -q -O "$TEMP_FILE" "https://releases.phaset.dev/phaset_api_latest.zip"
else
print_error "Neither curl nor wget is available"
exit 1
fi
# Extract the archive
print_info "Extracting files..."
TEMP_EXTRACT="/tmp/phaset_extract_$"
mkdir -p "$TEMP_EXTRACT"
if command -v unzip &> /dev/null; then
unzip -q -o "$TEMP_FILE" -d "$TEMP_EXTRACT"
else
print_error "unzip is not installed"
exit 1
fi
rm "$TEMP_FILE"
# Find the actual files (handle nested directories)
if [ -f "$TEMP_EXTRACT/phaset_api.mjs" ]; then
# Files are in root of zip
cp -r "$TEMP_EXTRACT"/* "$INSTALL_DIR/"
else
# Files might be in a subdirectory
SUBDIR=$(find "$TEMP_EXTRACT" -name "phaset_api.mjs" -type f | head -n 1 | xargs dirname)
if [ -n "$SUBDIR" ]; then
cp -r "$SUBDIR"/* "$INSTALL_DIR/"
else
print_error "Could not find phaset_api.mjs in the downloaded archive"
rm -rf "$TEMP_EXTRACT"
exit 1
fi
fi
rm -rf "$TEMP_EXTRACT"
# Verify installation
if [ ! -f "$API_FILE" ]; then
print_error "Installation failed: phaset_api.mjs not found after extraction"
exit 1
fi
# Check if VERSION file exists
if [ -f "$VERSION_FILE" ]; then
INSTALLED_VERSION=$(cat "$VERSION_FILE")
print_success "Phaset v$INSTALLED_VERSION installed successfully!"
else
print_success "Phaset installed successfully!"
fi
echo ""
print_info "Run 'phaset start' to start the API"
}
phaset_docs() {
print_info "Opening Phaset Documentation..."
echo ""
if command -v xdg-open &> /dev/null; then
# Linux
xdg-open "https://docs.phaset.dev"
elif command -v open &> /dev/null; then
# macOS
open "https://docs.phaset.dev"
elif command -v start &> /dev/null; then
# Windows (Git Bash, WSL, etc.)
start "https://docs.phaset.dev"
else
# Fallback - just print the URL
print_info "Could not automatically open browser. Visit:"
echo "https://docs.phaset.dev"
fi
echo ""
}
phaset_upgrade() {
print_info "Checking for updates to the Phaset API..."
# Check if Phaset is installed
if [ ! -d "$INSTALL_DIR" ]; then
print_error "Phaset is not installed. Run 'phaset install' first."
exit 1
fi
# Get current version
CURRENT_VERSION=""
if [ -f "$VERSION_FILE" ]; then
CURRENT_VERSION=$(cat "$VERSION_FILE" | tr -d '[:space:]')
print_info "Current version: v$CURRENT_VERSION"
else
print_info "No version file found. Assuming fresh install needed."
fi
# Check latest version from API
print_info "Checking latest version from api.phaset.dev..."
if command -v curl &> /dev/null; then
VERSION_JSON=$(curl -sSL "https://api.phaset.dev/prod/version/api")
elif command -v wget &> /dev/null; then
VERSION_JSON=$(wget -q -O - "https://api.phaset.dev/prod/version/api")
else
print_error "Neither curl nor wget is available"
exit 1
fi
# Parse JSON to extract version field
if command -v node &> /dev/null; then
LATEST_VERSION=$(echo "$VERSION_JSON" | node -e "console.log(JSON.parse(require('fs').readFileSync(0,'utf8')).version)")
else
# Fallback to grep/sed if Node.js not available
LATEST_VERSION=$(echo "$VERSION_JSON" | grep -o '"version"[[:space:]]*:[[:space:]]*"[^"]*"' | sed 's/.*"\([^"]*\)"$/\1/')
fi
LATEST_VERSION=$(echo "$LATEST_VERSION" | tr -d '[:space:]')
print_info "Latest version: v$LATEST_VERSION"
# Compare versions
if [ -z "$CURRENT_VERSION" ]; then
print_info "Installing latest version..."
phaset_install
elif [ "$CURRENT_VERSION" = "$LATEST_VERSION" ]; then
print_success "You already have the latest version (v$CURRENT_VERSION)"
else
print_info "Upgrading from v$CURRENT_VERSION to v$LATEST_VERSION..."
phaset_install
fi
}
phaset_uninstall() {
print_info "Uninstalling Phaset..."
if [ -d "$INSTALL_DIR" ]; then
rm -rf "$INSTALL_DIR"
print_success "Phaset directory removed"
else
print_info "Phaset directory not found"
fi
# Remove the CLI itself
if [ -f "$HOME/.local/bin/phaset" ]; then
rm "$HOME/.local/bin/phaset"
print_success "Phaset CLI removed"
fi
print_success "Phaset has been uninstalled"
echo ""
print_info "You may need to restart your shell or run 'hash -r' to clear the command cache"
}
phaset_init() {
print_info "Creating phaset.config.json..."
cat > "phaset.config.json" << 'CONFIGEOF'
{
"email": {
"emailSubject": "Sign In To Phaset",
"user": "my_user@my_domain.net",
"password": "my_password",
"host": "smtp.my_host.net",
"port": 465,
"secure": true
},
"auth": {
"jwtSecret": "your-jwt-secret",
"appUrl": "https://my_local_or_remote_site.net/app"
},
"server": {
"allowedDomains": [
"*"
]
},
"phaset": {
"demoMode": false,
"bootstrap": {
"organizationName": "My Organization",
"adminEmail": "me@my_domain.net"
}
}
}
CONFIGEOF
print_success "phaset.config.json created successfully!"
print_info "Edit the file to configure your Phaset installation"
echo ""
}
phaset_start() {
if [ ! -f "$API_FILE" ]; then
print_error "Phaset API not found. Run 'phaset install' first."
exit 1
fi
check_node
print_info "Starting Phaset API..."
export PHASET_USER_CWD="$USER_CWD"
echo $PHASET_USER_CWD
cd "$INSTALL_DIR"
PHASET_USER_CWD="$USER_CWD" node phaset_api.mjs "$@"
}
# Main command handler
case "$1" in
install)
phaset_install
;;
init)
phaset_init
;;
start)
shift # Remove 'start' from $@
phaset_start "$@"
;;
upgrade)
phaset_upgrade
;;
docs)
phaset_docs
;;
uninstall)
phaset_uninstall
;;
*)
echo "Phaset CLI (version 1)"
echo ""
echo "Usage: phaset <command>"
echo ""
echo "Commands:"
echo " install Install Phaset API"
echo " init Create phaset.config.json template"
echo " start Start the Phaset API server"
echo " upgrade Upgrade to the latest version"
echo " docs Read the Phaset documentation"
echo " uninstall Remove Phaset from your system"
echo ""
exit 1
;;
esac
EOF
chmod +x "$BIN_DIR/phaset"
print_success "Phaset CLI installed to $BIN_DIR/phaset"
}
# Main installation flow
print_info "Installing Phaset CLI (version $PHASET_CLI_VERSION)..."
create_cli
# Check if bin directory is in PATH
if [[ ":$PATH:" != *":$BIN_DIR:"* ]]; then
echo ""
print_info "Add the following line to your shell profile (~/.bashrc, ~/.zshrc, etc.):"
echo ""
echo " export PATH=\"\$HOME/.local/bin:\$PATH\""
echo ""
print_info "For now, updating PATH for this session..."
export PATH="$BIN_DIR:$PATH"
fi
print_success "CLI installation complete!"
echo ""
print_info "Next steps:"
echo ""
print_info "1. Run 'phaset install' to install the Phaset API (in your bin directory)"
print_info "2. Run 'phaset init' to initialize a Phaset configuration (created in your current directory)"
print_info "3. Run 'phaset start' from that directory to start the API"
print_info "4. Questions? Run 'phaset docs'!"
echo ""
print_info "Note: You may need to add ~/.local/bin to your PATH permanently"
echo " by adding this line to your ~/.zshrc (or ~/.bashrc):"
echo " export PATH=\"\$HOME/.local/bin:\$PATH\""