|
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | +import os from 'os'; |
| 4 | + |
| 5 | +/** |
| 6 | + * @description Writes the builder port information to mcp.json. |
| 7 | + * |
| 8 | + * The mcp.json file uses the following format: |
| 9 | + * { |
| 10 | + * portList: { |
| 11 | + * builder1: portNumber, |
| 12 | + * builder2: portNumber, |
| 13 | + * }, |
| 14 | + * port: portNumber // The port of the last builder is used by default |
| 15 | + * } |
| 16 | + * |
| 17 | + * @param {number} port - The port number to write. |
| 18 | + * @param {string} [builderName] - The name of the builder. |
| 19 | + */ |
| 20 | +export function writeMcpPort(port: number, builderName?: string) { |
| 21 | + const homeDir = os.homedir(); |
| 22 | + const rsdoctorDir = path.join(homeDir, '.cache/rsdoctor'); |
| 23 | + const mcpPortFilePath = path.join(rsdoctorDir, 'mcp.json'); |
| 24 | + |
| 25 | + if (!fs.existsSync(rsdoctorDir)) { |
| 26 | + fs.mkdirSync(rsdoctorDir, { recursive: true }); |
| 27 | + } |
| 28 | + |
| 29 | + let mcpJson: { portList: Record<string, number>; port: number } = { |
| 30 | + portList: {}, |
| 31 | + port: 0, |
| 32 | + }; |
| 33 | + if (fs.existsSync(mcpPortFilePath)) { |
| 34 | + mcpJson = JSON.parse(fs.readFileSync(mcpPortFilePath, 'utf8')); |
| 35 | + } |
| 36 | + |
| 37 | + if (!mcpJson.portList) mcpJson.portList = {}; |
| 38 | + mcpJson.portList[builderName || 'builder'] = port; |
| 39 | + |
| 40 | + // Use the latest generated port. |
| 41 | + mcpJson.port = port; |
| 42 | + |
| 43 | + fs.writeFileSync(mcpPortFilePath, JSON.stringify(mcpJson, null, 2), 'utf8'); |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * @description Gets the path to the mcp.json file. |
| 48 | + * @returns {string} The path to the mcp.json file. |
| 49 | + */ |
| 50 | +export function getMcpConfigPath() { |
| 51 | + const homeDir = os.homedir(); |
| 52 | + const rsdoctorDir = path.join(homeDir, '.cache/rsdoctor'); |
| 53 | + const mcpPortFilePath = path.join(rsdoctorDir, 'mcp.json'); |
| 54 | + return mcpPortFilePath; |
| 55 | +} |
0 commit comments