|
| 1 | +#!/usr/bin/env node |
| 2 | +'use strict'; |
| 3 | +// Postinstall script: downloads the platform-appropriate binary from GitHub Releases. |
| 4 | +// Runs automatically via `postinstall` in package.json. |
| 5 | + |
| 6 | +const https = require('https'); |
| 7 | +const fs = require('fs'); |
| 8 | +const path = require('path'); |
| 9 | +const os = require('os'); |
| 10 | +const { execSync } = require('child_process'); |
| 11 | + |
| 12 | +const REPO = 'DeusData/codebase-memory-mcp'; |
| 13 | +const VERSION = require('./package.json').version; |
| 14 | +const BIN_DIR = path.join(__dirname, 'bin'); |
| 15 | + |
| 16 | +function getPlatform() { |
| 17 | + switch (process.platform) { |
| 18 | + case 'linux': return 'linux'; |
| 19 | + case 'darwin': return 'darwin'; |
| 20 | + case 'win32': return 'windows'; |
| 21 | + default: throw new Error(`Unsupported platform: ${process.platform}`); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +function getArch() { |
| 26 | + switch (process.arch) { |
| 27 | + case 'arm64': return 'arm64'; |
| 28 | + case 'x64': return 'amd64'; |
| 29 | + default: throw new Error(`Unsupported architecture: ${process.arch}`); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +function download(url, dest) { |
| 34 | + return new Promise((resolve, reject) => { |
| 35 | + function follow(u) { |
| 36 | + https.get(u, (res) => { |
| 37 | + if (res.statusCode === 301 || res.statusCode === 302) { |
| 38 | + return follow(res.headers.location); |
| 39 | + } |
| 40 | + if (res.statusCode !== 200) { |
| 41 | + return reject(new Error(`HTTP ${res.statusCode} for ${u}`)); |
| 42 | + } |
| 43 | + const file = fs.createWriteStream(dest); |
| 44 | + res.pipe(file); |
| 45 | + file.on('finish', () => file.close(resolve)); |
| 46 | + file.on('error', reject); |
| 47 | + }).on('error', reject); |
| 48 | + } |
| 49 | + follow(url); |
| 50 | + }); |
| 51 | +} |
| 52 | + |
| 53 | +async function main() { |
| 54 | + const platform = getPlatform(); |
| 55 | + const arch = getArch(); |
| 56 | + const ext = platform === 'windows' ? 'zip' : 'tar.gz'; |
| 57 | + const binName = platform === 'windows' ? 'codebase-memory-mcp.exe' : 'codebase-memory-mcp'; |
| 58 | + const binPath = path.join(BIN_DIR, binName); |
| 59 | + |
| 60 | + if (fs.existsSync(binPath)) { |
| 61 | + return; // already installed, nothing to do |
| 62 | + } |
| 63 | + |
| 64 | + fs.mkdirSync(BIN_DIR, { recursive: true }); |
| 65 | + |
| 66 | + const archive = `codebase-memory-mcp-${platform}-${arch}.${ext}`; |
| 67 | + const url = `https://github.com/${REPO}/releases/download/v${VERSION}/${archive}`; |
| 68 | + |
| 69 | + process.stdout.write(`codebase-memory-mcp: downloading v${VERSION} for ${platform}/${arch}...\n`); |
| 70 | + |
| 71 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cbm-install-')); |
| 72 | + const tmpArchive = path.join(tmpDir, `cbm.${ext}`); |
| 73 | + |
| 74 | + try { |
| 75 | + await download(url, tmpArchive); |
| 76 | + |
| 77 | + if (ext === 'tar.gz') { |
| 78 | + execSync(`tar -xzf "${tmpArchive}" -C "${tmpDir}"`); |
| 79 | + } else { |
| 80 | + execSync( |
| 81 | + `powershell -NoProfile -Command "Expand-Archive -Path '${tmpArchive}' -DestinationPath '${tmpDir}' -Force"`, |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + const extracted = path.join(tmpDir, binName); |
| 86 | + if (!fs.existsSync(extracted)) { |
| 87 | + throw new Error(`Binary not found after extraction at ${extracted}`); |
| 88 | + } |
| 89 | + |
| 90 | + fs.copyFileSync(extracted, binPath); |
| 91 | + fs.chmodSync(binPath, 0o755); |
| 92 | + |
| 93 | + process.stdout.write(`codebase-memory-mcp: ready.\n`); |
| 94 | + } finally { |
| 95 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +main().catch((err) => { |
| 100 | + process.stderr.write(`\ncodebase-memory-mcp: install failed — ${err.message}\n`); |
| 101 | + process.stderr.write(`You can install manually: https://github.com/${REPO}#installation\n`); |
| 102 | + // Non-fatal: don't block the rest of npm install |
| 103 | + process.exit(0); |
| 104 | +}); |
0 commit comments