-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·258 lines (212 loc) · 5.87 KB
/
install.sh
File metadata and controls
executable file
·258 lines (212 loc) · 5.87 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
#!/bin/bash
set -e
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
# Configuration
REPO="tomtev/wp-md"
INSTALL_DIR="${WP_MD_INSTALL_DIR:-$HOME/.wp-md}"
BIN_DIR="$HOME/.local/bin"
print_banner() {
echo ""
echo -e "${CYAN} ╦ ╦╔═╗ ╔╦╗╔╦╗${NC}"
echo -e "${CYAN} ║║║╠═╝───║║║ ║║${NC}"
echo -e "${CYAN} ╚╩╝╩ ╩ ╩═╩╝${NC}"
echo ""
echo " Create & edit remote WordPress content as markdown files locally."
echo ""
}
info() {
echo -e "${BLUE}INFO${NC} $1"
}
success() {
echo -e "${GREEN}SUCCESS${NC} $1"
}
warn() {
echo -e "${YELLOW}WARN${NC} $1"
}
error() {
echo -e "${RED}ERROR${NC} $1"
exit 1
}
detect_os() {
OS="$(uname -s)"
ARCH="$(uname -m)"
case "$OS" in
Linux*) OS_TYPE="linux";;
Darwin*) OS_TYPE="darwin";;
MINGW*|MSYS*|CYGWIN*) OS_TYPE="windows";;
*) error "Unsupported operating system: $OS";;
esac
case "$ARCH" in
x86_64|amd64) ARCH_TYPE="x64";;
arm64|aarch64) ARCH_TYPE="arm64";;
*) error "Unsupported architecture: $ARCH";;
esac
info "Detected: $OS_TYPE ($ARCH_TYPE)"
}
check_node() {
if command -v node &> /dev/null; then
NODE_VERSION=$(node -v | cut -d 'v' -f 2)
NODE_MAJOR=$(echo "$NODE_VERSION" | cut -d '.' -f 1)
if [ "$NODE_MAJOR" -ge 18 ]; then
info "Node.js v$NODE_VERSION detected"
return 0
else
warn "Node.js v$NODE_VERSION found, but v18+ is required"
return 1
fi
fi
return 1
}
check_npm() {
if command -v npm &> /dev/null; then
NPM_VERSION=$(npm -v)
info "npm v$NPM_VERSION detected"
return 0
fi
return 1
}
install_via_npm() {
info "Installing via npm..."
if npm install -g wp-md 2>/dev/null; then
success "Installed via npm"
return 0
fi
# Try with sudo if permission denied
warn "Retrying with sudo..."
if sudo npm install -g wp-md; then
success "Installed via npm (with sudo)"
return 0
fi
return 1
}
install_via_github() {
info "Installing from GitHub..."
# Create directories
mkdir -p "$INSTALL_DIR"
mkdir -p "$BIN_DIR"
# Get latest release
LATEST_URL="https://api.github.com/repos/$REPO/releases/latest"
if command -v curl &> /dev/null; then
RELEASE_INFO=$(curl -sL "$LATEST_URL")
elif command -v wget &> /dev/null; then
RELEASE_INFO=$(wget -qO- "$LATEST_URL")
else
error "curl or wget is required"
fi
# Extract version
VERSION=$(echo "$RELEASE_INFO" | grep '"tag_name"' | cut -d '"' -f 4)
if [ -z "$VERSION" ]; then
VERSION="main"
TARBALL_URL="https://github.com/$REPO/archive/refs/heads/main.tar.gz"
else
TARBALL_URL="https://github.com/$REPO/archive/refs/tags/$VERSION.tar.gz"
fi
# Download and extract
TEMP_DIR=$(mktemp -d)
cd "$TEMP_DIR"
info "Downloading..."
if command -v curl &> /dev/null; then
curl -sL "$TARBALL_URL" | tar xz
else
wget -qO- "$TARBALL_URL" | tar xz
fi
# Find extracted directory
EXTRACTED_DIR=$(ls -d */ | head -n 1)
# Move to install directory
rm -rf "$INSTALL_DIR"
mv "$EXTRACTED_DIR" "$INSTALL_DIR"
# Install dependencies
cd "$INSTALL_DIR"
info "Installing dependencies..."
npm install --omit=dev --silent
# Create bin directory and symlink
mkdir -p "$BIN_DIR"
ln -sf "$INSTALL_DIR/bin/cli.js" "$BIN_DIR/wp-md"
chmod +x "$INSTALL_DIR/bin/cli.js"
# Cleanup
rm -rf "$TEMP_DIR"
success "Installed to $INSTALL_DIR"
}
setup_path() {
SHELL_NAME=$(basename "$SHELL")
case "$SHELL_NAME" in
bash)
PROFILE="$HOME/.bashrc"
[ -f "$HOME/.bash_profile" ] && PROFILE="$HOME/.bash_profile"
;;
zsh)
PROFILE="$HOME/.zshrc"
;;
fish)
PROFILE="$HOME/.config/fish/config.fish"
;;
*)
PROFILE="$HOME/.profile"
;;
esac
# Check if BIN_DIR is already in PATH
if [[ ":$PATH:" == *":$BIN_DIR:"* ]]; then
return 0
fi
# Add to profile
if [ -n "$PROFILE" ] && [ -f "$PROFILE" ]; then
if ! grep -q "wp-md" "$PROFILE" 2>/dev/null; then
echo "" >> "$PROFILE"
echo "# wp-md" >> "$PROFILE"
if [ "$SHELL_NAME" = "fish" ]; then
echo "set -gx PATH \$PATH $BIN_DIR" >> "$PROFILE"
else
echo "export PATH=\"\$PATH:$BIN_DIR\"" >> "$PROFILE"
fi
info "Added $BIN_DIR to PATH in $PROFILE"
warn "Run 'source $PROFILE' or restart your terminal"
fi
fi
}
verify_installation() {
# Add BIN_DIR to current PATH for verification
export PATH="$PATH:$BIN_DIR"
if command -v wp-md &> /dev/null; then
success "wp-md is installed!"
echo ""
wp-md --version
echo ""
echo -e "${GREEN}Get started:${NC}"
echo " cd your-project"
echo " wp-md init"
echo " wp-md pull"
return 0
else
error "Installation verification failed"
fi
}
main() {
print_banner
detect_os
if ! check_node || ! check_npm; then
echo ""
error "Node.js 18+ is required. Install it first:
- macOS: brew install node
- Ubuntu: curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - && sudo apt-get install -y nodejs
- Or visit: https://nodejs.org/"
fi
echo ""
install_via_github
setup_path
echo ""
success "wp-md installed!"
echo ""
echo -e "${GREEN}Get started:${NC}"
echo ""
echo " cd your-project"
echo " wp-md init"
echo " wp-md pull"
echo ""
}
main "$@"