问题

hexo 每次都要运行,试图创建一个脚本自动化执行

1
2
3
hexo clean
hexo g
hexo s

问题分析

使用cmd脚本

问题解决

Step1 LaunchClearAndOpenHtml.bat,

首先建立顶层脚本LaunchClearAndOpenHtml.bat,其中&&代表上一条命令执行后再执行下一条

1
hexo clean && hexo g && LaunchWithoutClear.bat

Step 2 LaunchWithoutClear.bat

LaunchWithoutClear.bat如下:

1
2
start cmd /c "hexo s"
start cmd /c "WaitToOpenHtml.bat"

两个start cmd命令可以创建两个cmd窗口,从而实现并行,因为hexo s运行期间是没有结束标志的,无法串行运行下一个脚本WaitToOpenHtml.bat

Step 3 WaitToOpenHtml.bat

WaitToOpenHtml.bat作用是等待http://localhost:4000/端口打开,再打开网页。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@echo off
set "host=localhost"
set "port=4000"

:CHECK_LOOP
powershell -Command "$ErrorActionPreference = 'SilentlyContinue'; $result = Test-NetConnection -ComputerName %host% -Port %port%; if ($result.TcpTestSucceeded) { exit 0 } else { exit 1 }" >nul 2>&1
set "checkResult=%errorlevel%"
if %checkResult% equ 0 (
echo The port %port% on host %host% is open and the TCP connection is normal.
start http://%host%:%port%/
) else (
echo The port %port% on host %host% is not open or the TCP connection is abnormal. Waiting for 5 seconds before checking again...
ping -n 1 127.0.0.1 >nul
goto CHECK_LOOP
)
echo Loop ended.

改进隐藏等待网页开启的cmd窗口

同目录下新建HideWaitToOpenHtmlBat.vbs

1
2
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "cmd /c WaitToOpenHtml.bat", 0, False

LaunchWithoutClear.bat中改为

1
2
start cmd /c "hexo s"
start cmd /c "HideWaitToOpenHtmlBat.vbs"

效果

image-20250316205449614
PixPin_2025-03-16_20-39-19