|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { spawn } from 'node:child_process'; |
| 4 | +import { existsSync } from 'node:fs'; |
| 5 | +import { dirname, resolve } from 'node:path'; |
| 6 | +import { fileURLToPath } from 'node:url'; |
| 7 | + |
| 8 | +const DEFAULT_FALLBACK_PACKAGE = '@geometra/mcp@1.61.3'; |
| 9 | +const RESOLVE_ONLY_FLAG = '--job-forge-resolve-target'; |
| 10 | + |
| 11 | +function normalizeEnv(value) { |
| 12 | + if (typeof value !== 'string') return null; |
| 13 | + const trimmed = value.trim(); |
| 14 | + return trimmed.length > 0 ? trimmed : null; |
| 15 | +} |
| 16 | + |
| 17 | +function resolveExplicitPath(rawPath) { |
| 18 | + const resolvedPath = resolve(rawPath); |
| 19 | + if (!existsSync(resolvedPath)) { |
| 20 | + throw new Error(`JOB_FORGE_GEOMETRA_MCP_PATH points to a missing file: ${resolvedPath}`); |
| 21 | + } |
| 22 | + return resolvedPath; |
| 23 | +} |
| 24 | + |
| 25 | +export function resolveGeometraMcpLaunchTarget() { |
| 26 | + const explicitPath = normalizeEnv(process.env.JOB_FORGE_GEOMETRA_MCP_PATH); |
| 27 | + if (explicitPath) { |
| 28 | + const resolvedPath = resolveExplicitPath(explicitPath); |
| 29 | + return { |
| 30 | + source: 'env-path', |
| 31 | + command: process.execPath, |
| 32 | + args: [resolvedPath], |
| 33 | + resolvedPath, |
| 34 | + }; |
| 35 | + } |
| 36 | + |
| 37 | + const scriptDir = dirname(fileURLToPath(import.meta.url)); |
| 38 | + const siblingRepoPath = resolve(scriptDir, '../../geometra/mcp/dist/index.js'); |
| 39 | + if (existsSync(siblingRepoPath)) { |
| 40 | + return { |
| 41 | + source: 'sibling-repo', |
| 42 | + command: process.execPath, |
| 43 | + args: [siblingRepoPath], |
| 44 | + resolvedPath: siblingRepoPath, |
| 45 | + }; |
| 46 | + } |
| 47 | + |
| 48 | + const packageSpec = normalizeEnv(process.env.JOB_FORGE_GEOMETRA_MCP_PACKAGE) ?? DEFAULT_FALLBACK_PACKAGE; |
| 49 | + return { |
| 50 | + source: 'npm-package', |
| 51 | + command: 'npx', |
| 52 | + args: ['-y', packageSpec], |
| 53 | + packageSpec, |
| 54 | + }; |
| 55 | +} |
| 56 | + |
| 57 | +export async function main(argv = process.argv.slice(2)) { |
| 58 | + const target = resolveGeometraMcpLaunchTarget(); |
| 59 | + |
| 60 | + if (argv.length === 1 && argv[0] === RESOLVE_ONLY_FLAG) { |
| 61 | + process.stdout.write(`${JSON.stringify(target, null, 2)}\n`); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + const child = spawn(target.command, [...target.args, ...argv], { |
| 66 | + stdio: 'inherit', |
| 67 | + env: process.env, |
| 68 | + }); |
| 69 | + |
| 70 | + child.on('error', (error) => { |
| 71 | + console.error(error instanceof Error ? error.message : String(error)); |
| 72 | + process.exit(1); |
| 73 | + }); |
| 74 | + |
| 75 | + child.on('exit', (code, signal) => { |
| 76 | + if (signal) { |
| 77 | + process.kill(process.pid, signal); |
| 78 | + return; |
| 79 | + } |
| 80 | + process.exit(code ?? 0); |
| 81 | + }); |
| 82 | +} |
| 83 | + |
| 84 | +if (process.argv[1] && fileURLToPath(import.meta.url) === resolve(process.argv[1])) { |
| 85 | + void main(); |
| 86 | +} |
0 commit comments