Files
Claude-Code-Workflow/ccw/src/utils/browser-launcher.js
catlog22 27273405f7 feat: CCW Dashboard 增强 - 停止命令、浏览器修复和MCP多源配置
- 新增 ccw stop 命令支持优雅停止和强制终止 (--force)
- 修复 ccw view 服务器检测时浏览器无法打开的问题
- MCP 配置现在从多个源读取:
  - ~/.claude.json (项目级)
  - ~/.claude/settings.json 和 settings.local.json (全局)
  - 各工作空间的 .claude/settings.json (工作空间级)
- 新增全局 MCP 服务器显示区域
- 修复路径选择模态框样式问题

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-08 10:28:07 +08:00

61 lines
1.6 KiB
JavaScript

import open from 'open';
import { platform } from 'os';
import { resolve } from 'path';
/**
* Launch a URL or file in the default browser
* Cross-platform compatible (Windows/macOS/Linux)
* @param {string} urlOrPath - HTTP URL or path to HTML file
* @returns {Promise<void>}
*/
export async function launchBrowser(urlOrPath) {
// Check if it's already a URL (http:// or https://)
if (urlOrPath.startsWith('http://') || urlOrPath.startsWith('https://')) {
try {
await open(urlOrPath);
return;
} catch (error) {
throw new Error(`Failed to open browser: ${error.message}`);
}
}
// It's a file path - convert to file:// URL
const absolutePath = resolve(urlOrPath);
// Construct file:// URL based on platform
let url;
if (platform() === 'win32') {
// Windows: file:///C:/path/to/file.html
url = `file:///${absolutePath.replace(/\\/g, '/')}`;
} else {
// Unix: file:///path/to/file.html
url = `file://${absolutePath}`;
}
try {
// Use the 'open' package which handles cross-platform browser launching
await open(url);
} catch (error) {
// Fallback: try opening the file path directly
try {
await open(absolutePath);
} catch (fallbackError) {
throw new Error(`Failed to open browser: ${error.message}`);
}
}
}
/**
* Check if we're running in a headless/CI environment
* @returns {boolean}
*/
export function isHeadlessEnvironment() {
return !!(
process.env.CI ||
process.env.CONTINUOUS_INTEGRATION ||
process.env.GITHUB_ACTIONS ||
process.env.GITLAB_CI ||
process.env.JENKINS_URL
);
}