|
| 1 | +#!/usr/bin/env node |
| 2 | +"use strict"; |
| 3 | + |
| 4 | +const os = require("os"); |
| 5 | +const path = require("path"); |
| 6 | +const fs = require("fs"); |
| 7 | + |
| 8 | +const VERSION = require("./package.json").version; |
| 9 | +const REPO = "cpoder/wm-mcp-server"; |
| 10 | + |
| 11 | +/** |
| 12 | + * Map Node.js platform/arch to GitHub Release asset names. |
| 13 | + */ |
| 14 | +function getAssetName() { |
| 15 | + const platform = os.platform(); |
| 16 | + const arch = os.arch(); |
| 17 | + |
| 18 | + const map = { |
| 19 | + "linux-x64": "wm-mcp-server-linux-x86_64", |
| 20 | + "linux-arm64": "wm-mcp-server-linux-aarch64", |
| 21 | + "darwin-x64": "wm-mcp-server-macos-x86_64", |
| 22 | + "darwin-arm64": "wm-mcp-server-macos-aarch64", |
| 23 | + "win32-x64": "wm-mcp-server-windows-x86_64.exe", |
| 24 | + }; |
| 25 | + |
| 26 | + const key = `${platform}-${arch}`; |
| 27 | + const asset = map[key]; |
| 28 | + if (!asset) { |
| 29 | + throw new Error( |
| 30 | + `Unsupported platform: ${platform}-${arch}. ` + |
| 31 | + `Supported: ${Object.keys(map).join(", ")}` |
| 32 | + ); |
| 33 | + } |
| 34 | + return asset; |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Returns the URL to download the binary from GitHub Releases. |
| 39 | + */ |
| 40 | +function getDownloadUrl() { |
| 41 | + const asset = getAssetName(); |
| 42 | + return `https://github.com/${REPO}/releases/download/v${VERSION}/${asset}`; |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Returns the local path where the binary is stored. |
| 47 | + */ |
| 48 | +function getBinaryPath() { |
| 49 | + const name = os.platform() === "win32" ? "wm-mcp-server.exe" : "wm-mcp-server"; |
| 50 | + return path.join(__dirname, name); |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Download a URL, following redirects (GitHub releases redirect to S3). |
| 55 | + * Returns a Promise<Buffer>. |
| 56 | + */ |
| 57 | +function download(url, maxRedirects = 5) { |
| 58 | + return new Promise((resolve, reject) => { |
| 59 | + if (maxRedirects <= 0) { |
| 60 | + return reject(new Error("Too many redirects")); |
| 61 | + } |
| 62 | + |
| 63 | + const lib = url.startsWith("https") ? require("https") : require("http"); |
| 64 | + lib.get(url, { headers: { "User-Agent": "wm-mcp-server-npm" } }, (res) => { |
| 65 | + if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) { |
| 66 | + return resolve(download(res.headers.location, maxRedirects - 1)); |
| 67 | + } |
| 68 | + if (res.statusCode !== 200) { |
| 69 | + return reject(new Error(`Download failed: HTTP ${res.statusCode} from ${url}`)); |
| 70 | + } |
| 71 | + |
| 72 | + const chunks = []; |
| 73 | + res.on("data", (chunk) => chunks.push(chunk)); |
| 74 | + res.on("end", () => resolve(Buffer.concat(chunks))); |
| 75 | + res.on("error", reject); |
| 76 | + }).on("error", reject); |
| 77 | + }); |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * Install the binary: download from GitHub Releases and save to disk. |
| 82 | + */ |
| 83 | +async function install() { |
| 84 | + const binPath = getBinaryPath(); |
| 85 | + |
| 86 | + if (fs.existsSync(binPath)) { |
| 87 | + console.log(`wm-mcp-server binary already exists at ${binPath}`); |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + const url = getDownloadUrl(); |
| 92 | + console.log(`Downloading wm-mcp-server v${VERSION}...`); |
| 93 | + console.log(` ${url}`); |
| 94 | + |
| 95 | + const data = await download(url); |
| 96 | + fs.writeFileSync(binPath, data); |
| 97 | + |
| 98 | + if (os.platform() !== "win32") { |
| 99 | + fs.chmodSync(binPath, 0o755); |
| 100 | + } |
| 101 | + |
| 102 | + console.log(`Installed wm-mcp-server to ${binPath}`); |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Run the binary, forwarding all arguments and stdio. |
| 107 | + */ |
| 108 | +function run() { |
| 109 | + const binPath = getBinaryPath(); |
| 110 | + |
| 111 | + if (!fs.existsSync(binPath)) { |
| 112 | + console.error( |
| 113 | + "wm-mcp-server binary not found. Run 'npm install' or 'node install.js' first." |
| 114 | + ); |
| 115 | + process.exit(1); |
| 116 | + } |
| 117 | + |
| 118 | + const { spawnSync } = require("child_process"); |
| 119 | + const result = spawnSync(binPath, process.argv.slice(2), { |
| 120 | + stdio: "inherit", |
| 121 | + env: process.env, |
| 122 | + }); |
| 123 | + |
| 124 | + process.exit(result.status ?? 1); |
| 125 | +} |
| 126 | + |
| 127 | +module.exports = { install, run, getBinaryPath, getDownloadUrl }; |
0 commit comments