-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-exe.bat
More file actions
455 lines (407 loc) · 12.3 KB
/
build-exe.bat
File metadata and controls
455 lines (407 loc) · 12.3 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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
@echo off
echo ========================================
echo Create Python Server Executable
echo ========================================
echo.
REM Check if Python is installed
python --version >nul 2>&1
if %ERRORLEVEL% NEQ 0 (
echo ERROR: Python not found!
echo.
echo Please install Python 3.8 or higher from:
echo https://www.python.org/downloads/
echo.
pause
exit /b 1
)
REM Check if PyInstaller is installed
python -c "import PyInstaller" >nul 2>&1
if %ERRORLEVEL% NEQ 0 (
echo PyInstaller not found. Installing...
echo.
python -m pip install pyinstaller
if %ERRORLEVEL% NEQ 0 (
echo.
echo ERROR: Failed to install PyInstaller!
echo.
echo Please install manually:
echo python -m pip install pyinstaller
echo.
pause
exit /b 1
)
echo.
echo PyInstaller installed successfully!
echo.
) else (
echo PyInstaller is already installed.
echo.
)
echo Creating executable...
echo.
REM Create executable with PyInstaller
REM --onefile: creates a single executable file
REM --windowed: doesn't show console window (GUI only)
REM --name: executable name
REM --icon: optional, can add a .ico file if available
REM --add-data: add data if necessary
REM --hidden-import: import hidden modules if necessary
python -m PyInstaller --onefile --windowed --name "HandoverServer" --clean server.py
if %ERRORLEVEL% NEQ 0 (
echo.
echo ========================================
echo ERROR creating executable!
echo ========================================
echo.
echo Check the errors above and try again.
echo.
pause
exit /b 1
)
echo.
echo ========================================
echo Copying required files...
echo ========================================
echo.
REM Create data folder if it doesn't exist
if not exist "dist\data" (
mkdir "dist\data"
echo Folder 'data' created.
)
REM Copy nodejs folder (from nodejs/ root OR dist/nodejs from setup-portable-nodejs)
if exist "dist\nodejs\node.exe" (
echo [OK] Portable Node.js already in dist\nodejs.
) else if exist "nodejs\node.exe" (
echo Copying 'nodejs' folder...
xcopy /E /I /Y "nodejs" "dist\nodejs" >nul
if %ERRORLEVEL% EQU 0 (
echo [OK] Folder 'nodejs' copied.
) else (
echo [WARNING] Error copying 'nodejs' folder
)
) else (
echo [WARNING] Portable Node.js not found!
echo Run: .\setup-portable-nodejs.ps1
echo This downloads Node.js to dist\nodejs for the standalone build.
)
REM Copy server folder
if exist "server" (
echo Copying 'server' folder...
xcopy /E /I /Y "server" "dist\server" >nul
if %ERRORLEVEL% EQU 0 (
echo [OK] Folder 'server' copied.
) else (
echo [WARNING] Error copying 'server' folder
)
) else (
echo [ERROR] Folder 'server' not found!
echo This folder is essential for operation.
)
REM Copy package.json for dependencies
if exist "package.json" (
echo Copying 'package.json'...
copy "package.json" "dist\package.json" >nul
if %ERRORLEVEL% EQU 0 (
echo [OK] package.json copied.
) else (
echo [WARNING] Error copying package.json
)
) else (
echo [ERROR] package.json not found!
echo This file is essential for installing dependencies.
)
REM Install Node.js dependencies
echo.
echo ========================================
echo Installing Node.js dependencies...
echo ========================================
echo.
cd dist
REM First, try to copy node_modules from root if it exists (much faster)
if exist "..\node_modules" (
echo Copying existing node_modules from root folder...
echo This is faster than downloading packages again.
echo.
xcopy /E /I /Y /Q "..\node_modules" "node_modules" >nul 2>&1
if %ERRORLEVEL% EQU 0 (
echo [OK] node_modules copied from root folder.
echo.
echo Verifying installation...
if exist "node_modules\express" (
echo [OK] Express module found.
echo [OK] Dependencies ready!
cd ..
goto :deps_ok
) else (
echo [WARNING] Express not found, will reinstall...
rmdir /S /Q "node_modules" >nul 2>&1
)
) else (
echo [INFO] Could not copy node_modules, will install fresh...
)
echo.
)
REM Check if node_modules already exists in dist
if exist "node_modules\express" (
echo node_modules already exists with express module.
echo [OK] Dependencies ready!
cd ..
goto :deps_ok
)
REM Install dependencies using npm
echo Installing dependencies from package.json...
echo This may take a few minutes depending on internet speed...
echo.
REM Check if we have portable Node.js (we're in dist folder)
if exist "nodejs\npm.cmd" (
echo Using portable Node.js from dist\nodejs...
call nodejs\npm.cmd install --production --no-optional --loglevel=error
) else if exist "..\dist\nodejs\npm.cmd" (
echo Using portable Node.js from dist\nodejs...
call ..\dist\nodejs\npm.cmd install --production --no-optional --loglevel=error
) else if exist "..\nodejs\npm.cmd" (
echo Using portable Node.js from nodejs folder...
call ..\nodejs\npm.cmd install --production --no-optional --loglevel=error
) else (
echo Using system Node.js...
call npm install --production --no-optional --loglevel=error
)
if %ERRORLEVEL% NEQ 0 (
echo.
echo ========================================
echo [ERROR] Failed to install dependencies!
echo ========================================
echo.
echo The server will NOT work without node_modules.
echo.
echo Please check:
echo 1. Node.js is available (portable in nodejs/ or system install)
echo 2. Internet connection for downloading packages
echo 3. package.json exists in dist folder
echo 4. Antivirus is not blocking npm
echo.
echo You can try installing manually:
echo cd dist
echo npm install --production
echo.
cd ..
pause
exit /b 1
)
REM Verify installation was successful
echo.
echo Verifying installation...
if not exist "node_modules" (
echo [ERROR] node_modules folder was not created!
cd ..
pause
exit /b 1
)
if not exist "node_modules\express" (
echo [ERROR] Express module not found after installation!
echo Installation may have failed silently.
cd ..
pause
exit /b 1
)
echo [OK] Express module found.
echo [OK] Dependencies installed successfully!
:deps_ok
REM Ensure we're back in the project root directory
cd /d %~dp0
REM Build frontend with relative URLs for dist
echo.
echo ========================================
echo Building Frontend for Distribution
echo ========================================
echo.
cd client
REM Check if Node.js is available (we're in client folder)
where node >nul 2>&1
if %ERRORLEVEL% NEQ 0 (
echo [WARNING] Node.js not found in PATH!
echo Trying to use portable Node.js...
if exist "..\dist\nodejs\node.exe" (
set "PATH=..\dist\nodejs;%PATH%"
echo Using portable Node.js from dist\nodejs
) else if exist "..\nodejs\node.exe" (
set "PATH=..\nodejs;%PATH%"
) else if exist "nodejs\node.exe" (
set "PATH=nodejs;%PATH%"
) else (
echo [ERROR] Node.js not found!
echo Run: .\setup-portable-nodejs.ps1
echo Or install Node.js from nodejs.org
cd ..
pause
exit /b 1
)
)
REM Set REACT_APP_API_URL as relative URL to work on any port
set REACT_APP_API_URL=/api
echo Configuration:
echo REACT_APP_API_URL=%REACT_APP_API_URL%
echo.
REM Install dependencies if necessary
if not exist "node_modules" (
echo Installing dependencies...
call npm install
if %ERRORLEVEL% NEQ 0 (
echo.
echo [ERROR] Failed to install dependencies!
cd ..
pause
exit /b 1
)
echo.
)
REM Clean old build folder to force fresh compilation
echo Cleaning old build folder...
if exist "build" (
rmdir /S /Q "build" >nul 2>&1
echo [OK] Old build folder removed.
)
REM Compile the frontend
echo Compiling frontend (this may take a few minutes)...
echo.
REM Force clean build with CI=true to prevent caching issues
set CI=true
call npm run build
set CI=
if %ERRORLEVEL% NEQ 0 (
echo.
echo [ERROR] Failed to compile frontend!
cd ..
pause
exit /b 1
)
echo [OK] Frontend compiled successfully!
cd /d %~dp0
REM Copy client/build folder to dist
echo.
echo ========================================
echo Copying client/build to dist/client/build
echo ========================================
echo.
REM Ensure we're in project root
cd /d %~dp0
REM Check if client/build exists
if not exist "client\build" (
echo [ERROR] client\build not found after compilation!
pause
exit /b 1
)
echo [OK] client\build found!
echo.
REM Ensure dist directory exists
if not exist "dist" (
mkdir "dist"
echo Created dist directory.
)
REM Remove old build if exists
if exist "dist\client\build" (
echo Removing old build folder...
rmdir /S /Q "dist\client\build" >nul 2>&1
)
REM Create destination directory structure
if not exist "dist\client" (
mkdir "dist\client"
echo Created dist\client directory.
)
if not exist "dist\client\build" (
mkdir "dist\client\build"
echo Created dist\client\build directory.
)
REM Copy files
echo Copying files from client\build to dist\client\build...
xcopy /E /I /Y /H "client\build\*" "dist\client\build\"
if %ERRORLEVEL% EQU 0 (
REM Verify index.html was copied
if exist "dist\client\build\index.html" (
echo [OK] client\build copied successfully to dist\client\build!
) else (
echo [ERROR] Files copied but index.html not found!
pause
exit /b 1
)
) else (
echo [ERROR] Failed to copy files! (Error code: %ERRORLEVEL%)
pause
exit /b 1
)
REM Copy config example file if it exists
if exist "data\config.json.example" (
if not exist "dist\data\config.json" (
copy "data\config.json.example" "dist\data\config.json.example" >nul
echo [OK] Config example file copied.
)
)
REM Copy package.json to dist for npm install
if exist "package.json" (
echo Copying 'package.json' for dependency installation...
copy "package.json" "dist\package.json" >nul
if %ERRORLEVEL% EQU 0 (
echo [OK] package.json copied.
)
)
REM Copy helper scripts
echo.
echo Copying helper scripts...
if exist "dist\open-firewall-port.bat" (
copy "dist\open-firewall-port.bat" "dist\open-firewall-port.bat.bak" >nul 2>&1
)
if exist "dist\find-my-ip.bat" (
copy "dist\find-my-ip.bat" "dist\find-my-ip.bat.bak" >nul 2>&1
)
if exist "dist\setup-auto-start.bat" (
copy "dist\setup-auto-start.bat" "dist\setup-auto-start.bat.bak" >nul 2>&1
)
if exist "dist\remove-auto-start.bat" (
copy "dist\remove-auto-start.bat" "dist\remove-auto-start.bat.bak" >nul 2>&1
)
REM Note: Helper scripts should be created manually in dist/ folder
REM They are not copied from root because they are distribution-specific
echo [INFO] Helper scripts should be in dist/ folder:
echo - open-firewall-port.bat
echo - find-my-ip.bat
echo - setup-auto-start.bat
echo - remove-auto-start.bat
echo - README.md
echo.
echo If these files don't exist, copy them from the dist/ folder
echo or create them using the templates in the project.
REM Install Node.js dependencies in dist folder
echo.
echo Installing Node.js dependencies in dist folder...
echo This may take a few minutes...
if exist "dist\nodejs\npm.cmd" (
cd dist
call nodejs\npm.cmd install
cd ..
if %ERRORLEVEL% EQU 0 (
echo [OK] Dependencies installed.
) else (
echo [WARNING] Error installing dependencies
)
) else (
echo [WARNING] npm.cmd not found - dependencies may not be installed
echo Make sure to run 'npm install' in dist folder manually
)
echo.
echo ========================================
echo Executable created successfully!
echo ========================================
echo.
echo The executable and files are in: dist\
echo.
echo Structure created:
echo dist\
echo +-- HandoverServer.exe
echo +-- nodejs\
echo +-- server\
echo +-- client\build\
echo +-- data\
echo.
echo You can distribute the entire 'dist\' folder as a complete package!
echo.
pause