|
| 1 | +import { spawn } from 'node:child_process'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { fileURLToPath } from 'node:url'; |
| 4 | +import { |
| 5 | + AgentOverChromeBridge, |
| 6 | + allConfigFromEnv, |
| 7 | + overrideAIConfig, |
| 8 | +} from '@midscene/web/bridge-mode'; |
| 9 | +import dotenv from 'dotenv'; |
| 10 | + |
| 11 | +// Get the directory name in ES module scope |
| 12 | +const __filename = fileURLToPath(import.meta.url); |
| 13 | +const __dirname = path.dirname(__filename); |
| 14 | + |
| 15 | +// Construct the path to the .env file (two levels up from scripts directory) |
| 16 | +const envPath = path.resolve(__dirname, '..', '..', '..', '.env'); |
| 17 | + |
| 18 | +console.log(`Attempting to load environment variables from: ${envPath}`); |
| 19 | + |
| 20 | +// Load environment variables from the specified path |
| 21 | +const configResult = dotenv.config({ |
| 22 | + path: envPath, |
| 23 | +}); |
| 24 | + |
| 25 | +if (configResult.error) { |
| 26 | + console.warn( |
| 27 | + `Warning: Could not load .env file from ${envPath}. Proceeding without it.`, |
| 28 | + configResult.error, |
| 29 | + ); |
| 30 | +} else { |
| 31 | + console.log(`.env file loaded successfully from ${envPath}`); |
| 32 | +} |
| 33 | + |
| 34 | +// Prepare the command and arguments |
| 35 | +const command = 'npx'; |
| 36 | +const keys = Object.keys(allConfigFromEnv()); |
| 37 | +const envOverrides = {}; |
| 38 | +for (const key of keys) { |
| 39 | + const value = process.env[key]; |
| 40 | + if (value !== undefined) { |
| 41 | + envOverrides[key] = value; |
| 42 | + } |
| 43 | +} |
| 44 | +console.log(envOverrides); |
| 45 | +const args = [ |
| 46 | + 'mcp-inspector', |
| 47 | + 'node', |
| 48 | + path.resolve(__dirname, '..', 'dist', 'index.cjs'), // Use resolved path for robustness |
| 49 | + ...Object.entries(envOverrides).map(([key, value]) => `-e ${key}=${value}`), |
| 50 | +]; |
| 51 | + |
| 52 | +console.log(`Executing command: ${command} ${args.join(' ')}`); |
| 53 | + |
| 54 | +// Spawn the child process |
| 55 | +const child = spawn(command, args, { |
| 56 | + stdio: 'inherit', // Inherit stdin, stdout, stderr from the parent process |
| 57 | + shell: process.platform === 'win32', // Use shell on Windows for npx compatibility |
| 58 | +}); |
| 59 | + |
| 60 | +// Handle errors during spawning (e.g., command not found) |
| 61 | +child.on('error', (error) => { |
| 62 | + console.error(`Failed to start subprocess: ${error.message}`); |
| 63 | +}); |
| 64 | + |
| 65 | +// Handle process exit |
| 66 | +child.on('close', (code) => { |
| 67 | + console.log(`Subprocess exited with code ${code}`); |
| 68 | + process.exit(code !== null && code !== undefined ? code : 1); |
| 69 | +}); |
| 70 | + |
| 71 | +// Handle signals to gracefully shut down the child process |
| 72 | +const handleSignal = (signal) => { |
| 73 | + console.log(`Received ${signal}. Forwarding to subprocess.`); |
| 74 | + child.kill(signal); |
| 75 | +}; |
| 76 | + |
| 77 | +process.on('SIGINT', handleSignal); |
| 78 | +process.on('SIGTERM', handleSignal); |
0 commit comments