|
|
#!/usr/bin/env node |
|
|
|
|
|
import path from 'path'; |
|
|
import { fileURLToPath } from 'url'; |
|
|
import { execSync } from 'child_process'; |
|
|
import fs from 'fs'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const __filename = fileURLToPath(import.meta.url); |
|
|
const __dirname = path.dirname(__filename); |
|
|
|
|
|
|
|
|
console.log('📋 MCPHub CLI'); |
|
|
console.log(`📁 CLI script location: ${__dirname}`); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function findPackageRoot() { |
|
|
const isDebug = process.env.DEBUG === 'true'; |
|
|
|
|
|
|
|
|
const possibleRoots = [ |
|
|
|
|
|
path.resolve(__dirname, '..'), |
|
|
|
|
|
path.resolve(__dirname, '..', '..', '..') |
|
|
]; |
|
|
|
|
|
|
|
|
if (process.argv[1] && process.argv[1].includes('_npx')) { |
|
|
const npxDir = path.dirname(process.argv[1]); |
|
|
possibleRoots.unshift(path.resolve(npxDir, '..')); |
|
|
} |
|
|
|
|
|
if (isDebug) { |
|
|
console.log('DEBUG: Checking for package.json in:', possibleRoots); |
|
|
} |
|
|
|
|
|
for (const root of possibleRoots) { |
|
|
const packageJsonPath = path.join(root, 'package.json'); |
|
|
if (fs.existsSync(packageJsonPath)) { |
|
|
try { |
|
|
const pkg = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')); |
|
|
if (pkg.name === 'mcphub' || pkg.name === '@samanhappy/mcphub') { |
|
|
if (isDebug) { |
|
|
console.log(`DEBUG: Found package.json at ${packageJsonPath}`); |
|
|
} |
|
|
return root; |
|
|
} |
|
|
} catch (e) { |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
console.log('⚠️ Could not find package.json, using default path'); |
|
|
return path.resolve(__dirname, '..'); |
|
|
} |
|
|
|
|
|
|
|
|
function checkFrontend(packageRoot) { |
|
|
const isDebug = process.env.DEBUG === 'true'; |
|
|
const frontendDistPath = path.join(packageRoot, 'frontend', 'dist'); |
|
|
|
|
|
if (isDebug) { |
|
|
console.log(`DEBUG: Checking frontend at: ${frontendDistPath}`); |
|
|
} |
|
|
|
|
|
if (fs.existsSync(frontendDistPath) && fs.existsSync(path.join(frontendDistPath, 'index.html'))) { |
|
|
console.log('✅ Frontend distribution found'); |
|
|
return true; |
|
|
} else { |
|
|
console.log('⚠️ Frontend distribution not found at', frontendDistPath); |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
const projectRoot = findPackageRoot(); |
|
|
console.log(`📦 Using package root: ${projectRoot}`); |
|
|
|
|
|
|
|
|
checkFrontend(projectRoot); |
|
|
|
|
|
|
|
|
console.log('🚀 Starting MCPHub server...'); |
|
|
import(path.join(projectRoot, 'dist', 'index.js')).catch(err => { |
|
|
console.error('Failed to start MCPHub:', err); |
|
|
process.exit(1); |
|
|
}); |