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 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
| const { spawn, execSync } = require('child_process'); const fs = require('fs'); const path = require('path'); const os = require('os'); const chokidar = require('chokidar'); const express = require('express'); const serveStatic = require('serve-static');
let PORT = 3000; let silent = false; let quickMode = false; let directServe = false;
const args = process.argv.slice(2); for (let i = 0; i < args.length; i++) { const arg = args[i]; if (arg === '-h' || arg === '--help') { console.log(` 使用方法: node dev.js [端口] [选项]
选项: -s, --silent 静默模式,不输出 hexo 日志(错误除外) -q, --quick 快速模式,省略手动添加的延迟(防抖延迟除外) -d, --direct 直接启动服务器,跳过首次构建(适用于已生成 public 的情况) -h, --help 显示此帮助信息
示例: node dev.js 4000 node dev.js -s 4000 node dev.js -q -d `); process.exit(0); } else if (arg === '-s' || arg === '--silent') { silent = true; } else if (arg === '-q' || arg === '--quick') { quickMode = true; } else if (arg === '-d' || arg === '--direct') { directServe = true; } else if (/^\d+$/.test(arg) && PORT === 3000) { PORT = parseInt(arg, 10); } }
const CWD = process.cwd(); const PUBLIC_DIR = path.join(CWD, 'public'); const WATCH_PATHS = [ 'source', '_config.yml', 'scaffolds', 'themes', 'package.json', ];
const DEBOUNCE_DELAY = 300; const BAR_LENGTH = 20; const BOUNCE_WIDTH = 3; const FRAME_ACTIVE = 100; const FRAME_IDLE = 500; const FILL_STAY = quickMode ? 0 : 500; const SPINNER_FRAMES = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
let isBuilding = false; let buildTimer = null; let server = null; let watcher = null; let lastBuildFailed = false; let watcherReady = false;
let sliderWidth = BOUNCE_WIDTH; let sliderPos = 0; let dir = 1; let progressMode = 'bounce'; let renderTimeout = null; let spinnerIdx = 0; let pendingLogs = []; let currentBuildProcess = null;
let initializing = false; let serverStarting = false; let buildStartTime = 0; let firstBuild = false; let hexoErrorContext = false;
const RESET = '\x1b[0m'; const BG_BUILD = '\x1b[48;5;208m\x1b[30m'; const BG_RUN = '\x1b[42m\x1b[37m'; const BG_FAIL = '\x1b[41m\x1b[37m'; const BG_INIT = '\x1b[44m\x1b[37m';
const LEVEL_COLORS = { INFO: '\x1b[36m', WARN: '\x1b[33m', ERROR: '\x1b[31m', HEXO_ERROR: '\x1b[31m', SUCCESS:'\x1b[32m', DEBUG: '\x1b[35m', };
function formatTime(seconds) { if (seconds >= 60) { const mins = Math.floor(seconds / 60); const secs = Math.floor(seconds % 60); return `${mins}m${secs}s`; } return `${seconds.toFixed(1)}s`; }
function getProgressBar() { const bg = '-', fg = '#'; if (progressMode === 'fill') return fg.repeat(BAR_LENGTH); const w = Math.round(sliderWidth); const pos = Math.round(sliderPos); let bar = ''; for (let i = 0; i < BAR_LENGTH; i++) bar += (i >= pos && i < pos + w) ? fg : bg; return bar; }
function flushLogs() { if (!pendingLogs.length) return; const lines = [...pendingLogs]; pendingLogs = []; process.stdout.write('\r\x1b[K'); lines.forEach(l => console.log(l)); }
const YARN_PATTERN = /This project is configured to use yarn/i;
function outputLog(category, levelOrType, message, extra = {}) { if (category === 'hexo') { let line = message.trimEnd(); if (!line || YARN_PATTERN.test(line)) return;
const match = line.match(/^(INFO|WARN|ERROR|DEBUG)\s+(.*)/i); let level = 'INFO', msg = line; if (match) { level = match[1].toUpperCase(); msg = match[2]; }
if (/\b(FATAL|YAMLException)\b/i.test(line)) { level = 'ERROR'; }
const isFatal = /\b(FATAL|YAMLException)\b/i.test(line); const isErrorDesc = /Error:\s/i.test(line); const isCodeLine = /^\s+\d+\s*\|/.test(line) || line.includes('---^'); const isStackLine = /^\s+at\s/.test(line); const isError = (level === 'ERROR') || isFatal || isErrorDesc || isCodeLine || isStackLine;
if (isError) { hexoErrorContext = true; } else if (hexoErrorContext) { if (level === 'INFO' && !isError) { hexoErrorContext = false; } else if (level === 'DEBUG') { hexoErrorContext = false; } else if (level === 'WARN') { } }
if (silent && !isError && !hexoErrorContext) return;
let prefixColor; if (level === 'INFO') prefixColor = LEVEL_COLORS.INFO; else if (level === 'DEBUG') prefixColor = LEVEL_COLORS.DEBUG; else prefixColor = LEVEL_COLORS.HEXO_ERROR;
let bodyColor = ''; if (level === 'ERROR' || isFatal || isErrorDesc) bodyColor = '\x1b[31m'; else if (isCodeLine) bodyColor = '\x1b[34m'; else if (isStackLine) bodyColor = '\x1b[33m';
const prefix = ` ${prefixColor}│➤ [hexo] [${level}]${RESET}`; const body = bodyColor ? `${bodyColor}${msg}${RESET}` : msg; pendingLogs.push(`${prefix} ${body}`);
} else if (category === 'dev') { process.stdout.write('\r\x1b[K'); let prefix, body, color = ''; const type = levelOrType;
if (type === 'stepStart') { color = LEVEL_COLORS.INFO; prefix = ` ${color}┌➤ [dev] [INFO]${RESET}`; body = `${extra.emoji || '🚀'} ${message}`; } else if (type === 'stepMid') { color = LEVEL_COLORS.INFO; prefix = ` ${color}│➤ [dev] [INFO]${RESET}`; body = `📘 ${message}`; } else if (type === 'stepEndSuccess') { color = LEVEL_COLORS.SUCCESS; prefix = ` ${color}└➤ [dev] [SUCCESS]${RESET}`; body = `✨ ${message}`; } else if (type === 'stepEndFail') { color = LEVEL_COLORS.ERROR; prefix = ` ${color}└➤ [dev] [ERROR]${RESET}`; body = `❌ ${message}`; } else if (type === 'info') { color = LEVEL_COLORS.INFO; prefix = ` ➤ ${color}[dev] [INFO]${RESET}`; body = `📘 ${message}`; } else { prefix = `[dev]`; body = message; } console.log(`${prefix} ${body}`); } }
function logHexoLine(line) { outputLog('hexo', null, line); } function logStepStart(msg, emoji = '🚀') { outputLog('dev', 'stepStart', msg, { emoji }); } function logStepMid(msg) { outputLog('dev', 'stepMid', msg); } function logStepEndSuccess(msg) { outputLog('dev', 'stepEndSuccess', msg); } function logStepEndFail(msg) { outputLog('dev', 'stepEndFail', msg); } function devInfo(msg) { outputLog('dev', 'info', msg); }
function printShortcuts() { devInfo('快捷键: Ctrl+C 退出 | Ctrl+R 完整重建 | Ctrl+S 启动/重启服务器'); }
function getLocalIP() { const nets = os.networkInterfaces(); for (const name of Object.keys(nets)) { for (const net of nets[name]) { if (net.family === 'IPv4' && !net.internal) return net.address; } } return '127.0.0.1'; }
function startServer() { if (server || serverStarting) return; serverStarting = true;
logStepStart('启动服务器');
const delay = quickMode ? 0 : 1000; setTimeout(() => { const app = express(); app.use(serveStatic(PUBLIC_DIR, { index: 'index.html', setHeaders: res => res.setHeader('Cache-Control', 'no-cache') })); app.use((req, res) => res.sendFile(path.join(PUBLIC_DIR, 'index.html')));
const httpServer = app.listen(PORT, () => { serverStarting = false; server = httpServer; const localIP = getLocalIP(); logStepMid(`本地访问: http://localhost:${PORT}`); logStepMid(`局域网访问: http://${localIP}:${PORT}`); if (watcher) logStepMid('文件监控已就绪'); logStepEndSuccess('服务器已启动'); printShortcuts(); scheduleScroll(); });
httpServer.on('error', err => { serverStarting = false; logStepEndFail(`服务器错误: ${err.message}`); }); }, delay); }
function startWatcher() { if (watcher) return; watcher = chokidar.watch(WATCH_PATHS, { ignoreInitial: true, persistent: true, cwd: CWD, ignored: [/(^|[\/\\])\../, /\.tmp$/i, /~$/, /\.sw[op]$/i, /\.bak$/i], }); watcher.on('add', p => { scheduleBuild(`新增: ${p}`); devInfo(`新增文件: ${p}`); }); watcher.on('change', p => { scheduleBuild(`变更: ${p}`); devInfo(`文件变化: ${p}`); }); watcher.on('unlink', p => { scheduleBuild(`删除: ${p}`); devInfo(`文件删除: ${p}`); }); watcher.on('ready', () => { watcherReady = true; startServer(); }); }
function abortCurrentBuild() { if (currentBuildProcess) { devInfo('检测到新的变更,中止当前构建...'); try { currentBuildProcess.kill('SIGTERM'); } catch (e) {} currentBuildProcess = null; } }
function build(trigger) { if (buildTimer) { clearTimeout(buildTimer); buildTimer = null; } if (isBuilding) abortCurrentBuild();
hexoErrorContext = false; isBuilding = true; lastBuildFailed = false; initializing = false; progressMode = 'bounce'; sliderWidth = BOUNCE_WIDTH; sliderPos = 0; dir = 1; buildStartTime = Date.now();
logStepStart(`构建开始 (${trigger})`); forceRender(); currentBuildProcess = runHexo(['g', '--incremental']); }
function fullRebuild() { if (isBuilding) abortCurrentBuild();
hexoErrorContext = false; isBuilding = true; lastBuildFailed = false; initializing = false; progressMode = 'bounce'; sliderWidth = BOUNCE_WIDTH; sliderPos = 0; dir = 1; const totalStart = Date.now(); buildStartTime = totalStart;
logStepStart('完整重建 - 清理 (hexo clean)'); forceRender();
const clean = spawn('hexo', ['clean'], { cwd: CWD, stdio: ['ignore','pipe','pipe'] }); currentBuildProcess = clean; let cleanOut = '', cleanErr = ''; clean.stdout.on('data', d => { cleanOut += d.toString(); const lines = cleanOut.split('\n'); cleanOut = lines.pop(); lines.forEach(logHexoLine); }); clean.stderr.on('data', d => { cleanErr += d.toString(); const lines = cleanErr.split('\n'); cleanErr = lines.pop(); lines.forEach(logHexoLine); });
clean.on('close', code => { if (cleanOut) logHexoLine(cleanOut); if (cleanErr) logHexoLine(cleanErr); if (currentBuildProcess !== clean) return;
const cleanTime = (Date.now() - totalStart) / 1000; if (code !== 0) { isBuilding = false; lastBuildFailed = true; logStepEndFail(`清理失败 (${formatTime(cleanTime)}),重建中止`); currentBuildProcess = null; startFillThenScroll(); return; } logStepEndSuccess(`清理完成 (${formatTime(cleanTime)})`);
logStepStart('完整重建 - 生成 (hexo g)'); const genStart = Date.now(); currentBuildProcess = runHexo(['g'], { suppressEndLog: true, onClose: (genCode) => { const genTime = (Date.now() - genStart) / 1000; const totalTime = (Date.now() - totalStart) / 1000; if (genCode === 0) { logStepEndSuccess(`生成完成 (${formatTime(genTime)})`); logStepEndSuccess(`完整重建完成,总耗时 ${formatTime(totalTime)}`); } else { logStepEndFail(`生成失败 (${formatTime(genTime)}),退出码 ${genCode}`); logStepEndFail(`完整重建失败,总耗时 ${formatTime(totalTime)}`); } } }); });
clean.on('error', err => { if (currentBuildProcess !== clean) return; const cleanTime = (Date.now() - totalStart) / 1000; isBuilding = false; lastBuildFailed = true; logStepEndFail(`清理错误: ${err.message} (${formatTime(cleanTime)})`); currentBuildProcess = null; startFillThenScroll(); }); }
function runHexo(args, opts = {}) { const { suppressEndLog = false, onClose } = opts; const child = spawn('hexo', args, { cwd: CWD, stdio: ['ignore','pipe','pipe'] }); let outBuf = '', errBuf = '';
child.stdout.on('data', d => { outBuf += d.toString(); const lines = outBuf.split('\n'); outBuf = lines.pop(); lines.forEach(logHexoLine); }); child.stderr.on('data', d => { errBuf += d.toString(); const lines = errBuf.split('\n'); errBuf = lines.pop(); lines.forEach(logHexoLine); });
child.on('close', code => { if (currentBuildProcess !== child) return; if (outBuf) logHexoLine(outBuf); if (errBuf) logHexoLine(errBuf);
const elapsed = (Date.now() - buildStartTime) / 1000; isBuilding = false; lastBuildFailed = (code !== 0); currentBuildProcess = null; hexoErrorContext = false;
if (!suppressEndLog) { if (code === 0) { logStepEndSuccess(`构建完成 (${formatTime(elapsed)})`); } else { logStepEndFail(`构建失败 (${formatTime(elapsed)}),退出码 ${code}`); } }
if (firstBuild && code === 0) { firstBuild = false; startWatcher(); startServer(); } else if (firstBuild && code !== 0) { firstBuild = false; logStepEndFail('首次构建失败,退出'); process.exit(1); }
if (onClose) onClose(code); startFillThenScroll(); });
child.on('error', err => { if (currentBuildProcess !== child) return; const elapsed = (Date.now() - buildStartTime) / 1000; isBuilding = false; lastBuildFailed = true; currentBuildProcess = null; hexoErrorContext = false;
if (!suppressEndLog) { logStepEndFail(`构建进程错误: ${err.message} (${formatTime(elapsed)})`); } if (firstBuild) { firstBuild = false; process.exit(1); } if (onClose) onClose(-1); startFillThenScroll(); });
return child; }
function startFillThenScroll() { if (progressMode === 'fill') return; sliderWidth = BAR_LENGTH; sliderPos = 0; progressMode = 'fill'; if (FILL_STAY > 0) { setTimeout(() => { if (server && !isBuilding) { sliderWidth = BOUNCE_WIDTH; sliderPos = -sliderWidth; progressMode = 'scroll'; } }, FILL_STAY); } else { if (server && !isBuilding) { sliderWidth = BOUNCE_WIDTH; sliderPos = -sliderWidth; progressMode = 'scroll'; } } }
function scheduleScroll() { if (!isBuilding && server && !serverStarting && progressMode !== 'scroll') { sliderWidth = BOUNCE_WIDTH; sliderPos = -sliderWidth; progressMode = 'scroll'; } }
function scheduleBuild(trigger) { if (buildTimer) clearTimeout(buildTimer); buildTimer = setTimeout(() => { buildTimer = null; build(trigger); }, DEBOUNCE_DELAY); }
function forceRender() { if (renderTimeout) { clearTimeout(renderTimeout); renderTimeout = null; } renderLine(); scheduleRender(); }
function scheduleRender() { if (renderTimeout) clearTimeout(renderTimeout); const active = initializing || isBuilding || serverStarting || progressMode === 'fill'; const interval = active ? FRAME_ACTIVE : FRAME_IDLE; renderTimeout = setTimeout(() => { spinnerIdx++; renderLine(); scheduleRender(); }, interval); }
function renderLine() { flushLogs(); updateProgress();
let bgStyle; if (initializing) { bgStyle = BG_INIT; } else if (isBuilding) { bgStyle = BG_BUILD; } else if (lastBuildFailed) { bgStyle = BG_FAIL; } else if (serverStarting || server) { bgStyle = BG_RUN; } else { bgStyle = RESET; }
const spinner = isBuilding ? SPINNER_FRAMES[spinnerIdx % SPINNER_FRAMES.length] : ' '; const mark = (server && !serverStarting) ? '' : '[x]'; let statusText;
if (initializing) { statusText = `初始化中 ${mark}端口:${PORT}`; } else if (serverStarting) { statusText = `启动服务器中 ${mark}端口:${PORT}`; } else if (isBuilding) { const elapsed = buildStartTime ? (Date.now() - buildStartTime) / 1000 : 0; statusText = `构建中 ${formatTime(elapsed)} ${mark}端口:${PORT}`; } else if (lastBuildFailed) { statusText = `构建失败 ${mark}端口:${PORT}`; } else if (server) { statusText = `运行中 端口:${PORT}`; } else { statusText = `就绪 ${mark}端口:${PORT}`; }
process.stdout.write(`\r\x1b[K${bgStyle} ${spinner} [${getProgressBar()}] ${statusText} ${RESET}`); }
function updateProgress() { if (progressMode === 'bounce') { sliderPos += dir; if (sliderPos + sliderWidth >= BAR_LENGTH) { sliderPos = BAR_LENGTH - sliderWidth; dir = -1; } else if (sliderPos <= 0) { sliderPos = 0; dir = 1; } } else if (progressMode === 'scroll') { sliderPos += 1; if (sliderPos > BAR_LENGTH) sliderPos = -sliderWidth; } }
function printWelcome() { flushLogs(); console.log(`\x1b[36m✨ 欢迎使用LJXH的Hexo构建脚本 (∠・ω< )⌒☆!\x1b[0m`); console.log(`\x1b[90m⚡ Node.js:\x1b[0m ${process.version}`);
let pm = 'npm', pmIcon = '📦'; try { if (fs.existsSync(path.join(CWD, 'yarn.lock'))) { pm = 'yarn'; pmIcon = '🧶'; } else if (fs.existsSync(path.join(CWD, 'pnpm-lock.yaml'))) { pm = 'pnpm'; pmIcon = '📦'; } else { const pkg = JSON.parse(fs.readFileSync(path.join(CWD, 'package.json'), 'utf8')); if (pkg.packageManager && pkg.packageManager.includes('yarn')) { pm = 'yarn'; pmIcon = '🧶'; } } const pmVersion = execSync(`${pm} --version`, { encoding: 'utf8' }).trim(); console.log(`${pmIcon} ${pm}: ${pmVersion}`); } catch {}
try { const hexoVersion = require(path.join(CWD, 'node_modules/hexo/package.json')).version; console.log(`🌐 Hexo: ${hexoVersion}`); } catch {}
try { const config = fs.readFileSync(path.join(CWD, '_config.yml'), 'utf8'); const theme = config.match(/^theme:\s*(\S+)/m)?.[1]; if (theme) console.log(`🎨 主题: ${theme}`); } catch {} console.log(''); printShortcuts(); }
function restartServer() { if (server) { devInfo('正在重启服务器...'); server.close(() => { server = null; startServer(); }); } else { devInfo('正在启动服务器...'); startServer(); } }
function gracefulExit() { process.stdout.write(`\r\x1b[K${RESET}\n`); console.log('👋 感谢使用,下次见 (´• ω •`)ノ ♪'); if (renderTimeout) clearTimeout(renderTimeout); if (server) server.close(); if (watcher) watcher.close(); if (process.stdin.isTTY) { process.stdin.setRawMode(false); process.stdin.pause(); } process.exit(0); }
function init() { initializing = true; hexoErrorContext = false; scheduleRender();
printWelcome();
if (!fs.existsSync(PUBLIC_DIR)) { if (directServe) { devInfo('public 目录不存在,服务器可能返回 404,建议先执行 hexo generate'); } else { fs.mkdirSync(PUBLIC_DIR, { recursive: true }); } }
const delay = quickMode ? 0 : 1000; setTimeout(() => { if (directServe) { initializing = false; logStepStart('直接启动模式 (跳过构建)', '⚡'); startWatcher(); startServer(); forceRender(); } else { logStepStart('首次构建'); firstBuild = true; isBuilding = true; lastBuildFailed = false; initializing = false; progressMode = 'bounce'; sliderWidth = BOUNCE_WIDTH; sliderPos = 0; dir = 1; buildStartTime = Date.now(); forceRender(); currentBuildProcess = runHexo(['g', '--incremental']); } }, delay);
if (process.stdin.isTTY) { process.stdin.setRawMode(true); process.stdin.resume(); process.stdin.on('data', chunk => { if (chunk.length === 1) { if (chunk[0] === 3) gracefulExit(); else if (chunk[0] === 18) fullRebuild(); else if (chunk[0] === 19) restartServer(); } }); } process.on('SIGINT', gracefulExit); }
init();
|