-
Notifications
You must be signed in to change notification settings - Fork 1
/
watch.cmd
116 lines (106 loc) · 4.11 KB
/
watch.cmd
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
@echo off
setlocal
cd /d %~dp0
::: Kill existing processes
set "FIELDS=handle^,name^,commandline"
rem taskkill /im "ngrok.exe" 2>nul
for /f "delims=, tokens=3" %%f in ('wmic path win32_process get %FIELDS% /format:csv ^| findstr /v findstr.exe ^| findstr ngrok.exe') do (
if not "%%f"=="name" (
echo PID/ngrok: %%f
TASKKILL /PID %%f
)
)
rem taskkill /im cmd.exe /fi "WINDOWTITLE eq http-server*"
for /f "delims=, tokens=3" %%f in ('wmic path win32_process get %FIELDS% /format:csv ^| findstr /v findstr.exe ^| findstr node.exe ^| findstr [lh][it][vt][ep]-server') do (
if not "%%f"=="name" (
echo PID/http-server: %%f
TASKKILL /PID %%f /F
)
)
taskkill /im WindowsTerminal.exe /fi "WINDOWTITLE eq http-server*" >nul
::: Check for required programs
where ngrok >nul
if ERRORLEVEL 1 (
echo ERROR: ngrok not found in PATH, check it is installed from: https://ngrok.com/ -- and ensure the command window is reopened
exit /b 1
)
set LIVE_SERVER=
for %%X in (live-server.cmd) do set LIVE_SERVER=%%~$PATH:X
if exist "./node_modules/.bin/live-server" (
set LIVE_SERVER=./node_modules/.bin/live-server
)
if "%LIVE_SERVER%"=="" (
echo ERROR: live-server not found in PATH or node_modules, install with: npm i -d live-server
exit /b 1
)
::: Run a live server on port 8080 (uses a WebSocket to indicate changes, this works through ngrok's https), and Ngrok to make available on the web
where wt.exe >nul
if errorlevel 1 goto run_cmd
:run_wt
::: Use Windows Terminal split panes to run the live server and ngrok
start "http-server-ngrok" wt new-tab --title "http-server-ngrok" -d . cmd /c "title http-server-ngrok && "%LIVE_SERVER%" --no-browser ." ; split-pane --title "http-server-ngrok" ngrok http 8080
goto wait_for_ngrok
:run_cmd
start "http-server" cmd /c "%LIVE_SERVER%" --no-browser .
start "ngrok" ngrok http 8080
goto wait_for_ngrok
::: Wait for Ngrok to start, then display Ngrok forwarding details
:wait_for_ngrok
echo.Waiting for ngrok...
choice /C 0 /D 0 /T 1 >nul
set TUNNEL=
for /F "tokens=* usebackq" %%F IN (`curl -s http://127.0.0.1:4040/api/tunnels ^| powershell -c "$input | select-string -pattern '(https://[-a-f0-9]+\.[a-z][a-z]\.ngrok\.io)' | %% {$url = $_.matches.groups[1].value; write-output $url} " `) DO set TUNNEL=%%F
if "%TUNNEL%"=="" goto wait_for_ngrok
echo Now listening on: %TUNNEL%
set URL=%TUNNEL%#debug
::: Display QR Code of the URL (if executable is found)
set QRCODE=
for %%X in (qrcode.exe) do set QRCODE=%%~$PATH:X
if defined QRCODE "%QRCODE%" --invert --output:medium "%URL%"
echo.%URL%
::: Start remote browser tab (otherwise: can use local "Send to your devices") -- chrome://inspect#devices
where adb >nul
if errorlevel 1 (
echo WARNING: adb not found.
goto skip_adb
)
adb devices -l | findstr "device:"
if errorlevel 1 (
echo WARNING: no adb devices found
goto no_devices
)
adb shell am start -n com.android.chrome/org.chromium.chrome.browser.ChromeTabbedActivity -d "%URL%" --activity-clear-task
:skip_adb
::: Start local browser
set CHROME=
if exist "%PROGRAMFILES(x86)%\Google\Chrome\Application\chrome.exe" set CHROME=%PROGRAMFILES(x86)%\Google\Chrome\Application\chrome.exe
if exist "%PROGRAMFILES%\Google\Chrome\Application\chrome.exe" set CHROME=%PROGRAMFILES%\Google\Chrome\Application\chrome.exe
if not exist "%CHROME%" (
echo WARNING: Chrome not found.
goto skip_chrome
)
rem chrome: URLs are blocked from command line, so send to info page
"%CHROME%" chrome-inspect-devices.html
:skip_chrome
::: Start Android screen mirroring (if not running)
tasklist /FI "IMAGENAME eq scrcpy.exe" | findstr "Image Name" > NUL
if not errorlevel 1 (
echo NOTE: Screen mirroring already active
goto skip_mirror
)
set SCRCPY=
for %%X in (scrcpy-noconsole.vbs) do set SCRCPY=%%~$PATH:X
if not defined SCRCPY (
echo NOTE: Screen mirroring executable not found in path.
goto skip_mirror
)
if exist "%SCRCPY%" (
CD /D "%SCRCPY%\.."
"%SCRCPY%
)
:skip_mirror
::: Delay - useful if launched as a preTask from VS Code before attaching to remote debugger
ECHO Waiting 5 seconds...
CHOICE /C 0 /D 0 /T 5 >NUL
:no_devices
echo NOTE: Will live reload any changes to the source files. (This project has no build process to watch.)