-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·213 lines (179 loc) · 8.69 KB
/
build.sh
File metadata and controls
executable file
·213 lines (179 loc) · 8.69 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
#!/bin/bash
# Build script using esbuild + bundled Node.js runtime
# Bundles all TypeScript into one JavaScript file, downloads Node.js
set -e
PLATFORM="linux-x64"
NODE_VERSION="v20.11.0"
NODE_ARCH="linux-x64"
RUFF_VERSION="0.14.3"
echo "╔════════════════════════════════════════════════════════╗"
echo "║ Building Pyright LSP Bridge (esbuild + Node.js) ║"
echo "╚════════════════════════════════════════════════════════╝"
echo ""
# Clean previous builds
echo "🧹 Cleaning previous builds..."
rm -rf output
mkdir -p output/${PLATFORM}
# Step 1: Bundle TypeScript into one JavaScript file with esbuild
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "📦 Bundling TypeScript with esbuild..."
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
npx esbuild index.ts \
--bundle \
--platform=node \
--target=node18 \
--format=esm \
--outfile=output/${PLATFORM}/bundle.js \
--external:ws \
--external:vscode-ws-jsonrpc \
--external:vscode-jsonrpc \
--external:dotenv
if [ ! -f "output/${PLATFORM}/bundle.js" ]; then
echo "❌ esbuild bundling failed!"
exit 1
fi
echo "✓ Bundled: output/${PLATFORM}/bundle.js"
echo ""
# Step 2: Download and extract Node.js runtime
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "📥 Downloading Node.js ${NODE_VERSION} for ${NODE_ARCH}..."
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
NODE_PKG="node-${NODE_VERSION}-${NODE_ARCH}"
NODE_URL="https://nodejs.org/dist/${NODE_VERSION}/${NODE_PKG}.tar.gz"
NODE_FILE="/tmp/${NODE_PKG}.tar.gz"
if [ ! -f "${NODE_FILE}" ]; then
curl -L "${NODE_URL}" -o "${NODE_FILE}" || {
echo "❌ Failed to download Node.js"
exit 1
}
else
echo "✓ Using cached Node.js"
fi
echo "📂 Extracting Node.js..."
tar -xzf "${NODE_FILE}" -C /tmp/
mv "/tmp/${NODE_PKG}" "output/${PLATFORM}/node"
echo "✓ Node.js extracted to output/${PLATFORM}/node"
# Strip unnecessary files from Node.js to reduce size
echo "🧹 Stripping unnecessary files from Node.js..."
cd "output/${PLATFORM}/node"
# Remove npm, npx, corepack (we don't need package managers at runtime)
rm -rf lib/node_modules/npm 2>/dev/null || true
rm -rf lib/node_modules/corepack 2>/dev/null || true
rm -f bin/npm bin/npx bin/corepack 2>/dev/null || true
# Remove docs and other non-essential files
rm -rf share/doc 2>/dev/null || true
rm -rf share/man 2>/dev/null || true
rm -rf share/systemtap 2>/dev/null || true
rm -rf include 2>/dev/null || true
rm -f README.md CHANGELOG.md LICENSE 2>/dev/null || true
rm -f *.md 2>/dev/null || true
# Strip debug symbols from node binary (optional, reduces size significantly)
if command -v strip &> /dev/null; then
strip bin/node 2>/dev/null || true
echo "✓ Stripped debug symbols from node binary"
fi
cd ../../..
echo "✓ Stripped unnecessary files from Node.js"
echo ""
# Step 3: Download Ruff binary for Linux
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "📥 Downloading Ruff ${RUFF_VERSION} for Linux x64..."
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
RUFF_URL="https://github.com/astral-sh/ruff/releases/download/${RUFF_VERSION}/ruff-x86_64-unknown-linux-gnu.tar.gz"
RUFF_FILE="/tmp/ruff-${RUFF_VERSION}-linux-x64.tar.gz"
curl -L -A "Mozilla/5.0" "${RUFF_URL}" -o "${RUFF_FILE}" || {
echo "❌ Failed to download Ruff"
exit 1
}
echo "📂 Extracting Ruff..."
mkdir -p "output/${PLATFORM}/bin"
tar -xzf "${RUFF_FILE}" -C /tmp/
cp "/tmp/ruff-x86_64-unknown-linux-gnu/ruff" "output/${PLATFORM}/bin/ruff"
chmod +x "output/${PLATFORM}/bin/ruff"
echo "✓ Ruff extracted to output/${PLATFORM}/bin/ruff"
# Step 4: Install production dependencies
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "📥 Installing production node_modules..."
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
cp package.json output/${PLATFORM}/
cd output/${PLATFORM}
npm install --production --no-optional
rm package-lock.json
echo '{"type":"module"}' > package.json
echo "✓ Installed production dependencies"
echo ""
# Step 4.5: Prune unnecessary files from node_modules
echo "🧹 Pruning unnecessary files from node_modules..."
find node_modules -type d -name "test" -exec rm -rf {} + 2>/dev/null || true
find node_modules -type d -name "tests" -exec rm -rf {} + 2>/dev/null || true
find node_modules -type d -name "docs" -exec rm -rf {} + 2>/dev/null || true
find node_modules -type d -name "examples" -exec rm -rf {} + 2>/dev/null || true
find node_modules -type d -name "example" -exec rm -rf {} + 2>/dev/null || true
find node_modules -type d -name ".github" -exec rm -rf {} + 2>/dev/null || true
find node_modules -type d -name "coverage" -exec rm -rf {} + 2>/dev/null || true
find node_modules -type d -name "benchmark" -exec rm -rf {} + 2>/dev/null || true
find node_modules -type f -name "*.md" -delete 2>/dev/null || true
find node_modules -type f -name "*.ts" ! -name "*.d.ts" -delete 2>/dev/null || true
find node_modules -type f -name "*.map" -delete 2>/dev/null || true
find node_modules -type f -name "LICENSE*" -delete 2>/dev/null || true
find node_modules -type f -name "CHANGELOG*" -delete 2>/dev/null || true
find node_modules -type f -name ".npmignore" -delete 2>/dev/null || true
find node_modules -type f -name ".eslintrc*" -delete 2>/dev/null || true
find node_modules -type f -name ".prettierrc*" -delete 2>/dev/null || true
find node_modules -type f -name "tsconfig.json" -delete 2>/dev/null || true
echo "✓ Pruned unnecessary files"
cd ../..
echo ""
# Step 5: Copy config template
echo "📋 Copying pyrightconfig.json..."
cp pyrightconfig.json output/${PLATFORM}/
echo "✓ Copied config template"
echo ""
# Step 6: Create start script
echo "📝 Creating start script..."
cat > "output/${PLATFORM}/start.sh" << 'EOF'
#!/bin/bash
# Pyright LSP WebSocket Bridge
# Usage: ./start.sh --port <PORT> --project-root <ROOT> --jesse-relative-path <JESSE> --bot-relative-path <BOT>
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
export RUFF_PATH="${DIR}/bin/ruff"
"${DIR}/node/bin/node" "${DIR}/bundle.js" "$@"
EOF
chmod +x "output/${PLATFORM}/start.sh"
echo "✓ Created start.sh"
echo ""
# Step 7: Create compressed archive
echo "📦 Creating compressed archive..."
cd output
tar -czf "${PLATFORM}.tar.gz" "${PLATFORM}/"
echo "✓ Created ${PLATFORM}.tar.gz"
# Get sizes before cleanup
DIR_SIZE=$(du -sh "${PLATFORM}" | cut -f1)
ARCHIVE_SIZE=$(du -sh "${PLATFORM}.tar.gz" | cut -f1)
# Remove extracted folder
rm -rf "${PLATFORM}"
echo "✓ Removed extracted folder"
cd ..
echo ""
# Show summary
echo "╔════════════════════════════════════════════════════════╗"
echo "║ ✅ BUILD COMPLETE ║"
echo "╚════════════════════════════════════════════════════════╝"
echo ""
echo "📦 Compressed archive: output/${PLATFORM}.tar.gz"
echo " Original size: ${DIR_SIZE}"
echo " Compressed: ${ARCHIVE_SIZE}"
echo ""
echo "📁 Archive contains:"
echo " - bundle.js (all your bridge code bundled)"
echo " - node/ (Node.js ${NODE_VERSION} runtime, optimized)"
echo " - node_modules/ (Pyright + pruned dependencies)"
echo " - bin/ruff (Ruff ${RUFF_VERSION} formatter)"
echo " - pyrightconfig.json (config template)"
echo " - start.sh (startup script)"
echo ""
echo "🚀 To deploy:"
echo " 1. Upload: scp output/${PLATFORM}.tar.gz server:/opt/"
echo " 2. Extract: tar -xzf ${PLATFORM}.tar.gz"
echo " 3. Run: cd ${PLATFORM} && ./start.sh --port 9011 --project-root /path/to/project ..."
echo ""