|
1 | 1 | import { serveCommand } from './serve.js'; |
| 2 | +import { launchBrowser } from '../utils/browser-launcher.js'; |
| 3 | +import { validatePath } from '../utils/path-resolver.js'; |
| 4 | +import chalk from 'chalk'; |
2 | 5 |
|
3 | 6 | /** |
4 | | - * View command handler - starts dashboard server (unified with serve mode) |
| 7 | + * Check if server is already running on the specified port |
| 8 | + * @param {number} port - Port to check |
| 9 | + * @returns {Promise<boolean>} True if server is running |
| 10 | + */ |
| 11 | +async function isServerRunning(port) { |
| 12 | + try { |
| 13 | + const controller = new AbortController(); |
| 14 | + const timeoutId = setTimeout(() => controller.abort(), 1000); |
| 15 | + |
| 16 | + const response = await fetch(`http://localhost:${port}/api/health`, { |
| 17 | + signal: controller.signal |
| 18 | + }); |
| 19 | + clearTimeout(timeoutId); |
| 20 | + |
| 21 | + return response.ok; |
| 22 | + } catch { |
| 23 | + return false; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * Switch workspace on running server |
| 29 | + * @param {number} port - Server port |
| 30 | + * @param {string} path - New workspace path |
| 31 | + * @returns {Promise<Object>} Result with success status |
| 32 | + */ |
| 33 | +async function switchWorkspace(port, path) { |
| 34 | + try { |
| 35 | + const response = await fetch( |
| 36 | + `http://localhost:${port}/api/switch-path?path=${encodeURIComponent(path)}` |
| 37 | + ); |
| 38 | + return await response.json(); |
| 39 | + } catch (err) { |
| 40 | + return { success: false, error: err.message }; |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * View command handler - opens dashboard for current workspace |
| 46 | + * If server is already running, switches workspace and opens browser |
| 47 | + * If not running, starts a new server |
5 | 48 | * @param {Object} options - Command options |
6 | 49 | */ |
7 | 50 | export async function viewCommand(options) { |
8 | | - // Forward to serve command with same options |
9 | | - await serveCommand({ |
10 | | - path: options.path, |
11 | | - port: options.port || 3456, |
12 | | - browser: options.browser |
13 | | - }); |
| 51 | + const port = options.port || 3456; |
| 52 | + |
| 53 | + // Resolve workspace path |
| 54 | + let workspacePath = process.cwd(); |
| 55 | + if (options.path) { |
| 56 | + const pathValidation = validatePath(options.path, { mustExist: true }); |
| 57 | + if (!pathValidation.valid) { |
| 58 | + console.error(chalk.red(`\n Error: ${pathValidation.error}\n`)); |
| 59 | + process.exit(1); |
| 60 | + } |
| 61 | + workspacePath = pathValidation.path; |
| 62 | + } |
| 63 | + |
| 64 | + // Check if server is already running |
| 65 | + const serverRunning = await isServerRunning(port); |
| 66 | + |
| 67 | + if (serverRunning) { |
| 68 | + // Server is running - switch workspace and open browser |
| 69 | + console.log(chalk.blue.bold('\n CCW Dashboard\n')); |
| 70 | + console.log(chalk.gray(` Server already running on port ${port}`)); |
| 71 | + console.log(chalk.cyan(` Switching workspace to: ${workspacePath}`)); |
| 72 | + |
| 73 | + const result = await switchWorkspace(port, workspacePath); |
| 74 | + |
| 75 | + if (result.success) { |
| 76 | + console.log(chalk.green(` Workspace switched successfully`)); |
| 77 | + |
| 78 | + // Open browser with the new path |
| 79 | + const url = `http://localhost:${port}/?path=${encodeURIComponent(result.path)}`; |
| 80 | + |
| 81 | + if (options.browser !== false) { |
| 82 | + console.log(chalk.cyan(' Opening in browser...')); |
| 83 | + try { |
| 84 | + await launchBrowser(url); |
| 85 | + console.log(chalk.green.bold('\n Dashboard opened!\n')); |
| 86 | + } catch (err) { |
| 87 | + console.log(chalk.yellow(`\n Could not open browser: ${err.message}`)); |
| 88 | + console.log(chalk.gray(` Open manually: ${url}\n`)); |
| 89 | + } |
| 90 | + } else { |
| 91 | + console.log(chalk.gray(`\n URL: ${url}\n`)); |
| 92 | + } |
| 93 | + } else { |
| 94 | + console.error(chalk.red(`\n Failed to switch workspace: ${result.error}\n`)); |
| 95 | + process.exit(1); |
| 96 | + } |
| 97 | + } else { |
| 98 | + // Server not running - start new server |
| 99 | + await serveCommand({ |
| 100 | + path: workspacePath, |
| 101 | + port: port, |
| 102 | + browser: options.browser |
| 103 | + }); |
| 104 | + } |
14 | 105 | } |
0 commit comments