|
| 1 | +#!/usr/bin/env node |
| 2 | +import { execFileSync, spawn } from "node:child_process"; |
| 3 | +import { existsSync } from "node:fs"; |
| 4 | +import { dirname, resolve } from "node:path"; |
| 5 | +import { fileURLToPath } from "node:url"; |
| 6 | +import { platform } from "node:os"; |
| 7 | + |
| 8 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 9 | + |
| 10 | +const os = platform(); |
| 11 | +const ext = os === "win32" ? ".exe" : ""; |
| 12 | +const binaryName = `rawdoc${ext}`; |
| 13 | + |
| 14 | +// Check for binary in plugin directory first, then PATH |
| 15 | +let binaryPath = resolve(__dirname, binaryName); |
| 16 | + |
| 17 | +if (!existsSync(binaryPath)) { |
| 18 | + // Try to find in PATH |
| 19 | + try { |
| 20 | + const which = os === "win32" ? "where" : "which"; |
| 21 | + binaryPath = execFileSync(which, [binaryName], { encoding: "utf8" }).trim().split("\n")[0]; |
| 22 | + } catch { |
| 23 | + // Try to build from source if Go is available |
| 24 | + try { |
| 25 | + console.error("[rawdoc] Binary not found, building from source..."); |
| 26 | + execFileSync("go", ["build", "-o", resolve(__dirname, binaryName), "."], { |
| 27 | + cwd: __dirname, |
| 28 | + stdio: "inherit", |
| 29 | + }); |
| 30 | + binaryPath = resolve(__dirname, binaryName); |
| 31 | + } catch { |
| 32 | + console.error("[rawdoc] Error: rawdoc binary not found and Go is not available to build it."); |
| 33 | + console.error("[rawdoc] Install with: go install github.com/RandomCodeSpace/rawdoc@latest"); |
| 34 | + process.exit(1); |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +// Spawn rawdoc in MCP server mode, piping stdio |
| 40 | +const child = spawn(binaryPath, ["--serve"], { |
| 41 | + stdio: ["inherit", "inherit", "inherit"], |
| 42 | +}); |
| 43 | + |
| 44 | +child.on("error", (err) => { |
| 45 | + console.error(`[rawdoc] Failed to start: ${err.message}`); |
| 46 | + process.exit(1); |
| 47 | +}); |
| 48 | + |
| 49 | +child.on("exit", (code) => { |
| 50 | + process.exit(code ?? 0); |
| 51 | +}); |
0 commit comments