zen-gitsync 2.0.8 → 2.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1322,9 +1322,7 @@ async function startUIServer() {
1322
1322
  console.log(`初始化文件系统监控器,路径: ${currentDir}`);
1323
1323
 
1324
1324
  // 检查是否是Git仓库
1325
- try {
1326
- execGitCommand('git rev-parse --is-inside-work-tree');
1327
- } catch (error) {
1325
+ if (!isGitRepo) {
1328
1326
  console.log('当前目录不是Git仓库,不启动监控');
1329
1327
  return;
1330
1328
  }
@@ -1366,6 +1364,17 @@ async function startUIServer() {
1366
1364
  // 获取并广播Git状态
1367
1365
  async function getAndBroadcastStatus() {
1368
1366
  try {
1367
+ // 如果不是Git仓库,发送特殊状态
1368
+ if (!isGitRepo) {
1369
+ io.emit('git_status_update', {
1370
+ isGitRepo: false,
1371
+ status: '当前目录不是Git仓库',
1372
+ porcelain: '',
1373
+ timestamp: new Date().toISOString()
1374
+ });
1375
+ return;
1376
+ }
1377
+
1369
1378
  // 获取常规状态
1370
1379
  const { stdout: statusOutput } = await execGitCommand('git status');
1371
1380
 
@@ -1374,6 +1383,7 @@ async function startUIServer() {
1374
1383
 
1375
1384
  // 广播到所有连接的客户端
1376
1385
  io.emit('git_status_update', {
1386
+ isGitRepo: true,
1377
1387
  status: statusOutput,
1378
1388
  porcelain: porcelainOutput,
1379
1389
  timestamp: new Date().toISOString()
@@ -1396,6 +1406,19 @@ async function startUIServer() {
1396
1406
  }, DEBOUNCE_DELAY);
1397
1407
  }
1398
1408
 
1409
+ // 检查当前目录是否是Git仓库
1410
+ let isGitRepo = false;
1411
+ try {
1412
+ const { stdout } = await execGitCommand('git rev-parse --is-inside-work-tree', { log: false });
1413
+ isGitRepo = stdout.trim() === 'true';
1414
+ } catch (error) {
1415
+ isGitRepo = false;
1416
+ console.log(chalk.yellow('======================================'));
1417
+ console.log(chalk.yellow(` 提示: 当前目录不是Git仓库`));
1418
+ console.log(chalk.yellow(` 目录: ${process.cwd()}`));
1419
+ console.log(chalk.yellow('======================================'));
1420
+ }
1421
+
1399
1422
  // 启动服务器
1400
1423
  const PORT = 3000;
1401
1424
  httpServer.listen(PORT, () => {
@@ -1403,11 +1426,15 @@ async function startUIServer() {
1403
1426
  console.log(chalk.green(` Zen GitSync 服务器已启动`));
1404
1427
  console.log(chalk.green(` 访问地址: http://localhost:${PORT}`));
1405
1428
  console.log(chalk.green(` 启动时间: ${new Date().toLocaleString()}`));
1429
+ if (isGitRepo) {
1430
+ console.log(chalk.green(` 当前目录是Git仓库,文件监控已启动`));
1431
+ // 启动文件监控
1432
+ initFileSystemWatcher();
1433
+ } else {
1434
+ console.log(chalk.yellow(` 当前目录不是Git仓库,文件监控未启动`));
1435
+ }
1406
1436
  console.log(chalk.green('======================================'));
1407
1437
 
1408
- // 启动文件监控
1409
- initFileSystemWatcher();
1410
-
1411
1438
  open(`http://localhost:${PORT}`);
1412
1439
  }).on('error', async (err) => {
1413
1440
  if (err.code === 'EADDRINUSE') {
@@ -1422,36 +1449,19 @@ async function startUIServer() {
1422
1449
  console.log(chalk.green(` 访问地址: http://localhost:${newPort}`));
1423
1450
  console.log(chalk.green(` 启动时间: ${new Date().toLocaleString()}`));
1424
1451
  console.log(chalk.green('======================================'));
1425
-
1426
- // 启动文件监控
1427
- initFileSystemWatcher();
1428
-
1429
- open(`http://localhost:${newPort}`);
1430
1452
  resolve();
1431
- }).on('error', (e) => {
1432
- if (e.code === 'EADDRINUSE') {
1433
- console.log(`端口 ${newPort} 也被占用,继续尝试...`);
1434
- newPort++;
1435
- reject(e);
1436
- } else {
1437
- reject(e);
1438
- }
1439
1453
  });
1440
1454
  });
1441
1455
  break;
1442
- } catch (e) {
1443
- if (newPort >= PORT + 100) {
1444
- console.error('无法找到可用端口,请手动指定端口');
1445
- process.exit(1);
1446
- }
1456
+ } catch (error) {
1457
+ newPort++;
1447
1458
  }
1448
1459
  }
1449
1460
  } else {
1450
- console.error('服务器启动失败:', err);
1461
+ console.error('启动服务器失败:', err);
1451
1462
  process.exit(1);
1452
1463
  }
1453
1464
  });
1454
1465
  }
1455
1466
 
1456
- export default startUIServer;
1457
-
1467
+ export default startUIServer;